You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.0 KiB
Bash

#!/usr/bin/env bash
# GPL3 2023 solarchemist
set -euo pipefail
usage() {
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") <flags>
Compiles Markdown manuscript and SI to PDF, and optionally converts to HTML,
ODT, or DOCX.
Available flags:
+ --help --> display this usage guide
+ --convert-all --> convert to HTML, ODT, or other formats
+ --convert-html --> convert to HTML
EOF
exit
}
msg() {
echo >&2 -e "${1-}"
}
# Examples:
# die "some message"
# die "some message and wait 6 seconds before exiting" 6
# die "some message and exit immediately" 0
die() {
local msg=$1
msg "$msg"
# short delay to aid reading last message in case terminal closes on exit
# if $2 was provided, set it as delay
# if $2 is unset or null, use a 3 second delay
simpledelay.sh ${2:-3}
exit 0
}
parse_params() {
while :; do
case "${1-}" in
-h | --help) usage ;;
-v | --verbose) set -x ;;
--convert-all) convert_formats=true ;;
--convert-html) convert_html=true ;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
return 0
}
convert_formats=false
convert_html=false
parse_params "$@"
# note the colons (same function as in $PATH), they are important
export TEXINPUTS=.:${PWD}/templates/:
# these are indexed bash arrays
# https://www.cyberciti.biz/faq/bash-for-loop-array/
declare -a MANUSCRIPT_SOURCES=("paper" "si")
declare -a MANUSCRIPT_TEMPLATES=("templates/wiley.latex" "templates/wiley-si.latex")
# these are just strings
BIBLIOGRAPHIES="--bibliography ./refmngr/zotero.bib --bibliography ./references.bib"
PANDOC=pandoc
LATEXMK=latexmk
bibtex_zotero="/media/bay/taha/chepec/literature/_refmngr/_bibrep/library.bib"
if [ -f "$bibtex_zotero" ]; then
msg "Updating the local copy of Zotero library.bib"
cp $bibtex_zotero ./refmngr/zotero.bib
fi
# for some reason, *.aux file is messing up the subsequent run (but not the first run):
# /paper.aux:224: Undefined control sequence. l.224 \BibFileName[0]{paper.html}
rm -f *.aux
# note the exclamation mark in the for loop variable, makes it the loop index
# https://stackoverflow.com/a/51794732/1198249
for M in "${!MANUSCRIPT_SOURCES[@]}"; do
# manuscript value in current loop
manuscript="${MANUSCRIPT_SOURCES[M]}"
# template value in current loop
template="${MANUSCRIPT_TEMPLATES[M]}"
# The --biblatex option is not for use with the --citeproc option or with PDF output
# https://pandoc.org/MANUAL.html#citation-rendering
msg "pandoc $manuscript.md to TeX"
# https://unix.stackexchange.com/questions/278502/accessing-array-index-variable-from-bash-shell-script-loop
$PANDOC --standalone --biblatex $manuscript.md -o $manuscript.tex --template $template $BIBLIOGRAPHIES
msg "latexmk $manuscript.tex to PDF"
$LATEXMK -bibtex -pdf $manuscript
if [[ $convert_formats == "true" || $convert_html == "true" ]]; then
# TeX to HTML
# successfully produces HTML but after lots of error-like warnings and non-zero exit
msg "Converting $manuscript.tex to HTML"
# this command creates a HTML file as expected, but returns a non-zero exit code
# which causes this script to exit immediately afterwards. Circumvented by "|| true"
# --output-dir works, but also produces all the auxiliary files in the root (very weird)
make4ht --format html5 $manuscript.tex || true
fi
if [[ $convert_formats == "true" ]]; then
# ODT is not too bad, better than html -> docx using pandoc
# pandoc cannot find figures or images, paths must be messed up...
msg "Converting $manuscript.html to ODT"
pandoc -s $manuscript.html -o $manuscript.odt
msg "Converting $manuscript.html to DOCX"
pandoc -s $manuscript.html -o $manuscript.docx
# to also convert to to DOC, consider abiword (works but produces broken equations and tables)
# https://stackoverflow.com/a/8384078/1198249
# if you want this, please run it on a workstation, not luxor
# (abiword is installed on workstations)
# abiword --to=doc $manuscript.odt
fi
done
exit 0