-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmakedown.sh
executable file
·261 lines (227 loc) · 7.5 KB
/
makedown.sh
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/bin/sh
#
# makedown.sh
# part of makedown, a build system for making markdown websites.
# https://github.com/somasis/makedown
#
# create nice HTML with `markdown` and add some templating functionality.
#
set -e
stderr() {
printf '%s: %s\n' "${0##*/}" "$*" >&2
}
edo() {
stderr "$*"
"$@"
}
die() {
stderr "$1"
exit $2
}
help() {
printf '%s\n\n' \
"usage: ${0##*/} [--help] [--flags <flags>] [--name <site name>] [--append <file>] [--print-template] [--print-title] [--print-description] <input>"
printf ' %-10s %s\n' \
"--flags <flag1,flag2>" "This argument is passed to \`markdown\`. Comma separated." \
"--print-template" "Print the template that would be used for generating <input>'s HTML." \
"--print-title" "Print <input>'s title." \
"--print-description" "Print <input>'s description." \
"--append <file>" "Markdown formatted file to append to the input before converting to HTML." \
"--name <site name>" "Set the site name. Used for setting the page titles." \
"--help" "This."
}
template_only=false
title_only=false
description_only=false
if [ "${1}" = "--help" ] || [ $# -lt 1 ];then
help
exit 255
fi
while [ $# -ne 1 ];do
case "${1}" in
--flags)
flags="${2}"
shift
;;
--append)
append="${2}"
shift
;;
--name)
site_name="${2}"
shift
;;
--print-template)
template_only=true
;;
--print-title)
title_only=true
;;
--print-description)
description_only=true
;;
esac
shift
done
input="${1}"
if [ -z "${input}" ];then
help
exit 255
elif ! [ -r "${input}" ];then
die "Can't read \"${input}\"." 13
fi
markdown_flags="-f toc,html5anchor${flags:+,${flags}}"
temp_recurse() {
# recurse to find parent templates
base=$(basename "${input}" .md)
old_pwd="${PWD}"
cd "${dir}"
until [ "${PWD}" = / ];do
for f in "${PWD}"/${base}.template "${PWD}"/page.template;do
[ -e "${f}" ] && printf '%s' "${f}" && return 0
done
cd ..
done
cd "${old_pwd}"
stderr "couldn't find a template to use for \"${input}\"."
return 1
}
finish() {
rm -f "${body}" "${bodymd}" "${html}" "${html2}" "${toc}" "${temp}" "${temp2}"
}
trap finish EXIT
dir=$(readlink -f "${input}")
dir=${dir%/*}
temp=$(temp_recurse "${input}")
if [ "${template_only}" = true ];then
printf '%s\n' "${temp}"
temp=
exit 0
fi
temp2=$(mktemp)
stderr "cat \"${temp}\" > \"${temp2}\""
cat "${temp}" > "${temp2}"
temp="${temp2}";temp2=$(mktemp)
# Remove top markdown header; just the body is needed
body=$(mktemp)
bodymd=$(mktemp)
html=$(mktemp)
html2=$(mktemp)
toc=$(mktemp)
has_title=false
has_description=false
# # title\n
if [ $(sed -n '1p' "${input}" | grep -Ec '^# ') -eq 1 ] && \
[ $(sed -n '2p' "${input}" | grep -Ec '^## ') -lt 1 ];then
title=$(sed '1!d;s/^# //' "${input}")
description=
has_title=true
has_description=false
stderr "${input} has a title and no description"
elif [ $(sed -n '1p;2p' "${input}" | grep -Ec '^#{1,2} ') -lt 2 ];then
stderr "error: \"${input}\" should contain a title and description in the format of:"
stderr "# Title"
stderr "## Description."
stderr "as the first two lines of the file."
exit 5
# # title\n## description\n
else
title=$(sed '1!d;s/^# //' "${input}")
description=$(sed '2!d;s/^## //' "${input}")
has_title=true
has_description=true
fi
if [ "${title_only}" = true ];then
"${has_title}" && printf '%s\n' "${title}" && exit
exit 1
elif [ "${description_only}" = true ];then
"${has_description}" && printf '%s\n' "${description}" && exit
exit 1
fi
title_unprefixed="${title}"
# <site name> - <page title>, unless site name isn't set
if [ -n "${site_name}" ] && [ "${title}" != "${site_name}" ];then
title="${site_name} - ${title}"
fi
case ${has_title}${has_description} in
truetrue)
stderr "sed '1d;2d' \"${input}\" > \"${bodymd}\""
sed '1d;2d' "${input}" > "${bodymd}"
;;
truefalse)
stderr "sed '1d' \"${input}\" > \"${bodymd}\""
sed '1d' "${input}" > "${bodymd}"
;;
*)
exit 6
;;
esac
if [ -n "${append}" ];then
stderr "cat \"${append}\" >> \"${bodymd}\""
cat "${append}" >> "${bodymd}"
fi
# FIXME: this would be better done in discount itself.
# sed generates two anchors, one with dashes and one with underscores; however, it makes incorrect changes
# with anchors that contain dashes.
# avoids breaking mediawiki style anchor links.
stderr "markdown ${markdown_flags} \"${bodymd}\" | sed -E '/^<a name=\".+\"><\/a>$/ { p; s/-/_/g; }' > \"${body}\""
markdown ${markdown_flags} "${bodymd}" | sed -E '/^<a name=".+"><\/a>$/ { p; s/-/_/g; }' > "${body}"
stderr "markdown ${markdown_flags} -T -n \"${bodymd}\" > \"${toc}\""
markdown ${markdown_flags} -T -n "${bodymd}" > "${toc}"
# get metadata about input file
if git rev-parse HEAD >/dev/null 2>&1 && git ls-files --error-unmatch "${input}" >/dev/null 2>&1;then
tree_commit=$(git rev-parse HEAD)
file_commit=$(git log --format='%H' -1 "${input}")
author=$(git shortlog -ns "${input}" | sed 's/.*\t//;s/$/,/')
if [ $(printf '%s\n' "${author}" | wc -l) -gt 10 ];then
author=$(printf '%s\n' "${author}" | head -n 10)
author=$(printf '%s\n' "${author}" "...")
fi
author=$(printf '%s\n' "${author}" | tr '\n' ' ' | sed -E 's/,? $//')
date=$(git log --date='format:%Y-%m-%d' --format='%ad' -1 "${input}")
fi
markdown_version=$(markdown ${markdown_flags} -VV)
htmlescape() {
printf '%s' "$@" | sed 's/</\</g;s/>/\>/g;s/&/\&/g'
}
# we can definitely make this a dynamic thing, it'll have to use eval, though.
tree_commit_escaped=$(htmlescape "$tree_commit")
file_commit_escaped=$(htmlescape "$file_commit")
site_name_escaped=$(htmlescape "$site_name")
title_escaped=$(htmlescape "$title")
title_unprefixed_escaped=$(htmlescape "$title_unprefixed")
description_escaped=$(htmlescape "$description")
author_escaped=$(htmlescape "$author")
date_escaped=$(htmlescape "$date")
markdown_version_escaped=$(htmlescape "${markdown_version}")
edo sed \
-e "s|<?makedown tree_commit?>|${tree_commit}|g" \
-e "s|<?makedown file_commit?>|${file_commit}|g" \
-e "s|<?makedown site_name?>|${site_name}|g" \
-e "s|<?makedown title?>|${title}|g" \
-e "s|<?makedown title_unprefixed?>|${title_unprefixed}|g" \
-e "s|<?makedown description?>|${description}|g" \
-e "s|<?makedown author?>|${author}|g" \
-e "s|<?makedown date?>|${date}|g" \
-e "s|<?makedown version?>|${markdown_version}|g" \
-e "s|<?makedown tree_commit_escaped?>|${tree_commit_escaped}|g" \
-e "s|<?makedown file_commit_escaped?>|${file_commit_escaped}|g" \
-e "s|<?makedown site_name_escaped?>|${site_name_escaped}|g" \
-e "s|<?makedown title_escaped?>|${title_escaped}|g" \
-e "s|<?makedown title_unprefixed_escaped?>|${title_unprefixed_escaped}|g" \
-e "s|<?makedown description_escaped?>|${description_escaped}|g" \
-e "s|<?makedown author_escaped?>|${author_escaped}|g" \
-e "s|<?makedown date_escaped?>|${date_escaped}|g" \
-e "s|<?makedown version_escaped?>|${markdown_version_escaped}|g" \
"${temp}" > "${temp2}"
edo sed \
"/<?makedown body?>/{
r ${body}
d
}" "${temp2}" > "${html}"
edo sed \
"/<?makedown toc?>/{
r ${toc}
d
}" "${html}" > "${temp2}"
cat "${temp2}"