-
Notifications
You must be signed in to change notification settings - Fork 0
/
xstatix
executable file
·296 lines (246 loc) · 6.08 KB
/
xstatix
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
#!/bin/bash
# xStatix - the second simplest static website generator in bash
# based on: https://gist.github.com/plugnburn/c2f7cc3807e8934b179e { (c) Revoltech 2015 }
# see LICENSE
# stubs
ROUTEFILE='routes.conf'
ASSETDIR='assets/'
DRAFTSDIR='drafts/'
OUTDIR='out/'
SRCDIR="$DRAFTSDIR"
GENROUTES=true
VERBOSE=false
## routines
# internal
function logVerbose {
if [ "$VERBOSE" == true ]; then
echo "[$1]"
fi
}
function prerenderBlock {
local blockFile="${1}"
local blockContent="$(<${blockFile})"
local L=''
local includes=$(grep -Po '<!--\s*#include:.*?-->' "$blockFile")
OLDIFS="$IFS"
IFS=$'\n'
for L in $includes; do
local includedFileName=$(echo -n "$L"| grep -Po '(?<=#include:).*?(?=-->)')
local includedFile="$includedFileName"
local includedContent="$(prerenderBlock ${includedFile})"
blockContent="${blockContent//$L/$includedContent}"
done
IFS="$OLDIFS"
echo -n "$blockContent"
}
function renderBlock {
local blockText="$(prerenderBlock $1)"
local sets=$(echo -n "$blockText" | grep -Po '<!--#set:.*?-->')
local L=''
OLDIFS="$IFS"
IFS=$'\n'
for L in $sets; do
local set=$(echo -n "$L" | grep -Po '(?<=#set:).*?(?=-->)')
local setvar="${set%%=*}"
local setval="${set#*=}"
blockText="${blockText//$L/}"
blockText="${blockText//<!--=@${setvar}-->/${setval}}"
blockText="${blockText//<!---@${setvar}-->/<!---@${setvar}-->${setval}}"
blockText="${blockText//<!--+@${setvar}-->/${setval}<!--+@${setvar}-->}"
done
IFS="$OLDIFS"
echo -n "$blockText"
}
function loadRoutes {
if [ "$GENROUTES" == true ]; then
rm "$ROUTEFILE"
touch "$ROUTEFILE"
# get list
local list=( "${1}.html" )
[ "${list[0]}" == ".html" ] && list=$(find ${SRCDIR}** 2>/dev/null)
for file in $list; do
if [ -f "$file" ]; then
name=$(basename $file)
name="${name%.*}"
if [ "$name" == "index" ]; then name=''; fi
echo "$file:$name/" >> "$ROUTEFILE"
fi
done
logVerbose "generated default routes"
fi
ROUTELIST="$(<$ROUTEFILE)"
logVerbose "loaded routes"
}
function generate {
OLDIFS="$IFS"
IFS=$'\n'
for route in $ROUTELIST; do
local blockname="${route%%:*}"
local blockpath="${route#*:}"
if [[ "$blockname" && "$blockpath" ]]; then
mkdir -p "${OUTDIR}${blockpath}"
renderBlock "$blockname" > "${OUTDIR}${blockpath}index.html"
fi
done
IFS="$OLDIFS"
}
function performAction {
local actionname="${1}"
local action="${2}"
local dest="${3}"
# parse
local prefix="${action:0:1}" # +/-/=
local rest="${action:2}"
local varname="${rest%%=*}"
local varval="${rest#*=}"
# set tokens
local valdelim=""
local valregex=""
local settext=""
local matchregex=""
if [ -z "$prefix" ]; then
logVerbose "got empty action: $actionname"
return
fi
if [ "$actionname" == "set" ]; then
valdelim="="
if [ ! -z "$varval" ] && [ "$prefix" == "-" ]; then
valregex="$varval"
else
valregex=".*"
fi
fi
matchregex="<!--#${actionname}:${varname}${valdelim}${valregex}-->"
settext="<!--#${actionname}:${rest}-->"
# apply
if [ "$prefix" == "+" ]; then
echo "$settext" >> ${dest}
elif [ "$prefix" == "=" ]; then
local match=$(grep "$matchregex" ${dest})
if [ -z "$match" ]; then
echo "$settext" >> ${dest}
else
sed -i -E "s/${matchregex}/${settext}/" ${dest}
fi
elif [ "$prefix" == "-" ]; then
local result=$(tac ${dest} | sed "0,/${matchregex}/{s/${matchregex}//}" | tac)
echo "$result" > ${dest}
elif [ "$prefix" == "r" ]; then
sed -i -E "s/$matchregex//" ${dest}
else
logVerbose "unknown prefix for action ${action}"
fi
}
function insertInclude {
local dest="${1}"
local include="${INCLUDE}"
local prefix="${include:0:1}" # +/-
local includedBlock="${include:2}"
if [ "$prefix" == "+" ]; then
if [ -z "$includedBlock" ] || [ ! -f "${includedBlock}" ]; then
echo "error: invalid include block"
exit 1
fi
fi
performAction "include" "$include" "$dest"
}
function insertSet {
local dest="${1}"
local setline="${SETOPTION}"
performAction "set" "$setline" "$dest"
}
# user end
function showHelp {
echo "xStatix"
echo " syntax: xstatix [-s|-i|-v|-g|-h|-w|-e|-d|-p|-u]"
echo " options:"
echo " s set variable <diff>"
echo " i include block <diff>"
echo " v verbose"
echo " g generate project tree"
echo " h print this help"
echo " w write"
echo " e edit"
echo " d delete"
echo " p publish"
echo " u unpublish"
echo " default:"
echo " generates default project file tree"
echo " notes:"
echo " - <diff>: =@ set, +@ to add, -@ remove, r@ remove all. (e.g: +@var=val)"
echo " - use the options in the order you want them!"
exit 0
}
function makeProjectTree {
mkdir -p "$ASSETDIR"
mkdir -p "$DRAFTSDIR"
rm -rf "${OUTDIR}"/*
if [[ "$ASSETDIR" ]]; then
cp -rd "$ASSETDIR" "${OUTDIR}/"
fi
touch "$ROUTEFILE"
logVerbose "created project tree"
}
function writeDraft {
loadRoutes
# read name
local name="${1}.html"
local dest="${DRAFTSDIR}${name}"
insertInclude "${dest}"
touch "${dest}"
# done
echo "draft $dest written"
}
function deleteDraft {
local dest="${DRAFTSDIR}${1}"
if [ "$dest" == "${DRAFTSDIR}" ] || [ ! -f "${dest}.html" ]; then
exit 1
fi
rm "${dest}.html"
echo "deleted ${dest}"
exit 0
}
function publishPage {
loadRoutes
generate
if [[ "$ASSETDIR" ]]; then
cp -rd "${ASSETDIR}/." "${OUTDIR}/"
fi
echo "published draft ${1}"
exit 0
}
function unpublishPage {
local dest=${OUTDIR}${1}.html
if [ "$dest" == "${OUTDIR}.html" ]; then
exit 1
fi
rm -rf "${dest}"
echo "unpublished ${dest}"
exit 0
}
function editDraft {
local draft="${1}.html"
local dest=${DRAFTSDIR}${draft}
insertInclude "$dest"
insertSet "$dest"
echo "edit complete"
exit 0
}
# main
# read options
while getopts ":s:i:vghw:e:d:p:u:" opt; do
case ${opt} in
s ) SETOPTION=${OPTARG} && logVerbose "setting ${OPTARG}" ;;
i ) INCLUDE=${OPTARG} && logVerbose "including ${OPTARG}" ;;
v ) VERBOSE=true ;;
g ) makeProjectTree ;;
h ) showHelp ;;
w ) writeDraft ${OPTARG} ;;
e ) editDraft ${OPTARG} ;;
d ) deleteDraft ${OPTARG} ;;
p ) publishPage ${OPTARG} ;;
u ) unpublishPage ${OPTARG} ;;
\? ) echo "Invalid option: $OPTARG" 1>&2; exit 1 ;;
esac
done
shift $((OPTIND -1))