From 72f3a9c0029923443e1587e1e6df7989d28860db Mon Sep 17 00:00:00 2001 From: eudoxia Date: Tue, 18 Jan 2022 02:43:02 -0500 Subject: md.sh: refactored table of contents generation (removed fzy and urlencode dependencies) --- md.sh | 67 +++++++++++++++++++++++------------------------------------- urlencode | Bin 14432 -> 16824 bytes urlencode.c | 2 +- 3 files changed, 26 insertions(+), 43 deletions(-) diff --git a/md.sh b/md.sh index a102e42..fbc5fec 100755 --- a/md.sh +++ b/md.sh @@ -7,7 +7,7 @@ usage="usage: # input: /src.md ./md-header ./md-footer /nav.html # output: /index.html -# dependencies: lowdown, fzy, ./urlencode +# dependencies: lowdown # Copyright 2019-2021 DistressNetwork° # @@ -32,10 +32,10 @@ OUT="$1/index.html" [ "$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 ; } +[ -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 ; } +[ -e $(which lowdown) ] || { printf "%s \tFATAL: lowdown not found\n" "[->]" ; exit 1 ; } cat md-header > $OUT lowdown -Thtml --html-no-owasp $IN >> $OUT @@ -51,14 +51,6 @@ 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 @@ -111,18 +103,10 @@ done toc() { -[ $(which fzy) ] || { printf "[warn] \tfzy not found, skipping toc\n" ; return ; } -[ -f ./urlencode ] || { printf "[warn] \turlencode not found, skipping toc\n" ; return ; } - -# tempfiles are more convenient for storage and operations tmp=$(mktemp -p /tmp) -# headers from source as md list -TOCLIST=$(grep -e '^#' $IN | sed -e 's; ;- ;' -e 's;^#;;' -e 's;#; ;g') -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 +# format headers from source as md list +grep -e '^#' $IN | sed -e 's; ;- ;' -e 's;^#;;' -e 's;#; ;g' > $tmp # convert to html, write to tempfile (reset) buffer=$(cat $tmp) ; echo "$buffer" | lowdown -Thtml > $tmp @@ -130,24 +114,23 @@ buffer=$(cat $tmp) ; echo "$buffer" | lowdown -Thtml > $tmp # prepend starting tag, remove empty lines sed -e '1i <nav class="toc">' -e '/^$/d' -i'' $tmp -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 :) ; - case "$HEADER" in # filter out bad tag matches - ("<nav class=\"toc\">") ;; - ("<ul>") ;; - ("</ul></li>") ;; - ("</ul>") ;; - ("</nav>") ;; - (*) - sed "${IDLINE}s;id=\".*\">.*</h;id=\"${ID}\">${HEADER}</h;" -i'' $OUT ; - printf "\tassembled header: %s\n" "$(echo $HEADER | unesc)" ;; - esac ; +# get queue of headers from output +HEADERS=$(grep -e "^<h[[:digit:]] id=" $OUT) + +# for each toc list item: +cat $tmp | grep -e "^<li>" | while IFS='' read -r data ; do + # get next header in queue, extract id + HEADER=$(echo "$HEADERS" | head -n 1) ; + ID=$(echo "$HEADER" | cut -f 2 -d \" | sedesc) ; + + # rewrite toc item as link with id + ITEM=$(echo "$data" | sed "s;^<li>\([^</>]*\)\(</li>\)*$;\1;" | sedesc) ; + sed -e "s;^<li>\(${ITEM}\);<li><a href=\"#${ID}\">\1;" -e "s;\([[:alnum:]]\)</li>$;\1</a></li>;" -i'' $tmp ; + + # remove processed header from queue + HEADERS=$(echo "$HEADERS" | sed '1d') ; + + printf "\tassembled header: %s\n" "$(echo $ITEM | unesc)" ; done # close link tags on open lines, append closing tag @@ -158,7 +141,7 @@ echo '</nav>' >> $tmp TOCLINE=$(wc -l md-header | cut -f 1 -d ' ') sed "${TOCLINE}r $tmp" -i'' $OUT -rm $tmp # cleanup +rm $tmp printf "[ok] \ttable of contents\n" diff --git a/urlencode b/urlencode index 517813d..e0a1e0c 100755 Binary files a/urlencode and b/urlencode differ diff --git a/urlencode.c b/urlencode.c index 693b98c..0255454 100755 --- a/urlencode.c +++ b/urlencode.c @@ -18,7 +18,7 @@ #include <string.h> int main() { - char str[2047]; + char str[2048]; char safe[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-._~"; char *ret; int i; -- cgit v1.2.3