4 # https://romanzolotarev.com/ssg/
5 # copyright 2018-2026 romanzolotarev.com
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.
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
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"
43 "$@" -print0 | xargs -r -0 -n256 sha256 -r | sort_relative "$dir"
46 # return find expression to exclude paths
50 */) echo "! -path $x*" ;;
51 *) echo "! -path $x" ;;
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
61 while read -r f; do d="$(dirname "$f")" && sed "s,^,${d}/," "$f"; done
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"; }
76 while read -r line; do
82 before="${rest%%\{\{*}"
86 if test $s -lt 0; then o="${o}${before}"; fi
90 if test $s -lt 0 -a -z "$(printenv "${tag#\#}")"; then s=$d; fi
92 '/'*) if test $s -eq $d; then s=-1; fi && d=$((d - 1)) ;;
95 if test $s -lt 0 -a -n "$(printenv "${tag#^}")"; then s=$d; fi
97 *) if test $s -lt 0; then o="${o}$(printenv "$tag")"; fi ;;
100 *) if test $s -lt 0; then o="${o}${rest}"; fi && break ;;
103 if test $s -lt 0 -o -n "$o"; then printf '%s\n' "$o"; fi
107 # returns page rendered with its template
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'; }
114 # use src directory name as site name
115 title="$(printf '%s' "$content" | get_title | tr -d '\n')"
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
128 --html-no-escapehtml \
130 --parse-no-autolink \
134 # write zip unless its gz variant found in src
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
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")
151 # return relative paths to files if they found in directory
153 while read -r f; do if test -f "$1/$f"; then echo "$f"; fi; done
156 # write and zip file to dst
158 mkdir -p "$(dirname "$DST/$1")"
159 cp "$SRC/$1" "$DST/$1"
162 info "copy gz $1 > $1.gz"
167 mkdir -p "$(dirname "$DST/$1")"
168 cp "$SRC/$1" "$DST/$1"
172 # execute script and write zips for output files to dst
174 command ksh -- "$SRC/$1" "$SRC" "$DST" |
176 if test -z "$f"; then continue; fi
177 if test -f "$SRC/$f"; then fail "fail: $1 collides with $f"; fi
184 # return true page has <html> tag
185 has_html_tag() { grep -qi '<html[^>]*>' "$SRC/$1"; }
187 # write html page and zip to dst
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"
196 info "html $1 > $1.gz"
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"
208 info "html $1, $t > $1.gz"
210 # ...or return content as is if template not found
211 cp "$SRC/$1" "$DST/$1"
214 info "html $1 > $1.gz"
218 # write markdown page and zip to dst
220 mkdir -p "$(dirname "$DST/$1")"
221 t=$(find_template "$1")
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"
228 info "md $1, $t > $h.gz"
230 md_to_html <"$SRC/$1" >"$DST/$h"
237 # return diff of two values
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" || :
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() {
252 if has_html_tag "$p"; then continue; fi &&
253 printf '%s\t%s\n' "$(find_template "$p")" "$p"
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
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
278 while read -r k f; do
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" |
286 if test -z "$f"; then continue; fi && echo "$f" && echo "$f.gz"
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
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"/}/"
312 # return files with their kind prepended
315 if test -z "$f"; then continue; fi
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' ;;
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
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" |
341 # return sorted list of unique updated files and pages for updated template
343 src_updated=$(echo "$2" | select_right | cut_sort)
344 pages=$(echo "$1" | grep -E '.html|.md' | cut_sort | pages_with_templates)
347 echo "$src_updated" | pages_by_templates "$pages"
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
362 if ! is_dir "$DST"; then mkdir_select_all "$1" && return; fi
364 if is_empty "$1"; then
365 clean_up_dst "$src_hash" "$dst_hash"
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
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
391 # generate sitemap.xml for all pages in dst
393 echo '<?xml version="1.0" encoding="UTF-8"?>
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 '
403 s,^(.*)$, <url><loc>https://'"$SITE"'/\1</loc></url>,'
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
414 sitemap: https://'"$SITE"'/sitemap.xml' >"$DST/$SSG_ROBOTS_TXT"
415 info "sitemap $SSG_ROBOTS_TXT"
417 info "sitemap $SSG_ROBOTS_TXT.gz"
420 # write files in dst directory
422 while read -r k f; do
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" ;;
434 generate_sitemap "$1"
437 # write src and dst hash files to dst directory
439 if ! test -d "$DST"; then return; fi
440 echo "$1" >"$DST/$SSG_SRC"
441 echo "$2" | tee "$DST/$SSG_DST" | sha256
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'
455 SSG_SITEMAP_XML='sitemap.xml'
456 SSG_ROBOTS_TXT='robots.txt'
459 select_src_files "$src_hash" | prepend_kind | generate "$src_hash"
460 write_hashes "$src_hash" "$(hash_dst)"