From 5e0c12c1d4940c3edb393c92f352ad4ca0c3b7c9 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Mon, 14 Nov 2022 11:43:19 -0600 Subject: Change indentation --- vienna | 428 +++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 215 insertions(+), 213 deletions(-) diff --git a/vienna b/vienna index 8c3c92f..c7b4ffe 100755 --- a/vienna +++ b/vienna @@ -6,7 +6,7 @@ ### Entry point usage() { - cat < @@ -37,131 +37,132 @@ which by default is ./.vienna.sh. vienna uses heredoc-inspired templating, so you can include shell snippets and variables by doubling the dollar signs. EOF - exit "${1:-0}" + exit "${1:-0}" } configure() { - ## Set up environment - DOMAIN="${VIENNA_DOMAIN:-https://www.example.com}" - TMPD="${VIENNA_TMPD:-/tmp/vienna}" - WORKD="${VIENNA_WORKD:-$PWD}" - OUTD="${VIENNA_OUTD:-out}" - CONFIG="${VIENNA_CONFIG:-./.vienna.sh}" - # Templates - PAGE_TEMPLATE="${VIENNA_PAGE_TEMPLATE:-.page.tmpl.html}" - INDEX_TEMPLATE="${VIENNA_INDEX_TEMPLATE:-.index.tmpl.html}" - FEED_TEMPLATE="${VIENNA_FEED_TEMPLATE:-.feed.tmpl.xml}" - # File extensions - PAGE_RAW_EXT="${VIENNA_PAGE_RAW_EXT:-htm}" - # Logging - LOG=true - ## Parse command line arguments - while getopts C:c:d:ho:q opt; do - case "$opt" in - C) WORKD="$OPTARG" ;; - c) - CONFIG="$OPTARG" - # To error later if a config is specified on the command - # line but doesn't exist. - CONFIG_ARG=1 - ;; - d) DOMAIN="$OPTARG" ;; - h) usage 0 ;; - o) OUTD="$OPTARG" ;; - q) LOG=false ;; - *) exit 1 ;; - esac - done - ## Log configuration variables - log config "domain: $DOMAIN" - log config "workdir: $WORKD" - log config "output: $OUTD" - ## Initialize state - FILE= - ## Cleanup after we're done - trap cleanup INT QUIT + ## Set up environment + DOMAIN="${VIENNA_DOMAIN:-https://www.example.com}" + TMPD="${VIENNA_TMPD:-/tmp/vienna}" + WORKD="${VIENNA_WORKD:-$PWD}" + OUTD="${VIENNA_OUTD:-out}" + CONFIG="${VIENNA_CONFIG:-./.vienna.sh}" + # Templates + PAGE_TEMPLATE="${VIENNA_PAGE_TEMPLATE:-.page.tmpl.html}" + INDEX_TEMPLATE="${VIENNA_INDEX_TEMPLATE:-.index.tmpl.html}" + FEED_TEMPLATE="${VIENNA_FEED_TEMPLATE:-.feed.tmpl.xml}" + # File extensions + PAGE_RAW_EXT="${VIENNA_PAGE_RAW_EXT:-htm}" + # Logging + LOG=true + ## Parse command line arguments + while getopts C:c:d:ho:q opt; do + case "$opt" in + C) WORKD="$OPTARG" ;; + c) + CONFIG="$OPTARG" + # To error later if a config is specified on the command + # line but doesn't exist. + CONFIG_ARG=1 + ;; + d) DOMAIN="$OPTARG" ;; + h) usage 0 ;; + o) OUTD="$OPTARG" ;; + q) LOG=false ;; + *) exit 1 ;; + esac + done + ## Log configuration variables + log config "domain: $DOMAIN" + log config "workdir: $WORKD" + log config "output: $OUTD" + ## Initialize state + FILE= + ## Cleanup after we're done + trap cleanup INT QUIT } main() { - # State predicates - alias pagep=false indexp=false feedp=false - # Convenience aliases - alias body=cat title='meta title' pubdate='meta date' - # Configure - configure "$@" - shift "$((OPTIND - 1))" - # Further argument processing --- pre-build - preprocess "$@" - # Prepare - cd "$WORKD" || exit 2 - if test -f "$CONFIG"; then - # Source ./.vienna.sh, if it exists. - . "$CONFIG" - elif test -n "$CONFIG_ARG"; then - # If a -c option was passed on the command line but the file - # doesn't exist, that's an error. If we're just looking for the - # default file, however, there is no error---the user might want - # to use the default configuration. - log "Can't find configuration \`$CONFIG'." - exit 2 - fi - mkdir -p "$OUTD" || exit 2 - mkdir -p "$TMPD" || exit 2 - # Build pages - alias pagep=true - genpage *."$PAGE_RAW_EXT" || exit 2 - alias pagep=false - # Build index - alias indexp=true - genlist index_item "$INDEX_TEMPLATE" *."$PAGE_RAW_EXT" >"$OUTD/index.html" || exit 2 - alias indexp=false - # Build feed - alias feedp=true - genlist feed_item "$FEED_TEMPLATE" *."$PAGE_RAW_EXT" >"$OUTD/feed.xml" || exit 2 - alias feedp=false - # Copy static files - static * || exit 2 - # Further argument processing --- post-build - postprocess "$@" + # State predicates + alias pagep=false indexp=false feedp=false + # Convenience aliases + alias body=cat title='meta title' pubdate='meta date' + # Configure + configure "$@" + shift "$((OPTIND - 1))" + # Further argument processing --- pre-build + preprocess "$@" + # Prepare + cd "$WORKD" || exit 2 + if test -f "$CONFIG"; then + # Source ./.vienna.sh, if it exists. + . "$CONFIG" + elif test -n "$CONFIG_ARG"; then + # If a -c option was passed on the command line but the file + # doesn't exist, that's an error. If we're just looking for the + # default file, however, there is no error---the user might want + # to use the default configuration. + log "Can't find configuration \`$CONFIG'." + exit 2 + fi + mkdir -p "$OUTD" || exit 2 + mkdir -p "$TMPD" || exit 2 + # Build pages + alias pagep=true + genpage *."$PAGE_RAW_EXT" || exit 2 + alias pagep=false + # Build index + alias indexp=true + genlist index_item "$INDEX_TEMPLATE" *."$PAGE_RAW_EXT" >"$OUTD/index.html" || exit 2 + alias indexp=false + # Build feed + alias feedp=true + genlist feed_item "$FEED_TEMPLATE" *."$PAGE_RAW_EXT" >"$OUTD/feed.xml" || exit 2 + alias feedp=false + # Copy static files + static * || exit 2 + # Further argument processing --- post-build + postprocess "$@" } preprocess() { - case "${1:-ok}" in - ok) ;; - clean) - log vienna "clean" - rm -r "$OUTD" - exit - ;; - esac + case "${1:-ok}" in + ok) ;; + clean) + log vienna "clean" + rm -r "$OUTD" + cleanup + exit + ;; + esac } postprocess() { - case "${1:-ok}" in - ok) ;; - publish) - log vienna "publish" - publish "$OUTD" - ;; - preview) - log vienna "preview" - preview "$OUTD" - ;; - *) - log vienna "Don't know command \`$1'." - exit 1 - ;; - esac + case "${1:-ok}" in + ok) ;; + publish) + log vienna "publish" + publish "$OUTD" + ;; + preview) + log vienna "preview" + preview "$OUTD" + ;; + *) + log vienna "Don't know command \`$1'." + exit 1 + ;; + esac } cleanup() { - test -z "$DEBUG" && - test -z "$NODEP_RM" && - rm -r "$TMPD" + test -z "$DEBUG" && + test -z "$NODEP_RM" && + rm -r "$TMPD" } publish() { - cat <&2 + cat <&2 I want to publish your website but I don't know how. Make a $CONFIG file in this directory and write a \`publish' @@ -169,11 +170,11 @@ function telling me what to do. I recommend using \`rsync', but you live your life." EOF - exit 3 + exit 3 } preview() { - cat <&2 + cat <&2 I want to show you a preview of your website but I don't know how. Make a $CONFIG file in this directory and write @@ -182,17 +183,17 @@ using something like \`python -m http.server', but you live your life." EOF - exit 3 + exit 3 } ### Utility log() { - if "$LOG"; then - t="$1" - shift - echo >&2 "[$t]" "$@" - fi + if "$LOG"; then + t="$1" + shift + echo >&2 "[$t]" "$@" + fi } ### File processing @@ -200,140 +201,141 @@ log() { ## Building block functions shellfix() { # shellfix FILE... - ## Replace ` with \`, $ with \$, and $$ with $ - # shellcheck disable=2016 - sed -E \ - -e 's/`/\\`/g' \ - -e 's/(^|[^\$])\$([^\$]|$)/\1\\$\2/g' \ - -e 's/\$\$/$/g' \ - "$@" + ## Replace ` with \`, $ with \$, and $$ with $ + # shellcheck disable=2016 + sed -E \ + -e 's/`/\\`/g' \ + -e 's/(^|[^\$])\$([^\$]|$)/\1\\$\2/g' \ + -e 's/\$\$/$/g' \ + "$@" } expand() { # expand TEMPLATE... < INPUT - ## Print TEMPLATE to stdout, expanding shell constructs. - end="expand_:_${count:=0}_:_end" - eval "$( - echo "cat<<$end" - shellfix "$@" - echo - echo "$end" - )" && count=$((count + 1)) + ## Print TEMPLATE to stdout, expanding shell constructs. + end="expand_:_${count:=0}_:_end" + eval "$( + echo "cat<<$end" + shellfix "$@" + echo + echo "$end" + )" && count=$((count + 1)) } phtml() { # phtml < INPUT - ## Output HTML, pretty much. - # Paragraphs unadorned with html tags will be wrapped in

