-
Notifications
You must be signed in to change notification settings - Fork 2
/
ssb
executable file
·328 lines (283 loc) · 6.68 KB
/
ssb
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/bin/sh
# This uses barebones POSIX sh so we disable warning about `expr` being
# antiquated.
# shellcheck disable=SC2003
# We also ignore SC2124, and SC2068 in specific lines to stick with
# space-separated iterables.
set -eu
usage()
{
printf \
"ssb - simple static blogger.
Translates input markdown files to html pages. Attaches html header and
footer to each output. Files from the posts directory are appended to a
blog list at the end of each html.
Usage: ssb [-d|-g|-h|-r] [-e HEADER_PATH] [-f FOOTER_PATH]\
[-m MD_RENDERER] [-o OUTPUT_DIR] [-p POSTS_DIR] <MARKDOWN_FILES ...>
Positional arguments:
MARKDOWN_FILES - Paths to arbitrary number of markdown files that will be
translated into html files but not appended to the posts list (but will still
display it at the bottom). It is intended to pass index.md here.
Options:
-d Don't attach posts list to each html file.\n\
-g Generate html templates for header, footer and stylesheet.
-h Show this help message.
-r Recurse the posts directory.
-v Be verbose (print each command being executed).
-e HEADER_PATH Path to a header html file (header.html by default).
-f FOOTER_PATH Path to a footer html file (footer.html by default).
-m MD_RENDERER Markdown renderer command name. Must be able to read
markdown form stdin and output html to stdout (pandoc by default).
-o OUTPUT_DIR Output directory for html files (. by default).
-p POSTS_DIR Path to a directory with markdown posts (posts by default).
Usage tips:
The timestamps near posts titles are generated from modification dates of
Markdown files.
If you wish to change them use the touch command (touch -t YYMMDDhhmm).
Credentials: https://github.com/maciejzj/ssb
"
}
parse_optargs()
{
while getopts "dghre:f:m:o:p:v:" opt; do
case $opt in
d)
DISABLE_POSTS=true;;
g)
gen_template
exit 0;;
h)
usage
exit 0;;
r)
RECURSE_POSTS=true;;
v)
VERBOSE=true;;
e)
HEADER_PATH=$OPTARG;;
f)
FOOTER_PATH=$OPTARG;;
m)
MD_RENDERER=$OPTARG;;
o)
OUTPUT_DIR=$OPTARG;;
p)
POSTS_DIR=$OPTARG;;
*)
printf "Received invalid argument\n\n" 1>&2
usage
# Exit code: EINVAL (invalid argument)
exit 22
esac
done
}
set_default_args()
{
DISABLE_POSTS=false
RECURSE_POSTS=false
VERBOSE=false
HEADER_PATH=./header.html
FOOTER_PATH=./footer.html
MD_RENDERER=pandoc
OUTPUT_DIR=.
POSTS_DIR=./posts
}
echo_header_template()
{
echo \
'<!DOCTYPE html>
<html lang=en>
<head>
<meta charset="utf-8">
<title>Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<header>
<h1>Title</h1>
<nav>
<a href="./index.html">home</a>
<a href="mailto:">email</a>
</nav>
</header>
<main>
'
}
echo_footer_template()
{
echo \
'
</main>
<footer>
Created with <a href="https://github.com/maciejzj/ssb">ssb</a> – a simple
static blogger.
</footer>
</body>
</html>'
}
echo_css_template()
{
echo \
'body { max-width: 40em; margin: auto; padding: 1.5em }
header { font-size: 1.2em; }
footer { text-align: right; }
main { }
nav { }
h1 { }
h2 { }
h3 { }
h4 { }
h5 { }
h6 { }
p { }
a { }
li { }
ul { }
ol { }
dl { }
dt { }
dd { }
hr { }
figure { }
figcaption { }
img { }
video { }
table { }
th { }
tr { }
td { }
blockquote { }
pre { }
img, video { width: 100%; }
pre { max-width: 100%; overflow-x: auto; }
table { max-width: 100%; overflow-x: auto; display: block; border-collapse: collapse; }
@media only screen and (max-width: 600px)
{
body { }
}
code span.al { color: #ff0000; font-weight: bold; }
/* Annotation */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
/* Attribute */
code span.at { color: #7d9029; }
/* BaseN */
code span.bn { color: #40a070; }
/* BuiltIn */
code span.bu { }
/* ControlFlow */
code span.cf { color: #007020; font-weight: bold; }
/* Char */
code span.ch { color: #4070a0; }
/* Constant */
code span.cn { color: #880000; }
/* Comment */
code span.co { color: #60a0b0; font-style: italic; }
/* CommentVar */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
/* Documentation */
code span.do { color: #ba2121; font-style: italic; }
/* DataType */
code span.dt { color: #902000; }
/* DecVal */
code span.dv { color: #40a070; }
/* Error */
code span.er { color: #ff0000; font-weight: bold; }
/* Extension */
code span.ex { }
/* Float */
code span.fl { color: #40a070; }
/* Function */
code span.fu { color: #06287e; }
/* Import */
code span.im { }
/* Information */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
/* Keyword */
code span.kw { color: #007020; font-weight: bold; }
/* Operator */
code span.op { color: #666666; }
/* Other */
code span.ot { color: #007020; }
/* Preprocessor */
code span.pp { color: #bc7a00; }
/* SpecialChar */
code span.sc { color: #4070a0; }
/* SpecialString */
code span.ss { color: #bb6688; }
/* String */
code span.st { color: #4070a0; }
/* Variable */
code span.va { color: #19177c; }
/* VerbatimString */
code span.vs { color: #4070a0; }
/* Warning */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }'
}
gen_template()
{
echo_header_template > ./header.html
echo_footer_template > ./footer.html
echo_css_template > ./styles.css
}
get_posts()
{
if [ "$RECURSE_POSTS" = false ]; then
MAXDEPTH_OPT="-maxdepth"
DEPTH_LIMITER="1"
else
MAXDEPTH_OPT=""
DEPTH_LIMITER=""
fi
find "$POSTS_DIR" "$MAXDEPTH_OPT" "$DEPTH_LIMITER" -type f -name "*.md"
}
get_mod_date()
{
case $(uname) in
Linux)
stat -c %y "$1" | cut -d ' ' -f 1;;
Darwin|*BSD)
stat -t "%Y-%m-%d" -f "%Sm" "$1";;
esac
}
append_posts_list()
{
posts_list=""
# shellcheck disable=SC2068
for post in $@; do
file_base="$(basename "$post" .md)"
date=$(get_mod_date "$post")
post_title="$(grep -m 1 "^# .*" "$post" | cut -c 3-)"
post_link="$date – [$post_title]($file_base.html)<br>\n"
posts_list="$posts_list$post_link"
done
printf "\n---\n"
echo "$posts_list" | sort -r
}
make_html_files()
{
# shellcheck disable=SC2068
for md_file in $@; do
file_base=$(basename "$md_file" .md)
output_file="$OUTPUT_DIR/${file_base}.html"
tempf=$(mktemp)
# Ignore the suggestion because tempf can be expanded immediately
# shellcheck disable=SC2064
trap "rm $tempf" 0 2 3 15
append_posts_list "$posts" | cat "$md_file" - | eval "$MD_RENDERER" > "$tempf"
cat "$HEADER_PATH" "$tempf" "$FOOTER_PATH" > "$output_file"
done
}
set_default_args
parse_optargs "$@"
shift "$(expr "$OPTIND" - 1)"
if "$VERBOSE"; then
set -x
fi
posts=""
# shellcheck disable=SC2124
pages="$@"
[ -z "$pages" ] && echo No markdown pages given to render && exit 1
[ $DISABLE_POSTS != true ] && posts="$(get_posts)"
md_files="$pages $posts"
make_html_files "$md_files"