-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLToTeX.js
More file actions
171 lines (145 loc) · 6.02 KB
/
HTMLToTeX.js
File metadata and controls
171 lines (145 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
function rowsToArray(t) {
if (!t) return [];
return t.replace(/\r\n|\r/g, '\n').split('\n');
}
function HTMLToTeX(parent, header, activeLanguage, config, ctx, document, author) {
function clearHash(text) {
let cleanedText = text;
if (text.endsWith('#'))
cleanedText = text.substring(0, text.length - 1);
return cleanedText.trim();
}
const handlerBold = (node, ctx, children) => `\\textbf\{${children}\}`;
function getLabel(node) {
if (node?.lastChild?.getAttribute)
return `\\label\{sec:${node.lastChild.getAttribute('href').substring(1).replace(/-/g, '--')}\}`;
return '';
}
const handlers = {
ul: (node, ctx, children) => {
ctx.listStack.push('itemize');
const body = children;
ctx.listStack.pop();
return `\\begin{itemize}\n${body}\\end{itemize}\n`;
},
ol: (node, ctx, children) => {
ctx.listStack.push('enumerate');
const body = children;
ctx.listStack.pop();
return `\\begin{enumerate}\n${body}\\end{enumerate}\n`;
},
li: (node, ctx, children) => {
return `\\item ${children}\n`;
},
img: (node, ctx, children) => {
ctx.i_img++;
const path = ctx.embeds.get(node.src) || '';
return `\\begin{figure}[H]\n\\centering\n\\includegraphics\{${path}\}\n\\caption{${node.getAttribute('title') || ''}}\n\\label{fig:I_${ctx.i_img}}\n\\end{figure}\n`;
},
svg: (node, ctx, children) => {
ctx.i_svg++;
const path = ctx.embeds.get(node) || '';
return `\\begin{figure}[H]\n\\centering\n\\includesvg[width=1\\textwidth]\{${path}\}\n\\caption{${node.getAttribute('title') || ''}}\n\\label{fig:IS_${ctx.i_svg}}\n\\end{figure}\n`;
},
h1: (node, ctx, children) => `\\section\{${clearHash(children)}\}${getLabel(node)}\n`,
h2: (node, ctx, children) => `\\subsection\{${clearHash(children)}\}${getLabel(node)}\n`,
h3: (node, ctx, children) => `\\subsubsection\{${clearHash(children)}\}${getLabel(node)}\n`,
h4: (node, ctx, children) => `\\paragraph\{${clearHash(children)}\}${getLabel(node)}\n`,
h5: (node, ctx, children) => `\\subparagraph\{${clearHash(children)}\}${getLabel(node)}\n`,
h6: (node, ctx, children) => `\\subsubparagraph\{${clearHash(children)}\}${getLabel(node)}\n`,
p: (node, ctx, children) => `${children}\\par\n`,
div: (node, ctx, children) => {
if (node.classList.contains('toolbar-item') || node.classList.contains('toolbar'))
return '';
if (node.classList.contains('page-break'))
return '\\newpage';
return children;
},
code: (node, ctx, children) => {
if (node.classList.length == 0)
return children;
const codeText = node.textContent;
const prefix = 'language-';
const langClass = Array.from(node.classList).find(cls => cls.startsWith(prefix));
let lang = langClass ? langClass.replace(prefix, '') : '{}';
lang = lang == 'none' ? '{}' : lang;
return `\\begin{lstlisting}[language=${lang}, caption={}]\n${codeText}\\end{lstlisting}`;
},
strong: handlerBold,
b: handlerBold,
em: (node, ctx, children) => `\\emph\{${children}\}\n`,
//TODO: Beware of \\end{tabularx} used elsewhere wantedly because of HTML to TeX wrong output.
table: (node, ctx, children) => `\\begin{center}\\begin{longtable}%${children}\n \\end{longtable}\\end{center}\n`,
thead: (node, ctx, children) => {
let cols = rowsToArray(children.trim());
const mCols = 'l|'.repeat(cols.length - 1);
const mColData = cols.map(c => `\\textbf{${c}}`).join(' & ');
return `{|${mCols}p{6.5cm}|}\n\\hline\n${mColData} \n\\\\ \\hline\n\\endhead\n`;
},
td: (node, ctx, children) => children,
tr: (node, ctx, children) => {
if (node.parentElement.tagName.toLowerCase() === 'thead') return children;
const tds = Array.from(node.children).filter(child =>
child.tagName.toLowerCase() === 'td' || child.tagName.toLowerCase() === 'th'
);
const colTexts = tds.map(td => walk(td, ctx));
return colTexts.join(' & ') + ' \n\\\\ \\hline';
},
a: (node, ctx, children) => {
if (children.trim().replace('\\#', '#').length == 1)
return '';
let href = node.getAttribute('href') || '';
if (!href) {
href = node.getAttribute('id') || '';
if (href)
return `\\phantomsection\n\\label{sec:${href.replace(/-/g, '--')}}\n`;
}
href = decodeURI(href);
href = href
.replace(/([#\$%&~^{}])/g, '\\$1')
.replace(/_/g, '\\_');
if (href.startsWith('http'))
return `\\href{${href}}{${children}}`;
href = node.getAttribute('href')?.replace(/-/g, '--') || '';
if (href.startsWith('#'))
return `\\hyperref[sec:${href.substring(1)}]{${children}}`;
else
return `\\href{${decodeURI(node.href)}}{${children}}`;
},
script: (node, ctx, children) => '',
style: (node, ctx, children) => '',
br: (node, ctx, children) => `\n\\par ${children}`,
default: (node, ctx, children) => children
};
function escapeLaTeX(text) {
return text.replace(/([_%&#$\\~^])/g, match => {
const escapes = {
'%': '\\%',
'_': '\\_',
'#': '\\#',
'$': '\\$',
'\\': '\\\\',
'~': '\\~{}',
'^': '\\^{}'
};
return escapes[match];
});
}
function walk(node, ctx) {
if (node.nodeType === Node.TEXT_NODE)
return escapeLaTeX(node.textContent);
const children = Array.from(node.childNodes)
.map(child => walk(child, ctx))
.join('');
const tag = node.nodeName.toLowerCase();
const handler = handlers[tag] || handlers.default;
return handler(node, ctx, children);
}
// TODO : Resolve author name in export
document = document.replace(/_AUTH_/g, author);
document = document.replace(/_DOCNAME_/g, header);
document = document.replace(/_LANG_/g, config[activeLanguage] || activeLanguage);
document = document.replace(/_LSTSET_/g, config[`${activeLanguage}-lstset`] || '');
const latex = walk(parent, ctx);
return [document, latex];
}