From fe99acd428db9040f36a534f881ad2ad35f5cac0 Mon Sep 17 00:00:00 2001
From: tavo-wasd <gustavo@gustavocalvo.xyz>
Date: Thu, 28 Sep 2023 14:57:41 -0600
Subject: [PATCH] pimp and optimize groff script

---
 scripts/groff-compiler | 60 +++++++++++++++++++++++++++++++++---------
 scripts/groff-img      | 20 --------------
 shell/envvar           |  2 +-
 3 files changed, 48 insertions(+), 34 deletions(-)
 delete mode 100755 scripts/groff-img

diff --git a/scripts/groff-compiler b/scripts/groff-compiler
index 3f58e51..89cc676 100755
--- a/scripts/groff-compiler
+++ b/scripts/groff-compiler
@@ -1,23 +1,57 @@
 #!/bin/sh
-#
 # Compiler script for groff
 #
 # [-k (preconv)] Convert encoding to something groff understands
 # [-e (eqn)]     Format equations for troff or MathML
 # [-t (tbl)]     Format tables for troff
 # [-p (pic)]     Compile pictures for troff or TeX
-# [-G (grap)]    Typesetting graphs (grap usually not installed by default)
-# [-j (chem)]    Chemical structure diagrams (messes up spacing!)
+# [-G (grap)]    Typesetting graphs, grap usually not installed by default
+# [-j (chem)]    Chemical structure diagrams
 
 # Configuration
-REFS="$HOME/Documents/bibliography" # Bibliography file
-MAC="$HOME/.config/groff/"          # Macros dir
-GROMACS="-mspdf"                    # groff macros
-PRE="-ketp"                         # Preprocessors
-OUT="pdf"                           # Output format
+BIB="$HOME/Documents/bibliography" # Bibliography file
+MAC="$HOME/.config/groff/"         # Macros dir
+PRE="-ketpG"                       # Preprocessors
+
+# NOTE:
+#
+# I have had some truble using chem, keep in mind:
+# - Messes with spacing
+# - Crashes when writing text inside eqn, like:
+# .EQ
+# roman {"Text"}
+# .EN
+#
+# Also, grap doesn't usually come pre-installed
+# and I believe it is not packaged for termux.
+
+# Takes a line with .IMG command, converts given image
+# to pdf format and inserts properly formatted .PDFPIC
+auto_pdfpic() {
+    LINE="$1"
+    IMAGE="$(echo "$LINE" | cut -d '"' -f 2)"
+    [ "$IMAGE" != "${IMAGE%%.*}.pdf" ] && ! [ -e "${IMAGE%%.*}.pdf" ] &&
+        convert -quiet "$IMAGE" "${IMAGE%%.*}.pdf"
+    echo "$LINE" | sed 's|^.IMG|.PDFPIC|' | sed "s|$IMAGE|${IMAGE%%.*}.pdf|"
+}
+
+# If there is any .IMG command, replace with
+# auto_pdfpic output in a temp file.
+FILE="$1"
+if [ "$(grep -q '^.IMG\s' "$FILE")" ] ; then
+    TEMP="${FILE%%.*}.tmp"
+    cp "$FILE" "$TEMP"
+    while true ; do
+        LINE="$(grep -m 1 '^.IMG\s' "$TEMP")"
+        [ -n "$LINE" ] &&
+            NEW_LINE="$(auto_pdfpic "$LINE")" &&
+            sed -i "s@$LINE@$NEW_LINE@g" "$TEMP" ||
+            break
+    done
+    FILE="$TEMP"
+fi
+
+# Enable custom macros   | Bibliography    | Generate PDF using preprocessors
+soelim -I "$MAC" "$FILE" | refer -p "$BIB" | groff -mspdf -T pdf -U "$PRE"
+rm -f "$TEMP"
 
-# groff-img: Personal script, image manipulation
-# soelim: Enable macros dir
-# refer: Enable bibliography
-groff-img "$1" | soelim -I "$MAC" | refer -p "$REFS" |
-    groff "$GROMACS" -T "$OUT" -U "$PRE"
diff --git a/scripts/groff-img b/scripts/groff-img
deleted file mode 100755
index 1e589c6..0000000
--- a/scripts/groff-img
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/bin/sh
-# Replace .IMG commands for proper 
-# .PDFPIC and convert given image files.
-
-FILE="$1"
-OUTPUT="$(cat "$FILE")"
-
-format() {
-    LINE="$1"
-    IMAGE="$(echo "$LINE" | cut -d '"' -f 2)"
-    [ "$IMAGE" != "${IMAGE%%.*}.pdf" ] && ! [ -e "${IMAGE%%.*}.pdf" ] && convert -quiet "$IMAGE" "${IMAGE%%.*}.pdf"
-    echo "$LINE" | sed 's|^.IMG|.PDFPIC|' | sed "s|$IMAGE|${IMAGE%%.*}.pdf|"
-}
-
-for i in $(seq $(grep '^.IMG\s' "$FILE" | wc -l)) ; do
-    LINE="$(echo "$OUTPUT" | grep '^.IMG\s' | head -n 1)"
-    [ -n "$LINE" ] && NEW_LINE="$(format "$LINE")" && OUTPUT=$(echo "$OUTPUT" | sed "s|$LINE|$NEW_LINE|") || break
-done
-
-echo "$OUTPUT"
diff --git a/shell/envvar b/shell/envvar
index fc7e8f2..0965842 100644
--- a/shell/envvar
+++ b/shell/envvar
@@ -12,7 +12,7 @@ PATH="$HOME/.local/bin${PATH:+:${PATH}}"
 export \
     BOOKMARKS="$HOME/Documents/bookmarks" \
     GIT="ssh://git@gitlab.com/tavo-wasd" \
-    REFS="$HOME/Documents/bibliography" \
+    BIB="$HOME/Documents/bibliography" \
     WEEK=$(date '+%U') \
     DATE=$(date -I) \