Blame


1 8b393fbb 2026-01-22 hi #!/bin/sh -eu
2 b77b250a 2026-01-11 hi
3 89f9d3c9 2026-04-01 hi # ssg.sh 7.4.1
4 b77b250a 2026-01-11 hi # https://romanzolotarev.com/ssg/
5 b77b250a 2026-01-11 hi # copyright 2018-2026 romanzolotarev.com
6 b77b250a 2026-01-11 hi #
7 b77b250a 2026-01-11 hi # permission to use, copy, modify, and/or distribute this software for any
8 b77b250a 2026-01-11 hi # purpose with or without fee is hereby granted, provided that the above
9 b77b250a 2026-01-11 hi # copyright notice and this permission notice appear in all copies.
10 b77b250a 2026-01-11 hi #
11 b77b250a 2026-01-11 hi # the software is provided "as is" and the author disclaims all warranties with
12 b77b250a 2026-01-11 hi # regard to this software including all implied warranties of merchantability
13 b77b250a 2026-01-11 hi # and fitness. in no event shall the author be liable for any special, direct,
14 b77b250a 2026-01-11 hi # indirect, or consequential damages or any damages whatsoever resulting from
15 b77b250a 2026-01-11 hi # loss of use, data or profits, whether in an action of contract, negligence or
16 b77b250a 2026-01-11 hi # other tortious action, arising out of or in connection with the use or
17 b77b250a 2026-01-11 hi # performance of this software.
18 b77b250a 2026-01-11 hi
19 a3cde1e1 2026-01-25 hi info() { echo "$@" >&3; }
20 b77b250a 2026-01-11 hi fail() { echo "$@" >&2 && exit 1; }
21 4242eafc 2026-01-22 hi usage() { fail "usage: ${0##*/} <src> <dst>"; }
22 b77b250a 2026-01-11 hi
23 b77b250a 2026-01-11 hi # exit if less than two arguments
24 b77b250a 2026-01-11 hi fail_no_args() { if test $# -ne 2; then usage; fi; }
25 b77b250a 2026-01-11 hi
26 b77b250a 2026-01-11 hi # exit if src directory not found
27 b77b250a 2026-01-11 hi fail_no_src() { if ! test -d "$1"; then fail "fail: $1 not found"; fi; }
28 b77b250a 2026-01-11 hi
29 b77b250a 2026-01-11 hi # return relative paths sorted
30 b77b250a 2026-01-11 hi sort_relative() { sed "s,$1/,," | sort; }
31 b77b250a 2026-01-11 hi
32 b77b250a 2026-01-11 hi # return sorted file hashes for directory
33 b77b250a 2026-01-11 hi hash_dir() {
34 b77b250a 2026-01-11 hi dir="$1"
35 b77b250a 2026-01-11 hi if ! test -d "$dir"; then return; fi
36 b77b250a 2026-01-11 hi set -- find "$dir" -type f
37 b77b250a 2026-01-11 hi while read -r line; do
38 b77b250a 2026-01-11 hi if test -z "$line"; then continue; fi
39 b77b250a 2026-01-11 hi set -f && for word in $line; do
40 b77b250a 2026-01-11 hi if test -z "$word"; then continue; fi && set -- "$@" "$word"
41 b77b250a 2026-01-11 hi done && set +f
42 b77b250a 2026-01-11 hi done
43 00e9d71a 2026-01-22 hi "$@" -print0 | xargs -r -0 -n256 sha256 -r | sort_relative "$dir"
44 b77b250a 2026-01-11 hi }
45 b77b250a 2026-01-11 hi
46 b77b250a 2026-01-11 hi # return find expression to exclude paths
47 b77b250a 2026-01-11 hi not_path() {
48 b77b250a 2026-01-11 hi while read -r x; do
49 b77b250a 2026-01-11 hi case "$x" in
50 b77b250a 2026-01-11 hi */) echo "! -path $x*" ;;
51 b77b250a 2026-01-11 hi *) echo "! -path $x" ;;
52 b77b250a 2026-01-11 hi esac
53 b77b250a 2026-01-11 hi done
54 b77b250a 2026-01-11 hi }
55 b77b250a 2026-01-11 hi
56 b77b250a 2026-01-11 hi # return all .ssg.ignore files
57 b77b250a 2026-01-11 hi find_ignore_files() { find "$SRC" -type f -name "$SSG_IGNORE"; }
58 b77b250a 2026-01-11 hi
59 b77b250a 2026-01-11 hi # return ignored paths from all .ssg.ignore files
60 b77b250a 2026-01-11 hi ignored_paths() {
61 b77b250a 2026-01-11 hi while read -r f; do d="$(dirname "$f")" && sed "s,^,${d}/," "$f"; done
62 b77b250a 2026-01-11 hi }
63 b77b250a 2026-01-11 hi
64 b77b250a 2026-01-11 hi # return src directory hash, excluding paths from .ssg.ignore
65 b77b250a 2026-01-11 hi hash_src() { find_ignore_files | ignored_paths | not_path | hash_dir "$SRC"; }
66 b77b250a 2026-01-11 hi
67 b77b250a 2026-01-11 hi # return find expression to exclude src and dst hash files
68 b77b250a 2026-01-11 hi exclude_hash_files() { echo "! -name $SSG_DST ! -name $SSG_SRC"; }
69 b77b250a 2026-01-11 hi
70 b77b250a 2026-01-11 hi # return dst directory hash, excluding hash files
71 b77b250a 2026-01-11 hi hash_dst() { exclude_hash_files | hash_dir "$DST"; }
72 b77b250a 2026-01-11 hi
73 b77b250a 2026-01-11 hi mustache() {
74 b77b250a 2026-01-11 hi d=0
75 b77b250a 2026-01-11 hi s=-1
76 b77b250a 2026-01-11 hi while read -r line; do
77 b77b250a 2026-01-11 hi o=""
78 b77b250a 2026-01-11 hi rest="$line"
79 b77b250a 2026-01-11 hi while :; do
80 b77b250a 2026-01-11 hi case "$rest" in
81 b77b250a 2026-01-11 hi *'{{'*)
82 b77b250a 2026-01-11 hi before="${rest%%\{\{*}"
83 b77b250a 2026-01-11 hi rest="${rest#*\{\{}"
84 b77b250a 2026-01-11 hi tag="${rest%%\}\}*}"
85 b77b250a 2026-01-11 hi rest="${rest#*\}\}}"
86 b77b250a 2026-01-11 hi if test $s -lt 0; then o="${o}${before}"; fi
87 b77b250a 2026-01-11 hi case "$tag" in
88 b77b250a 2026-01-11 hi '#'*)
89 b77b250a 2026-01-11 hi d=$((d + 1))
90 b77b250a 2026-01-11 hi if test $s -lt 0 -a -z "$(printenv "${tag#\#}")"; then s=$d; fi
91 b77b250a 2026-01-11 hi ;;
92 b77b250a 2026-01-11 hi '/'*) if test $s -eq $d; then s=-1; fi && d=$((d - 1)) ;;
93 b77b250a 2026-01-11 hi '^'*)
94 b77b250a 2026-01-11 hi d=$((d + 1))
95 b77b250a 2026-01-11 hi if test $s -lt 0 -a -n "$(printenv "${tag#^}")"; then s=$d; fi
96 b77b250a 2026-01-11 hi ;;
97 b77b250a 2026-01-11 hi *) if test $s -lt 0; then o="${o}$(printenv "$tag")"; fi ;;
98 b77b250a 2026-01-11 hi esac
99 b77b250a 2026-01-11 hi ;;
100 b77b250a 2026-01-11 hi *) if test $s -lt 0; then o="${o}${rest}"; fi && break ;;
101 b77b250a 2026-01-11 hi esac
102 b77b250a 2026-01-11 hi done
103 b77b250a 2026-01-11 hi if test $s -lt 0 -o -n "$o"; then printf '%s\n' "$o"; fi
104 b77b250a 2026-01-11 hi done
105 b77b250a 2026-01-11 hi }
106 b77b250a 2026-01-11 hi
107 b77b250a 2026-01-11 hi # returns page rendered with its template
108 b77b250a 2026-01-11 hi render_page() {
109 b77b250a 2026-01-11 hi # replace newlines with spaces and extract title from the first <h1> tag
110 b77b250a 2026-01-11 hi get_title() { tr '\n' ' ' |
111 c1267652 2026-01-25 hi sed -n 's/.*<[Hh]1[^>]*>\(.*\)<\/[Hh]1[^>]*>.*/\1/p' |
112 c1267652 2026-01-25 hi sed 's/<[^>]*>//g'; }
113 b77b250a 2026-01-11 hi content="$(cat)"
114 b092913f 2026-01-23 hi # use src directory name as site name
115 b092913f 2026-01-23 hi title="$(printf '%s' "$content" | get_title | tr -d '\n')"
116 b092913f 2026-01-23 hi site="$SITE"
117 b77b250a 2026-01-11 hi # replace {{title}} and {{site}} tags with values from variables,
118 b77b250a 2026-01-11 hi # replace {{content}} tag with page content.
119 b77b250a 2026-01-11 hi # use truthy tag to show title only when it's found in content, for example:
120 b77b250a 2026-01-11 hi # {{#title}}{{title}: {{/title}}
121 b092913f 2026-01-23 hi export content title site && mustache <"$1"
122 b77b250a 2026-01-11 hi }
123 b77b250a 2026-01-11 hi
124 b77b250a 2026-01-11 hi # return html converted from markdown
125 b77b250a 2026-01-11 hi md_to_html() {
126 b77b250a 2026-01-11 hi lowdown \
127 b77b250a 2026-01-11 hi --out-no-smarty \
128 b77b250a 2026-01-11 hi --html-no-escapehtml \
129 b77b250a 2026-01-11 hi --html-no-skiphtml \
130 b77b250a 2026-01-11 hi --parse-no-autolink \
131 b77b250a 2026-01-11 hi --parse-no-metadata
132 b77b250a 2026-01-11 hi }
133 b77b250a 2026-01-11 hi
134 b77b250a 2026-01-11 hi # write zip unless its gz variant found in src
135 b77b250a 2026-01-11 hi gz() {
136 b77b250a 2026-01-11 hi if test -f "$SRC/$1.gz"; then return; fi
137 b77b250a 2026-01-11 hi gzip -n -9 <"$DST/$1" >"$DST/$1.gz"
138 b77b250a 2026-01-11 hi }
139 b77b250a 2026-01-11 hi
140 b77b250a 2026-01-11 hi # return nearest template for page
141 b77b250a 2026-01-11 hi find_template() {
142 b77b250a 2026-01-11 hi dir=$(cd "$(dirname "$SRC/$1")" && pwd)
143 b77b250a 2026-01-11 hi root=$(dirname "$(cd "$SRC" && pwd)")
144 b77b250a 2026-01-11 hi while test "$dir" != "$root"; do
145 b77b250a 2026-01-11 hi t="$dir/$SSG_TEMPLATE"
146 b77b250a 2026-01-11 hi if test -f "$t"; then echo "${t#"$SRC/"}" && return; fi
147 b77b250a 2026-01-11 hi dir=$(dirname "$dir")
148 b77b250a 2026-01-11 hi done
149 b77b250a 2026-01-11 hi }
150 b77b250a 2026-01-11 hi
151 b77b250a 2026-01-11 hi # return relative paths to files if they found in directory
152 b77b250a 2026-01-11 hi files_in() {
153 b77b250a 2026-01-11 hi while read -r f; do if test -f "$1/$f"; then echo "$f"; fi; done
154 b77b250a 2026-01-11 hi }
155 b77b250a 2026-01-11 hi
156 b77b250a 2026-01-11 hi # write and zip file to dst
157 16ada7f8 2026-01-31 hi generate_copy_gz() {
158 b77b250a 2026-01-11 hi mkdir -p "$(dirname "$DST/$1")"
159 b77b250a 2026-01-11 hi cp "$SRC/$1" "$DST/$1"
160 16ada7f8 2026-01-31 hi info "copy gz $1"
161 b77b250a 2026-01-11 hi gz "$1"
162 16ada7f8 2026-01-31 hi info "copy gz $1 > $1.gz"
163 b77b250a 2026-01-11 hi }
164 b77b250a 2026-01-11 hi
165 b77b250a 2026-01-11 hi # write file to dst
166 b77b250a 2026-01-11 hi generate_copy() {
167 b77b250a 2026-01-11 hi mkdir -p "$(dirname "$DST/$1")"
168 b77b250a 2026-01-11 hi cp "$SRC/$1" "$DST/$1"
169 b77b250a 2026-01-11 hi info "copy $1"
170 b77b250a 2026-01-11 hi }
171 b77b250a 2026-01-11 hi
172 b77b250a 2026-01-11 hi # execute script and write zips for output files to dst
173 b77b250a 2026-01-11 hi generate_sh() {
174 b77b250a 2026-01-11 hi command ksh -- "$SRC/$1" "$SRC" "$DST" |
175 b77b250a 2026-01-11 hi while read -r f; do
176 b77b250a 2026-01-11 hi if test -z "$f"; then continue; fi
177 b77b250a 2026-01-11 hi if test -f "$SRC/$f"; then fail "fail: $1 collides with $f"; fi
178 b77b250a 2026-01-11 hi info "sh $1 > $f"
179 b77b250a 2026-01-11 hi gz "$f"
180 b77b250a 2026-01-11 hi info "sh $1 > $f.gz"
181 b77b250a 2026-01-11 hi done
182 b77b250a 2026-01-11 hi }
183 b77b250a 2026-01-11 hi
184 b77b250a 2026-01-11 hi # return true page has <html> tag
185 b77b250a 2026-01-11 hi has_html_tag() { grep -qi '<html[^>]*>' "$SRC/$1"; }
186 b77b250a 2026-01-11 hi
187 b77b250a 2026-01-11 hi # write html page and zip to dst
188 b77b250a 2026-01-11 hi generate_html() {
189 b77b250a 2026-01-11 hi mkdir -p "$(dirname "$DST/$1")"
190 b77b250a 2026-01-11 hi
191 b77b250a 2026-01-11 hi # return content as is for pages with <html> tag
192 b77b250a 2026-01-11 hi if has_html_tag "$1"; then
193 b77b250a 2026-01-11 hi cp "$SRC/$1" "$DST/$1"
194 b77b250a 2026-01-11 hi info "html $1"
195 b77b250a 2026-01-11 hi gz "$1"
196 b77b250a 2026-01-11 hi info "html $1 > $1.gz"
197 b77b250a 2026-01-11 hi return
198 b77b250a 2026-01-11 hi fi
199 b77b250a 2026-01-11 hi
200 b77b250a 2026-01-11 hi # find template
201 b77b250a 2026-01-11 hi t=$(find_template "$1")
202 b77b250a 2026-01-11 hi
203 b77b250a 2026-01-11 hi if test -f "$SRC/$t"; then
204 b77b250a 2026-01-11 hi # return page rendered with template
205 b77b250a 2026-01-11 hi render_page "$SRC/$t" <"$SRC/$1" >"$DST/$1"
206 b77b250a 2026-01-11 hi info "html $1, $t > $1"
207 b77b250a 2026-01-11 hi gz "$1"
208 b77b250a 2026-01-11 hi info "html $1, $t > $1.gz"
209 b77b250a 2026-01-11 hi else
210 b77b250a 2026-01-11 hi # ...or return content as is if template not found
211 b77b250a 2026-01-11 hi cp "$SRC/$1" "$DST/$1"
212 b77b250a 2026-01-11 hi info "html $1"
213 b77b250a 2026-01-11 hi gz "$1"
214 b77b250a 2026-01-11 hi info "html $1 > $1.gz"
215 b77b250a 2026-01-11 hi fi
216 b77b250a 2026-01-11 hi }
217 b77b250a 2026-01-11 hi
218 b77b250a 2026-01-11 hi # write markdown page and zip to dst
219 b77b250a 2026-01-11 hi generate_md() {
220 b77b250a 2026-01-11 hi mkdir -p "$(dirname "$DST/$1")"
221 b77b250a 2026-01-11 hi t=$(find_template "$1")
222 b77b250a 2026-01-11 hi h="${1%.md}.html"
223 b77b250a 2026-01-11 hi if test -f "$SRC/$h"; then fail "fail: $1 collides with $h"; fi
224 b77b250a 2026-01-11 hi if test -f "$SRC/$t"; then
225 b77b250a 2026-01-11 hi md_to_html <"$SRC/$1" | render_page "$SRC/$t" >"$DST/$h"
226 b77b250a 2026-01-11 hi info "md $1, $t > $h"
227 b77b250a 2026-01-11 hi gz "$h"
228 b77b250a 2026-01-11 hi info "md $1, $t > $h.gz"
229 b77b250a 2026-01-11 hi else
230 b77b250a 2026-01-11 hi md_to_html <"$SRC/$1" >"$DST/$h"
231 b77b250a 2026-01-11 hi info "md $1 > $h"
232 b77b250a 2026-01-11 hi gz "$h"
233 b77b250a 2026-01-11 hi info "md $1 > $h.gz"
234 b77b250a 2026-01-11 hi fi
235 b77b250a 2026-01-11 hi }
236 b77b250a 2026-01-11 hi
237 b77b250a 2026-01-11 hi # return diff of two values
238 b77b250a 2026-01-11 hi diff_lines() {
239 b77b250a 2026-01-11 hi fifo=$(mktemp -u) || fail 'fail: diff lines: can not mktemp'
240 b77b250a 2026-01-11 hi mkfifo "$fifo" || fail 'fail: diff lines: can not mkfifo'
241 b77b250a 2026-01-11 hi (printf '%s\n' "$2" >"$fifo") &
242 b77b250a 2026-01-11 hi printf '%s\n' "$1" | diff - "$fifo" || :
243 b77b250a 2026-01-11 hi rm -f "$fifo"
244 b77b250a 2026-01-11 hi }
245 b77b250a 2026-01-11 hi
246 b77b250a 2026-01-11 hi # return second column and sort
247 b77b250a 2026-01-11 hi cut_sort() { cut -d' ' -f2 | sort; }
248 b77b250a 2026-01-11 hi
249 b77b250a 2026-01-11 hi # return pages with their templates, excluding pages with <html> tag
250 b77b250a 2026-01-11 hi pages_with_templates() {
251 b77b250a 2026-01-11 hi while read -r p; do
252 b77b250a 2026-01-11 hi if has_html_tag "$p"; then continue; fi &&
253 b77b250a 2026-01-11 hi printf '%s\t%s\n' "$(find_template "$p")" "$p"
254 b77b250a 2026-01-11 hi done
255 b77b250a 2026-01-11 hi }
256 b77b250a 2026-01-11 hi
257 b77b250a 2026-01-11 hi # return pages related to template
258 b77b250a 2026-01-11 hi pages_by_templates() {
259 b77b250a 2026-01-11 hi grep "$SSG_TEMPLATE" | cut_sort |
260 b77b250a 2026-01-11 hi while read -r t; do echo "$1" | grep "$t" | cut -f2; done
261 b77b250a 2026-01-11 hi }
262 b77b250a 2026-01-11 hi
263 49127160 2026-01-12 hi plan_sitemap() {
264 49127160 2026-01-12 hi # if src_hash is empty do nothing
265 49127160 2026-01-12 hi if test -z "$1"; then return; fi
266 49127160 2026-01-12 hi # if sitemap.xml found in src do nothing
267 49127160 2026-01-12 hi if test -f "$SRC/$SSG_SITEMAP_XML"; then return; fi
268 49127160 2026-01-12 hi echo "$SSG_SITEMAP_XML"
269 16ada7f8 2026-01-31 hi echo "$SSG_SITEMAP_XML.gz"
270 49127160 2026-01-12 hi # if robots.txt found in src do nothing
271 49127160 2026-01-12 hi if test -f "$SRC/$SSG_ROBOTS_TXT"; then return; fi
272 49127160 2026-01-12 hi echo "$SSG_ROBOTS_TXT"
273 16ada7f8 2026-01-31 hi echo "$SSG_ROBOTS_TXT.gz"
274 49127160 2026-01-12 hi }
275 49127160 2026-01-12 hi
276 b77b250a 2026-01-11 hi # return file expected in dst directory
277 b77b250a 2026-01-11 hi plan() {
278 b77b250a 2026-01-11 hi while read -r k f; do
279 b77b250a 2026-01-11 hi case "$k" in
280 b77b250a 2026-01-11 hi copy) echo "$f" ;;
281 16ada7f8 2026-01-31 hi copy_gz) echo "$f" && echo "$f.gz" ;;
282 b77b250a 2026-01-11 hi html) echo "$f" && echo "$f.gz" ;;
283 b77b250a 2026-01-11 hi md) echo "${f%.md}.html" && echo "${f%.md}.html.gz" ;;
284 b5a8db69 2026-02-14 hi sh) command ksh -- "$SRC/$f" "$SRC" |
285 b77b250a 2026-01-11 hi while read -r f; do
286 b77b250a 2026-01-11 hi if test -z "$f"; then continue; fi && echo "$f" && echo "$f.gz"
287 b77b250a 2026-01-11 hi done ;;
288 b77b250a 2026-01-11 hi *) continue ;;
289 b77b250a 2026-01-11 hi esac
290 b77b250a 2026-01-11 hi done
291 49127160 2026-01-12 hi plan_sitemap "$1"
292 b77b250a 2026-01-11 hi }
293 b77b250a 2026-01-11 hi
294 b77b250a 2026-01-11 hi # make dst directory and return src hash as is
295 b77b250a 2026-01-11 hi mkdir_select_all() { mkdir -p "$DST" && echo "$1" | cut_sort; }
296 b77b250a 2026-01-11 hi
297 b77b250a 2026-01-11 hi # remove dst directory and return src hash as is
298 b77b250a 2026-01-11 hi rmdir_select_all() { rm -rf "$DST" && echo "$1" | cut_sort; }
299 b77b250a 2026-01-11 hi
300 b77b250a 2026-01-11 hi # remove files in dst
301 b77b250a 2026-01-11 hi rm_files() { while read -r f; do rm "$DST/$f" && info "rm $f"; done; }
302 b77b250a 2026-01-11 hi
303 b77b250a 2026-01-11 hi # remove empty directories in dst
304 b77b250a 2026-01-11 hi rm_empty_dirs() {
305 b77b250a 2026-01-11 hi find "$DST" -type d -mindepth 1 | while read -r d; do
306 b77b250a 2026-01-11 hi if test -z "$(find "$d" -mindepth 1 -type f)"; then echo "$d"; fi
307 b77b250a 2026-01-11 hi done | sort -r | while read -r e; do
308 b77b250a 2026-01-11 hi rmdir "$e" && info "rmdir ${e#"$DST"/}/"
309 b77b250a 2026-01-11 hi done
310 b77b250a 2026-01-11 hi }
311 b77b250a 2026-01-11 hi
312 b77b250a 2026-01-11 hi # return files with their kind prepended
313 b77b250a 2026-01-11 hi prepend_kind() {
314 b77b250a 2026-01-11 hi while read -r f; do
315 b77b250a 2026-01-11 hi if test -z "$f"; then continue; fi
316 b77b250a 2026-01-11 hi case "$f" in
317 b77b250a 2026-01-11 hi *.html) k='html' ;;
318 b77b250a 2026-01-11 hi *.md) k='md' ;;
319 b77b250a 2026-01-11 hi *.ssg.ignore) k='ignore' ;;
320 b77b250a 2026-01-11 hi *.ssg.*.sh | *.ssg.sh) k='sh' ;;
321 b77b250a 2026-01-11 hi *.ssg.template) k='template' ;;
322 c6dd2b00 2026-01-31 hi *.asc | *.css | *.js | *.json | *.sh | *.svg | *.txt | *.xml) k='copy_gz' ;;
323 16ada7f8 2026-01-31 hi *) k='copy' ;;
324 b77b250a 2026-01-11 hi esac
325 b77b250a 2026-01-11 hi echo "$k $f"
326 b77b250a 2026-01-11 hi done
327 b77b250a 2026-01-11 hi }
328 b77b250a 2026-01-11 hi
329 b77b250a 2026-01-11 hi # return right side of diff
330 b77b250a 2026-01-11 hi select_right() { sed -n 's/^> \([^\ ]*\).*/\1/p'; }
331 b77b250a 2026-01-11 hi
332 b77b250a 2026-01-11 hi # remove files and directories not present in plan from dst
333 b77b250a 2026-01-11 hi clean_up_dst() {
334 49127160 2026-01-12 hi dst_plan=$(echo "$1" | cut_sort | prepend_kind | plan "$1" | sort)
335 b77b250a 2026-01-11 hi dst_files=$(echo "$2" | sort_relative "$DST" | cut_sort)
336 b77b250a 2026-01-11 hi diff_lines "$dst_plan" "$dst_files" | select_right | files_in "$DST" |
337 b77b250a 2026-01-11 hi rm_files
338 b77b250a 2026-01-11 hi rm_empty_dirs
339 b77b250a 2026-01-11 hi }
340 b77b250a 2026-01-11 hi
341 b77b250a 2026-01-11 hi # return sorted list of unique updated files and pages for updated template
342 b77b250a 2026-01-11 hi select_updated() {
343 b77b250a 2026-01-11 hi src_updated=$(echo "$2" | select_right | cut_sort)
344 b77b250a 2026-01-11 hi pages=$(echo "$1" | grep -E '.html|.md' | cut_sort | pages_with_templates)
345 b77b250a 2026-01-11 hi {
346 b77b250a 2026-01-11 hi echo "$src_updated"
347 b77b250a 2026-01-11 hi echo "$src_updated" | pages_by_templates "$pages"
348 b77b250a 2026-01-11 hi } | sort -u
349 b77b250a 2026-01-11 hi }
350 b77b250a 2026-01-11 hi
351 b77b250a 2026-01-11 hi is_empty() { test -z "$1"; }
352 b77b250a 2026-01-11 hi is_dir() { test -d "$1"; }
353 b77b250a 2026-01-11 hi is_ssg_dst() { test -f "$DST/$SSG_DST"; }
354 b77b250a 2026-01-11 hi is_ssg_src() { test -f "$DST/$SSG_SRC"; }
355 b77b250a 2026-01-11 hi is_matching_ssg_dst() {
356 b77b250a 2026-01-11 hi test "$(sha256 <"$DST/$SSG_DST")" = "$(echo "$1" | sha256)"
357 b77b250a 2026-01-11 hi }
358 b77b250a 2026-01-11 hi diff_src() { diff_lines "$(cat "$DST/$SSG_SRC")" "$1"; }
359 b77b250a 2026-01-11 hi
360 b77b250a 2026-01-11 hi # return files to be updated
361 b77b250a 2026-01-11 hi select_src_files() {
362 43659a89 2026-01-25 hi if ! is_dir "$DST"; then mkdir_select_all "$1" && return; fi
363 49127160 2026-01-12 hi dst_hash=$(hash_dst)
364 49127160 2026-01-12 hi if is_empty "$1"; then
365 49127160 2026-01-12 hi clean_up_dst "$src_hash" "$dst_hash"
366 49127160 2026-01-12 hi return
367 49127160 2026-01-12 hi fi
368 b77b250a 2026-01-11 hi if ! is_ssg_src || ! is_ssg_dst; then rmdir_select_all "$1" && return; fi
369 b77b250a 2026-01-11 hi if ! is_matching_ssg_dst "$dst_hash"; then rmdir_select_all "$1" && return; fi
370 b77b250a 2026-01-11 hi src_hash_diff=$(diff_src "$1")
371 b77b250a 2026-01-11 hi if is_empty "$src_hash_diff"; then return; fi
372 b77b250a 2026-01-11 hi clean_up_dst "$src_hash" "$dst_hash"
373 b77b250a 2026-01-11 hi select_updated "$src_hash" "$src_hash_diff"
374 b77b250a 2026-01-11 hi }
375 b77b250a 2026-01-11 hi
376 49127160 2026-01-12 hi # write sitemap to dst
377 49127160 2026-01-12 hi generate_sitemap() {
378 49127160 2026-01-12 hi # if src_hash is empty do nothing
379 49127160 2026-01-12 hi if test -z "$1"; then return; fi
380 49127160 2026-01-12 hi # if sitemap.xml found in src do nothing
381 49127160 2026-01-12 hi if test -f "$SRC/$SSG_SITEMAP_XML"; then return; fi
382 49127160 2026-01-12 hi dst_pages=$(find "$DST" -type f -name '*.html' | sort_relative "$DST")
383 49127160 2026-01-12 hi # if dst_pages is empty do nothing
384 49127160 2026-01-12 hi if test -z "$dst_pages"; then return; fi
385 49127160 2026-01-12 hi # if no pages added or removed do nothing
386 49127160 2026-01-12 hi if test -f "$DST/.ssg.dst"; then
387 49127160 2026-01-12 hi dst_pages_was=$(cut_sort <"$DST/.ssg.dst" | grep '.html$')
388 49127160 2026-01-12 hi dst_pages_updated=$(diff_lines "$dst_pages_was" "$dst_pages")
389 49127160 2026-01-12 hi if test -z "$dst_pages_updated"; then return; fi
390 49127160 2026-01-12 hi fi
391 49127160 2026-01-12 hi # generate sitemap.xml for all pages in dst
392 49127160 2026-01-12 hi {
393 49127160 2026-01-12 hi echo '<?xml version="1.0" encoding="UTF-8"?>
394 49127160 2026-01-12 hi <urlset
395 49127160 2026-01-12 hi xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
396 49127160 2026-01-12 hi xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
397 49127160 2026-01-12 hi http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
398 49127160 2026-01-12 hi xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
399 49127160 2026-01-12 hi echo "$dst_pages" | sed -E '
400 49127160 2026-01-12 hi s,^$,,
401 49127160 2026-01-12 hi s,^'"$DST"',,
402 49127160 2026-01-12 hi s,index.html$,,
403 b092913f 2026-01-23 hi s,^(.*)$, <url><loc>https://'"$SITE"'/\1</loc></url>,'
404 49127160 2026-01-12 hi echo '</urlset>'
405 49127160 2026-01-12 hi } >"$DST/$SSG_SITEMAP_XML"
406 49127160 2026-01-12 hi info "sitemap $SSG_SITEMAP_XML"
407 16ada7f8 2026-01-31 hi gz "$SSG_SITEMAP_XML"
408 16ada7f8 2026-01-31 hi info "sitemap $SSG_SITEMAP_XML.gz"
409 49127160 2026-01-12 hi
410 49127160 2026-01-12 hi # if robots.txt found in src do nothing
411 49127160 2026-01-12 hi if test -f "$SRC/$SSG_ROBOTS_TXT"; then return; fi
412 49127160 2026-01-12 hi # generate robots.txt in dst
413 49127160 2026-01-12 hi echo 'user-agent: *
414 b092913f 2026-01-23 hi sitemap: https://'"$SITE"'/sitemap.xml' >"$DST/$SSG_ROBOTS_TXT"
415 49127160 2026-01-12 hi info "sitemap $SSG_ROBOTS_TXT"
416 16ada7f8 2026-01-31 hi gz "$SSG_ROBOTS_TXT"
417 16ada7f8 2026-01-31 hi info "sitemap $SSG_ROBOTS_TXT.gz"
418 49127160 2026-01-12 hi }
419 49127160 2026-01-12 hi
420 b77b250a 2026-01-11 hi # write files in dst directory
421 b77b250a 2026-01-11 hi generate() {
422 b77b250a 2026-01-11 hi while read -r k f; do
423 b77b250a 2026-01-11 hi case "$k" in
424 b77b250a 2026-01-11 hi copy) generate_copy "$f" ;;
425 16ada7f8 2026-01-31 hi copy_gz) generate_copy_gz "$f" ;;
426 b77b250a 2026-01-11 hi html) generate_html "$f" ;;
427 b77b250a 2026-01-11 hi md) generate_md "$f" ;;
428 b77b250a 2026-01-11 hi sh) generate_sh "$f" ;;
429 b77b250a 2026-01-11 hi template) info "template $f" ;;
430 b77b250a 2026-01-11 hi ignore) info "ignore $f" ;;
431 b77b250a 2026-01-11 hi *) info "unknown $f" ;;
432 b77b250a 2026-01-11 hi esac
433 b77b250a 2026-01-11 hi done
434 49127160 2026-01-12 hi generate_sitemap "$1"
435 b77b250a 2026-01-11 hi }
436 b77b250a 2026-01-11 hi
437 b77b250a 2026-01-11 hi # write src and dst hash files to dst directory
438 b77b250a 2026-01-11 hi write_hashes() {
439 b77b250a 2026-01-11 hi if ! test -d "$DST"; then return; fi
440 b77b250a 2026-01-11 hi echo "$1" >"$DST/$SSG_SRC"
441 a3cde1e1 2026-01-25 hi echo "$2" | tee "$DST/$SSG_DST" | sha256
442 b77b250a 2026-01-11 hi }
443 b77b250a 2026-01-11 hi
444 b77b250a 2026-01-11 hi main() {
445 b77b250a 2026-01-11 hi fail_no_args "${@}"
446 b77b250a 2026-01-11 hi fail_no_src "${@}"
447 b77b250a 2026-01-11 hi
448 b77b250a 2026-01-11 hi SRC=$(cd "$1" && pwd)
449 43659a89 2026-01-25 hi DST="$(dirname "$2")/$(basename "$2")"
450 b092913f 2026-01-23 hi SITE="$(basename "$SRC")"
451 b77b250a 2026-01-11 hi SSG_IGNORE='.ssg.ignore'
452 b77b250a 2026-01-11 hi SSG_TEMPLATE='.ssg.template'
453 b77b250a 2026-01-11 hi SSG_SRC='.ssg.src'
454 b77b250a 2026-01-11 hi SSG_DST='.ssg.dst'
455 49127160 2026-01-12 hi SSG_SITEMAP_XML='sitemap.xml'
456 49127160 2026-01-12 hi SSG_ROBOTS_TXT='robots.txt'
457 b77b250a 2026-01-11 hi
458 b77b250a 2026-01-11 hi src_hash=$(hash_src)
459 49127160 2026-01-12 hi select_src_files "$src_hash" | prepend_kind | generate "$src_hash"
460 b77b250a 2026-01-11 hi write_hashes "$src_hash" "$(hash_dst)"
461 b77b250a 2026-01-11 hi }
462 b77b250a 2026-01-11 hi
463 a3cde1e1 2026-01-25 hi main "${@}" 3>&1