-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiki2html_pandoc
executable file
·100 lines (93 loc) · 3.82 KB
/
wiki2html_pandoc
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
#!/bin/bash
# Vimwiki (markdown style) conversion script using pandoc.
# This script shall be used as the source for vimwiki's custom wiki2html
# conversion binary.
#
# Usage: cf https://github.com/vimwiki/vimwiki/blob/master/doc/vimwiki.txt#L2216
# Requirements: pandoc (https://pandoc.org/)
# check that pandoc is available
if ! command -v pandoc >/dev/null; then
echo -e "pandoc cannot be found! Please install it first from https://pandoc.org"
exit 1
fi
# parse `force` argument: assert that it is either 0 or 1
force=$1
if [[ ${force} -ne 0 ]] && [[ ${force} -ne 1 ]]; then
echo -e "Invalid 'force' argument! It should be either 0 or 1."
exit 1
fi
# parse `syntax` argument: map vimwiki namings and assert it is supported by pandoc
syntax=$2
if [[ "${syntax}" == "default" ]]; then
syntax="vimwiki"
elif [[ "${syntax}" == "media" ]]; then
syntax="mediawiki"
fi
# shellcheck disable=SC2207
syntaxes=($(awk '{print}' <(pandoc --list-input-formats)))
if ! [[ ${syntaxes[*]} =~ (^|[[:space:]])$syntax($|[[:space:]]) ]]; then
echo -e "Unsupported input format '${syntax}' for pandoc!"
exit 1
fi
# parse `extension` argument
extension=$3
# parse `output_dir` argument: assert directory exists
output_dir=$4
if [[ ! -d "${output_dir}" ]]; then
echo -e "The output directory '${output_dir}' does not exist or is not a directory!"
exit 1
fi
# parse `input_file` argument: assert file exists
input_file=$5
if [[ ! -f "${input_file}" ]]; then
echo -e "The input file '${input_file}' does not exist or is not a regular file!"
exit 1
fi
# parse `css_file` argument: assert file exists
css_file=$6
if [[ "${css_file}" != "" ]] && [[ ! -f "${css_file}" ]]; then
echo -e "The css file '${css_file}' does not exist or is not a regular file!"
exit 1
fi
# NOTE: the remaning arguments are optional! If unspecified, they default to '-'
# parse `template_path` argument: assert directory exists
template_path=$7
if [[ "${template_path}" != -* ]] && [[ ! -d "${template_path}" ]]; then
echo -e "The template path '${template_path}' does not exist or is not a directory!"
exit 1
fi
# parse `template_default` argument
template_default=$8
# parse `template_ext` argument
template_ext=$9
# assert template `template_path/template_default.templat_ext` exists
template_file="${template_path}/${template_default}.${template_ext}"
if [[ "${template_file}" != -* ]] && [[ ! -f "${template_file}" ]]; then
echo -e "The template file '${template_file}' does not exist or is not a regular file!"
exit 1
fi
# NOTE: the remaining optional arguments (`root_path` and `custom_args`) are ignored!
output_file=${input_file##*/}
output_file=${output_file%.${extension}}
if [[ -f "${output_dir}/${output_file}.html" ]] && [[ ${force} -ne 1 ]]; then
echo -e "File ${output_file}.html already exists! Specify force = 1 to overwrite."
exit 1
fi
# preprocess the input_file
contents=$(cat "${input_file}")
# Links to specific sections [desc](#section) need to have spaces replaced by hyphens
contents=$(sed -E ":a;s/\[([^]]+)\]\(\#([^\)]+)\ ([^\)]+)\)/\[\1\]\(\#\2-\3\)/;ta;" <<< "${contents}")
# Links to specific sections [desc](#section) need to be all lowercase
contents=$(sed -E "s/\[([^]]+)\]\(\#([^\)]+)\)/\[\1\]\(\#\L\2\)/g" <<< "${contents}")
# Links of other files [desc](file) need to have .html appended to them [desc](file.html)
contents=$(sed -E "/(!\[.*|.*http.*)/b; s/\[([^]]+)\]\(([^\#\)]+)\)/\[\1\]\(\2.html\)/g" <<< "${contents}")
# perform actual conversion with pandoc
if [[ "${template_file}" != -* ]]; then
pandoc -f "${syntax}" -t html5 -o "${output_dir}"/"${output_file}".html \
--self-contained --css="${css_file}" --template="${template_file}" \
< "${contents}"
else
pandoc -f "${syntax}" -t html5 -o "${output_dir}"/"${output_file}".html \
--self-contained --css="${css_file}" \
<<< "${contents}"
fi