diff --git a/javascript/latexContent.js b/javascript/latexContent.js index 1d9c43cca..31fee02f8 100644 --- a/javascript/latexContent.js +++ b/javascript/latexContent.js @@ -121,7 +121,7 @@ export const preamble = `\\documentclass[a4paper, 12pt]{report} escapechar=^ } -\\usepackage{xcolor} +\\usepackage[svgnames]{xcolor} \\definecolor{LeftBarClickable}{RGB}{187, 187, 187} \\lstdefinestyle{JavaScript}{ diff --git a/javascript/parseXmlLatexSplit.js b/javascript/parseXmlLatexSplit.js new file mode 100644 index 000000000..59b2845da --- /dev/null +++ b/javascript/parseXmlLatexSplit.js @@ -0,0 +1,386 @@ +import { getChildrenByTagName, ancestorHasTag } from "./utilityFunctions"; +import { checkIndexBadEndWarning } from "./processingFunctions/warnings.js"; + +import { + replaceTagWithSymbol, + processEpigraphPdf, + processFigurePdf, + processFigureEpub, + generateImage, + processExercisePdf, + processExerciseEpub, + processFileInput, + processSnippetPdf, + processSnippetEpub, + processTable, + recursiveProcessPureText, + processList, + addName +} from "./processingFunctions"; + +const tagsToRemove = new Set([ + "#comment", + "COMMENT", + "CHANGE", + "EDIT", + "EXCLUDE", + "HISTORY", + "NAME", + "INDEX", + "CODEINDEX", + "EM_NO_INDEX", + "ORDER", + "SOLUTION", + "WEB_ONLY" +]); +// SOLUTION tag handled by processSnippet + +const ignoreTags = new Set([ + "CHAPTERCONTENT", + "JAVASCRIPT", + "SCHEME", + "NOBR", + "SECTIONCONTENT", + "span", + "SPLIT", + "SPLITINLINE" +]); + +const processTextFunctionsDefaultLatex = { + SCHEME: (node, writeTo) => { + writeTo.push("{\\color{LightGrey} "); + recursiveProcessTextLatex(node.firstChild, writeTo); + writeTo.push("}"); + }, + + JAVASCRIPT: (node, writeTo) => { + writeTo.push("{\\color{DarkGrey} "); + recursiveProcessTextLatex(node.firstChild, writeTo); + writeTo.push("}"); + }, + + PDF_ONLY: (node, writeTo) => { + recursiveProcessTextLatex(node.firstChild, writeTo); + }, + + "#text": (node, writeTo) => { + const trimedValue = node.nodeValue + .replace(/[\r\n]+/, " ") + .replace(/\s+/g, " ") + .replace(/\^/g, "^{}") + .replace(/%/g, "\\%"); + if (trimedValue.match(/&(\w|\.)+;/)) { + processFileInput(trimedValue.trim(), writeTo); + } else { + writeTo.push(trimedValue); + } + // if (!trimedValue.match(/^\s*$/)) { + // } + }, + + ABOUT: (node, writeTo) => { + writeTo.push("\\chapter*{"); + const name = addName(node, writeTo); + writeTo.push("\n\\addcontentsline{toc}{chapter}{"); + writeTo.push(name + "}\n\n"); + recursiveProcessTextLatex(node.firstChild, writeTo); + }, + REFERENCES: (node, writeTo) => + processTextFunctionsLatex["ABOUT"](node, writeTo), + WEBPREFACE: (node, writeTo) => + processTextFunctionsLatex["ABOUT"](node, writeTo), + MATTER: (node, writeTo) => processTextFunctionsLatex["ABOUT"](node, writeTo), + + B: (node, writeTo) => { + writeTo.push("\\textbf{"); + recursiveProcessTextLatex(node.firstChild, writeTo); + writeTo.push("}"); + }, + + BR: (node, writeTo) => { + writeTo.push("\n\\noindent "); + }, + + BLOCKQUOTE: (node, writeTo) => { + writeTo.push("\n\\begin{quote}"); + recursiveProcessTextLatex(node.firstChild, writeTo); + writeTo.push("\\end{quote}\n"); + }, + + CHAPTER: (node, writeTo) => { + writeTo.push("\\chapter{"); + addName(node, writeTo); + writeTo.push("\\pagestyle{main}\n"); + recursiveProcessTextLatex(node.firstChild, writeTo); + }, + + CITATION: (node, writeTo) => { + // Currently just text. Not linked to biblography. + const text = node.getElementsByTagName("TEXT")[0]; + if (text) { + recursiveProcessTextLatex(text.firstChild, writeTo); + } else { + recursiveProcessTextLatex(node.firstChild, writeTo); + } + }, + + EM: (node, writeTo) => processTextFunctionsLatex["em"](node, writeTo), + em: (node, writeTo) => { + writeTo.push("{\\em "); + recursiveProcessTextLatex(node.firstChild, writeTo); + writeTo.push("}"); + }, + + EPIGRAPH: (node, writeTo) => { + processEpigraphPdf(node, writeTo); + }, + + EXERCISE: (node, writeTo) => { + processExercisePdf(node, writeTo); + }, + + FIGURE: (node, writeTo) => { + processFigurePdf(node, writeTo); + }, + + FOOTNOTE: (node, writeTo) => { + writeTo.push("\\cprotect\\footnote{"); + recursiveProcessTextLatex(node.firstChild, writeTo); + writeTo.push("}\n"); + }, + + H2: (node, writeTo) => { + writeTo.push("\n\\subsection*{"); + recursiveProcessTextLatex(node.firstChild, writeTo); + writeTo.push("}\n"); + }, + + /* + INDEX: (node, writeTo) => { + writeTo.push("\\index{"); + const indexArr = []; + const order = getChildrenByTagName(node, "ORDER")[0]; + if (order) { + recursiveProcessTextLatex(order.firstChild, indexArr); + indexArr.push("@"); + } + recursiveProcessTextLatex(node.firstChild, indexArr); + const indexStr = indexArr.join("").trim(); + + // Do error checking + checkIndexBadEndWarning(indexStr); + writeTo.push(indexStr); + writeTo.push("}"); + }, + */ + + IMAGE: (node, writeTo) => { + writeTo.push( + "\\begin{figure}[H]\n\\centering" + + generateImage(node.getAttribute("src")) + + "\n\\end{figure}\n" + ); + }, + + LABEL: (node, writeTo) => { + writeTo.push("\\label{" + node.getAttribute("NAME") + "}\n"); + }, + + LINK: (node, writeTo) => { + writeTo.push("\\href{" + node.getAttribute("address") + "}{"); + recursiveProcessTextLatex(node.firstChild, writeTo); + writeTo.push("}"); + }, + + LATEX: (node, writeTo) => + processTextFunctionsLatex["LATEXINLINE"](node, writeTo), + LATEXINLINE: (node, writeTo) => { + recursiveProcessPureText(node.firstChild, writeTo); + }, + + MATTERSECTION: (node, writeTo) => { + writeTo.push("\\section*{"); + addName(node, writeTo); + recursiveProcessTextLatex(node.firstChild, writeTo); + }, + + OL: (node, writeTo) => { + writeTo.push("\n\\begin{enumerate}"); + writeTo.push(ancestorHasTag(node, "EXERCISE") ? "[a.]\n" : "\n"); + processList(node.firstChild, writeTo); + writeTo.push("\\end{enumerate}\n"); + }, + + P: (node, writeTo) => processTextFunctionsLatex["TEXT"](node, writeTo), + TEXT: (node, writeTo) => { + writeTo.push("\n\n"); + recursiveProcessTextLatex(node.firstChild, writeTo); + writeTo.push("\n"); + }, + + QUOTE: (node, writeTo) => { + writeTo.push("\\enquote{"); + recursiveProcessTextLatex(node.firstChild, writeTo); + writeTo.push("}"); + }, + + REF: (node, writeTo) => { + writeTo.push("\\ref{" + node.getAttribute("NAME") + "}"); + }, + + REFERENCE: (node, writeTo) => { + // Doesn't do anything special + writeTo.push("\n"); + recursiveProcessTextLatex(node.firstChild, writeTo); + writeTo.push("\n"); + }, + + SC: (node, writeTo) => { + writeTo.push("{\\scshape "); + recursiveProcessTextLatex(node.firstChild, writeTo); + writeTo.push("}"); + }, + + SECTION: (node, writeTo) => { + writeTo.push("\\section{"); + addName(node, writeTo); + writeTo.push("\\pagestyle{section}\n"); + recursiveProcessTextLatex(node.firstChild, writeTo); + }, + + SUBHEADING: (node, writeTo) => { + writeTo.push("\\subsubsection{"); + addName(node, writeTo); + recursiveProcessTextLatex(node.firstChild, writeTo); + }, + SUBSUBSECTION: (node, writeTo) => + processTextFunctionsLatex["SUBHEADING"](node, writeTo), + + SCHEMEINLINE: (node, writeTo) => + processTextFunctionsLatex["JAVASCRIPTINLINE"](node, writeTo), + JAVASCRIPTINLINE: (node, writeTo) => { + writeTo.push("{\\lstinline[mathescape=true]$"); + recursiveProcessPureText(node.firstChild, writeTo, { + removeNewline: "all" + }); + writeTo.push("$}"); + }, + + SNIPPET: (node, writeTo) => { + processSnippetPdf(node, writeTo); + }, + + SUBHEADING: (node, writeTo) => { + writeTo.push("\\subsubsection{"); + addName(node, writeTo); + recursiveProcessTextLatex(node.firstChild, writeTo); + }, + + SUBINDEX: (node, writeTo) => { + // should occur only within INDEX + // also should only exist after stuff in the main index + writeTo.push("!"); + const order = getChildrenByTagName(node, "ORDER")[0]; + if (order) { + recursiveProcessTextLatex(order.firstChild, writeTo); + writeTo.push("@"); + } + recursiveProcessTextLatex(node.firstChild, writeTo); + }, + + SUBSECTION: (node, writeTo) => { + writeTo.push("\\subsection{"); + addName(node, writeTo); + writeTo.push("\\pagestyle{subsection}\n"); + recursiveProcessTextLatex(node.firstChild, writeTo); + }, + + TABLE: (node, writeTo) => { + processTable(node, writeTo); + }, + + TT: (node, writeTo) => { + writeTo.push("\\texttt{"); + recursiveProcessTextLatex(node.firstChild, writeTo, true); + writeTo.push("}"); + }, + + UL: (node, writeTo) => { + writeTo.push("\n\\begin{itemize}\n"); + processList(node.firstChild, writeTo); + writeTo.push("\\end{itemize}\n"); + } +}; + +const processTextFunctionsEpub = { + EXERCISE: (node, writeTo) => { + processExerciseEpub(node, writeTo); + }, + FIGURE: (node, writeTo) => { + processFigureEpub(node, writeTo); + }, + SECTION: (node, writeTo) => { + writeTo.push("\\section{"); + addName(node, writeTo); + recursiveProcessTextLatex(node.firstChild, writeTo); + }, + JAVASCRIPTINLINE: (node, writeTo) => { + writeTo.push("{\\lstinline[mathescape=true, language=JavaScript]$"); + recursiveProcessPureText(node.firstChild, writeTo, { + removeNewline: "all" + }); + writeTo.push("$}"); + }, + SNIPPET: (node, writeTo) => { + processSnippetEpub(node, writeTo); + }, + SUBSECTION: (node, writeTo) => { + writeTo.push("\\subsection{"); + addName(node, writeTo); + recursiveProcessTextLatex(node.firstChild, writeTo); + }, + SUBHEADING: (node, writeTo) => { + writeTo.push("\\paragraph{"); + addName(node, writeTo); + recursiveProcessTextLatex(node.firstChild, writeTo); + }, + SUBSUBSECTION: (node, writeTo) => + processTextFunctionsEpub["SUBHEADING"](node, writeTo) +}; + +let processTextFunctionsLatex = processTextFunctionsDefaultLatex; + +export const switchParseFunctionsLatex = parseType => { + if (parseType == "pdf") { + processTextFunctionsLatex = processTextFunctionsDefaultLatex; + } else if (parseType == "epub") { + console.log("using parsetype epub"); + processTextFunctionsLatex = { + ...processTextFunctionsDefaultLatex, + ...processTextFunctionsEpub + }; + } +}; + +export const processTextLatex = (node, writeTo) => { + const name = node.nodeName; + if (processTextFunctionsLatex[name]) { + processTextFunctionsLatex[name](node, writeTo); + return true; + } else { + if (replaceTagWithSymbol(node, writeTo) || tagsToRemove.has(name)) { + return true; + } else if (ignoreTags.has(name)) { + recursiveProcessTextLatex(node.firstChild, writeTo); + return true; + } + } + console.log("WARNING Unrecognised Tag:\n" + node.toString() + "\n"); + return false; +}; + +export const recursiveProcessTextLatex = (node, writeTo) => { + if (!node) return; + processTextLatex(node, writeTo); + return recursiveProcessTextLatex(node.nextSibling, writeTo); +}; diff --git a/javascript/processingFunctions/processFigureHtml.js b/javascript/processingFunctions/processFigureHtml.js index 426fcfd8e..0edc63700 100755 --- a/javascript/processingFunctions/processFigureHtml.js +++ b/javascript/processingFunctions/processFigureHtml.js @@ -11,11 +11,24 @@ export const processFigureHtml = (node, writeTo) => { if (!src && node.getElementsByTagName("FIGURE")[0]) { src = node.getElementsByTagName("FIGURE")[0].getAttribute("src"); } + + let scale_factor = "scale_factor_0"; + if ( + !node.getAttribute("scale_factor") && + node.getElementsByTagName("FIGURE")[0] + ) { + scale_factor = + "scale_factor_" + + node.getElementsByTagName("FIGURE")[0].getAttribute("scale_factor"); + } else { + scale_factor = "scale_factor_" + node.getAttribute("scale_factor"); + } + const label = node.getElementsByTagName("LABEL")[0]; if (src && !label) { writeTo.push(` - + `); return; } else if (!src) { @@ -24,7 +37,9 @@ export const processFigureHtml = (node, writeTo) => { const images = node.getElementsByTagName("IMAGE"); for (let i = 0; i < images.length; i++) { writeTo.push(` - + `); } } @@ -39,7 +54,7 @@ export const processFigureHtml = (node, writeTo) => { if (src && label) { writeTo.push(`
- `); + `); } const snippet = node.getElementsByTagName("SNIPPET")[0]; diff --git a/static/assets/stylesheet.css b/static/assets/stylesheet.css index 706e90fe5..5c86006bc 100644 --- a/static/assets/stylesheet.css +++ b/static/assets/stylesheet.css @@ -74,6 +74,10 @@ figure > div.snippet { text-align: left; } +figure > pre { + text-align: left; +} + caption { margin: 20px auto !important; text-weight: bold; @@ -84,10 +88,26 @@ h2 { font-size: 18pt !important; } -img { +img.scale_factor_0 { margin: 10px 5px; } +img.scale_factor_1 { + margin: 10px 10%; +} +img.scale_factor_2 { + margin: 10px 15%; +} +img.scale_factor_3 { + margin: 10px 20%; +} +img.scale_factor_4 { + margin: 10px 25%; +} +img.scale_factor_5 { + margin: 10px 30%; +} + reference { display: list-item; } @@ -625,10 +645,10 @@ a.gray { color: #eee; } - a.gray:visited { color: #eee; } + pre.prettyprint{display:block;background-color:#333; cursor: pointer;}pre .nocode{background-color:none;color:#000}pre .str{color:#ffa0a0}pre .kwd{color:#f0e68c;font-weight:bold}pre .com{color:#87ceeb}pre .typ{color:#98fb98}pre .lit{color:#cd5c5c}pre .pun{color:#fff}pre .pln{color:#fff}pre .tag{color:#f0e68c;font-weight:bold}pre .atn{color:#bdb76b;font-weight:bold}pre .atv{color:#ffa0a0}pre .dec{color:#98fb98}ol.linenums{margin-top:0;margin-bottom:0;color:#aeaeae}li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8{list-style-type:none}@media print{pre.prettyprint{background-color:none}pre .str,code .str{color:#060}pre .kwd,code .kwd{color:#006;font-weight:bold}pre .com,code .com{color:#600;font-style:italic}pre .typ,code .typ{color:#404;font-weight:bold}pre .lit,code .lit{color:#044}pre .pun,code .pun{color:#440}pre .pln,code .pln{color:#000}pre .tag,code .tag{color:#006;font-weight:bold}pre .atn,code .atn{color:#404}pre .atv,code .atv{color:#060}} pre .str,code .str{color:#fec243}pre .kwd,code .kwd{color:#8470ff}pre .com,code .com{color:#32cd32;font-style:italic}pre .typ,code .typ{color:#6ecbcc}pre .lit,code .lit{color:#d06}pre .pun,code .pun{color:#8b8970}pre .pln,code .pln{color:#0f0f0f}pre .tag,code .tag{color:#9c9cff}pre .htm,code .htm{color:#dda0dd}pre .xsl,code .xsl{color:#d0a0d0}pre .atn,code .atn{color:#46eeee;font-weight:normal}pre .atv,code .atv{color:#eeb4b4}pre .dec,code .dec{color:#3387cc}a{text-decoration:none}pre.prettyprint,code.prettyprint{font-family:'Inconsolata', 'Droid Sans Mono','CPMono_v07 Bold','Droid Sans';font-weight:medium;font-size:9pt;background-color:#0f0f0f;}pre.prettyprint{width:95%;white-space:pre-wrap}pre.prettyprint a,code.prettyprint a{text-decoration:none}ol.linenums{margin-top:0;margin-bottom:0;color:#8b8970}li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8{list-style-type:none}@media print{pre.prettyprint,code.prettyprint{background-color:#fff}pre .str,code .str{color:#088}pre .kwd,code .kwd{color:#006;font-weight:bold}pre .com,code .com{color:#oc3;font-style:italic}pre .typ,code .typ{color:#404;font-weight:bold}pre .lit,code .lit{color:#044}pre .pun,code .pun{color:#440}pre .pln,code .pln{color:#000}pre .tag,code .tag{color:#b66ff7;font-weight:bold}pre .htm,code .htm{color:#606;font-weight:bold}pre .xsl,code .xsl{color:#606;font-weight:bold}pre .atn,code .atn{color:#c71585;font-weight:normal}pre .atv,code .atv{color:#088;font-weight:normal}} pre.prettyprint.no-eval {background-color: #fff; cursor: default;} .prettyprint.no-eval span.kwd{color: #f16e6e;}.prettyprint.no-eval span.lit{color: #56bfa2;} @@ -661,6 +681,7 @@ pre.prettyprint{ border: none; } + div.pre-prettyprint > pre.prettyprint.no-eval.prettyprinted { border-left: 1ex solid white; } diff --git a/static/img_javascript/Fig5.4c.std.pdf b/static/img_javascript/Fig5.4c.std.pdf index 9bd723851..3955bf41c 100644 Binary files a/static/img_javascript/Fig5.4c.std.pdf and b/static/img_javascript/Fig5.4c.std.pdf differ diff --git a/static/img_javascript/Fig5.4c.std.svg b/static/img_javascript/Fig5.4c.std.svg index f7d7bcc6c..b448450fa 100644 --- a/static/img_javascript/Fig5.4c.std.svg +++ b/static/img_javascript/Fig5.4c.std.svg @@ -26,11 +26,11 @@ guidetolerance="10" inkscape:pageopacity="0" inkscape:pageshadow="2" - inkscape:window-width="640" - inkscape:window-height="480" + inkscape:window-width="1568" + inkscape:window-height="1136" id="namedview5781" showgrid="false" - inkscape:zoom="0.29207921" + inkscape:zoom="1.1683168" inkscape:cx="215.98286" inkscape:cy="136.55563" inkscape:window-x="0" @@ -269,7 +269,7 @@ image/svg+xml - + @@ -537,7 +537,7 @@ style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:20px;line-height:125%;font-family:Inconsolata;-inkscape-font-specification:Sans;text-align:center;letter-spacing:0px;word-spacing:0px;text-anchor:middle;display:inline;fill:#383838;fill-opacity:1;stroke:none">read + id="tspan6691-7-5-6-0">prompt print + id="tspan6691-7-5-6-0-0">display - factorial_iterative_definition factorial_example diff --git a/xml/chapter1/section3/subsection2.xml b/xml/chapter1/section3/subsection2.xml index 38126e809..a4e3e15c8 100644 --- a/xml/chapter1/section3/subsection2.xml +++ b/xml/chapter1/section3/subsection2.xml @@ -749,10 +749,10 @@ function f(x, y) { Names that are declared with const - directly inside of function declarations have the surrounding function as + inside of a block have the body of the immediately surrounding block as their scope.