#!/bin/sh set -e usage="usage: ./md.sh ./md.sh " # input: /src.md ./md-header ./md-footer /nav.html # output: /index.html # dependencies: lowdown, fzy, ./urlencode # Copyright 2019-2021 DistressNetwork° # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . IN="$1/src.md" OUT="$1/index.html" [ "$2" ] && { IN="$1" ; OUT="$2" ; } [ "$1" ] || { echo "$usage" ; exit 1 ; } [ -f $IN ] || { printf "%s \tFATAL: %s not found\n" "[->]" "$IN"; exit 1 ; } [ -f md-header ] || { printf "%s \tFATAL: md-header not found\n" "[->]" ; exit 1 ; } [ -f md-footer ] || { printf "%s \tFATAL: md-footer not found\n" "[->]" ; exit 1 ; } [ $(which lowdown) ] || { printf "%s \tFATAL: lowdown not found\n" "[->]" ; exit 1 ; } cat md-header > $OUT lowdown -Thtml $IN >> $OUT cat md-footer >> $OUT printf "[ok] \tinitial assembly\n" sedesc() { sed -e 's;&;\\\&;g' -e 's/;/\\\;/g' } unesc() { sed -e 's;\\\&;\&;g' -e 's/\\\;/;/g' } encode() { ./urlencode } decode() { /bin/printf '%b' "$(echo "$1" | sed 's;%;\\x;g')" } TITLE=$(grep -e '^title: ' -m 1 $IN | cut -f 2- -d ' ' | sedesc) sed "s//&${TITLE} \&mdash\; DistressNetwork°/1" -i'' $OUT printf "[ok] \ttitle: %s\n" "$(echo "$TITLE" | unesc)" BOMBER=$(grep -e '^bomber: ' -m 1 $IN | cut -f 2- -d ' ' | sedesc) sed "s/<div class=\"bomber\">/&${BOMBER}/1" -i'' $OUT printf "[ok] \tbomber: %s\n" "$(echo "$BOMBER" | unesc)" MDATE=$(stat -c '%y' $IN | sed -e 's/\.[0-9]* //' -e 's/\(..\)\(..\)$/\1:\2/') # format: "yyyy-mm-dd hh:mm:ss±zz:zz" (ISO 8601) DATE=$(echo $MDATE | cut -f 1 -d ' ' | sed -e 's/-//g' -e 's/^..//') # format: "yymmdd" (for humans) sed -e "s;<div class=\"ident\"><time datetime=\";&${MDATE};1" -e "s;</time>;${DATE}&;1" -i'' $OUT sed "s;<meta http-equiv=\"last-modified\" content=\";&${MDATE};1" -i'' $OUT printf "[ok] \tdate: %s (%s)\n" "$DATE" "$MDATE" LEADING=$(grep -e '^leading: ' -m 1 $IN | cut -f 2- -d ' ' | sedesc) sed "s;<div class=\"leading\">;&${LEADING};1" -i'' $OUT printf "[ok] \tleading: %s\n" "$(echo "$LEADING" | unesc)" METADESC="\«\;${BOMBER}\»\; \&mdash\; ${LEADING}" sed "s;<meta name=\"description\" content=\";&${METADESC};1" -i'' $OUT printf "[ok] \tmeta description\n" if [ -f $(echo $IN | cut -f 1 -d /)/nav.html ] ; then NAVLINE=$(grep -n '<nav>' $OUT | cut -f 1 -d :) ; sed "${NAVLINE}r $(echo $OUT | cut -f 1 -d /)/nav.html" -i'' $OUT ; sed '/^<!--title:/d' -i'' $OUT ; printf "[ok] \tsitemap\n" ; fi [ "$(grep -n '<p><img' $OUT)" ] && { grep -n '<p><img' $OUT | \ while IFS='' read -r data ; do LINE=$(echo $data | cut -f 1 -d :) ; # extract line number FILE=$(echo $data | cut -f 2 -d \") ; # extract file path CAPT=$(echo $data | cut -f 4 -d \") ; # extract image caption if [ "$CAPT" = "#tex" ] ; then # for tex figures: only remove caption and containers sed "${LINE}s;.*;<figure><img src=\"${FILE}\" alt=\"\"></figure>;" -i'' $OUT ; printf "[ok] \ttex figure at line %s\n" "$LINE" ; else sed "${LINE}s;.*;<figure><a href=\"${FILE}\"><img src=\"${FILE}\" alt=\"\"></a><figcaption><p>${CAPT}</p></figcaption></figure>;" -i'' $OUT ; printf "[ok] \timage at line %s\n" "$LINE" ; fi done } toc() { [ $(which fzy) ] || { printf "[warn] \tfzy not found, skipping toc\n" ; return ; } [ -f ./urlencode ] || { printf "[warn] \turlencode not found, skipping toc\n" ; return ; } tmp=$(mktemp -p /tmp) # using a tempfile, more convenient for storage and operations TOCLIST=$(grep -e '^#' $IN | sed -e 's; ;- ;' -e 's;^#;;' -e 's;#; ;g') # headers from source as md list echo "$TOCLIST" | while IFS='' read -r data ; do # encode headers as urls HEADER=$(echo "$data" | sed 's/^[[:space:]-]*//' | encode) ; echo "$data" | sed "s/^\([[:space:]-]*\)\(.*\)$/\1${HEADER}/" >> $tmp ; done buffer=$(cat $tmp) ; echo "$buffer" | lowdown -Thtml > $tmp # convert to html, write to tempfile (reset) sed -e '1i <nav class="toc">' -e '/^$/d' -i'' $tmp # prepend starting tag, remove empty lines cat $tmp | while IFS='' read -r data ; do # get header (both encoded and raw), write as link ID=$(echo $data | sed "s;^<li>\([^</>]*\)\(</li>\)*$;\1;") ; HEADER=$(decode "$ID" | sedesc) ; sed -e "s;^<li>\(${ID}\);<li><a href=\"#\1\">${HEADER};" -e "s;\([[:alnum:]]\)</li>$;\1</a></li>;" -i'' $tmp ; # find old id in output, replace with new id IDLINE=$(grep -ne "^<h[[:digit:]] id=" $OUT | fzy -e "$(echo $HEADER | unesc)" | head -n 1 | cut -f 1 -d :) ; if [ "$HEADER" = "<ul>" ] ; then IDLINE="" ; fi ; if [ "$HEADER" = "</ul></li>" ] ; then IDLINE="" ; fi ; if [ "$HEADER" = "</ul>" ] ; then IDLINE="" ; fi ; if [ "$IDLINE" ] ; then sed "${IDLINE}s;id=\".*\">.*</h;id=\"${ID}\">${HEADER}</h;" -i'' $OUT ; printf "\tassembled header: %s\n" "$(echo $HEADER | unesc)" ; fi ; done # close link tags on open lines, append closing tag sed "s;[[:alnum:]]$;&</a>;" -i'' $tmp echo '</nav>' >> $tmp # get toc line number in output, insert data from tempfile TOCLINE=$(wc -l md-header | cut -f 1 -d ' ') sed "${TOCLINE}r $tmp" -i'' $OUT rm $tmp # cleanup printf "[ok] \ttable of contents\n" } opts=$(grep -e '^opts: ' -m 1 $IN | cut -f 2- -d ' ') while [ "$opts" ]; do case "$opts" in (*[Cc]*) toc opts=$(echo $opts | sed 's/[Cc]//g') ;; (*[Ff]*) sed "/^<footer>/,/^<\/footer>/d" -i'' $OUT printf "[ok] \tfooterless\n" opts=$(echo $opts | sed 's/[Ff]//g') ;; (*[Hh]*) sed -e "/^<header>/,/^<\/header>/d" -e '/^<meta name="description"/d' -i'' $OUT sed "/bg.js/d" -i'' $OUT printf "[ok] \theaderless\n" opts=$(echo $opts | sed 's/[Hh]//g') ;; (*) printf "[warn] \tmetadata: unrecognized option(s) \"%s\"\n" "${opts}" ;; esac done printf "%s \tbuild complete: %s\n" "[->]" "$OUT"