commit b092913f81f1f9c3cb01801a45a6088bc96288a0 from: roman zolotarev date: Fri Jan 23 19:11:29 2026 UTC fix: render_page(): remove unnecessary esc() and extract $SITE commit - 4242eafc944ceef1e2748b3698e2a1c93bd246ba commit + b092913f81f1f9c3cb01801a45a6088bc96288a0 blob - 2fa134a6e421d5d03dda235f88233894f0ae538a blob + f5305e5e2f5c4b2f0a5956e453a548ef3ee8951d --- ssg.sh +++ ssg.sh @@ -105,20 +105,18 @@ mustache() { # returns page rendered with its template render_page() { - # strip newlines and escape ampersands and slashes - esc() { tr -d '\n' | sed 's/[&/]/\\&/g'; } # replace newlines with spaces and extract title from the first

tag get_title() { tr '\n' ' ' | sed -n 's/^[^<]*<[Hh]1[^>]*>\([^<]*\)<\/[Hh]1[^>]*>.*/\1/p'; } content="$(cat)" - # use src dir name as site name - site="$(basename "$SRC" | esc)" - title="$(printf '%s' "$content" | get_title | esc)" + # use src directory name as site name + title="$(printf '%s' "$content" | get_title | tr -d '\n')" + site="$SITE" # replace {{title}} and {{site}} tags with values from variables, # replace {{content}} tag with page content. # use truthy tag to show title only when it's found in content, for example: # {{#title}}{{title}: {{/title}} - export content site title && mustache <"$1" + export content title site && mustache <"$1" } # return html converted from markdown @@ -388,7 +386,6 @@ generate_sitemap() { fi # generate sitemap.xml for all pages in dst { - site=$(basename "$SRC") echo ' https://'"$site"'/\1,' + s,^(.*)$, https://'"$SITE"'/\1,' echo '' } >"$DST/$SSG_SITEMAP_XML" info "sitemap $SSG_SITEMAP_XML" @@ -408,7 +405,7 @@ generate_sitemap() { if test -f "$SRC/$SSG_ROBOTS_TXT"; then return; fi # generate robots.txt in dst echo 'user-agent: * -sitemap: https://'"$site"'/sitemap.xml' >"$DST/$SSG_ROBOTS_TXT" +sitemap: https://'"$SITE"'/sitemap.xml' >"$DST/$SSG_ROBOTS_TXT" info "sitemap $SSG_ROBOTS_TXT" } @@ -442,6 +439,7 @@ main() { SRC=$(cd "$1" && pwd) DST="$2" + SITE="$(basename "$SRC")" SSG_IGNORE='.ssg.ignore' SSG_TEMPLATE='.ssg.template' SSG_SRC='.ssg.src' blob - 6a771627ed42f0433cd24383fd758424703d17ca blob + 371c3087f96573cf33fd12f8a371ddc020675b01 --- ssg.test.sh +++ ssg.test.sh @@ -1,7 +1,7 @@ #!/bin/ksh -e ok_count=0 -ok_expected=27 +ok_expected=28 plan() { echo "$ok_expected..$ok_count" @@ -420,6 +420,20 @@ sitemap sitemap.xml cat "$dst/robots.txt" | not_ok_diff "$1" '' ;; + generate_html_with_template_title) + mkdir "$src" "$dst" + echo '

'\''→ +

' >"$src/h.html" + echo '{{title}}' >"$src/.ssg.template" + + "$cmd" "$src" "$dst" 2>/dev/null + hexdump -C "$dst/h.html" | not_ok_diff_n "$1: h.html" ' +00000000 3c 74 69 74 6c 65 3e 27 26 72 61 72 72 3b 20 3c |'\''→ <| +00000010 2f 74 69 74 6c 65 3e 0a |/title>.| +00000018 +' + ;; + generate_html_with_template) mkdir "$src" "$dst" echo '<h1>h1</h1>' >"$src/h.html" @@ -719,6 +733,7 @@ t generate_copy t generate_file t generate_html t generate_html_with_template +t generate_html_with_template_title t generate_html_with_template_no_title t generate_html_with_template_in_dir t generate_html_template_not_found