/
md-to-html5
executable file
·181 lines (156 loc) · 4.13 KB
/
md-to-html5
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
#! /bin/sh
HERE=$(cd $(dirname $0); pwd)
THIS=$(basename $0)
# Our HTML5 template, produced from the default pandoc template with some
# massaging by 'mk-pandoc-template'.
template="$HERE/../inc/pandoc-template.html5"
# Use the solarized highlight theme, since that's what we use in the blog
# See https://pandoc.org/MANUAL.html#syntax-highlighting
highlightstyle="$HERE/../inc/pandoc-solarized.theme"
######################################################################
#
# Option processing
#
output=
index=
shortopts='io:h'
longopts='index,output:,help'
usage="\
Usage 1: $THIS [ options ] [ key=value ... ] < file.md > file
Usage 2: $THIS [ options ] [ key=value ... ] file.md ...
Options:
-o, --output=PATH Relative path of the output file
-i, --index Indicates that this is an index file
-h, --help Output this usage and do nothing else
In usage 1, the template is read from standard input and the processing
result is output to the path given by --output. In this usage, the --output
option is mandatory.
In usage 2, the templates are read from the files given as argument, and
the processing result for each of them is written to a corresponding file
without the '.md' suffix. All given file names must have the '.md' suffix.
In this usage, the --output option is forbidden.
In both usages, one can also set template variables with the form key=value.
They must come before any file name."
# Standard getopt calling sequence
if ! TEMP=$(getopt -o "$shortopts" --long "$longopts" -n $THIS -- "$@"); then
echo >&2 "$usage"
exit 1
fi
eval set -- "$TEMP"
unset TEMP
# Check the parsed options
while true; do
case "$1" in
'-o' | '--output' )
output="$2"
shift 2
;;
'-i' | '--index' )
index=1
shift
;;
'-h' | '--help' )
echo >&2 "$usage"
exit 0
;;
'--' )
shift
break
;;
* )
echo >&2 'Internal error!'
echo >&2 "$usage"
exit 1
;;
esac
done
######################################################################
#
# Main
#
# If there are no other arguments, read from stdin, write to path given with
# --output. Otherwise, read from the input files and write to corresponding
# output files.
if [ $# -eq 0 ]; then
if [ -z "$output" ]; then
echo >&2 'Output path must be set with -o / --output in this mode'
exit 1
fi
if [ "$(basename "$output" .html)" = "$output" ]; then
echo >&2 'Output path must end with .html'
exit 1
fi
# Set '-' to mean stdin / stdout
set -- -
elif [ -n "$output" ]; then
echo >&2 '-o / --output is confusing in this mode'
exit 1
fi
# Check that all the arguments are existing and correctly named files
errfiles=
nofiles=
for f in "$@"; do
[ "$f" = "-" ] && continue
base=$(basename "$f" .md)
if [ "$base" = "$f" ]; then
errfiles="$errfiles '$f'"
elif [ ! -f "$f" ]; then
nofiles="$nofiles '$f'"
fi
done
if [ -n "$errfiles" ]; then
echo >&2 "Files not ending with .md:$errfiles"
fi
if [ -n "$nofiles" ]; then
echo >&2 "Files no present:$nofiles"
fi
if [ -n "$errfiles" -o -n "$nofiles" ]; then
exit 1
fi
for f in "$@"; do
if [ "$f" != "-" ]; then
base=$(basename "$f" .md)
dir=$(dirname "$f")
if [ "$f" = "$base" ]; then
continue;
fi
else
base=$(basename "$output" .html)
dir=$(dirname "$output")
fi
if [ "$dir" = "." -o "$dir" = "" ]; then
title="/$base.html"
top=""
else
title="/$dir/$base.html"
top=`echo "$dir" | sed -E -e 's|[^/]+|..|g'`/
fi
# is it an index file?
def_isindex=
if [ -n "$index" -o "$base" = "index" ]; then
def_isindex="-M is-index=true"
fi
(
# metadata
meta="$dir/dirdata.yaml"
if [ -f "$meta" ]; then
cat $meta
fi
if [ "$f" = "-" ]; then
cat
else
cat "$f"
fi
) | (
cd $dir
pandoc -t html5 -f markdown --template="$template" \
--highlight-style="$highlightstyle" \
--tab-stop=8 --shift-heading-level-by=1 \
-M author-meta='OpenSSL Foundation, Inc.' \
-M lang=en \
-M pagetitle="$title" \
$def_isindex -M top="$top"
) | (
cat > "$dir/$base.html"
)
done