-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtex_engine.cpp
More file actions
520 lines (458 loc) · 13.2 KB
/
Copy pathtex_engine.cpp
File metadata and controls
520 lines (458 loc) · 13.2 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
Jordan Dehmel
2023 - present
*/
#include "tex_engine.hpp"
#include <cstdint>
#include <cwctype>
#include <stack>
void handle_md(const std::list<std::string> &, std::ostream &);
// Translate into latex
void TEXEngine::knit(const std::list<Chunk> &_chunks)
{
// Header
for (const auto &l : latexHeader)
{
target << l << '\n';
}
if (!forceFormalFont)
{
target << "\\allsectionsfont{\\sffamily}\n"
<< "\\sffamily\n";
}
// Body
for (const auto &chunk : _chunks)
{
if (chunk.lines.empty())
{
// Skip empty chunks
continue;
}
else if (chunk.type == "TEXT")
{
// Markdown text: This is the hard part.
handle_md(chunk.lines, target);
}
else if (chunk.type == "OUTPUT")
{
if (!chunk.show_output)
{
continue;
}
// Code output
for (const auto &l : startOutput)
{
target << l << '\n';
}
for (const auto &l : chunk.lines)
{
target << l << '\n';
}
for (const auto &l : endOutput)
{
target << l << '\n';
}
}
else
{
// Code input
if (!chunk.show_code)
{
continue;
}
if (lstSupportedLangs.count(chunk.type) != 0)
{
target << "\\lstset{language=" << chunk.type
<< "}\n";
}
else
{
target << "\\lstset{language=C++}\n";
}
for (const auto &l : startCode)
{
target << l << '\n';
}
for (const auto &l : chunk.lines)
{
target << l << '\n';
}
for (const auto &l : endCode)
{
target << l << '\n';
}
}
}
// Footer
for (const auto &l : latexFooter)
{
target << l << '\n';
}
}
////////////////////////////////////////////////////////////////
void TEXEngine::handle_md(const std::list<std::string> &_lines,
std::ostream &_target)
{
const static std::string specialCharacters = "%$~#&^";
const static std::string listChars = "-:;.,)]";
std::string line;
bool in_blockquote = false;
int64_t ws_offset = -1, prev_ws_offset;
std::stack<std::pair<int, std::string>> list_closure_stack;
for (auto it = _lines.begin(); it != _lines.end(); ++it)
{
line = *it;
if (line.empty())
{
if (in_blockquote)
{
_target << "\\end{displayquote}\n";
in_blockquote = false;
}
}
prev_ws_offset = ws_offset;
ws_offset = 0;
// Get new whitespace offset
while (ws_offset < (int64_t)line.size() &&
iswspace(line[ws_offset]))
{
++ws_offset;
}
// Trim off whitespace for further processing
line = line.substr(ws_offset);
// Deal with de-indentation within lists
while (!list_closure_stack.empty() &&
ws_offset < list_closure_stack.top().first)
{
_target << list_closure_stack.top().second;
list_closure_stack.pop();
}
if (line[0] == '%')
{
// Ignore comment lines
continue;
}
if (line[0] == '>')
{
// Block quote
if (!in_blockquote)
{
_target << "\\begin{displayquote}\n";
in_blockquote = true;
}
handle_md({line.substr(1)}, _target);
continue;
}
else if (in_blockquote)
{
_target << "\\end{displayquote}\n";
in_blockquote = false;
}
if (line[0] == '#')
{
// Header
// # => \section{}
// ## => \subsection{}
// ### => \subsubsection{}
// #### => \paragraph{}
// #####+ => \subparagraph{}
_target << "\\bigskip{}\n";
int num_pounds = 0;
while (line[num_pounds] == '#')
{
num_pounds++;
}
switch (num_pounds)
{
case 1:
_target << "\\section*{";
break;
case 2:
_target << "\\subsection*{";
break;
case 3:
_target << "\\subsubsection*{";
break;
case 4:
_target << "\\paragraph*{";
break;
default:
_target << "\\subparagraph*{";
break;
};
handle_md({line.substr(num_pounds)}, _target);
_target << "}~\n";
_target << "\\bigskip{}\n";
continue;
}
if (line[0] == '[')
{
// Link
std::string title, link;
// Scan until end of title
unsigned int i = 1;
while (i < line.size() && line[i] != ']')
{
if (specialCharacters.find(line[i]) !=
std::string::npos)
{
title += std::string("\\texttt{") +
line[i] + "}";
}
else
{
title += line[i];
}
i++;
}
if (line[i + 1] != '(')
{
for (auto c : line)
{
if (specialCharacters.find(c) !=
std::string::npos)
{
_target << "\\texttt{" << c << "}";
}
else
{
_target << c;
}
}
_target << '\n';
continue;
}
// Scan until end of link
i += 2;
while (i < line.size() && line[i] != ')')
{
if (specialCharacters.find(line[i]) !=
std::string::npos)
{
line += std::string("\\texttt{") + line[i] +
"}";
}
else
{
link += line[i];
}
i++;
}
// Write latex
_target << "\\href{" << link << "}{";
// Catch any internal markdown syntax
handle_md({title}, _target);
_target << "}\n";
continue;
}
if (line[0] == '!')
{
// Image
std::string alt, path, options;
// Parse caption, path and options
unsigned int i = 2;
while (i < line.size() && line[i] != ']')
{
if (specialCharacters.find(line[i]) !=
std::string::npos)
{
alt += std::string("\\texttt{") + line[i] +
"}";
}
else
{
alt += line[i];
}
i++;
}
i += 2;
while (i < line.size() && line[i] != ')')
{
if (specialCharacters.find(line[i]) !=
std::string::npos)
{
path += std::string("\\texttt{") + line[i] +
"}";
}
else
{
path += line[i];
}
i++;
}
i += 2;
while (i < line.size() && line[i] != '}')
{
// Percentage parsing
if (line[i] >= '0' && line[i] <= '9')
{
std::string num = "00";
while (i < line.size() && line[i] >= '0' &&
line[i] <= '9')
{
num += line[i];
i++;
}
if (i < line.size() && line[i] == '%')
{
num = num.substr(0, num.size() - 2) +
"." + num.substr(num.size() - 2);
options += num + "\\textwidth ";
i++;
}
else
{
options += num;
}
}
// Avoid issues with commenting
if (i < line.size() && line[i] != '%' &&
line[i] != '}')
{
options += line[i];
}
i++;
}
if (options == "")
{
options = "width=0.5\\textwidth";
}
// Convert to latex
_target << "\\begin{figure}[h]\n"
<< "\\centering\n"
<< "\\includegraphics[" << options << "]{"
<< path << "}\n";
if (!alt.empty())
{
_target << "\\caption {";
handle_md({alt}, _target);
_target << "}\n";
}
_target << "\\end {figure}\n";
continue;
}
if (line.starts_with("--") || line.starts_with("~~") ||
line.starts_with("___") || line.starts_with("=="))
{
// Horizontal rule
_target << "\\hrule{}\n";
continue;
}
if (listChars.find(line[0]) != std::string::npos)
{
// Unnumbered list
if (list_closure_stack.empty() ||
ws_offset > prev_ws_offset)
{
// New sublist
_target << "\\begin{itemize}\n";
list_closure_stack.push(
{ws_offset, "\\end{itemize}\n"});
}
_target << "\\item ";
// Write the rest of this line
handle_md({line.substr(2)}, _target);
continue;
}
if (line.size() > 1 && isalnum(line[0]) &&
listChars.find(line[1]) != std::string::npos)
{
// Numbered list
if (list_closure_stack.empty() ||
ws_offset > prev_ws_offset)
{
// New sublist
_target << "\\begin{enumerate}\n";
list_closure_stack.push(
{ws_offset, "\\end{enumerate}\n"});
}
_target << "\\item ";
// Write the rest of this line
handle_md({line.substr(2)}, _target);
continue;
}
// Normal text line
for (uint64_t i = 0; i < line.size(); ++i)
{
const char c = line[i];
switch (c)
{
case '\\':
if (i + 1 < line.size() &&
(line[i + 1] == '_' || line[i + 1] == '*'))
{
_target << line[i + 1];
++i;
}
else
{
_target << c;
}
break;
case '*':
case '_':
++i;
if (line[i] == c)
{
// Boldface
_target << "\\textbf{";
for (++i;
i + 1 < line.size() &&
!(line[i] == c && line[i + 1] == c);
++i)
{
_target << line[i];
}
++i;
_target << '}';
}
else
{
// Italics
_target << "\\textit{";
for (; i + 1 < line.size() && line[i] != c;
++i)
{
_target << line[i];
}
_target << '}';
}
break;
case '$':
_target << c;
for (++i; line[i] != c; ++i)
{
_target << line[i];
}
_target << c;
break;
case '`':
_target << "\\texttt{";
for (++i; line[i] != c; ++i)
{
_target << line[i];
}
_target << '}';
break;
// Otherwise uncovered TeX-illegal characters
case '%':
case '~':
case '#':
case '^':
_target << '\\' << c;
break;
// Base case
default:
_target << c;
break;
}
}
_target << '\n';
continue;
}
while (!list_closure_stack.empty())
{
_target << list_closure_stack.top().second;
list_closure_stack.pop();
}
}