tags, and - # &, <, > will be escaped unless prepended with \. Paragraphs where the - # first character is < will be left as-is, excepting indentation on the - # first line (an implementation detail). - sed -E \ - '/./{H;1h;$!d;}; x; - s#^[ \n\t]+[^<].*#&#; - t par; b; - :par; - s#([^\\])&#\1\&#g; s#\\&#\&#g; - s#([^\\])<#\1\<#g; s#\\<#<#g; - s#([^\\])>#\1\>#g; s#\\>#>#g;' + ## Output HTML, pretty much. + # Paragraphs unadorned with html tags will be wrapped in

tags, and + # &, <, > will be escaped unless prepended with \. Paragraphs where the + # first character is < will be left as-is, excepting indentation on the + # first line (an implementation detail). + sed -E \ + '/./{H;1h;$!d;}; x; + s#^[ \n\t]+[^<].*#&#; + t par; b; + :par; + s#([^\\])&#\1\&#g; s#\\&#\&#g; + s#([^\\])<#\1\<#g; s#\\<#<#g; + s#([^\\])>#\1\>#g; s#\\>#>#g;' } meta() { # meta FIELD [FILE] < INPUT - ## Extract metadata FIELDS from INPUT. - # FILE gives the filename to save metadata to in the $WORKD. It - # defaults to the current value for $FILE. - # - # Metadata should exist as colon-separated data in an HTML comment at - # the beginning of an input file. - field="$1" - file="${2:-$FILE}" - metafile="$TMPD/${file}.meta" - test -f "$metafile" || - sed '//q' >"$metafile" - sed -n "s/^[ \t]*$field:[ \t]*//p" <"$metafile" + ## Extract metadata FIELDS from INPUT. + # FILE gives the filename to save metadata to in the $WORKD. It + # defaults to the current value for $FILE. + # + # Metadata should exist as colon-separated data in an HTML comment at + # the beginning of an input file. + field="$1" + file="${2:-$FILE}" + metafile="${TMPD:=.}/${file}.meta" + test -f "$metafile" || + sed '//q' >"$metafile" + sed -n "s/^[ \t]*$field:[ \t]*//p" <"$metafile" } ## Customizable bits filters() { # filters < INPUT - ## The filters to run input through. - # This is a good candidate for customization in .vienna.sh. - expand | phtml + ## The filters to run input through. + # This is a good candidate for customization in .vienna.sh. + expand | phtml } ### Site building genpage() { # genpage PAGE... - ## Compile PAGE(s) into $OUTD for publication. - # Outputs a file of the format $OUTD//index.html. - test -f "$PAGE_TEMPLATE" || return 1 - for FILE; do - log genpage "$FILE" - outd="$OUTD/${FILE%.$PAGE_RAW_EXT}" - outf="$outd/index.html" - tmpf="$TMPD/$FILE.tmp" - mkdir -p "$outd" - filters <"$FILE" >"$tmpf" - expand "$PAGE_TEMPLATE" <"$tmpf" >"$outf" - done + ## Compile PAGE(s) into $OUTD for publication. + # Outputs a file of the format $OUTD//index.html. + test -f "$PAGE_TEMPLATE" || return 1 + for FILE; do + log genpage "$FILE" + outd="$OUTD/${FILE%.$PAGE_RAW_EXT}" + outf="$outd/index.html" + tmpf="$TMPD/$FILE.tmp" + mkdir -p "$outd" + filters <"$FILE" >"$tmpf" + expand "$PAGE_TEMPLATE" <"$tmpf" >"$outf" + done } genlist() { # genlist PERITEM_FUNC TEMPLATE_FILE PAGE... - peritem_func="$1" - template_file="$2" - shift 2 || return 2 - test -f "$template_file" || return 1 - for FILE; do - log genlist "$peritem_func/$template_file: $FILE" - LINK="$DOMAIN${DOMAIN:+/}${1%.PAGE_RAW_EXT}" - done | expand "$template_file" + peritem_func="$1" + template_file="$2" + shift 2 || return 2 + test -f "$template_file" || return 1 + for FILE; do + log genlist "$peritem_func: $template_file: $FILE" + + LINK="$DOMAIN${DOMAIN:+/}${1%.PAGE_RAW_EXT}" + done | expand "$template_file" } index_item() { # index_item PAGE - ## Construct a single item in an index.html. - echo "

  • $(meta title "$1")
  • " + ## Construct a single item in an index.html. + echo "
  • $(meta title "$1")
  • " } feed_item() { # feed_item PAGE - ## Construct a single item in an RSS feed. - date="$(pubdate "$1")" - echo "" - echo "$(meta title "$1")" - echo "$LINK" - echo "$LINK" - test -n "$date" && echo "$date" - echo "" + ## Construct a single item in an RSS feed. + date="$(pubdate "$1")" + echo "" + echo "$(meta title "$1")" + echo "$LINK" + echo "$LINK" + test -n "$date" && echo "$date" + echo "" } index() { # index PAGE... - ## Build a site index from all PAGE(s) passed to it. - # Wraps each PAGE in a
  • structure. - test -f "$INDEX_TEMPLATE" || return 1 - for FILE; do - log index "$FILE" - index_item "$FILE" - done | expand "$INDEX_TEMPLATE" >"$OUTD/index.html" + ## Build a site index from all PAGE(s) passed to it. + # Wraps each PAGE in a
  • structure. + test -f "$INDEX_TEMPLATE" || return 1 + for FILE; do + log index "$FILE" + index_item "$FILE" + done | expand "$INDEX_TEMPLATE" >"$OUTD/index.html" } feed() { # feed PAGE... - ## Build an RSS 2.0 feed from PAGE(s). - test -f "$FEED_TEMPLATE" || return 1 - for FILE; do - log feed "$FILE" - feed_item "$FILE" - done | expand "$FEED_TEMPLATE" >"$OUTD/feed.xml" + ## Build an RSS 2.0 feed from PAGE(s). + test -f "$FEED_TEMPLATE" || return 1 + for FILE; do + log feed "$FILE" + feed_item "$FILE" + done | expand "$FEED_TEMPLATE" >"$OUTD/feed.xml" } static() { # static FILE... - ## Copy static FILE(s) to $OUTD as-is. - # Performs a simple heuristic to determine whether to copy a file or - # not. - for FILE; do - case "$FILE" in - .*) continue ;; - *.htm) continue ;; - "$OUTD") continue ;; - *) cp -r "$FILE" "$OUTD/" ;; - esac - done + ## Copy static FILE(s) to $OUTD as-is. + # Performs a simple heuristic to determine whether to copy a file or + # not. + for FILE; do + case "$FILE" in + .*) continue ;; + *.htm) continue ;; + "$OUTD") continue ;; + *) cp -r "$FILE" "$OUTD/" ;; + esac + done } ### Do the thing! -- cgit 1.4.1-21-gabe81