dotfiles/scripts/groff-compiler
tavo-wasd 1f5b9b09f6 fix
2023-09-30 07:04:54 -06:00

58 lines
1.8 KiB
Bash
Executable file

#!/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
# Configuration
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 '^.IMG\s' "$FILE")" ] ; then
TEMP="${FILE%%.*}.tmp"
echo $TEMP
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"