-
Notifications
You must be signed in to change notification settings - Fork 0
/
pandoc-postprocessing.js
executable file
·41 lines (28 loc) · 1.64 KB
/
pandoc-postprocessing.js
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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const process = require('process');
const path = process.argv[2];
let content = fs.readFileSync(path, 'utf8');
// allow midrules in multiline tables with ###
content = content.replace(/\\begin\{minipage\}.*?\n(?:\\#){3,}(?:.|\n)+?\\end\{minipage\}\\tabularnewline/g, '\\midrule');
// give inline code a gray background + special handling for #tbl:calc-sampling-pos
content = content.replace(/\\lstinline!(.*?)!/g, (substring, code) => {
if (code.match(/^(_*)↑(_*)$/)) {
return `\\small\\verb!${' '.repeat(RegExp.$1.length)}↑${' '.repeat(RegExp.$2.length)}!`;
}
return `\\colorbox{WhiteSmoke}{\\lstinline!${code}!}`;
});
// allow arraystrech to be set for each table individually
content = content.replace(/(\\begin\{longtable\}[^\n]+\n\\caption\{(?:\\label\{[^{}]+\})?)arraystretch=([0-9.]+)\s+((?:.|\n)+?\\end\{longtable\})/g, `{
\\renewcommand{\\arraystretch}{$2}
$1$3}`);
// cut off longer caption links before first "." in list of figures / tables
content = content.replace(/\\caption\{(\\label\{[^{}]+\})?([^.{}]+|(?:[^.{}]*\{[^{}]*\}[^.{}]*)*)(\.(?:[^{}]*|(?:[^{}]*\{[^{}]*\}[^{}]*)*))\}/gm, '\\caption[$2]{$1$2$3}');
// prevent glsadd from adding a paragraph on its own
content = content.replace(/^(\\glsadd(?:\[[^\]]*?\])?\{[^}]+?\})$/gm, '$1\\vspace{-1.2\\baselineskip}');
// make links to chapters work
content = content.replace(/^\\hypertarget\{([^}]+?)\}\{\\chapter/gm, '\\cleardoublepage\\hypertarget{$1}{\\chapter');
// make TODOs more visible:
content = content.replace(/\\emph\{(\\textbf\{TODO:\} [^}]+)\}/g, '\\colorbox{yellow}{$1}');
fs.writeFileSync(path, content, 'utf8');