Download tex2html.js from EncryptidSystems/Multi-Radix-Geometry: direct link, hf CLI and curl.
- Browser
- Download file 6.23 kB
-
https://huggingface.co/spaces/EncryptidSystems/Multi-Radix-Geometry/resolve/main/tex2html.js
- Command line
-
hf download hf://spaces/EncryptidSystems/Multi-Radix-Geometry/tex2html.js
-
curl -L -o tex2html.js https://huggingface.co/spaces/EncryptidSystems/Multi-Radix-Geometry/resolve/main/tex2html.js
6.23 kB
| // Minimal LaTeX -> HTML converter for the pandoc-generated paper source. | |
| // Math is passed through untouched (HTML-escaped) for MathJax to typeset. | |
| (function (root) { | |
| 'use strict'; | |
| var ESC = { '&': '&', '<': '<', '>': '>', '"': '"' }; | |
| function esc(s) { return s.replace(/[&<>"]/g, function (c) { return ESC[c]; }); } | |
| var THM = /^<strong>(Theorem|Lemma|Proposition|Corollary|Definition|Remark|Problem|Conjecture|Example|Algorithm|Assumption|Claim|Notation)\b/; | |
| // Index of the matching close brace for the '{' at position i. | |
| function matchBrace(s, i) { | |
| var depth = 0; | |
| for (var j = i; j < s.length; j++) { | |
| var c = s[j]; | |
| if (c === '\\') { j++; continue; } | |
| if (c === '{') depth++; | |
| else if (c === '}') { depth--; if (depth === 0) return j; } | |
| } | |
| return -1; | |
| } | |
| var ACCENT = { "'": 'Μ', '`': 'Μ', '^': 'Μ', '"': 'Μ', '~': 'Μ', 'c': 'Μ§' }; | |
| function inline(s) { | |
| s = s.replace(/\{\[\}/g, '[').replace(/\{\]\}/g, ']'); | |
| // accents like \'e or \'{e} | |
| s = s.replace(/\\(['`^"~])\{?([A-Za-z])\}?/g, function (_, a, ch) { return (ch + ACCENT[a]).normalize('NFC'); }); | |
| var out = '', i = 0; | |
| while (i < s.length) { | |
| var c = s[i]; | |
| if (c === '\\') { | |
| var m = /^\\([A-Za-z]+)\*?\s*/.exec(s.slice(i)); | |
| if (!m) { | |
| var nx = s[i + 1]; | |
| if (nx === '&') { out += '&'; i += 6; continue; } // "\&" after escaping | |
| if (nx === ' ') { out += ' '; i += 2; continue; } | |
| if (nx === '\\') { out += '<br>'; i += 2; continue; } | |
| out += nx || ''; i += 2; continue; | |
| } | |
| var name = m[1]; | |
| i += m[0].length; | |
| var arg = null; | |
| if (s[i] === '{') { | |
| var e = matchBrace(s, i); | |
| if (e > 0) { arg = s.slice(i + 1, e); i = e + 1; } | |
| } | |
| switch (name) { | |
| case 'emph': case 'textit': out += '<em>' + inline(arg || '') + '</em>'; break; | |
| case 'textbf': out += '<strong>' + inline(arg || '') + '</strong>'; break; | |
| case 'nolinkurl': case 'texttt': case 'url': out += '<code>' + (arg || '') + '</code>'; break; | |
| case 'textup': case 'textrm': case 'mbox': out += inline(arg || ''); break; | |
| case 'S': out += 'Β§'; if (arg !== null) out += inline(arg); break; | |
| case 'ldots': case 'dots': out += 'β¦'; break; | |
| case 'label': case 'raggedright': case 'noindent': case 'tightlist': break; | |
| default: if (arg !== null) out += inline(arg); | |
| } | |
| continue; | |
| } | |
| if (c === '{' || c === '}') { i++; continue; } | |
| out += c; i++; | |
| } | |
| return out | |
| .replace(/---/g, 'β').replace(/--/g, 'β') | |
| .replace(/``/g, 'β').replace(/''/g, 'β') | |
| .replace(/~/g, 'Β '); | |
| } | |
| function convert(tex) { | |
| var b = tex.indexOf('\\begin{document}'), e = tex.indexOf('\\end{document}'); | |
| var body = tex.slice(b >= 0 ? b + 16 : 0, e >= 0 ? e : tex.length); | |
| // TeX comments: unescaped % eats the rest of the line and the newline. | |
| body = body.replace(/(^|[^\\])%[^\n]*\n?/g, '$1'); | |
| var math = []; | |
| function stash(open, close) { | |
| return function (_, m) { math.push(open + esc(m) + close); return 'ξ' + (math.length - 1) + 'ξ'; }; | |
| } | |
| body = body.replace(/\\\[([\s\S]*?)\\\]/g, stash('\\[', '\\]')); | |
| body = body.replace(/\\\(([\s\S]*?)\\\)/g, stash('\\(', '\\)')); | |
| body = esc(body); | |
| var blocks = []; | |
| function block(html) { blocks.push(html); return '\n\nξ' + (blocks.length - 1) + 'ξ\n\n'; } | |
| var toc = []; | |
| body = body.replace(/\\hypertarget\{[^}]*\}\{\s*\\(sub)?section\{([\s\S]*?)\}\\label\{([^}]*)\}\}/g, | |
| function (_, sub, title, id) { | |
| title = inline(title.replace(/\s+/g, ' ').trim()); | |
| toc.push({ id: id, title: title, sub: !!sub }); | |
| return block('<h' + (sub ? 3 : 2) + ' id="' + id + '">' + title + '</h' + (sub ? 3 : 2) + '>'); | |
| }); | |
| body = body.replace(/\\(sub)?section\{([\s\S]*?)\}\\label\{([^}]*)\}\}?/g, function (_, sub, title, id) { | |
| title = inline(title.replace(/\s+/g, ' ').trim()); | |
| toc.push({ id: id, title: title, sub: !!sub }); | |
| return block('<h' + (sub ? 3 : 2) + ' id="' + id + '">' + title + '</h' + (sub ? 3 : 2) + '>'); | |
| }); | |
| body = body.replace(/\\begin\{longtable\}[\s\S]*?\\end\{longtable\}/g, function (t) { | |
| var head = [], rows = []; | |
| var hm = /\\toprule\\noalign\{\}([\s\S]*?)\\\\\s*\\midrule/.exec(t); | |
| if (hm) head = hm[1].split('&').map(function (c) { | |
| return inline(c.replace(/\\begin\{minipage\}(\[[^\]]*\])?\{[^}]*\}|\\end\{minipage\}/g, '').trim()); | |
| }); | |
| var rb = t.indexOf('\\endlastfoot'), re = t.lastIndexOf('\\end{longtable}'); | |
| t.slice(rb + 12, re).split(/\\\\\s*/).forEach(function (r) { | |
| if (r.trim()) rows.push(r.split('&').map(function (c) { return inline(c.trim()); })); | |
| }); | |
| var h = '<div class="table-wrap"><table><thead><tr>' + head.map(function (c) { return '<th>' + c + '</th>'; }).join('') + | |
| '</tr></thead><tbody>' + rows.map(function (r) { | |
| return '<tr>' + r.map(function (c) { return '<td>' + c + '</td>'; }).join('') + '</tr>'; | |
| }).join('') + '</tbody></table></div>'; | |
| return block(h); | |
| }); | |
| body = body.replace(/\\maketitle/g, '') | |
| .replace(/\\begin\{abstract\}/g, block('<div class="abstract"><h2 id="abstract">Abstract</h2>')) | |
| .replace(/\\end\{abstract\}/g, block('</div>')); | |
| var html = body.split(/\n\s*\n/).map(function (p) { | |
| p = p.trim(); | |
| if (!p) return ''; | |
| var bm = /^ξ(\d+)ξ$/.exec(p); | |
| if (bm) return blocks[+bm[1]]; | |
| var h = inline(p.replace(/\s*\n\s*/g, ' ')); | |
| var cls = THM.test(h) ? ' class="thm"' : /^<em>Proof/.test(h) ? ' class="proof"' : ''; | |
| return '<p' + cls + '>' + h + '</p>'; | |
| }).join('\n'); | |
| html = html.replace(/ξ(\d+)ξ/g, function (_, n) { return math[+n]; }); | |
| toc = toc.map(function (t) { t.title = t.title.replace(/ξ(\d+)ξ/g, function (_, n) { return math[+n]; }); return t; }); | |
| return { html: html, toc: toc }; | |
| } | |
| root.tex2html = convert; | |
| if (typeof module !== 'undefined') module.exports = convert; | |
| })(typeof window !== 'undefined' ? window : globalThis); | |