#!/usr/bin/env bash # solarchemist # GPL3 2023 set -euo pipefail usage() { cat < Compiles Markdown manuscript and SI to PDF, and optionally converts it to HTML, ODT, and/or DOCX. Requires .mdtemplates file to exist in the current working directory. 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 ;; --paper) only_paper=true;; --si) only_si=true;; -?*) die "Unknown option: $1" ;; *) break ;; esac shift done return 0 } convert_formats=false convert_html=false only_paper=false only_si=false parse_params "$@" # https://ostechnix.com/bash-associative-array # https://phoenixnap.com/kb/bash-associative-array # I expect .mdtemplates to look like this: # (note the use of "keys" named "paper" and "si", these key names should always be paper/si) # ``` # $ cat .mdtemplates # declare -A MANUSCRIPT=( ["paper"]="templates/pmichaillat.latex", ["si"]="templates/pmichaillat-si.latex" ) # ``` # so if your main manuscript is named something other than "paper", just adjust accordingly in your .mdtemplates # and ditto for the relative path to your latex template file markdown should use. msg "--- Looking for .mdtemplates" if [ -e .mdtemplates ]; then msg "--- Found in .mdtemplates:" # https://stackoverflow.com/a/4685432/1198249 source ./.mdtemplates for M in "${!MANUSCRIPT[@]}"; do echo " ${M}: ${MANUSCRIPT[${M}]}" done else die "Found no .mdtemplates file. Quitting..." 0 # for later, add the ability define the array as arg to the --paper and --si flags fi if [[ $only_paper == "true" ]]; then # msg "Detected --paper flag" declare -A MANUSCRIPT_PAPER MANUSCRIPT_PAPER["paper"]=${MANUSCRIPT["paper"]} unset MANUSCRIPT declare -A MANUSCRIPT MANUSCRIPT["paper"]=${MANUSCRIPT_PAPER["paper"]} # declare -a MANUSCRIPT_TEMPLATES=( "templates/pmichaillat.latex" ) fi if [[ $only_si == "true" ]]; then # msg "Detected --si flag" declare -A MANUSCRIPT_SI # in this assignment we lose the "si" key, gets replaced by integer # declare -a MANUSCRIPT_SI=${MANUSCRIPT["si"]} # we effectively need to build a new array from the old # https://unix.stackexchange.com/a/464627/411416 MANUSCRIPT_SI["si"]=${MANUSCRIPT["si"]} # echo "${MANUSCRIPT_SI[@]}" # echo "${!MANUSCRIPT_SI[@]}" # echo "${#MANUSCRIPT_SI[@]}" # simpledelay.sh 10 unset MANUSCRIPT declare -A MANUSCRIPT MANUSCRIPT["si"]=${MANUSCRIPT_SI["si"]} # declare -a MANUSCRIPT_TEMPLATES=( "templates/pmichaillat-si.latex" ) fi # echo "${MANUSCRIPT[@]}" # echo "${!MANUSCRIPT[@]}" # echo "${#MANUSCRIPT[@]}" # simpledelay.sh 15 # NOTE "templates/" is hard-coded here! # note the colons (same function as in $PATH), they are important export TEXINPUTS=.:${PWD}/templates/: # 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[@]}"; do # array key (manuscript name) manuscript="${M}" # array value (template path) template="${MANUSCRIPT[${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