-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_tutorials.sh
More file actions
executable file
·113 lines (94 loc) · 2.88 KB
/
build_tutorials.sh
File metadata and controls
executable file
·113 lines (94 loc) · 2.88 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
#!/usr/bin/env bash
set -euo pipefail
# ------------------------------------------------------------
# Build a single PDF from BoolForge tutorial notebooks
# ------------------------------------------------------------
TUTORIAL_DIR="tutorials"
MD_DIR="tutorials_md"
OUT_PDF="BoolForge_Tutorials.pdf"
HEADER_TEX="tutorials_header.tex"
TITLE="BoolForge Tutorials"
AUTHOR="Claus Kadelka"
echo "==> Cleaning previous build artifacts"
rm -rf "$MD_DIR" "$OUT_PDF" "$HEADER_TEX"
mkdir -p "$MD_DIR"
# ------------------------------------------------------------
# Convert notebooks to Markdown
# ------------------------------------------------------------
echo "==> Converting notebooks to Markdown"
for nb in "$TUTORIAL_DIR"/*.ipynb; do
jupyter nbconvert \
--to markdown \
--TemplateExporter.exclude_input_prompt=True \
--TemplateExporter.exclude_output_prompt=True \
"$nb" \
--output-dir "$MD_DIR"
done
# ------------------------------------------------------------
# Remove "png" captions
# ------------------------------------------------------------
echo "==> Cleaning figure captions"
for f in "$MD_DIR"/*.md; do
sed -i '' 's/!\[png\]/![]/g' "$f"
done
# ------------------------------------------------------------
# Add tutorial titles from filenames
# ------------------------------------------------------------
#echo "==> Adding tutorial titles"
#
#for f in "$MD_DIR"/*.md; do
# title=$(basename "$f" .md | sed 's/_/ /g')
# sed -i '' "1s/^/# $title\n\n/" "$f"
#done
# ------------------------------------------------------------
# Create LaTeX header
# ------------------------------------------------------------
echo "==> Writing LaTeX header"
cat > "$HEADER_TEX" <<'EOF'
\usepackage{graphicx}
\usepackage{float}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{caption}
% allow alt= in includegraphics
\makeatletter
\define@key{Gin}{alt}{}
\makeatother
% prevent oversized figures
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\makeatother
\setkeys{Gin}{width=\maxwidth,height=0.8\textheight,keepaspectratio}
% new page per tutorial
\usepackage{etoolbox}
\pretocmd{\section}{\clearpage}{}{}
% nicer captions
\captionsetup{
font=small,
labelfont=bf
}
EOF
# ------------------------------------------------------------
# Build PDF
# ------------------------------------------------------------
echo "==> Building $OUT_PDF"
pandoc \
"$MD_DIR"/*.md \
--resource-path="$MD_DIR" \
--from markdown-yaml_metadata_block+tex_math_dollars \
--pdf-engine=xelatex \
--toc \
--number-sections \
--syntax-highlighting=tango \
-M title="$TITLE" \
-M author="$AUTHOR" \
-M date="$(date '+%B %d, %Y')" \
-V geometry:margin=1in \
-V documentclass=article \
-V mainfont="Latin Modern Roman" \
-V monofont="Menlo" \
-V colorlinks=true \
-V linkcolor=blue \
-H "$HEADER_TEX" \
-o "$OUT_PDF"
echo "==> Done: $OUT_PDF"