diff options
-rwxr-xr-x | md.sh | 79 | ||||
-rwxr-xr-x | svgtex.sh | 51 |
2 files changed, 70 insertions, 60 deletions
@@ -38,7 +38,7 @@ OUT="$1/index.html" [ $(which lowdown) ] || { printf "%s \tFATAL: lowdown not found\n" "[->]" ; exit 1 ; } cat md-header > $OUT -lowdown -Thtml $IN >> $OUT +lowdown -Thtml --html-no-owasp $IN >> $OUT cat md-footer >> $OUT printf "[ok] \tinitial assembly\n" @@ -89,23 +89,21 @@ sed '/^<!--title:/d' -i'' $OUT ; printf "[ok] \tsitemap\n" ; fi -[ "$(grep -n '<p><img' $OUT)" ] && { - -grep -n '<p><img' $OUT | \ +[ "$(grep -n '<p><img' $OUT)" ] && { # skip if no figures found +# escaping backslashes with '@', to be unescaped later: +grep -n '<p><img' $OUT | sed 's/\\/@/g' | \ 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" ; + if [ "$FILE" = "%tex" ] ; then # for tex figures (to be caught and rendered later) + sed "${LINE}s;.*;<figure><%tex \"${CAPT}\" /></figure>;" -i'' $OUT ; 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 - } @@ -143,7 +141,7 @@ cat $tmp | while IFS='' read -r data ; do fi ; if [ "$HEADER" = "</ul>" ] ; then IDLINE="" ; - fi ; + fi ; # TODO use case expr instead if [ "$IDLINE" ] ; then sed "${IDLINE}s;id=\".*\">.*</h;id=\"${ID}\">${HEADER}</h;" -i'' $OUT ; printf "\tassembled header: %s\n" "$(echo $HEADER | unesc)" ; @@ -167,6 +165,64 @@ printf "[ok] \ttable of contents\n" } +math() { + +# [ $(npx katex -V 2>&1 | grep '404 Not Found') ] && { printf "[warn] \tkatex not found, skipping math rendering\n" ; return ; } + +sed '/main.css/a <link rel="stylesheet" href="/css/katex.css">' -i'' $OUT + +# display / "block" style: +grep -n '<figure><%tex' $OUT | \ +while IFS='' read -r data ; do + LINE=$(echo "$data" | cut -f 1 -d :) ; + EQNBODY=$(echo "$data" | cut -f 2 -d \" ) ; + printf "\trendering tex figure at line %s ... " "$LINE" ; + + tmp=$(mktemp -p /tmp) ; + echo "<figure>" > $tmp ; + echo "$EQNBODY" | sed 's/@/\\/g' | npx katex -d 2>&1 >> $tmp ; + echo "</figure>" >> $tmp ; + sed ':a;N;$!ba;s/\n//g' -i'' $tmp ; + + if [ $(grep "KaTeX parse error" $tmp) ] ; then + printf "\n[warn] \ttex expression at line %s contained errors, skipping\n" "$LINE" ; + else + sed "${LINE}d" -i'' $OUT ; + sed "$(expr $LINE - 1)r $tmp" -i'' $OUT ; + printf "complete\n" ; + fi ; + + rm $tmp ; +done + +# inline / "span" style: +sed 's;<img src="%itex" alt=".*" />;\n&\n;g' -i'' $OUT +grep -n '<img src="%itex"' $OUT | sed 's/\\/@/g' | \ +while IFS='' read -r data ; do + LINE=$(echo "$data" | cut -f 1 -d :) ; + EQNBODY=$(echo "$data" | cut -f 4 -d \" ) ; + printf "\trendering inline tex figure at line %s ... " "$LINE" ; + + tmp=$(mktemp -p /tmp) ; + echo "$EQNBODY" | sed 's/@/\\/g' | npx katex 2>&1 > $tmp ; + sed ':a;N;$!ba;s/\n//g' -i'' $tmp ; + + if [ $(grep "KaTeX parse error" $tmp) ] ; then + printf "\n[warn] \tinline tex expression at line %s contained errors, skipping\n" "$LINE" ; + else + sed "${LINE}d" -i'' $OUT ; + sed "$(expr $LINE - 1)r $tmp" -i'' $OUT ; + printf "complete\n" ; + fi ; + + rm $tmp ; +done + +printf "[ok] \tmath typesetting\n" + +} + + opts=$(grep -e '^opts: ' -m 1 $IN | cut -f 2- -d ' ') while [ "$opts" ]; do case "$opts" in @@ -188,6 +244,11 @@ case "$opts" in opts=$(echo $opts | sed 's/[Hh]//g') ;; + (*[Mm]*) + math + opts=$(echo $opts | sed 's/[Mm]//g') + ;; + (*) printf "[warn] \tmetadata: unrecognized option(s) \"%s\"\n" "${opts}" ;; diff --git a/svgtex.sh b/svgtex.sh deleted file mode 100755 index 5098954..0000000 --- a/svgtex.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/sh -set -e - -# usage: echo "<equation>" | ./svgtex.sh <output path> - -# Copyright 2019-2021 DistressNetwork° <uplink@distress.network> -# -# 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 <http://www.gnu.org/licenses/>. - -tmp=$(mktemp -p /tmp) -eqn=$(cat /dev/stdin) - -printf "[ok] \teqn: %s\n" "$eqn" - -echo "\\\documentclass[10pt,fleqn]{standalone} -\\\usepackage{standalone,amsmath,amssymb,xcolor} -\\\begin{document} -\\\color[HTML]{DDDDDD}" >> $tmp - -printf "\\\[ %s \\\]\n" "$eqn" >> $tmp - -echo "\\\end{document}" >> $tmp - -# now this is where the fun begins - -latex -interaction=batchmode $tmp > /dev/null - -[ -f tmp.dvi ] || { printf "%s \tFATAL: tex output failed\n" "[->]" ; exit 1 ; } # make sure output exists before continuing - -printf "[ok] \ttex finished typesetting\n" - -dvisvgm tmp.dvi -f ttf -T S2 > /dev/null 2>&1 - -printf "[ok] \tconversion to svg\n" - -mv tmp.svg $1 # final move to desired path - -rm tmp.aux tmp.log tmp.dvi $tmp # cleanup - -printf "%s \tbuild complete: %s\n" "[->]" "$1" |