From c149b2944f0759ea4daa1bf12539ee489bd420b4 Mon Sep 17 00:00:00 2001 From: eudoxia Date: Sat, 5 Dec 2020 21:35:46 -0500 Subject: several reliability improvements, other slight refactors --- md.sh | 65 +++++++++++++++++++++++++++------------------------------------- plain.sh | 13 ++++--------- 2 files changed, 31 insertions(+), 47 deletions(-) diff --git a/md.sh b/md.sh index b8d48c1..21aed6f 100755 --- a/md.sh +++ b/md.sh @@ -25,22 +25,21 @@ set -e # along with this program. If not, see . -# i/o variables - IN="$1/src.md" OUT="$1/index.html" -if [ "$2" ] ; then - IN="$1" ; - OUT="$2" ; -fi - -# confirm source exists before continuing - -[ -f $IN ] || { echo "---- [FATAL] $IN not found ----" ; exit 1 ; } +[ "$2" ] && { IN="$1" ; OUT="$2" ; } +[ "$1" ] || { +echo "usage: + ./md.sh + ./md.sh " ; exit 1 ; +} -### initial write +[ -f $IN ] || { echo "---- [FATAL] $IN not found ----" ; exit 1 ; } +[ -f md-header ] || { echo "---- [FATAL] md-header not found ----" ; exit 1 ; } +[ -f md-footer ] || { echo "---- [FATAL] md-footer not found ----" ; exit 1 ; } +[ $(which lowdown) ] || { echo "---- [FATAL] lowdown not found ----" ; exit 1 ; } cat md-header > $OUT lowdown -Thtml $IN >> $OUT @@ -48,8 +47,6 @@ cat md-footer >> $OUT echo '[ok] initial assembly' -### useful functions - sedesc() { sed -e 's;&;\\\&;g' -e 's/;/\\\;/g' } @@ -58,24 +55,22 @@ unesc() { sed -e 's;\\\&;\&;g' -e 's/\\\;/;/g' } -encode() ( +encode() { ./urlencode -) +} decode() { /bin/printf '%b' "$(echo "$1" | sed 's;%;\\x;g')" } -### header variables extracted from metadata section - TITLE=$(grep -e '^title: ' -m 1 $IN | cut -f 2- -d ' ' | sedesc) sed "s/#/<title>${TITLE} \&mdash\; DistressNetwork°/1" -i'' $OUT -printf '[ok] title: %s\n' "$(echo $TITLE | unesc)" +printf '[ok] title: %s\n' "$(echo "$TITLE" | unesc)" BOMBER=$(grep -e '^bomber: ' -m 1 $IN | cut -f 2- -d ' ' | sedesc) sed "s/<div class=\"bomber\">#/<div class=\"bomber\">${BOMBER}/1" -i'' $OUT -printf '[ok] bomber: %s\n' "$(echo $BOMBER | unesc)" +printf '[ok] bomber: %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) @@ -85,14 +80,14 @@ printf '[ok] date: %s (%s)\n' "$DATE" "$MDATE" LEADING=$(grep -e '^leading: ' -m 1 $IN | cut -f 2- -d ' ' | sedesc) sed "s;<div class=\"leading\">#;<div class=\"leading\">${LEADING};1" -i'' $OUT -printf '[ok] leading: "%s"\n' "$(echo $LEADING | unesc)" +printf '[ok] leading: %s\n' "$(echo "$LEADING" | unesc)" METADESC="\«\;${BOMBER}\»\; \&mdash\; ${LEADING}" sed "s;<meta name=\"description\" content=\"#;<meta name=\"description\" content=\"${METADESC};1" -i'' $OUT printf '[ok] meta description\n' -### image handling +[ "$(grep -n '<p><img' $OUT)" ] && { grep -n '<p><img' $OUT | \ while IFS='' read -r data ; do @@ -100,19 +95,22 @@ while IFS='' read -r data ; do 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: remove caption and containers + 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] tex figure at line %s\n' "$LINE" ; - else # use new image format + else sed "${LINE}s;.*;<figure><a href=\"${FILE}\"><img src=\"${FILE}\" alt=\"\"></a><figcaption><p>${CAPT}</p></figcaption></figure>;" -i'' $OUT ; printf '[ok] image at line %s\n' "$LINE" ; fi done +} + -### table of contents generation +toc() { -tocgen() ( +[ "$(which fzf)" ] || { echo "[warning] fzf not found, skipping toc" ; return ; } +[ -f ./urlencode ] || { echo "[warning] urlencode not found, skipping toc" ; return ; } tmp=$(mktemp -p /tmp) # using a tempfile, more convenient for storage and operations @@ -128,7 +126,6 @@ buffer=$(cat $tmp) ; echo "$buffer" | lowdown -Thtml > $tmp # convert to html, w 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) ; @@ -138,12 +135,7 @@ cat $tmp | while IFS='' read -r data ; do IDLINE=$(grep -ne "^<h[[:digit:]] id=" $OUT | fzf -f "$(echo $HEADER | unesc)" | head -n 1 | cut -f 1 -d :) ; if [ "$IDLINE" ] ; then sed -e "${IDLINE}s;id=\".*\">.*</h;id=\"${ID}\">${HEADER}</h;" -i'' $OUT ; - printf ' assembled header "%s"\n' "$(echo $HEADER | unesc)" ; - # else - # printf '[warning] could not match header id: "%s"\n' "$HEADER" ; - # echo "---- [FATAL] could not find header id (possible escapement issue) ----" ; - # rm $tmp ; - # exit 1 ; + printf ' assembled header: %s\n' "$(echo $HEADER | unesc)" ; fi ; done @@ -161,17 +153,14 @@ rm $tmp # cleanup echo '[ok] table of contents' -) - +} -### metadata option handling opts=$(grep -e '^opts: ' -m 1 $IN | cut -f 2- -d ' ') - while [ "$opts" ]; do case "$opts" in (*[Cc]*) - tocgen + toc opts=$(echo $opts | sed 's/[Cc]//g') ;; @@ -189,7 +178,7 @@ case "$opts" in ;; (*) - echo "[warning] metadata: unrecognized option(s)" + echo "[warning] metadata: unrecognized option(s) \"${opts}\"" ;; esac done diff --git a/plain.sh b/plain.sh index 530c592..6208da7 100755 --- a/plain.sh +++ b/plain.sh @@ -18,15 +18,15 @@ set -e # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -# i/o variables IN="$1" OUT="$2" -[ -f $IN ] || { echo "---- [FATAL] $IN not found ----" ; exit 1 ; } +[ "$1" ] || { echo "usage: ./plain.sh <input txt> <output html>" ; exit 1 ; } +[ -f $IN ] || { echo "---- [FATAL] $IN not found ----" ; exit 1 ; } +[ -f plain-header ] || { echo "---- [FATAL] plain-header not found ----" ; exit 1 ; } -# initial write cat plain-header > $2 cat $1 >> $2 @@ -34,15 +34,10 @@ printf "</pre>\n</body>\n</html>" >> $2 echo '[ok] initial assembly' # hack to fix double <pre> when generating from nav.sh - sed -e "/^<pre>$/ {n;/^<pre>$/d}" -e "/^<\/pre>$/ {n;/^<\/pre>$/d}" -i'' $2 -# metadata - -sedesc() { - sed -e 's;\&;\\\&;' -e 's/;/\\\;/' -e 's;-->;;' -} +sedesc() {sed -e 's;\&;\\\&;' -e 's/;/\\\;/' -e 's;-->;;'} TITLE=$(grep -e '^<!--title: ' -m 1 $1 | cut -f 2- -d ' ' | sedesc) sed -e "/^<!--title:/d" -e "s;<title>#;<title>${TITLE};1" -i'' $2 -- cgit v1.2.3