This repository has been archived by the owner on Aug 23, 2022. It is now read-only.
generated from nulib-oer/lantern
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lantern.sh
307 lines (269 loc) · 10.1 KB
/
lantern.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
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
#!/usr/bin/bash
# custom settings
output_filename='LanternFinalReport'
output_directory='public'
siteurl='https://nulib-oer.github.io/lantern-report/'
output_formats() {
html
pdf
epub
docx
}
# utilities
pandoc_command='pandoc --verbose' # change to 'pandoc --quiet' for fewer logging messages
# setup
mkdir -p _temp/
mkdir -p $output_directory
# convert manuscript files to markdown
manuscript() {
local docx_files=`ls -1 manuscript/*.docx 2>/dev/null | wc -l`
local odt_files=`ls -1 manuscript/*.odt 2>/dev/null | wc -l`
local latex_files=`ls -1 manuscript/*.tex 2>/dev/null | wc -l`
if [ $docx_files != 0 ] ; then
for FILE in manuscript/*.docx
do
$pandoc_command "$FILE" \
--to markdown \
--wrap=none \
--extract-media=images \
--standalone \
--output "${FILE%.*}.md"
mv "${FILE%.docx}.md" text
done
fi
if [ $odt_files != 0 ] ; then
for FILE in manuscript/*.odt
do
$pandoc_command "$FILE" \
--to markdown \
--wrap=none \
--extract-media=images \
--standalone \
--output "${FILE%.*}.md"
mv "${FILE%.docx}.md" text
done
fi
if [ $latex_files != 0 ] ; then
for FILE in manuscript/*.tex
do
$pandoc_command "$FILE" \
--to markdown \
--wrap=none \
--extract-media=images \
--standalone \
--output "${FILE%.*}.md"
mv "${FILE%.docx}.md" text
done
fi
}
# lantern output formats
pdf_context() {
# combine all markdown files into one
$pandoc_command text/*.md -o _temp/chapters.md
# convert markdown to ConTeXt
$pandoc_command _temp/chapters.md \
--to context \
--defaults settings/context.yml \
--output $output_directory/$output_filename.tex
# convert ConTeXt to PDF
$pandoc_command _temp/chapters.md \
--to context \
--defaults settings/context.yml \
--output $output_directory/$output_filename.pdf
echo "📖 The PDF edition is now available in the $output_directory folder"
}
pdf() {
# combine all markdown files into one
$pandoc_command text/*.md -o _temp/chapters.md
# convert markdown to LaTeX
$pandoc_command _temp/chapters.md \
--to latex \
--defaults settings/latex.yml \
--output $output_directory/$output_filename.tex
# convert LaTeX to PDF
$pandoc_command _temp/chapters.md \
--to latex \
--defaults settings/latex.yml \
--output $output_directory/$output_filename.pdf
echo "📖 The PDF edition is now available in the $output_directory folder"
}
docx() {
$pandoc_command text/*.md -o _temp/chapters.md
$pandoc_command _temp/chapters.md \
--defaults settings/docx.yml \
-o $output_directory/$output_filename.docx
echo "📖 The DOCX edition is now available in the $output_directory folder"
}
epub() {
$pandoc_command text/*.md -o _temp/chapters.md
$pandoc_command _temp/chapters.md \
--defaults settings/epub.yml \
--resource-path=.:images \
--mathml \
--output $output_directory/$output_filename.epub
echo "📖 The EPUB edition is now available in the $output_directory folder";
}
oai() {
touch _temp/empty.txt
$pandoc_command _temp/empty.txt \
--to plain \
--metadata-file metadata.yml \
--template templates/oai.xml \
-o $output_directory/oai.xml
echo "🌐 The OAI-PMH record is now available in the $output_directory folder"
}
markdown() {
$pandoc_command text/*.md \
--metadata-file metadata.yml \
--wrap=none \
-s -o $output_directory/$output_filename.md
echo "📖 The Markdown file is now available in the $output_directory folder";
}
# these next set of functions help build the website
copy_assets() {
echo "Copying assets..."
if [ -d "images" ]
then
echo "Copying images..."
cp -r images $output_directory;
else
echo "No images directory. Skipping..."
fi
cp -r lib/css/ $output_directory;
cp -r lib/js/ $output_directory;
}
extract_metadata() {
echo "Extracting chapter metadata..."
for FILE in text/*.md; do
# sets the h1 markdown heading as the chapter title
local chapter_title="$(grep '^# ' $FILE | sed 's/# //')"
local basename="$(basename "$FILE" .md)"
# assigns categories
$pandoc_command "$FILE" \
--metadata basename=$basename \
--template templates/category.template.txt \
--to html \
--output "_temp/$basename.category.txt"
# converts metadata to json
$pandoc_command "$FILE" \
--metadata chapter_title="$chapter_title" \
--metadata htmlfile="$basename.html" \
--template templates/metadata.template.json \
--to html \
--output "_temp/$basename.metadata.json"
done;
}
build_chapter_index() {
echo "Building the chapter index..."
echo "{\"chapter_list\": [" > _temp/chapters.json
local SEPARATOR=""
for FILE in _temp/*.metadata.json; do
printf '%s' "$SEPARATOR" >> _temp/chapters.json
cat "$FILE" >> _temp/chapters.json
SEPARATOR=","
done
echo "]}" >> _temp/chapters.json
}
build_index() {
# consolidates the metadata into a single json file
echo "Grouping metadata by category..." # (yep, this #is a right mess)
echo "{\"categories\": [" > _temp/index.json
local SEPARATOR_OUTER="" # no comma before first list element (categories)
local SEPARATOR_INNER="" # ditto (recipes per category)
local IFS=$'\n' # tell for loop logic to split on #newlines only, not spaces
local CATS="$(cat _temp/*.category.txt)"
for CATEGORY in $(echo "$CATS" | cut -d" " -f2- | sort | uniq); do
printf '%s' "$SEPARATOR_OUTER" >> _temp/index.json
local CATEGORY_FAUX_URLENCODED="$(echo "$CATEGORY" | awk -f "templates/faux_urlencode.awk")"
# some explanation on the next line and similar ones: this uses `tee -a`
# instead of `>>` to append to two files instead of one, but since we don't
# actually want to see the output, pipe that to /dev/null
printf '%s' "{\"category\": \"$CATEGORY\", \"category_faux_urlencoded\": \"$CATEGORY_FAUX_URLENCODED\", \"info\": [" | tee -a "_temp/index.json" "_temp/$CATEGORY_FAUX_URLENCODED.category.json" >/dev/null
for C in $CATS; do
BASENAME=$(echo "$C" | cut -d" " -f1)
C_CAT=$(echo "$C" | cut -d" " -f2-)
if [[ "$C_CAT" == "$CATEGORY" ]]; then
printf '%s' "$SEPARATOR_INNER" | tee -a "_temp/index.json" "_temp/$CATEGORY_FAUX_URLENCODED.category.json" >/dev/null
cat "_temp/$BASENAME.metadata.json" | tee -a "_temp/index.json" "_temp/$CHAPTER_FAUX_URLENCODED.category.json" > /dev/null
SEPARATOR_INNER=","
fi
done
printf "]}\n" | tee -a "_temp/index.json" "_temp/$CATEGORY_FAUX_URLENCODED.category.json" > /dev/null
local SEPARATOR_OUTER=","
local SEPARATOR_INNER=""
done
unset IFS
echo "]}" >> _temp/index.json
}
html() {
local TIME_START=$(date +%s)
touch _temp/empty.txt
copy_assets
extract_metadata
build_chapter_index
build_index
echo "Building chapter pages..."
for FILE in text/*.md;do
echo "⚙️ Processing $FILE..."
local CATEGORY_FAUX_URLENCODED="$(cat "_temp/$(basename "$FILE" .md).category.txt" | cut -d" " -f2- | awk -f "templates/faux_urlencode.awk")"
# when running under GitHub Actions, all file modification dates are set to
# the date of the checkout (i.e., the date on which the workflow was
# executed), so in that case, use the most recent commit date of each file
# as its update date – you'll probably also want to set the TZ environment
# variable to your local timezone in the workflow file (#21)
if [[ "$GITHUB_ACTIONS" = true ]]; then
local UPDATED_AT="$(git log -1 --date=short-local --pretty='format:%cd' "$FILE")"
else
local UPDATED_AT="$(date -r "$FILE" "+%Y-%m-%d")"
fi
local basename="$(basename "$FILE" .md)"
$pandoc_command "$FILE" \
--metadata-file _temp/chapters.json \
--metadata siteurl=$siteurl \
--metadata category_faux_urlencoded="$CATEGORY_FAUX_URLENCODED" \
--metadata updatedtime="$UPDATED_AT" \
--metadata htmlfile="$basename.html" \
--defaults settings/html.yml \
--output "$output_directory/$basename.html"
done
echo "Building the home page..."
$pandoc_command _temp/empty.txt \
--metadata-file _temp/chapters.json \
--metadata-file metadata.yml \
--metadata-file settings/config.yml \
--template templates/home.html \
--metadata updatedtime="$(date "+%Y-%m-%d")" \
--standalone \
--output $output_directory/index.html
echo "Assembling search index..."
echo "[" > _temp/search.json
local SEPARATOR=""
for FILE in _temp/*.metadata.json; do
printf '%s' "$SEPARATOR" >> _temp/search.json
cat "$FILE" >> _temp/search.json
SEPARATOR=","
done
echo "]" >> _temp/search.json
cp -r _temp/search.json $output_directory
local TIME_END=$(date +%s)
local TIME_TOTAL=$((TIME_END-TIME_START))
echo "🚀 All done after $TIME_TOTAL seconds!"
}
reset() {
rm -rf $output_directory
rm -rf _temp
echo "🗑️ Let's start over.";
}
server() {
# runs a local development server for testing
# requires Python 3.x installed on the machine
html;
python3 -m http.server --directory $output_directory;
}
# If no arguments are specified in the $ bash lantern.sh command,
# then run the output_formats function (which builds all formats)
if [ -z "$1" ]
then
output_formats
fi
"$@"