@
/* "CodeWorker":	a scripting language for parsing and generating text.

Copyright (C) 1996-1997, 1999-2002 Cédric Lemaire

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

To contact the author: codeworker@free.fr
*/

function LaTeX2HTML(sLaTeX : value) {
	local myHTMLDoc;
	insert myHTMLDoc.sParsingMode = "description";
	createVirtualFile(".#f1", sLaTeX);
	createVirtualFile(".#f2", "");
	try {
		translate("LaTeX2HTML.cwp", myHTMLDoc, ".#f1", ".#f2");
	} catch(sError) {
		traceLine("error in the piece of LaTeX:");
		traceLine(sLaTeX);
		error(sError);
	}
	local sResult = loadVirtualFile(".#f2");
	deleteVirtualFile(".#f2");
	deleteVirtualFile(".#f1");
	return sResult;
}

function HTMLAnchor(sLabel) {
	set sLabel = replaceString("#",  "_", sLabel);
	set sLabel = replaceString("&",  "_", sLabel);
	set sLabel = replaceString(";",  "_", sLabel);
	set sLabel = replaceString(" ",  "_", sLabel);
	set sLabel = replaceString("%",  "_", sLabel);
	set sLabel = replaceString("@",  "_", sLabel);
	set sLabel = replaceString("=",  "_", sLabel);
	set sLabel = replaceString("<",  "_", sLabel);
	set sLabel = replaceString(">",  "_", sLabel);
	set sLabel = replaceString("\"", "_", sLabel);
	set sLabel = replaceString("'",  "_", sLabel);
	set sLabel = replaceString("/",  "_", sLabel);
	set sLabel = replaceString("-",  "_", sLabel);
	set sLabel = replaceString("+",  "_", sLabel);
	set sLabel = replaceString(".",  "_", sLabel);
	return sLabel;
}

function fileDescription(sFilename : value) {
	local myFile;
	parseAsBNF("fileDescription.cwp", myFile, sFilename);
	local sResult;
	if myFile.description set sResult = LaTeX2HTML(myFile.description);
	if existVariable(myFile.input) {
		set sResult += endl() + "<P/>" + endl() + "<B>Input files:</B>";
		set sResult += endl();
		foreach i in myFile.input {
			if !first(i) set sResult += ", ";
			set sResult += "<A href=\"" + i + "\">" + i + "</A>";
		}
		set sResult += "." + endl();
	}
	if existVariable(myFile.file) {
		set sResult += endl() + "<P/>" + endl() + "<B>quoted files:</B>";
		set sResult += endl();
		foreach i in myFile.file {
			if !first(i) set sResult += ", ";
			set sResult += "<A href=\"" + i + "\">" + i + "</A>";
		}
		set sResult += "." + endl();
	}
	if existVariable(myFile.output) {
		set sResult += endl() + "<P/>" + endl() + "<B>output files:</B>";
		set sResult += endl();
		foreach i in myFile.output {
			if !first(i) set sResult += ", ";
			set sResult += "<A href=\"" + i + "\">" + i + "</A>";
		}
		set sResult += "." + endl();
	}
	return sResult;
}


