arnaucube
arnaucube/davinci-paperPublic Self-hosted repository
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# Support extensionless \includegraphics{...} paths by adding symlink aliases.
26if [[ -d "$TMP_TEX_ROOT/figures" ]]; then
27  while IFS= read -r -d '' figure_file; do
28    figure_base="${figure_file%.*}"
29    if [[ "$figure_file" != "$figure_base" && ! -e "$figure_base" ]]; then
30      ln -s "$(basename "$figure_file")" "$figure_base"
31    fi
32  done < <(find "$TMP_TEX_ROOT/figures" -maxdepth 1 -type f -print0)
33fi
34
35(
36  cd "$TMP_TEX_ROOT/build"
37  latexpand main.tex > "$ROOT_DIR/build/flat/flattened.tex"
38)
39
40pandoc build/flat/flattened.tex \
41  --from=latex+raw_tex \
42  --to=gfm+tex_math_dollars \
43  --wrap=none \
44  --markdown-headings=atx \
45  --resource-path=build/flat:build/flat/texsrc/build:build/flat/texsrc/sections:build/flat/texsrc/figures:v2/build:v2/sections:v2/figures:v2/bibliography \
46  --citeproc \
47  --bibliography=v2/bibliography/bibtex.bib \
48  --extract-media=docs/media \
49  --strip-comments \
50  -o docs/paper.md
51
52test -s docs/paper.md