-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·236 lines (151 loc) · 5.88 KB
/
run.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
#!/usr/bin/env bash
set -euf -o pipefail
SCRIPT_DIR=`cd $(dirname $0) > /dev/null;pwd`
DEFAULT_WIKI_IN_REPO_URL="https://my/url/to/jrg_wiki"
WIKI_IN_REPO_URL="${1:-$DEFAULT_WIKI_IN_REPO_URL}"
WIKI_IN_SUBSET="${2:-.}"
INCREMENTAL="${3:-}"
NO_GIT_SYNC="${4:-}"
ASSUME_PREBUILT_FILTERS="${5:-}"
OUT_TYPE="${6:-html}"
OUT_DIR="${7:-$SCRIPT_DIR/out_${OUT_TYPE}}"
WIKI_IN_DIR="${8:-$SCRIPT_DIR/wiki_in}"
if [ -z "$NO_GIT_SYNC" ]; then
echo "Updating wiki repository from \`$WIKI_IN_REPO_URL\` to \`$WIKI_IN_DIR\`."
echo "--------------------------------------------------------------------------"
if [ -d "$WIKI_IN_DIR" ]; then
git -C "$WIKI_IN_DIR" pull
else
git clone "$WIKI_IN_REPO_URL" "$WIKI_IN_DIR"
fi
echo ""
echo ""
fi
if [ -z "$INCREMENTAL" ]; then
echo "Removing existing output directory at \`$OUT_DIR\`."
echo "--------------------------------------------------------------------------"
# The following allows the output directory to be a pre-existing git directory.
if [ -d "$OUT_DIR" ]; then
# Remove all the non dot files from the output directory.
find "$OUT_DIR" -mindepth 1 -maxdepth 1 ! -name "\.*" | xargs rm -rf
# Remove the directory itself when found to be not empty.
rmdir --ignore-fail-on-non-empty "$OUT_DIR"
fi
echo ""
echo ""
fi
FILTERS_DIR="$SCRIPT_DIR/filters"
FILTERS_BIN_DIR="$FILTERS_DIR/bin"
export PATH="$PATH:$FILTERS_BIN_DIR:$FILTERS_DIR"
if [ -z "$ASSUME_PREBUILT_FILTERS" ]; then
echo "Compiling all pandoc filters found under \`$FILTERS_DIR\` to \`$FILTERS_BIN_DIR\`."
echo "--------------------------------------------------------------------------"
echo ""
getMinimalPandocTypeVersion() {
echo "1.16"
}
getCurrentPandocTypeVersion() {
if ! command -v ghc-pkg >/dev/null 2>&1 ; then
return 1 # False
fi
if ! local ghcPkgPandocTypesVersionField="$(2>/dev/null ghc-pkg field pandoc-types version)"; then
return 1 # False
fi
if [ -z "$ghcPkgPandocTypesVersionField" ]; then
return 1 # False
fi
local pandocTypesV="$(echo "${ghcPkgPandocTypesVersionField}" | sed -r -e 's/version: ([0-9\.]+)/\1/')"
echo "$pandocTypesV"
}
foundMinimalPandocTypeVersion() {
if ! local pandocTypesV="$(getCurrentPandocTypeVersion)"; then
return 1 # False
fi
if [ -z "$pandocTypesV" ]; then
return 1 # False
fi
test "$(echo "$pandocTypesV" | sed -r -e 's/([0-9]+)\.[0-9]+\.[0-9]\.[0-9]+/\1/')" -eq '1' &&
test "$(echo "$pandocTypesV" | sed -r -e 's/[0-9]+\.([0-9]+)\.[0-9]\.[0-9]+/\1/')" -ge '16'
}
if ! foundMinimalPandocTypeVersion; then
>&2 echo "ERROR: Could not find minimal \`$(getMinimalPandocTypeVersion)\` \`pandoc-types\` "\
"library version required to build \`pandoc\` filters. Current version "\
"is \`$(getCurrentPandocTypeVersion)\`."
exit 1
fi
# Compile pandoc filters
mkdir -p "$FILTERS_BIN_DIR"
find "$FILTERS_DIR" -type f -name "*.hs" | \
while read filterF; do
filerName="`basename "$filterF" .hs`"
if [ ! -e "$FILTERS_BIN_DIR/$filerName" ] || [ "$filterF" -nt "$FILTERS_BIN_DIR/$filerName" ]; then
echo "$filerName"
ghc -o "$FILTERS_BIN_DIR/$filerName" --make "$filterF"
fi
done
echo ""
echo ""
else
PRECOMPILED_DIR="$FILTERS_DIR/bin__${OSTYPE}-`uname -m`"
echo "Validating that pandoc filters under \`$FILTERS_DIR\` are available in precompiled form, possibly under \"$PRECOMPILED_DIR\"."
echo "---------------------------------------------------------------------------------------------"
echo ""
if [ -d "$PRECOMPILED_DIR" ]; then
mkdir -p "$FILTERS_BIN_DIR"
cp -a -t "$FILTERS_BIN_DIR" "$PRECOMPILED_DIR/."
fi
find "$FILTERS_DIR" -type f -name "*.hs" | \
while read filterF; do
filerName="`basename "$filterF" .hs`"
command -v $filerName >/dev/null 2>&1 || \
{ echo "Filter \"${filerName}\" could not be found in precompiled form. Please compile it first and make in available in \"PATH\""; exit 1; }
done
fi
IN_PAGE_EXT="md"
OUT_PAGE_EXT="html"
IN_ROOT_DIR="$WIKI_IN_DIR"
pushd "$IN_ROOT_DIR" > /dev/null
# Copy images and other files which are not pages but where
# delivered alongside pages.
echo "Copying non page files recursively from \`$IN_ROOT_DIR\` to \`$OUT_DIR\`."
echo "--------------------------------------------------------------------------"
echo ""
find "$WIKI_IN_SUBSET" -type f ! -name "*.md" ! -path "./.git/*" | \
while read inF; do
outF="$inF"
outD="`dirname "$OUT_DIR/$outF"`"
if [ ! -e "$OUT_DIR/$outF" ] || [ "$inF" -nt "$OUT_DIR/$outF" ]; then
echo "$inF"
mkdir -p "$outD"
cp -T "$inF" "$OUT_DIR/$inF"
fi
done
echo ""
echo ""
# Transform pages.
echo "Transform pages from \`$IN_ROOT_DIR\` to \`$OUT_DIR\` using pandoc."
echo "--------------------------------------------------------------------------"
echo ""
#echo "./Tests/Plantuml.md" | \
find "$WIKI_IN_SUBSET" -type f -name "*.md" ! -path "./.git/*" | \
while read inF; do
inDir=`dirname $inF`
pathFromInDirToInRoot=`realpath --relative-to="$inDir" $IN_ROOT_DIR`
outF="`echo "$inF" | sed -r -e 's#^(.+)\.'${IN_PAGE_EXT}'$#\1.'${OUT_PAGE_EXT}'#g'`"
if [ ! -e "$OUT_DIR/$outF" ] || [ "$WIKI_IN_DIR/$inF" -nt "$OUT_DIR/$outF" ]; then
echo "$inF"
outD="`dirname "$OUT_DIR/$outF"`"
mkdir -p "$outD"
cat "$WIKI_IN_DIR/$inF" | \
pandoc -f markdown -t json | \
MSYS2_ARG_CONV_EXCL="/img" dot_filter "$inF" "$OUT_DIR/img" "/img" | \
MSYS2_ARG_CONV_EXCL="/img" plantuml_filter "$inF" "$OUT_DIR/img" "/img" | \
adapt_local_page_link_filter "$OUT_PAGE_EXT" "$pathFromInDirToInRoot" | \
pandoc -s -f json -t html --mathjax --toc --toc-depth=4 > "$OUT_DIR/$outF" || \
{ echo "Error converting markdown input file \"$WIKI_IN_DIR/$inF\" to \"$OUT_DIR/$outF\" \"$OUT_TYPE\" output."; \
rm -f "$OUT_DIR/$outF"; exit 1; }
fi
done
echo ""
echo ""
popd > /dev/null