-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuilddoc
executable file
·157 lines (131 loc) · 3.64 KB
/
builddoc
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
#----------------------------------------------------
# name: builddoc
# author: Seamus Tuohy
# created: 02/18/2015
# version: 0.0.1
# description: A simple script to automate building pdf's from markdown.
#----------------------------------------------------
#exit the script if any statement returns a non-true return value
#set -e
#set -x
readonly BUILD_DIRECTORY=$(mktemp -d)
readonly PROG_DIR=$(readlink -m $(dirname $0))
readonly RUN_DIR=$(pwd)
readonly DOC_MARKDOWN=markdown_test.md
readonly DOC_HTML=html_test.html
usage() {
cat <<EOF
Usage:
builddoc [-hxv] [-i <Input Directory>] [-t <Theme Directory>] [-o <Output Filename>]
Options:
i) The input directory containing an index file.
I) The index file name (REQUIRED if index is not named index.md)
o) The output file name.
t) The theme directory containing the theme file and style sheet and all resources they reference.
T) The theme file name (REQUIRED if theme not named theme.template)
s) The css style sheet file name (REQUIRED if style sheet not named style.css)
h) show this screen.
v) Turn on verbosity.
d) Turn on debugging.
EOF
}
build() {
cd "$BUILD_DIRECTORY"
make_markdown
make_html
make_pdf
}
make_markdown() {
cd "$BUILD_DIRECTORY"/target
markdown-pp -o "$BUILD_DIRECTORY"/"$DOC_MARKDOWN" "$BUILD_DIRECTORY"/target/"$INDEX_FILE"
cd "$RUN_DIR"
}
make_html() {
cd "$BUILD_DIRECTORY"/target
pandoc -s --template="$BUILD_DIRECTORY"/theme/"$DOC_THEME" \
--to=html5 "$BUILD_DIRECTORY"/"$DOC_MARKDOWN" \
--output="$BUILD_DIRECTORY"/"$DOC_HTML"
cd "$RUN_DIR"
}
make_pdf() {
cd "$BUILD_DIRECTORY"/target
#wkhtml creates links starting at the path of the html file
cp "$BUILD_DIRECTORY"/"$DOC_HTML" "$BUILD_DIRECTORY"/target/"$DOC_HTML"
wkhtmltopdf --user-style-sheet "$BUILD_DIRECTORY"/theme/"$DOC_STYLE_SHEET" \
--disable-smart-shrinking \
--load-error-handling skip \
--load-media-error-handling skip \
--footer-center "Page [page] of [toPage]" \
--outline \
--outline-depth 3 \
"$BUILD_DIRECTORY"/target/"$DOC_HTML" "$RUN_DIR"/"$DOC_FILENAME"
cd "$RUN_DIR"
}
copy_theme() {
cp -fr "$THEME_DIR" "$BUILD_DIRECTORY"/theme
}
copy_raw() {
cp -fr "$TARGET_DIR" "$BUILD_DIRECTORY"/target
}
clean() {
echo "Cleaning build directory."
rm -fr "$BUILD_DIRECTORY"
echo "Build directory cleaned."
}
main() {
copy_theme
copy_raw
build
if [[ $DEBUG != "true" ]]; then
clean
else
echo "Debug mode is activated. The temporary build directory $BUILD_DIRECTORY will not be deleted after building."
fi
}
while getopts "vhds:t:T:i:I:o:" OPTION; do
case $OPTION in
o)
readonly DOC_FILENAME=$OPTARG
;;
i)
readonly TARGET_DIR=$OPTARG
;;
I)
indexfile=$OPTARG
;;
h)
usage
exit 0
;;
d)
readonly DEBUG="true"
set -x
;;
v)
readonly VERBOSE=1
;;
s)
docstyle="$OPTARG"
;;
t)
readonly THEME_DIR=$OPTARG
;;
T)
doctheme="$OPTARG"
;;
esac
done
if [[ $indexfile == "" ]]; then
indexfile="index.md"
fi
readonly INDEX_FILE="$indexfile"
if [[ $docstyle == "" ]]; then
docstyle="style.css"
fi
readonly DOC_STYLE_SHEET="$docstyle"
if [[ $doctheme == "" ]]; then
doctheme="theme.template"
fi
readonly DOC_THEME="$doctheme"
main