#Site build script

#vim: set colorcolumn=81 ft=sh:hi ColorColumn ctermbg=224
# [x] means x is optional

txt2html() {
  # cut off the modeline and title
  tail -n +3 |

  # convert lines starting with `##` to comments
  sed 's|^\(##.*\)$|<span id=d>\1</span>|' |
  # convert <,> to HTML-compatible format
  sed 's|<|\<|g' |
  sed 's|>|\>|g' |
  # convert ` ` italics to html
  sed 's|\|<em>|g' |
  sed 's|}\*|</em>|g' |
  # convert ` ``[`] [`]`` ` codeblocks into css-ified codeblocks
  sed 's|``[`]{|<pre id=c>|' |
  sed 's|}[`]``|</pre>|' |
  # convert ` `[{] [}]` inline codeblocks into css-ified codeblocks
  sed 's|`[{]|<span id=c>|g' |
  sed 's|[}]`|</span>|g' |
  # See next comment. $/url -> /url.html
  sed 's|\([$]/\)\([^ \)]*\)[.]txt\([,.]\)|<a href=/\2.html>\1\2</a>\3|g' |
  sed 's|\([$]/\)\([^ \)<>]*\)[.]txt|<a href=/\2.html>\1\2</a>|g' |
  # convert `url` to html links relative to root domain
  #sed 's|\([$]/\)\([^ \)]*\)\([,.]\)|<a href=/\2>\1\2</a>\3|g' |
  sed 's!^\([$]/\)\([^ \)<>]*\)!<a href=/\2>\1\2</a>!g;s!\([^>]\)\([$]/\)\([^ \)<>]*\)!\1<a href=/\3>\3</a>!g' |
  # See next comment. %/url -> url.html
  sed 's|\(\)\([^ \)]*\)[.]txt\([,.]\)|<a href=\2.html>\1\2</a>\3|g' |
  sed 's|\(\)\([^ \)<>]*\)[.]txt|<a href=\2.html>\1\2</a>|g' |
  # convert `url` to html links relative to current directory
  sed 's!^\(\)\([^ \)<>]*\)!<a href=\2>\1\2</a>!g;s!\([^>]\)\(\)\([^ \)<>]*\)!\1<a href=\3>\3</a>!g' |
  # convert `url` to html links
  #sed 's|\([#]/\)\([^ )]*\)\([,.)]\)|<a href=//\2>\1\2</a>\3|g' |
  sed 's|\([#]/\)\([^ )<>]*\)|<a href=//\2>\2</a>|g' |

  # convert `-[0-9]` into IDs
  sed 's|^\([ -]*\)\[\([0-9.][0-9.]*\)\]|\1<span id=\2>[\2]</span>|g' |
  # convert `[0-9]` into references to IDs
  sed 's|\([^\"#]\)\[\([0-9.][0-9.]*\)\]|\1[<a href=#\2>\2</a>]|g' |

  # set content
  sed '/%%CONTENT%r /dev/stdin' html_template |
  sed '/%%CONTENT%d' |
  # set title
  sed "s|#Site build script|${1:-Index}|" |
  # Substitute shell blocks of the form:
  # %%SHELL:
  # command(s)
  # %%
  awk '
  BEGIN {shell_mode=0; shell_cmd=""}
  {
    if (shell_mode) {
      if ($0 == "%%") {
        # Execute the collected shell command and print its output
        while (( "bash -c \"" shell_cmd "\"") | getline line ) {
          print line
        }
        close("bash -c \"" shell_cmd "\"")
        shell_mode=0
        shell_cmd=""
      } else {
        shell_cmd = shell_cmd $0 "\n"
      }
    } else if ($0 ~ /^%%SHELL:) {
      shell_mode=1
      shell_cmd=""
    } else {
      print $0
    }
  }
  '
}

{
  # process arguments if available, else all *.txt
  [ $# -eq 0 ] && find . -name '*.txt' | grep -ve 'robots.txt$' #| tee /dev/stderr
  while [ ! $# -eq 0 ]; do
    case "$1" in
      sitemap*)
        shift
        break
        ;;
    esac
    printf '%s\n' "$1"
    shift
  done
} | while read -r PAGE; do
  title=$(sed '2p;d' "$PAGE")
  printf 'txt2html %s\n' "$title"
  txt2html "$title" < "$PAGE" > "${PAGE%%.txt}.html"
  #gzip -9kf "${PAGE%%.txt}.html"
done
printf 'txt2html Sitemap\n'
find . -name '*.html' | cut -d/ -f2- | grep -v ^sitemap.html\$ | while read -r PAGE; do
  printf '<a href=/%s>%s</a>\n' "$PAGE" "$(sed '2p;d' "${PAGE%%.html}.txt")"
done |
  sed '/%%CONTENT%r /dev/stdin' html_template |
  sed '/%%CONTENT%d' |
  sed 's|#Site build script|Sitemap|' |
  awk '
  BEGIN {shell_mode=0; shell_cmd=""}
  {
    if (shell_mode) {
      if ($0 == "%%") {
        # Execute the collected shell command and print its output
        while (( "bash -c \"" shell_cmd "\"") | getline line ) {
          print line
        }
        close("bash -c \"" shell_cmd "\"")
        shell_mode=0
        shell_cmd=""
      } else {
        shell_cmd = shell_cmd $0 "\n"
      }
    } else if ($0 ~ /^%%SHELL:) {
      shell_mode=1
      shell_cmd=""
    } else {
      print $0
    }
  }
  ' > sitemap.html