diff options
Diffstat (limited to 'src/html')
-rwxr-xr-x | src/html | 307 |
1 files changed, 149 insertions, 158 deletions
@@ -1,174 +1,165 @@ #!/bin/sh -set -eu +set -euo pipefail + usage() { - echo 'Usage: html FILENAME' + echo 'Usage: html FILENAME.htmlbody' } FILENAME="${1:-}" -eval "$(assert-arg -- "$FILENAME" 'FILENAME')" - -. "${FILENAME%.*}.conf" - -# -# Utility functions -# - -INDENT=' ' -markdown_to_html() { - md2html | awk -vINDENT="$INDENT" ' - BEGIN { - in_block = 0 - } - - { - if (in_block == 0) { - printf "%s", INDENT - } - print - } - - /^<\/code><\/pre>$/ { - in_block = 0 - } - - /^<pre><code/ { - in_block = 1 - } - ' +eval "$(assert-arg -- "$FILENAME" 'FILENAME.htmlbody')" +. "$(realpath -- "${FILENAME%.*}.conf")" + + +dates() { + if [ -z "$date_formatted" ]; then + return + fi + + cat <<EOF + <section id="timestamps"> + <p id="published-at"> + Posted on <time datetime="$date_iso">$date_formatted</time> + </p> +EOF + + if [ -n "$updatedat_formatted" ]; then + cat <<EOF + <p id="updated-at"> + Updated on <time datetime="$updatedat_iso">"$updatedat_formatted</time>" + </p> +EOF + fi + + cat <<EOF + </section> +EOF } -extract_plaintext_snippets() ( - SNIPPETS="${FILENAME%.*}.snippets" - printf '' > "$SNIPPETS" - F="$(mkstemp)" - cat > "$F" - IFS='' - BLOCK_NUMBER=0 - IN_BLOCK= - while read -r line; do - if [ "$line" = '</code></pre>' ]; then - IN_BLOCK= - fi - - if [ -n "$IN_BLOCK" ]; then - printf '%s\n' "$line" | htmlesc -d >> "$OUT" - fi - - if printf '%s' "$line" | grep -q "^$INDENT<pre><code"; then - IN_BLOCK=1 - OUT="${FILENAME%.*}.html.$BLOCK_NUMBER.txt" - BLOCK_NUMBER=$((BLOCK_NUMBER + 1)) - printf '%s\n' "$line" | - sed "s|^\($INDENT<pre><code.*>\)\(.*\)$|\2|" | - htmlesc -d > "$OUT" - printf '%s\n' "$OUT" >> "$SNIPPETS" - fi - done < "$F" - - BLOCK_NUMBER=0 - while read -r line; do - printf '%s\n' "$line" - - if [ "$line" = '</code></pre>' ]; then - printf '%s<p class="plaintext-link"><a href="%s">plaintext</a></p>\n' \ - "$INDENT" \ - "$(basename "${url_part:?}").$BLOCK_NUMBER.txt" - BLOCK_NUMBER=$((BLOCK_NUMBER + 1)) - fi - done < "$F" -) - -add_line_numbers() { - awk ' - /^<\/code><\/pre>$/ { - in_block = 0 - printf "</tbody></table>%s\n", $0 - next - } - - match($0, /^( +<pre><code.*>)(.*)$/, a) { - printf "%s<table rules=columns class=\"code-block\"><tbody>", a[1] - - n = 1 - block_count++ - printf "<tr><td class=\"line-number\"><a id=\"B%s-L%s\" href=\"#B%s-L%s\">%s</a></td><td class=\"code-line\">%s</td></tr>\n", block_count, n, block_count, n, n, a[2] - in_block = 1 - next - } - - in_block == 1 { - n++ - printf "<tr><td class=\"line-number\"><a id=\"B%s-L%s\" href=\"#B%s-L%s\">%s</a></td><td class=\"code-line\">%s</td></tr>\n", block_count, n, block_count, n, n, $0 - next - } - - { print } - ' -} +h1() { + if [ "${custom_body:-}" = true ]; then + return + fi -add_headings_anchors() ( - IFS='' - while read -r line; do - if ! printf '%s' "$line" | grep -q "^$INDENT<h[2-6]>"; then - printf '%s\n' "$line" - continue - fi - LVL="$(printf '%s' "$line" | sed "s|^$INDENT<h\(.\)>.*|\1|")" - HEADING="$(printf '%s' "$line" | sed "s|^$INDENT<h.>\(.*\)</h.>$|\1|")" - SLUG="$(slugify "$HEADING")" - printf '%s<h%s class="header-anchor" id="%s">%s<a href="#%s" aria-hidden="true"><img class="svg-icon" src="%s" /></a></h%s>\n' \ - "$INDENT" \ - "$LVL" \ - "$SLUG" \ - "$HEADING" \ - "$SLUG" \ - "${icon_link_url:?}" \ - "$LVL" - done -) - - -warn_duplicate_ids() { - F="$(mkstemp)" - tee "$F" - { - grep "^$INDENT<h[2-6] class=\"header-anchor\" id=\"" | - sed "s|^$INDENT<h[2-6] class=\"header-anchor\" id=\"\(.*\)\">.*<a href=.*$|\1|" | - sort | - uniq -c | - awk -v F="$FILENAME" '$1 != 1 { - printf "WARNING: duplicate header id: %s: %s\n", F, $2 - }' - } >&2 < "$F" - rm "$F" + cat <<EOF + <h1> + $title_html + </h1> +EOF } -emit_body() { - < "${FILENAME%.*}.content" \ - markdown_to_html | - extract_plaintext_snippets | - add_line_numbers | - add_headings_anchors | - warn_duplicate_ids +comments() { + if [ "${custom_body:-}" = true ]; then + return + fi + + cat <<EOF + <section> + <hr /> + <footer> + <p id="comment"> + <a href="$comment_url">Comment</a> + about this page and see + <a href="$discussions_url">existing discussions</a> + | + <a href="$sourcecode_url_prefix/$source_path">view source</a> + </p> + </footer> + </section> +EOF } -if [ -r "$FILENAME".prev ]; then - collection_head_prev_html="$( - printf ' <link rel="prev" type="text/html" href="%s" />' \ - "$(url-for < "$FILENAME".prev)" - )" - export collection_head_prev_html -fi -if [ -r "$FILENAME".next ]; then - collection_head_next_html="$( - printf ' <link rel="next" type="text/html" href="%s" />' \ - "$(url-for < "$FILENAME".next)" - )" - export collection_head_next_html +head_meta_author_html= +if [ -n "${author:-}" ]; then + head_meta_author_html=" <meta name=\"author\" content=\"$author\" />" fi -envsubst < lib/preamble.html | sed '/^$/d' -emit_body | tee "${FILENAME%.*}.htmlbody" -envsubst < lib/postamble.html | sed '/^$/d' + +headlinks() { + if [ -z "${header_links:-}" ]; then + return + fi + + echo ' <ol>' + + while read -r line; do + link="$(printf '%s\n' "$line" | cut -d' ' -f1)" + name="$(printf '%s\n' "$line" | cut -d' ' -f2)" + cat <<EOF + <li> + <a href="$base_url_prefix/$link">$name</a> + </li> +EOF + done < "$header_links" + + echo ' </ol>' +} + + +collection_head_prev_html= +collection_head_post_html= +cat <<EOF | sed '/^$/d' +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="UTF-8" /> + <meta name="viewport" content="width=device-width, initial-scale=1" /> +$head_meta_author_html + <link rel="stylesheet" type="text/css" href="$css_url" /> + <link rel="shortcut icon" type="image/svg+xml" href="$favicon_url" /> + <link rel="canonical" type="text/html" href="$url_absolute" /> + <link rel="alternate" type="application/atom+xml" href="$atom_url" title="$feed_title_html" /> +$collection_head_prev_html +$collection_head_post_html + <title>$titlefull_html</title> + </head> + <body> + <header> + <nav> + <a href="$base_url_prefix/"> + <picture> + <source srcset="$logo_url_prefix/dark.svg" media="(prefers-color-scheme: dark)" /> + <img src="$logo_url_prefix/light.svg" alt="$logo_alt" /> + </picture> + </a> +$(headlinks) + </nav> + <hr /> + </header> + <main> + <article> +EOF + +h1 +dates + +cat "$FILENAME" + +comments + +cat <<EOF + </article> + </main> + <footer> + <hr /> + <ul> + <li> + <picture> + <source srcset="$envelopeicon_url_prefix/dark.svg" media="(prefers-color-scheme: dark)" /> + <img src="$envelopeicon_url_prefix/light.svg" class="icon" alt="An outlined icon of an envelope" /> + </picture> + <a href="mailto:$email">$email</a> + </li> + </ul> + <p id="license"> + The content for this site is licensed under + <a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a>. + The <a href="$sourcecode_url/">code</a> is + <a rel="license" href="$sourcecode_url_prefix/COPYING">AGPLv3 or later</a>. + Patches welcome. + </p> + </footer> + </body> +</html> +EOF |