-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown2pdf
executable file
·106 lines (90 loc) · 2.2 KB
/
markdown2pdf
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
#!/bin/bash
#
# Converts FYST markdown documents to pdf using pandoc.
#
set -eu
usage() {
echo "Usage: markdown2pdf [OPTION]... INPUT.md [OUTPUT.pdf]"
}
error() {
echo "$@"
exit 1
}
# defaults
data_dir=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
echo $data_dir
pandoc_opts=(
"--data-dir=$data_dir"
"--filter=$data_dir/filters/diagrams.py"
"--filter=pandoc-citeproc"
"--latex-engine=xelatex"
"--standalone"
"--variable=fontsize:11pt"
"--variable=geometry:margin=1in"
"--variable=graphics-path:$data_dir"
"--variable=mainfont:DejaVu Serif"
)
# options
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
--table-of-contents|--toc)
pandoc_opts+=("--table-of-contents")
shift
;;
--)
shift
break
;;
*)
break
;;
esac
done
case $# in
1)
md=$1
pdf=$(basename "${md%.md}.pdf")
;;
2)
md=$1
pdf=$2
;;
*)
usage
exit 1
;;
esac
[[ -f $md ]] || error "not a regular file: $md"
[[ -d $pdf ]] && pdf="$pdf/$(basename "${md%.md}.pdf")"
# colophon
colophon="pdf created $(date --iso-8601=seconds) by $USER from $(realpath $md)"
colophon=$(sed 's/_/\\_/g' <<< "$colophon")
pandoc_opts+=("--variable=colophon:$colophon")
# git info
wd=$(pwd)
cd $(dirname "$md")
if [[ -d .git ]]; then
git_desc=$(git describe --all --always --long 2>/dev/null || echo none)
git_origin=$(git config remote.origin.url || echo none)
git_origin=${git_origin#git@}
git_origin=${git_origin%.git}
git_status=$(git status --porcelain "$(basename "$md")" | cut -c-2)
case $git_status in
'') git_status=ok ;;
' M') git_status=modified ;;
'??') git_status=untracked ;;
esac
git_info="git origin=$git_origin commit=$git_desc status=$git_status"
git_info=$(sed 's/_/\\_/g' <<< "$git_info")
echo $git_info
pandoc_opts+=("--variable=git-info:$git_info")
fi
cd "$wd"
if [[ -f bibliography.bib ]]; then
pandoc_opts+=("--bibliography=bibliography.bib")
fi
pandoc "$md" -o "$pdf" "${pandoc_opts[@]}"