davinci-paper/scripts/convert-paper.sh
scripts/convert-paper.shRaw
1#!/usr/bin/env bash
2set -euo pipefail
3
4ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
5cd "$ROOT_DIR"
6
7mkdir -p build/flat docs
8
9TMP_TEX_ROOT="build/flat/texsrc"
10rm -rf "$TMP_TEX_ROOT"
11cp -R v2 "$TMP_TEX_ROOT"
12
13# latexpand does not resolve macro-based include prefixes like \secs/, so
14# normalize them to explicit relative paths in a temporary conversion tree.
15while IFS= read -r -d '' tex_file; do
16 sed -E \
17 -e 's#\\input\{\\secs/#\\input{../sections/#g' \
18 -e 's#\\input\{\\figs/#\\input{../figures/#g' \
19 -e 's#\\includegraphics(\[[^]]*\])?\{\\figs/#\\includegraphics\1{../figures/#g' \
20 -e 's#\\bibliography\{\\bib/#\\bibliography{../bibliography/#g' \
21 "$tex_file" > "${tex_file}.tmp"
22 mv "${tex_file}.tmp" "$tex_file"
23done < <(find "$TMP_TEX_ROOT" -type f -name '*.tex' -print0)
24
25# Use markdown-friendly macro styles for conversion output.
26cat >> "$TMP_TEX_ROOT/build/preamble-macros.tex" <<'EOF'
27\renewcommand{\hlset}[1]{#1}
28\renewcommand{\hlget}[1]{#1}
29\renewcommand{\circuitstyle}[1]{\texttt{#1}\xspace}
30\renewcommand{\smartcontractstyle}[1]{\texttt{#1}\xspace}
31\renewcommand{\blobstyle}[1]{\texttt{#1}\xspace}
32\renewcommand{\methodstyle}[1]{\texttt{#1}\xspace}
33\renewcommand{\vocstyle}[1]{\texttt{#1}\xspace}
34\renewcommand{\blinder}[1][]{\texttt{blinder}\xspace}
35\renewcommand{\msg}[1][]{\texttt{message}\xspace}
36\renewcommand{\enc}[1][]{\texttt{ciphertext}\xspace}
37\renewcommand{\pk}[1][]{\texttt{pk}\xspace}
38\renewcommand{\sk}[1][]{\texttt{sk}\xspace}
39\renewcommand{\conc}{||}
40\renewcommand{\F}{\mathbb{F}\xspace}
41EOF
42
43# Support extensionless \includegraphics{...} paths by adding symlink aliases.
44if [[ -d "$TMP_TEX_ROOT/figures" ]]; then
45 while IFS= read -r -d '' figure_file; do
46 figure_base="${figure_file%.*}"
47 if [[ "$figure_file" != "$figure_base" && ! -e "$figure_base" ]]; then
48 ln -s "$(basename "$figure_file")" "$figure_base"
49 fi
50 done < <(find "$TMP_TEX_ROOT/figures" -maxdepth 1 -type f -print0)
51fi
52
53(
54 cd "$TMP_TEX_ROOT/build"
55 latexpand main.tex > "$ROOT_DIR/build/flat/flattened.tex"
56)
57
58# Preserve references as labels (instead of dropping to empty text) so markdown
59# remains readable even without TeX cross-reference resolution.
60perl -0777 -i -pe '
61 s/\\labelcref\{([^}]+)\}/$1/g;
62 s/\\eqref\{([^}]+)\}/$1/g;
63 s/\\ref\{([^}]+)\}/$1/g;
64 s/\\Cref\{([^}]+)\}/$1/g;
65 s/\\cref\{([^}]+)\}/$1/g;
66 s/\{\\sf\{([^{}]+)\}\}/\\texttt{$1}/g;
67 s/\\sf\{([^{}]+)\}/\\texttt{$1}/g;
68 s/\{\\tt\{([^{}]+)\}\}/\\texttt{$1}/g;
69 s/\\tt\{([^{}]+)\}/\\texttt{$1}/g;
70' build/flat/flattened.tex
71
72pandoc build/flat/flattened.tex \
73 --from=latex+raw_tex \
74 --to=gfm+tex_math_dollars \
75 --wrap=none \
76 --markdown-headings=atx \
77 --resource-path=build/flat:build/flat/texsrc/build:build/flat/texsrc/sections:build/flat/texsrc/figures:v2/build:v2/sections:v2/figures:v2/bibliography \
78 --citeproc \
79 --bibliography=v2/bibliography/bibtex.bib \
80 --extract-media=docs/media \
81 --strip-comments \
82 -o docs/paper.md
83
84# Clean residual LaTeX wrappers that are not useful in markdown output.
85perl -0777 -i -pe '
86 s/\\hyperlink\{[^{}]*\}\{([^{}]*)\}/$1/g;
87 s/\\hypertarget\{[^{}]*\}\{([^{}]*)\}/$1/g;
88 s/\\ensuremath\{([^{}]+)\}/$1/g;
89 s/\\detokenize\{([^{}]+)\}/$1/g;
90 s/\{\\sf\{([^{}]+)\}\}/\\texttt{$1}/g;
91 s/\\sf\{([^{}]+)\}/\\texttt{$1}/g;
92 s/\{\\tt\{([^{}]+)\}\}/\\texttt{$1}/g;
93 s/\\tt\{([^{}]+)\}/\\texttt{$1}/g;
94 s/\\xspace//g;
95 s/\\hspace\{[^}]*\}//g;
96 s/\\rule\{[^}]*\}\{[^}]*\}//g;
97 s/\\allowbreak//g;
98 s/\$\$\\label\{[^}]+\}\s*/\$\$/g;
99 s/\\:\|\|\\:/ || /g;
100 s/\[\]\(#\\detokenize\{([^}]*)\}\)/$1/g;
101' docs/paper.md
102
103test -s docs/paper.md