|
| 1 | +import replaceTagWithSymbol from './replaceTagWithSymbol'; |
| 2 | +import processFigure from './processFigure'; |
| 3 | + |
| 4 | +const tagsToRemove = new Set(["#comment", "COMMENT", "CHANGE", "EXCLUDE", "HISTORY", "SCHEME", "SCHEMEINLINE", "EXERCISE", "SOLUTION"]); |
| 5 | +const ignoreTags = new Set(["JAVASCRIPT", "SPLIT", "SPLITINLINE", "NOBR"]); |
| 6 | + |
| 7 | +export const processTextFunctions = { |
| 8 | + "#text": ((node, writeTo) => { |
| 9 | + const trimedValue = node.nodeValue.replace(/[\r\n]+/, " ").replace(/\s+/g, " "); |
| 10 | + if (!trimedValue.match(/^\s*$/)) { |
| 11 | + writeTo.push(trimedValue.replace(/%/g, "\\%")); |
| 12 | + } |
| 13 | + }), |
| 14 | + |
| 15 | + "B": ((node, writeTo) => { |
| 16 | + writeTo.push("\\textbf{"); |
| 17 | + recursiveProcessText(node.firstChild, writeTo); |
| 18 | + writeTo.push("}"); |
| 19 | + }), |
| 20 | + |
| 21 | + "BLOCKQUOTE": ((node, writeTo) => { |
| 22 | + writeTo.push("\n\\begin{quote}"); |
| 23 | + recursiveProcessText(node.firstChild, writeTo); |
| 24 | + writeTo.push("\\end{quote}\n"); |
| 25 | + }), |
| 26 | + |
| 27 | + "EM": ((node, writeTo) => processTextFunctions["em"](node, writeTo)), |
| 28 | + "em": ((node, writeTo) => { |
| 29 | + writeTo.push("{\\em "); |
| 30 | + recursiveProcessText(node.firstChild, writeTo); |
| 31 | + writeTo.push("}"); |
| 32 | + }), |
| 33 | + |
| 34 | + "FIGURE": ((node, writeTo) => { |
| 35 | + processFigure(node, writeTo); |
| 36 | + }), |
| 37 | + |
| 38 | + "IMAGE": ((node, writeTo) => { |
| 39 | + writeTo.push("\n\\includegraphics{" |
| 40 | + + node.getAttribute("src").replace(/\.gif$/, ".png").replace(/_/g, "\\string_") |
| 41 | + + "}\n"); |
| 42 | + }), |
| 43 | + |
| 44 | + "FOOTNOTE": ((node, writeTo) => { |
| 45 | + writeTo.push("\n\\cprotect\\footnote{"); |
| 46 | + recursiveProcessText(node.firstChild, writeTo); |
| 47 | + writeTo.push("}\n"); |
| 48 | + }), |
| 49 | + |
| 50 | + "INDEX": ((node, writeTo) => { |
| 51 | + processIndex(node, writeTo); |
| 52 | + }), |
| 53 | + |
| 54 | + "LABEL": ((node, writeTo) => { |
| 55 | + writeTo.push("\\label{" |
| 56 | + + node.getAttribute("NAME") |
| 57 | + + "}\n"); |
| 58 | + }), |
| 59 | + |
| 60 | + "LATEX": ((node, writeTo) => processTextFunctions["LATEXINLINE"](node, writeTo)), |
| 61 | + "LATEXINLINE": ((node, writeTo) => { |
| 62 | + recursiveProcessPureText(node.firstChild, writeTo); |
| 63 | + }), |
| 64 | + |
| 65 | + "NAME": ((node, writeTo) => { |
| 66 | + recursiveProcessText(node.firstChild, writeTo); |
| 67 | + writeTo.push("}\n"); |
| 68 | + }), |
| 69 | + |
| 70 | + "OL": ((node, writeTo) => { |
| 71 | + writeTo.push("\n\\begin{enumerate}\n"); |
| 72 | + processList(node.firstChild, writeTo); |
| 73 | + writeTo.push("\\end{enumerate}\n"); |
| 74 | + }), |
| 75 | + |
| 76 | + "P": ((node, writeTo) => processTextFunctions["TEXT"](node, writeTo)), |
| 77 | + "TEXT": ((node, writeTo) => { |
| 78 | + writeTo.push("\n\n"); |
| 79 | + recursiveProcessText(node.firstChild, writeTo); |
| 80 | + writeTo.push("\n"); |
| 81 | + }), |
| 82 | + |
| 83 | + "QUOTE": ((node, writeTo) => { |
| 84 | + writeTo.push("\\enquote{"); |
| 85 | + recursiveProcessText(node.firstChild, writeTo); |
| 86 | + writeTo.push("}"); |
| 87 | + }), |
| 88 | + |
| 89 | + "REF": ((node, writeTo) => { |
| 90 | + writeTo.push("~\\ref{" |
| 91 | + + node.getAttribute("NAME") |
| 92 | + + "}"); |
| 93 | + }), |
| 94 | + |
| 95 | + "SCHEMEINLINE": ((node, writeTo) => processTextFunctions["JAVASCRIPTINLINE"](node, writeTo)), |
| 96 | + "JAVASCRIPTINLINE": ((node, writeTo) => { |
| 97 | + writeTo.push("\\lstinline|"); |
| 98 | + recursiveProcessPureText(node.firstChild, writeTo, true); |
| 99 | + writeTo.push("|"); |
| 100 | + }), |
| 101 | + |
| 102 | + "SNIPPET": ((node, writeTo) => { |
| 103 | + processSnippet(node, writeTo); |
| 104 | + }), |
| 105 | + |
| 106 | + "SUBHEADING": ((node, writeTo) => { |
| 107 | + writeTo.push("\\subsubsection{"); |
| 108 | + recursiveProcessText(node.firstChild, writeTo); |
| 109 | + }), |
| 110 | + |
| 111 | + "UL": ((node, writeTo) => { |
| 112 | + writeTo.push("\n\\begin{itemize}\n"); |
| 113 | + processList(node.firstChild, writeTo); |
| 114 | + writeTo.push("\\end{itemize}\n"); |
| 115 | + }) |
| 116 | +} |
| 117 | + |
| 118 | +export const processList = (node, writeTo) => { |
| 119 | + if (!node) return; |
| 120 | + if (node.nodeName == "LI"){ |
| 121 | + writeTo.push("\\item{"); |
| 122 | + recursiveProcessText(node.firstChild, writeTo) |
| 123 | + writeTo.push("}\n"); |
| 124 | + } |
| 125 | + return processList(node.nextSibling, writeTo); |
| 126 | +} |
| 127 | + |
| 128 | +export const processSnippet = (node, writeTo) => { |
| 129 | + const jsSnippet = node.getElementsByTagName("JAVASCRIPT")[0]; |
| 130 | + if (jsSnippet) { |
| 131 | + writeTo.push("\n\\begin{lstlisting}"); |
| 132 | + recursiveProcessPureText(jsSnippet.firstChild, writeTo); |
| 133 | + writeTo.push("\\end{lstlisting}\n"); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +const recursiveProcessPureText = (node, writeTo, removeNewline = false) => { |
| 138 | + if (!node) return; |
| 139 | + if (!replaceTagWithSymbol(node, writeTo)) { |
| 140 | + if (removeNewline) { |
| 141 | + writeTo.push(node.nodeValue.replace(/[\r\n]+/g, " ")); |
| 142 | + } else { |
| 143 | + writeTo.push(node.nodeValue); |
| 144 | + } |
| 145 | + } |
| 146 | + return recursiveProcessPureText(node.nextSibling, writeTo) |
| 147 | +} |
| 148 | + |
| 149 | +export const recursiveProcessText = (node, writeTo) => { |
| 150 | + if (!node) return; |
| 151 | + if (!processText(node, writeTo)){ |
| 152 | + console.log("recusive process:\n" + node.toString()); |
| 153 | + } |
| 154 | + return recursiveProcessText(node.nextSibling, writeTo) |
| 155 | +} |
| 156 | + |
| 157 | +export const processText = (node, writeTo) => { |
| 158 | + const name = node.nodeName; |
| 159 | + if (processTextFunctions[name]) { |
| 160 | + processTextFunctions[name](node, writeTo); |
| 161 | + return true; |
| 162 | + } else { |
| 163 | + if (replaceTagWithSymbol(node, writeTo) || tagsToRemove.has(name)) { |
| 164 | + return true; |
| 165 | + } else if (ignoreTags.has(name)) { |
| 166 | + recursiveProcessText(node.firstChild, writeTo); |
| 167 | + return true; |
| 168 | + } else { |
| 169 | + return false; |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +export const processIndex = (index, writeTo) => { |
| 175 | + writeTo.push("\\index{"); |
| 176 | + for (let child = index.firstChild; child; child = child.nextSibling) { |
| 177 | + const name = child.nodeName; |
| 178 | + switch (name) { |
| 179 | + case "SUBINDEX": |
| 180 | + writeTo.push("!"); |
| 181 | + recursiveProcessText(child.firstChild, writeTo); |
| 182 | + break; |
| 183 | + |
| 184 | + default: |
| 185 | + processText(child, writeTo); |
| 186 | + } |
| 187 | + } |
| 188 | + writeTo.push("}"); |
| 189 | +} |
| 190 | + |
0 commit comments