major revamp of groff compiler

This commit is contained in:
tavo-wasd 2023-11-17 03:09:30 -06:00
parent 79363d5619
commit 138bdbf380
2 changed files with 30 additions and 21 deletions

View file

@ -2,8 +2,8 @@
# Compiler script for groff # Compiler script for groff
# #
# TODO: # TODO:
# Make groff auto-calculate image height (spacing below) # - Make groffc auto-calculate image height (spacing below) (already done with 1.23.0?)
# based on given width and the image's aspect ratio # based on given width and the image's aspect ratio.
# #
# [-k (preconv)] Convert encoding to something groff understands # [-k (preconv)] Convert encoding to something groff understands
# [-e (eqn)] Format equations for troff or MathML # [-e (eqn)] Format equations for troff or MathML
@ -14,6 +14,7 @@
FILE="$1" FILE="$1"
OUT="${FILE%%.ms}.pdf" OUT="${FILE%%.ms}.pdf"
[ "$FILE" = "clean" ] && MODE="clean"
# Configuration # Configuration
@ -25,14 +26,20 @@ BIB="$HOME/Documents/bibliography" # Bibliography file
MAC="$HOME/.config/groff/" # Macros dir MAC="$HOME/.config/groff/" # Macros dir
PRE="-ketpG" # Preprocessors PRE="-ketpG" # Preprocessors
print() {
printf "\033[2mgroff-compiler:\033[0m \033[32m%s\033[0m\n" "$1"
}
# Takes a line with .IMG command, converts given image # Takes a line with .IMG command, converts given image
# to pdf format and inserts properly formatted .PDFPIC # to pdf format and inserts properly formatted .PDFPIC
auto_pdfpic() { auto_pdfpic() {
LINE="$1" LINE="$1"
IMAGE="$(echo "$LINE" | cut -d '"' -f 2)" IMAGE="$(echo "$LINE" | cut -d '"' -f 2)"
[ "$IMAGE" != "${IMAGE%%.*}.pdf" ] && ! [ -e "${IMAGE%%.*}.pdf" ] && IMAGE_BASE="${IMAGE##*/}"
convert -quiet "$IMAGE" "${IMAGE%%.*}.pdf" [ "$IMAGE" != "${IMAGE%%.*}.pdf" ] && ! [ -e "_img/${IMAGE_BASE%%.*}.pdf" ] &&
echo "$LINE" | sed 's|^.IMG|.PDFPIC|' | sed "s|$IMAGE|${IMAGE%%.*}.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 # Downloads image
@ -41,12 +48,21 @@ get_image() {
URL="$(echo "$LINE" | cut -d '"' -f 2)" URL="$(echo "$LINE" | cut -d '"' -f 2)"
UID="$(echo "$URL" | sha1sum | head -c 8)" UID="$(echo "$URL" | sha1sum | head -c 8)"
EXT="${URL##*.}" EXT="${URL##*.}"
echo "$EXT" | grep -q '.jpg\|.png\|.gif\|.svg\|.jpeg\|.tiff' || EXT="img"
mkdir -p "_img" mkdir -p "_img"
! [ -e "_img/$UID.$EXT" ] && wget -qO "_img/$UID.$EXT" "$URL" ! [ -e "_img/$UID.$EXT" ] && wget -qO "_img/$UID.$EXT" "$URL"
URL="$(echo "$URL" | sed -e 's`[][\\/.*^$]`\\&`g')" echo "$LINE" | sed "s|$URL|_img/$UID.$EXT|"
echo "$LINE" | sed "s|$URL|_img/$UID\.$EXT|"
} }
clean() {
print "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 # If there is any .IMG command, replace with
# auto_pdfpic output in a temp file. # auto_pdfpic output in a temp file.
if [ "$(grep '^.IMG\s' "$FILE")" ] ; then if [ "$(grep '^.IMG\s' "$FILE")" ] ; then
@ -56,7 +72,7 @@ if [ "$(grep '^.IMG\s' "$FILE")" ] ; then
LINE="$(grep -m 1 '^.IMG\s' "$TEMP")" LINE="$(grep -m 1 '^.IMG\s' "$TEMP")"
[ -z "$LINE" ] && break [ -z "$LINE" ] && break
echo "$LINE" | grep -q '.*"http.*://.*' && echo "$LINE" | grep -q '.*"http.*://.*' &&
PREV="$(echo "$LINE" | sed -e 's`[][\\/.*^$]`\\&`g')" && PREV="$LINE" &&
LINE="$(get_image "$LINE")" && LINE="$(get_image "$LINE")" &&
sed -i "s@$PREV@$LINE@g" "$TEMP" sed -i "s@$PREV@$LINE@g" "$TEMP"
NEW_LINE="$(auto_pdfpic "$LINE")" NEW_LINE="$(auto_pdfpic "$LINE")"
@ -66,17 +82,10 @@ if [ "$(grep '^.IMG\s' "$FILE")" ] ; then
fi fi
sed " sed "
s/Á/\\\['A\]/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/á/\\\['a\]/g; s/é/\\\['e\]/g; s/í/\\\['i\]/g; s/ó/\\\['o\]/g; s/ú/\\\['u\]/g; s/ý/\\\['y\]/g; s/ć/\\\['c\]/g;
s/Í/\\\['I\]/g; s/Ë/\\\[:E\]/g; s/Ÿ/\\\[:Y\]/g; s/Ü/\\\[:U\]/g; s/Ï/\\\[:I\]/g; s/Ö/\\\[:O\]/g; s/Ä/\\\[:a\]/g;
s/Ó/\\\['O\]/g; s/ë/\\\[:e\]/g; s/ÿ/\\\[:y\]/g; s/ü/\\\[:u\]/g; s/ï/\\\[:i\]/g; s/ö/\\\[:o\]/g; s/ä/\\\[:a\]/g;
s/Ú/\\\['U\]/g; " "$FILE" | "$SOELIM" -I "$MAC" | "$REFER" -p "$BIB" | "$GROFF" -mspdf -T pdf -U "$PRE" > "$OUT"
s/á/\\\['a\]/g;
s/é/\\\['e\]/g;
s/í/\\\['i\]/g;
s/ó/\\\['o\]/g;
s/ú/\\\['u\]/g;
" "$FILE" |
"$SOELIM" -I "$MAC" | "$REFER" -p "$BIB" | "$GROFF" -mspdf -T pdf -U "$PRE" > "$OUT"
rm -f "$TEMP" rm -f "$TEMP"

View file

@ -50,7 +50,7 @@ noremap N /@@@<CR>
" groff " groff
autocmd FileType groff noremap <buffer> <silent> <F9> :!zathura --fork %:r.pdf<CR> & disown autocmd FileType groff noremap <buffer> <silent> <F9> :!zathura --fork %:r.pdf<CR> & disown
autocmd FileType groff noremap <buffer> <C-s> :w<CR> :!groff-compiler %<CR><CR> autocmd FileType groff noremap <buffer> <C-s> :w<CR> :!groffc %<CR><CR>
au BufNewFile,BufRead *.ms set filetype=groff au BufNewFile,BufRead *.ms set filetype=groff
" LaTeX " LaTeX