-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_generate.sh
executable file
·359 lines (308 loc) · 10.9 KB
/
03_generate.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env bash
#--------------------------------------------
# Default Bash Script Header
set -eu
trap stacktrace EXIT
function stacktrace {
if [ $? != 0 ]; then
echo -e "\nThe command '$BASH_COMMAND' triggerd a stacktrace:"
for i in $(seq 1 $((${#FUNCNAME[@]} - 2))); do j=$(($i+1)); echo -e "\t${BASH_SOURCE[$i]}: ${FUNCNAME[$i]}() called in ${BASH_SOURCE[$j]}:${BASH_LINENO[$i]}"; done
fi
}
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
#--------------------------------------------
. "$SCRIPT_DIR/material/lib.sh"
cd "$SCRIPT_DIR"
## Check for Dependencies
# VERSION 1
#if ! which aws >/dev/null 2>&1 ; then
# echo -e "Please install awscli with:\n\n\tpip install --user awscli\nor:\n\tpip install awscli\n\nand configure it with:\n\n\taws configure\n"
# trap '' EXIT
# exit 1
#fi
# VERSION 2
#missingDeps=""
#command -v curl >/dev/null 2>&1 || missingDeps="$(echo -e "$missingDeps\ncurl")"
#command -v bc >/dev/null 2>&1 || missingDeps="$(echo -e "$missingDeps\nbc")"
#
#if [ "$missingDeps" != "" ]; then
# echo "This script reqires the following missing programs:"
# echo -e "$missingDeps\n"
# echo "Aborting due to missing dependencies..."
# trap '' EXIT
# exit 1
#fi
outputDir="./04_blog"
indexDir="$outputDir/indices"
primaryIndexFile="$outputDir/posts.json"
rssFile="$outputDir/rss.xml"
settingsFile="settings.txt"
defaultSettingsFile="settings-default.txt"
if [ ! -e "$settingsFile" ]; then
echo "$settingsFile does not exist. Please configure your PlutoBlog before using it."
exit
fi
function getSetting {
local settingName="$1"
local line="$(grep -i "$settingName" "$settingsFile")"
if [ "$line" == "" ]; then
# settings file does not contain the option, falling back to default settings file
line="$(grep -i "$settingName" "$defaultSettingsFile")"
fi
# gsub to trim whitespaces
#grep -i "$settingName" settings.txt | awk -F ':' '{gsub(/^[ \t]+/, "", $2); print $2}'
echo "$line" | cut -d: -f2- | sed 's/^[ \t]\+//'
}
function replacePlaceholders {
local targetFile="$1"
sed -e 's/\[RSS Feed Title\]/'"$(getSetting "Title")"'/' \
-e 's/\[Pluto Blog Title\]/'"$(getSetting "Title")"'/' \
-e 's/\[Author\]/'"$(getSetting "Author")"'/' \
-e 's/\[Description\]/'"$escapedDescription"'/' \
-e 's/\[Color1\]/'"$(getSetting "Color1")"'/' \
-e 's/\[Color2\]/'"$(getSetting "Color2")"'/' \
-e 's/\[Disqus-Site\]/'"$(getSetting "Disqus-Site")"'/' \
"$targetFile" > "$targetFile.bak" && mv "$targetFile.bak" "$targetFile"
}
settingsTitle="$(getSetting "Title")"
settingsAuthor="$(getSetting "Author")"
settingsDescription="$(getSetting "Description")"
settingsUrl="$(getSetting "Url")"
settingsPrimaryLanguage="$(getSetting "Primary-Language")"
escapedDescription="$(getSetting "Description" | sed -e 's/\[/\\\[/g' -e 's/\]/\\\]/g' -e 's;/;\\\/;g' -e 's/\$/\\\$/g')"
pagesExtension="$(getSetting "GitHubPages-Extension")"
# starting GitHub Extension before modifying "04_blog/"
if [ "$pagesExtension" == "true" ]; then
echo "Using GitHub Pages Extension..."
if [ ! -d "04_blog/.git" ]; then
echo "04_blog/.git does not exist. Your PlutoBlog does not seem to be configured correctly for the GitHub Pages Extension, exiting."
exit
fi
cd 04_blog
echo "Pulling latest changes from GitHub Pages repo"
git pull
cd -
fi
# update index.html
cp material/index.html.template 04_blog/index.html
replacePlaceholders "04_blog/index.html"
cp material/css/app.css.template 04_blog/css/app.css
replacePlaceholders "04_blog/css/app.css"
cp material/js/config.js.template 04_blog/js/config.js
replacePlaceholders "04_blog/js/config.js"
# update logo.svg
if [ -e "logo.svg" ]; then
cp "logo.svg" "04_blog/img/"
else
cp "04_blog/img/logo-default.svg" "04_blog/img/logo.svg"
fi
# clear indexDir
if [ -f "$primaryIndexFile" ]; then
rm "$primaryIndexFile"
fi
if [ -d "$indexDir" ]; then
rm "$indexDir" -r
fi
mkdir -p "$indexDir"
# update posts rss.xml
echo -n '<?xml version="1.0" ?>
<rss version="2.0">
<channel>
<title>'"$settingsTitle"'</title>
<link>'"$settingsUrl"'</link>
<description>'"$settingsDescription"'</description>
<image>
<url>img/logo.svg</url>
<link>'"$settingsUrl"'</link>
</image>' > $rssFile
# copy the posts directory into the blog directory
mkdir -p "04_blog/posts"
rm -r "04_blog/posts"
cp -a 02_posts 04_blog/posts
function getLanguageArray {
local postFullPath="$1"
local primaryPostFullPath="$(toPrimaryPost "$postFullPath")"
local postLanguageBase="$(echo "$primaryPostFullPath" | sed 's/\.md$//')"
local languages='[]'
if [ -f "$primaryPostFullPath" ]; then
languages='["'"$settingsPrimaryLanguage"'"]'
fi
# if other languages exist (for + break + if)
for p in "$postLanguageBase".[a-z][a-z]*.md; do
if [ -e "$p" ]; then
if [ -f "$primaryPostFullPath" ]; then
languages='["'"$settingsPrimaryLanguage"'", '"$(ls "$postLanguageBase".[a-z][a-z]*.md | sed 's/^.*\.\([a-z]\{2\}\(\-[A-Z]\{2\}\)\?\)\.md$/"\1" /' | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n//g' -e 's/" "/", "/g')"']'
else
# skip the settingsPrimaryLanguage if the primary post file does not exist
languages='['"$(ls "$postLanguageBase".[a-z][a-z]*.md | sed 's/^.*\.\([a-z]\{2\}\(\-[A-Z]\{2\}\)\?\)\.md$/"\1" /' | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n//g' -e 's/" "/", "/g')"']'
fi
fi
break;
done
echo "$languages"
}
function getIndexEntry {
local postDir=$1
local postFile=$2
local postFullPath="02_posts/$postDir$postFile"
local primaryPostFile="$(toPrimaryPost "$postFile")"
local primaryPostFullPath="02_posts/$postDir$primaryPostFile"
local metaFile="$primaryPostFullPath.meta"
local date="$(getDateFromMeta "$metaFile" "$primaryPostFullPath")"
local uuid="$(getUUIDFromMeta "$metaFile")"
local title="$(cat "$postFullPath" | grep -m 1 -e "^#" | sed 's/^# //' || true)"
# remove everything after the first / and the .md suffix
local prettyId="$(getPrettyId "$postDir" "$postFile")"
local link="$settingsUrl/#!posts/$(rawurlencode "$prettyId")"
local file="$postDir$postFile"
local description="$(cat "$postFullPath" | grep -m 1 -e '^[^# ]\+' || true)..."
# languages
local languages="$(getLanguageArray "$primaryPostFullPath")"
# index
echo -n ' {
"title":"'"$title"'",
"link":"'"$link"'",
"guid":"'"$uuid"'",
"prettyId":"'"$prettyId"'",
"file":"'"$file"'",
"date":"'"$date"'",
"languages":'"$languages"',
"description":"'"$description"'"
}'
}
function getOrphanIndexEntry {
local postDir=$1
local postFile=$2
local postFullPath="02_posts/$postDir$postFile"
local primaryPostFile="$(toPrimaryPost "$postFile")"
local primaryPostFullPath="02_posts/$postDir$primaryPostFile"
local metaFile="$primaryPostFullPath.meta"
local firstTranslation="$(getFirstTranslationFromMeta "$metaFile" "$postFullPath")"
local date="$(getDateFromMeta "$metaFile" "$postFullPath")"
local uuid="$(getUUIDFromMeta "$metaFile")"
local prettyId="$(getPrettyId "$postDir" "$postFile")"
# languages
local languages="$(getLanguageArray "$postFullPath")"
echo -n ' {
"guid":"'"$uuid"'",
"prettyId":"'"$prettyId"'",
"date":"'"$date"'",
"firstTranslation":"'"$firstTranslation"'",
"languages":'"$languages"'
}'
}
function getRssIndexEntry {
local postDir=$1
local postFile=$2
local postFullPath="02_posts/$postDir$postFile"
local primaryPostFile="$(toPrimaryPost "$postFile")"
local primaryPostFullPath="02_posts/$postDir$primaryPostFile"
local metaFile="$primaryPostFullPath.meta"
local uuid="$(getUUIDFromMeta "$metaFile")"
local title="$(cat "$postFullPath" | grep -m 1 -e "^#" | sed 's/^# //' || true)"
# remove everything after the first / and the .md suffix
local prettyId="$(echo "$postDir$primaryPostFile" | sed -e 's/\/.*$//g' -e 's/\.md$//g' -e 's/ /-/g')"
local link="$settingsUrl/#!posts/$(rawurlencode "$prettyId")"
local description="$(cat "$postFullPath" | grep -m 1 -e '^[^# ]\+' || true)..."
echo -n '
<item>
<title>'"$title"'</title>
<link>'"$link"'</link>
<description>'"$description"'</description>
<guid isPermaLink="false">'"$uuid"'</guid>
</item>'
}
function createIndexForPost {
local postDir="$1"
local postFile="$2"
local entryType=$3
local primaryPostFile="$(toPrimaryPost "$postFile")"
local primaryPostFullPath="02_posts/$postDir$primaryPostFile"
# generate meta data
local metaFile="$primaryPostFullPath.meta"
if [ -f "$primaryPostFullPath" ]; then
local date="$(getDateFromMeta "$metaFile" "$primaryPostFullPath")"
else
local date="$(getDateFromMeta "$metaFile" "$postFullPath")"
fi
local uuid="$(getUUIDFromMeta "$metaFile")"
if [ -f "$primaryIndexFile" ] && grep -q "$uuid" "$primaryIndexFile"; then
# this post was added to the index already
return
fi
# create primary index entry
local indexFile="$primaryIndexFile"
prepareIndex "$indexFile"
if [ "$entryType" == "orphan" ]; then
getOrphanIndexEntry "$postDir" "$postFile" >> "$indexFile"
else
getIndexEntry "$postDir" "$primaryPostFile" >> "$indexFile"
fi
# loop translations of this post
# enabling extended globbing: http://www.linuxjournal.com/content/bash-extended-globbing
# shopt -s extglob
# for p in 02_posts/FolderPost/Test?(.[a-z][a-z]|.[a-z][a-z]-[A-Z][A-Z]).md; do
local basePath="$(echo "$primaryPostFullPath" | sed 's/\.md$//')"
for p in "$basePath".[a-z][a-z].md "$basePath".[a-z][a-z]-[A-Z][A-Z].md; do
if [ -e "$p" ]; then # $p can be one of the above wildcard expressions
local lang="$(langFromFilename "$p")"
local indexFile="$indexDir/posts.$lang.json"
prepareIndex "$indexFile"
getIndexEntry "$postDir" "$(basename "$p")" >> "$indexFile"
fi
done
# rss
getRssIndexEntry "$postDir" "$postFile" >> $rssFile
}
function loopPosts {
SAVEIFS="$IFS"
IFS="$(echo -en "\n\b")"
for f in $(ls -t "02_posts/"); do
# skipping *.meta files
if [[ ! -d "02_posts/$f" && ! "$f" =~ ^.*\.md$ ]]; then
continue
fi
local postDir=""
local postFile="$f";
if [ -d "02_posts/$postFile" ]; then
local postDir="$postFile/"
local postFile="$(cd "02_posts/$postDir/"; ls *.md | head -n 1)"
fi
local primaryPostFile="$(toPrimaryPost "$postFile")"
local postFullPath="02_posts/$postDir$postFile"
local primaryPostFullPath="02_posts/$postDir$primaryPostFile"
if [ -f "$primaryPostFullPath" ]; then
createIndexForPost "$postDir" "$primaryPostFile" "normal"
else
createIndexForPost "$postDir" "$postFile" "orphan"
fi
done
IFS="$SAVEIFS"
}
loopPosts
# close all secondary index files
for f in "$indexDir"/*; do
echo -e '\n\t]\n}' >> "$f"
done
# Add a list of secondary indices to the primary index
echo -e '\n\t],
"primaryLanguage": "'"$settingsPrimaryLanguage"'",
"secondaryIndices": [
'"$(ls "$indexDir" | sed -e 's/^.*\.\([a-z]\{2\}\(\-[A-Z]\{2\}\)\?\)\.json$/"\1" /' | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n//g' -e 's/" "/", "/g' -e 's/ $//g')"'
]
}' >> "$primaryIndexFile"
# close rss file
echo -n '
</channel>
</rss>
' >> $rssFile
if [ "$pagesExtension" == "true" ]; then
echo "Publishing with GitHub Pages Extension..."
cd "04_blog"
git add .
git status
git commit -am "Update by GitHub Pages Extension"
git push
cd -
echo "Done"
fi