-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmd2HTML.sh
More file actions
73 lines (67 loc) · 2.54 KB
/
md2HTML.sh
File metadata and controls
73 lines (67 loc) · 2.54 KB
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
#!/bin/bash
# Convert all Markdown note files to HTML using pandoc.
# - By default, only regenerates an existing HTML file if the Markdown file is newer.
# - Assumes pandoc is in PATH
#
# Supported arguments (mutually exclusive)
# -c, --clean: remove existing HTML files and exit
# -f, --force: (re)generate all HTML files
# SETUP
set -e -o pipefail
# get directory of this script, which is located with the markdown files
directory=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# (If specified) CLEAN
toclean="$1"
if [ ! -z "$toclean" ] && ([ "$toclean" = "-c" ] || [ "$toclean" = "--clean" ]); then
# need to check for existence of HTML files first, otherwise rm will complain
# solution from https://unix.stackexchange.com/a/214066
html_files=("${directory}"/*.html)
[ -f ${html_files[0]} ] && rm "${directory}"/*.html
html_files=("${directory}"/*/*.html)
[ -f ${html_files[0]} ] && rm "${directory}"/*/*.html
exit 0
fi
# CONVERT MARKDOWN TO HTML
force="$1"
if [ ! -z "$force" ]; then
if [ ! "$force" = "-f" ] && [ ! "$force" = "--force" ]; then
echo "Did not understand argument $force"
exit 1
fi
force=true
else
force=false
fi
for path in "${directory}"/*.md; do
file="$(basename "$path")"
filename="${file%.*}"
output_path="${path%.*}.html"
if [[ "$file" == "README.md" ]]; then
if [ "$force" = true ] || [ ! -f "$output_path" ] || [ "$path" -nt "$output_path" ]; then
echo "$filename"
sed -e 's/\.md/.html/g' "$path" | \
pandoc -s --mathjax \
--toc --variable toc-title="Contents" --metadata title="$filename" \
-f gfm -t html -o "$output_path"
fi
elif [[ ! $file =~ "LICENSE.md" ]]; then
if [ "$force" = true ] || [ ! -f "$output_path" ] || [ "$path" -nt "$output_path" ]; then
echo "$filename"
pandoc -s --mathjax \
--toc --variable toc-title="Contents" --metadata title="$filename" \
-f gfm -t html -o "$output_path" "$path"
fi
fi
done
for path in "${directory}"/*/*.md; do
file="$(basename "$path")"
subdir="$(basename "$(dirname "$path")")"
filename="${file%.*}"
output_path="${path%.*}.html"
if [ "$force" = true ] || [ ! -f "$output_path" ] || [ "$path" -nt "$output_path" ]; then
echo "${subdir}/${filename}"
pandoc -s --mathjax \
--toc --variable toc-title="Contents" --metadata title="$filename" \
-f gfm -t html -o "$output_path" "$path"
fi
done