Blob


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