dotfiles/scripts/groffc
2024-01-08 19:41:47 -06:00

99 lines
3.5 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, usually not installed by default and not available on termux repo
# [-j (chem)] Chemical structure diagrams, messes with spacing, crashes when using text inside eqn
BIN="$0"
# Configuration
BIB="$HOME/Documents/bibliography" # Bibliography file
MAC="$HOME/.config/groff/" # Macros dir
PRE="-ketp" # Preprocessors
success() {
printf "\033[2m%s:\033[0m \033[32m%s\033[0m\n" "$BIN" "$1"
}
error() {
printf "\033[2m%s:\033[0m \033[31m%s\033[0m\n" "$BIN" "$1"
}
FILE="$1"
[ -z "$FILE" ] && error "No file given" && exit 1
OUT="${FILE%%.ms}.pdf"
[ "$FILE" = "clean" ] && MODE="clean"
[ -e "/usr/bin/soelim" ] && SOELIM="/usr/bin/soelim"
[ -e "/usr/local/bin/soelim" ] && SOELIM="/usr/local/bin/soelim"
[ -z "$SOELIM" ] && SOELIM="soelim"
[ -e "/usr/bin/refer" ] && REFER="/usr/bin/refer"
[ -e "/usr/local/bin/refer" ] && REFER="/usr/local/bin/refer"
[ -z "$REFER" ] && REFER="refer"
[ -e "/usr/bin/groff" ] && GROFF="/usr/bin/groff"
[ -e "/usr/local/bin/groff" ] && GROFF="/usr/local/bin/groff"
[ -z "$GROFF" ] && GROFF="groff"
# 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_BASE="${IMAGE##*/}"
[ "$IMAGE" != "${IMAGE%%.*}.pdf" ] && ! [ -e "_img/${IMAGE_BASE%%.*}.pdf" ] &&
mkdir -p "_img" &&
convert -quiet "$IMAGE" "_img/${IMAGE_BASE%%.*}.pdf"
echo "$LINE" | sed 's|^.IMG|.PDFPIC|' | sed "s|$IMAGE|_img/${IMAGE_BASE%%.*}.pdf|"
}
# Downloads image
get_image() {
LINE="$1"
URL="$(echo "$LINE" | cut -d '"' -f 2)"
UID="$(echo "$URL" | sha1sum | head -c 8)"
EXT="${URL##*.}"
echo "$EXT" | grep -q '.jpg\|.png\|.gif\|.svg\|.jpeg\|.tiff' || EXT="img"
mkdir -p "_img"
! [ -e "_img/$UID.$EXT" ] && wget -qO "_img/$UID.$EXT" "$URL"
echo "$LINE" | sed "s|$URL|_img/$UID.$EXT|"
}
# Remove cached images and generated pdfs
clean() {
success "Cleaning up..."
for msdoc in *.ms ; do
rm -fv ${msdoc%%.*}.pdf
done
rm -rfv "_img"
}
[ "$MODE" = "clean" ] && clean && exit 0
# If there is any .IMG command, replace with
# auto_pdfpic output in a temp file.
if [ "$(grep '^.IMG\s' "$FILE")" ] ; then
TEMP="${FILE%%.*}.tmp"
cp "$FILE" "$TEMP"
while true ; do
LINE="$(grep -m 1 '^.IMG\s' "$TEMP")"
[ -z "$LINE" ] && break
echo "$LINE" | grep -q '.*"http.*://.*' &&
PREV="$LINE" &&
LINE="$(get_image "$LINE")" &&
sed -i "s@$PREV@$LINE@g" "$TEMP"
NEW_LINE="$(auto_pdfpic "$LINE")"
sed -i "s@$LINE@$NEW_LINE@g" "$TEMP"
done
FILE="$TEMP"
fi
sed "
s/Á/\\\['A\]/g; s/É/\\\['E\]/g; s/Í/\\\['I\]/g; s/Ó/\\\['O\]/g; s/Ú/\\\['U\]/g; s/Ý/\\\['Y\]/g; s/Ć/\\\['C\]/g;
s/á/\\\['a\]/g; s/é/\\\['e\]/g; s/í/\\\['i\]/g; s/ó/\\\['o\]/g; s/ú/\\\['u\]/g; s/ý/\\\['y\]/g; s/ć/\\\['c\]/g;
s/Ë/\\\[:E\]/g; s/Ÿ/\\\[:Y\]/g; s/Ü/\\\[:U\]/g; s/Ï/\\\[:I\]/g; s/Ö/\\\[:O\]/g; s/Ä/\\\[:a\]/g;
s/ë/\\\[:e\]/g; s/ÿ/\\\[:y\]/g; s/ü/\\\[:u\]/g; s/ï/\\\[:i\]/g; s/ö/\\\[:o\]/g; s/ä/\\\[:a\]/g;
" "$FILE" | "$SOELIM" -I "$MAC" | "$REFER" -p "$BIB" | "$GROFF" -mspdf -T pdf -U "$PRE" > "$OUT"
rm -f "$TEMP"