diff options
Diffstat (limited to '.local/bin/ssg')
-rwxr-xr-x | .local/bin/ssg | 126 |
1 files changed, 51 insertions, 75 deletions
diff --git a/.local/bin/ssg b/.local/bin/ssg index e220a41..82b88b1 100755 --- a/.local/bin/ssg +++ b/.local/bin/ssg @@ -21,23 +21,21 @@ main() { test -n "$2" || usage test -n "$3" || usage test -n "$4" || usage - test -d "$1" || no_dir "$1" - test -d "$2" || no_dir "$2" + test -d "$1" || no_dir "$1" + test -d "$2" || no_dir "$2" src=$(readlink_f "$1") dst=$(readlink_f "$2") IGNORE=$( - if ! test -f "$src/.ssgignore" - then + if ! test -f "$src/.ssgignore"; then printf ' ! -path "*/.*"' return fi - while read -r x - do + while read -r x; do test -n "$x" || continue printf ' ! -path "*/%s*"' "$x" - done < "$src/.ssgignore" + done <"$src/.ssgignore" ) # files @@ -50,35 +48,31 @@ main() { test -f "$h_file" && HEADER=$(cat "$h_file") && export HEADER list_dirs "$src" | - (cd "$src" && cpio -pdu "$dst") + (cd "$src" && cpio -pdu "$dst") fs=$( - if test -f "$dst/.files" - then list_affected_files "$src" "$dst/.files" - else list_files "$1" - fi + if test -f "$dst/.files"; then + list_affected_files "$src" "$dst/.files" + else + list_files "$1" + fi ) - if test -n "$fs" - then + if test -n "$fs"; then echo "$fs" | tee "$dst/.files" - if echo "$fs" | grep -q '\.md$' - then - if test -x "$(which pandoc 2> /dev/null)" - then + if echo "$fs" | grep -q '\.md$'; then + if test -x "$(which pandoc 2>/dev/null)"; then echo "Using pandoc" echo "$fs" | grep '\.md$' | - render_md_files_pandoc "$src" "$dst" "$title" - elif test -x "$(which lowdown 2> /dev/null)" - then + render_md_files_pandoc "$src" "$dst" "$title" + elif test -x "$(which lowdown 2>/dev/null)"; then echo "$fs" | grep '\.md$' | - render_md_files_lowdown "$src" "$dst" "$title" + render_md_files_lowdown "$src" "$dst" "$title" else - if test -x "$(which Markdown.pl 2> /dev/null)" - then + if test -x "$(which Markdown.pl 2>/dev/null)"; then echo "$fs" | grep '\.md$' | - render_md_files_Markdown_pl "$src" "$dst" "$title" + render_md_files_Markdown_pl "$src" "$dst" "$title" else echo "couldn't find lowdown nor Markdown.pl" exit 3 @@ -87,36 +81,33 @@ main() { fi echo "$fs" | grep '\.html$' | - render_html_files "$src" "$dst" "$title" + render_html_files "$src" "$dst" "$title" echo "$fs" | grep -Ev '\.md$|\.html$' | - (cd "$src" && cpio -pu "$dst") + (cd "$src" && cpio -pu "$dst") fi printf '[ssg] ' >&2 print_status 'file, ' 'files, ' "$fs" >&2 - # sitemap base_url="$4" date=$(date +%Y-%m-%d) - urls=$(list_pages "$src") + urls=$(list_pages "$src") test -n "$urls" && - render_sitemap "$urls" "$base_url" "$date" > "$dst/sitemap.xml" + render_sitemap "$urls" "$base_url" "$date" >"$dst/sitemap.xml" print_status 'url' 'urls' "$urls" >&2 echo >&2 } - readlink_f() { file="$1" cd "$(dirname "$file")" file=$(basename "$file") - while test -L "$file" - do + while test -L "$file"; do file=$(readlink "$file") cd "$(dirname "$file")" file=$(basename "$file") @@ -125,7 +116,6 @@ readlink_f() { echo "$dir/$file" } - print_status() { test -z "$3" && printf 'no %s' "$2" && return @@ -136,13 +126,11 @@ print_status() { }' } - usage() { echo "usage: ${0##*/} src dst title base_url" >&2 exit 1 } - no_dir() { echo "${0##*/}: $1: No such directory" >&2 exit 2 @@ -152,14 +140,12 @@ list_dirs() { cd "$1" && eval "find . -type d ! -name '.' ! -path '*/_*' $IGNORE" } - list_files() { cd "$1" && eval "find . -type f ! -name '.' ! -path '*/_*' $IGNORE" } - -list_dependant_files () { - e="\\( -name '*.html' -o -name '*.md' -o -name '*.css' -o -name '*.js' \\)" +list_dependant_files() { + e="\\( -name '*.html' -o -name '*.md' -o -name '*.css' -o -name '*.js' \\)" cd "$1" && eval "find . -type f ! -name '.' ! -path '*/_*' $IGNORE $e" } @@ -167,65 +153,58 @@ list_newer_files() { cd "$1" && eval "find . -type f ! -name '.' $IGNORE -newer $2" } - has_partials() { grep -qE '^./_.*\.html$|^./_.*\.js$|^./_.*\.css$' } - list_affected_files() { fs=$(list_newer_files "$1" "$2") - if echo "$fs" | has_partials - then list_dependant_files "$1" - else echo "$fs" + if echo "$fs" | has_partials; then + list_dependant_files "$1" + else + echo "$fs" fi } - render_html_files() { - while read -r f - do render_html_file "$3" < "$1/$f" > "$2/$f" + while read -r f; do + render_html_file "$3" <"$1/$f" >"$2/$f" done } - render_md_files_lowdown() { - while read -r f - do + while read -r f; do lowdown \ - --html-no-head-ids \ - --html-no-escapehtml \ - --html-no-skiphtml \ - < "$1/$f" | - render_html_file "$3" "$1/$f" \ - > "$2/${f%\.md}.html" + --html-no-head-ids \ + --html-no-escapehtml \ + --html-no-skiphtml \ + <"$1/$f" | + render_html_file "$3" "$1/$f" \ + >"$2/${f%\.md}.html" done } render_md_files_pandoc() { - while read -r f - do - pandoc -f markdown --from markdown-auto_identifiers-implicit_figures < "$1/$f" | \ + while read -r f; do + pandoc -f markdown --from markdown-auto_identifiers-implicit_figures <"$1/$f" | render_html_file "$3" "$1/$f" \ - > "$2/${f%\.md}.html" + >"$2/${f%\.md}.html" done } render_md_files_Markdown_pl() { - while read -r f - do - Markdown.pl < "$1/$f" | - render_html_file "$3" "$1/$f" \ - > "$2/${f%\.md}.html" + while read -r f; do + Markdown.pl <"$1/$f" | + render_html_file "$3" "$1/$f" \ + >"$2/${f%\.md}.html" done } - render_html_file() { - # h/t Devin Teske - lastmod=$(date -d @$(stat -c %Y "$2")) - awk -v title="$1" -v moddate="$lastmod" -v srcfile="$2" ' + # h/t Devin Teske + lastmod=$(date -d @$(stat -c %Y "$2")) + awk -v title="$1" -v moddate="$lastmod" -v srcfile="$2" ' { body = body "\n" $0 } END { body = substr(body, 2) @@ -263,14 +242,11 @@ render_html_file() { }' } - - list_pages() { cd "$1" && find -name '*.md' -not -name '404.md' -type f -printf "%TY-%Tm-%Td %p\n" | - sed 's#./##;s#.md$#.html#;s#/index.html$#/#' + sed 's#./##;s#.md$#.html#;s#/index.html$#/#' } - render_sitemap() { urls="$1" base_url="$2" @@ -283,7 +259,7 @@ render_sitemap() { echo 'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"' echo 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' echo "$urls" | - awk -v base="$base_url" '{print "<url><loc>"base"/"$2"</loc><lastmod>"$1"</lastmod><priority>1.0</priority></url>"}' + awk -v base="$base_url" '{print "<url><loc>"base"/"$2"</loc><lastmod>"$1"</lastmod><priority>1.0</priority></url>"}' echo '</urlset>' } |