if getMarkupKey() == "today" {
	@@formatDate(getNow(), "%B %d, %Y")@
@
} else if getMarkupKey() == "aujourd'hui" {
	local frenchMonths;
	insert frenchMonths["jan"] = "janvier";
	insert frenchMonths["feb"] = "février";
	insert frenchMonths["mar"] = "mars";
	insert frenchMonths["apr"] = "avril";
	insert frenchMonths["may"] = "mai";
	insert frenchMonths["jun"] = "juin";
	insert frenchMonths["jul"] = "juillet";
	insert frenchMonths["aug"] = "août";
	insert frenchMonths["sep"] = "septembre";
	insert frenchMonths["oct"] = "octobre";
	insert frenchMonths["nov"] = "novembre";
	insert frenchMonths["dec"] = "décembre";
	@@formatDate(getNow(), "%d")@ @frenchMonths[formatDate(getNow(), "%b")]@ @formatDate(getNow(), "%Y")@
@
} else if getMarkupKey() == "version" {
	@@getVersion()@
@
} else if startString(getMarkupKey(), "size:") {
	local iSize = fileSize(getWorkingPath() + "WebSite/" + subString(getMarkupKey(), 5));
	@ (@floor($iSize / 1024$)@kb)@
} else if getMarkupKey() == "repository-content" {
	@
						<UL>
@
	foreach i in this.theme {
		local sHTML = LaTeX2HTML(i);
		@							<LI><A class="left" href="#@HTMLAnchor(sHTML)@">@sHTML@</A></LI>
@
	}
	@
						</UL>
@
} else if getMarkupKey() == "repository" {
	local listOfFileAnchors;
	foreach i in this.theme {
		local sHTMLTheme = LaTeX2HTML(i);
		@
				<TABLE border="1" cellpadding="7" cellspacing="0" width="100%">
					<TR>
						<TD class="company" valign="top"><A name="@HTMLAnchor(sHTMLTheme)@"/>@sHTMLTheme@</TD>
					</TR>
					<TR>
						<TD class="content" valign="top">@LaTeX2HTML(i.description)@</TD>
					</TR>
@
		foreach j in i.subject {
			local sHTMLSubject = LaTeX2HTML(j);
			local sSubjectAnchor = HTMLAnchor(sHTMLTheme + '_' + sHTMLSubject);
			@					<TR>
						<TD>
							<A name="@sSubjectAnchor@"/>
							<TABLE border="1" cellpadding="7" cellspacing="0" width="100%">
								<TR>
									<TD class="role" valign="top" width="60">@j.state@&nbsp;</TD>
									<TD class="role" valign="top" colspan="2">@sHTMLSubject@</TD>
								</TR>
								<TR>
									<TD class="content" valign="top" colspan="3">@LaTeX2HTML(j.description)@</TD>
								</TR>
@
			if existVariable(j.script) {
				@					<TR>
						<TD class="content" valign="top" width="60">&nbsp;</TD>
						<TD class="role" valign="top" width="90">Script</TD>
						<TD class="role" valign="top">Description</TD>
					</TR>
@
				foreach k in j.script {
					local sFileAnchor = "repository_" + HTMLAnchor(k);
					local sScriptLink;
					if k.rightString(4) in {".cws", ".cwt", ".cwp"} {
						local theHTMLTranslation;
						insert theHTMLTranslation.docURL = "../";
						set sScriptLink = "highlighting/" + replaceString(k.rightString(4), ".html", k);
						translate("WebSite/repository/CWscript2HTML.cwp", theHTMLTranslation, "WebSite/repository/" + k, "WebSite/" + sScriptLink);
					} else {
						set sScriptLink = "repository/" + k;
					}
					@					<TR>
						<TD class="content" valign="top">&nbsp;</TD>
						<TD class="content" valign="top"><A href="@sScriptLink@">@k@</A></TD>
						<TD class="content" valign="top">@
					if k.link || findElement(sFileAnchor, listOfFileAnchors) {
						@see <A href="#@sFileAnchor@">@LaTeX2HTML(i + "::" + j)@</A>@
					} else {
						insert listOfFileAnchors[sFileAnchor] = sFileAnchor;
						@						<A name="@sFileAnchor@"/>
@fileDescription("WebSite/repository/" + k)@@
					}
					@
						</TD>
					</TR>
@
				}
			}
			if existVariable(j.file) {
				@					<TR>
						<TD class="content" valign="top" width="60">&nbsp;</TD>
						<TD class="role" valign="top" width="90">File</TD>
						<TD class="role" valign="top">Description</TD>
					</TR>
@
				foreach k in j.file {
					local sScriptLink;
					if k.rightString(4) in {".cws", ".cwt", ".cwp"} {
						local theHTMLTranslation;
						insert theHTMLTranslation.docURL = "../";
						set sScriptLink = "highlighting/" + k.rsubString(4) + ".html";
						translate("WebSite/repository/CWscript2HTML.cwp", theHTMLTranslation, "WebSite/repository/" + k, "WebSite/" + sScriptLink);
					} else {
						set sScriptLink = "repository/" + k;
					}
					@					<TR>
						<TD class="content" valign="top">&nbsp;</TD>
						<TD class="content" valign="top"><A href="@sScriptLink@">@k@</A></TD>
						<TD class="content" valign="top">@fileDescription("WebSite/repository/" + k)@</TD>
					</TR>
@
				}
			}
			if existVariable(j.example) {
				@					<TR>
						<TD class="content" valign="top">&nbsp;</TD>
						<TD class="role" valign="top">Example</TD>
						<TD class="role" valign="top">Description</TD>
					</TR>
@
				foreach k in j.example {
					local sScriptLink;
					if k.rightString(4) in {".cws", ".cwt", ".cwp"} {
						local theHTMLTranslation;
						insert theHTMLTranslation.docURL = "../";
						set sScriptLink = "highlighting/" + k.rsubString(4) + ".html";
						translate("WebSite/repository/CWscript2HTML.cwp", theHTMLTranslation, "WebSite/examples/" + k, "WebSite/" + sScriptLink);
					} else {
						set sScriptLink = "examples/" + k;
					}
					@					<TR>
						<TD class="content" valign="top">&nbsp;</TD>
						<TD class="content" valign="top"><A href="@sScriptLink@">@k@</A></TD>
						<TD class="content" valign="top">@fileDescription("WebSite/examples/" + k)@@
					try {
						traceLine("executing example '" + k + "' of the repository...");
						local sOldIncludePath = getIncludePath();
						setIncludePath("WebSite/");
						local sOldWorkingPath = getWorkingPath();
						setWorkingPath("WebSite/");
						setCommentBegin("//");
						setCommentEnd("\n");
						executeString(this, loadFile("examples/" + k));
						setCommentBegin("<!--");
						setCommentEnd("-->");
						setWorkingPath(sOldWorkingPath);
						setIncludePath(sOldIncludePath);
					} catch(sError) {
						error("on Web site, example '" + k + "':" + endl() + sError);
					}
					@</TD>
					</TR>
@
				}
			}
			@							</TABLE>
						</TD>
					</TR>
@
		}
@				</TABLE>
@
	}
} else if getMarkupKey() == "repository-working" {
	foreach i in this.working {
		local sHTMLVersion = LaTeX2HTML(i);
		@
				<TABLE border="1" cellpadding="7" cellspacing="0" width="100%">
					<TR>
						<TD class="company" valign="top" colspan="2">@sHTMLVersion@</TD>
					</TR>
					<TR>
						<TD class="role" valign="top" width="80">State</TD>
						<TD class="role" valign="top">Description</TD>
					</TR>
					<TR>
						<TD class="content" valign="top">@
		switch(i.state) {
			case "progress":
				@in progress@
				break;
			case "pending":
			case "complete":
				@@i.state@@
				break;
		}
		@</TD>
						<TD class="content" valign="top">@LaTeX2HTML(i.description)@</TD>
					</TR>
				</TABLE>
@
	}
} else if startString(getMarkupKey(), "quick access:") {
	local sMode = subString(getMarkupKey(), 14);
	foreach i in project.headings if i.modes.findElement(sMode) {
		@				@repeatString("&nbsp", 8)@<A href="#category_@key(i)@"><B>Category <I>@key(i)@</I></B></A><BR/>
@
	}
	@				<P/>
@
	foreach i in project.headings if i.modes.findElement(sMode) {
		@				<A name="category_@key(i)@" />
				<TABLE border="1" cellpadding="7" cellspacing="0" width="100%">
					<TR>
						<TD class="company" valign="top">Category <I>@key(i)@</I></TD>
						<TD class="company" valign="top">@LaTeX2HTML(i.description)@</TD>
					</TR>
@
		foreach j in sorted i.members if !j.mode  || j.mode == sMode {
			@					<TR>
						<TD class="content" valign="top"><A href="manual_The_scripting_language.html#@j.name@"><CODE>@j.name@</CODE></A></TD>
						<TD class="content" valign="top">@LaTeX2HTML(j.abstract)@</TD>
					</TR>
@
		}
		@				</TABLE>
				<BR/>
@
	}
}
