-
Notifications
You must be signed in to change notification settings - Fork 2
/
pagez
executable file
·136 lines (113 loc) · 2.53 KB
/
pagez
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
#!/usr/bin/zsh
function parse_template {
declare line c flag_variable variable inline_command
while IFS= read -r -ku0 c; do
if (( flag_inline )); then
if (( flag_variable )); then
case $c in
'}')
flag_variable=0
flag_inline=0
printf '%s' ${(P)variable}
unset variable
;;
*) variable+=$c;;
esac
elif (( flag_command )); then
case $c in
')')
flag_command=0
flag_inline=0
eval "$inline_command"
unset inline_command
;;
*) inline_command+=$c;;
esac
else
if [[ $c == '{' ]]; then
flag_variable=1
elif [[ $c == '(' ]]; then
flag_command=1
else
flag_inline=0
printf '%%%s' $c
fi
fi
else
if [[ $c == '%' ]]; then
flag_inline=1
else
printf '%s' $c
fi
fi
done
}
function display_page {
declare page_name page_title
while (( $# )); do
case $1 in
(-n) page_name=$2; shift;;
(-t) page_title=$2; shift;;
esac
shift
done
if ! [[ $page_title ]]; then
page_title=$page_name
fi
parse_template < "$src_dir/html/header.html"
parse_template | $markdown_parser
parse_template < "$src_dir/html/footer.html"
}
function err {
printf '%s\n' "$*" >&2
}
function main {
js_minify='/usr/bin/uglifyjs'
less_parser='/usr/bin/lessc'
markdown_parser='/usr/bin/cmark'
while (( $# )); do
case $1 in
(-s) src_dir=$2; shift;;
(-o) out_dir=$2; shift;;
(-j) js_minify=$2; shift;;
(-l) less_parser=$2; shift;;
(-m) markdown_parser=$2; shift;;
esac
shift
done
if ! [[ $src_dir =~ ^/ ]]; then
src_dir=$PWD/$src_dir
fi
# Source config
source $src_dir/config
# Custom code
for m in $src_dir/mod/*; do
if ! source $m; then
err "Could not load functions from $m"
return 1
fi
done
# Minify JS into a single file
install -dm755 "$out_dir/js"
:> "$out_dir/site.js"
for j in ${js_files[@]}; do
printf 'Adding %s to site.js\n' $j
$js_minify "$src_dir/js/$j" >> "$out_dir/site.js"
done
# Generate CSS from site.less
install -dm755 "$out_dir/css"
printf 'Generating CSS from %s/less/site.less\n' "$src_dir"
$less_parser "$src_dir/less/site.less" > "$out_dir/site.css"
# Generate pages
install -dm755 "$out_dir/pages"
for p in $src_dir/pages/*; do
page_filename=${p##*/}
page_name=${page_filename%.*}
printf 'Generating page from %s\n' $p
display_page -n $page_name -t $page_name < $p > "$out_dir/pages/$page_name.html"
done
# Add an index page
cd $out_dir
ln -fs pages/$index_page.html index.html
}
main $@