New script to compile Markdown manuscript

master
Taha Ahmed 1 year ago
parent 205c8a8afe
commit d587da571a

@ -0,0 +1,125 @@
#!/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

@ -124,6 +124,8 @@ TeXfiletype="tex"
tikzfiles="*.tikz"
Rnwfiles="*.Rnw"
# aux files
aux_4ct="*.4ct" # TOC entries for HTML output
aux_4tc="*.4tc" # TOC entries for HTML output
aux_acn="*.acn"
aux_acr="*.acr"
aux_alg="*.alg" # glossaries
@ -139,6 +141,7 @@ aux_glg="*.glg"
aux_glo="*.glo" # glossaries
aux_gls="*.gls*" # glossaries and bib2gls
aux_ist="*.ist" # glossaries (makeindex style file)
aux_lg="*.lg" # tex4ht
aux_lob="*.lob"
aux_lof="*.lof"
aux_log="*.log"
@ -156,13 +159,15 @@ aux_run="*.run*"
aux_slg="*.slg"
aux_slo="*.slo"
aux_sls="*.sls"
aux_stc="*.stc*" # minitoc
aux_stc="*.stc*" # minitoc
aux_tikz="*-tikzDictionary"
aux_tdo="*.tdo"
aux_tmp="*.tmp" # tex4ht
aux_toc="*.toc"
aux_xdy="*.xdy" # glossaries (xindy)
aux_xdy="*.xdy" # glossaries (xindy)
aux_xref="*.xref" # pandoc or tex4ht cross-refs
# all aux files in a single string
auxfiles="${aux_acn} ${aux_acr} ${aux_alg} ${aux_aux} ${aux_bbl} ${aux_blg} ${aux_dep} ${aux_dpth} ${aux_fdb} ${aux_fig} ${aux_fls} ${aux_glg} ${aux_glo} ${aux_gls} ${aux_ist} ${aux_lob} ${aux_lof} ${aux_log} ${aux_lol} ${aux_lor} ${aux_los} ${aux_lot} ${aux_maf} ${aux_out} ${aux_lox} ${aux_make} ${aux_map} ${aux_mtc} ${aux_run} ${aux_slg} ${aux_slo} ${aux_sls} ${aux_stc} ${aux_tikz} ${aux_tdo} ${aux_toc} ${aux_xdy}"
auxfiles="${aux_4ct} ${aux_4tc} ${aux_acn} ${aux_acr} ${aux_alg} ${aux_aux} ${aux_bbl} ${aux_blg} ${aux_dep} ${aux_dpth} ${aux_fdb} ${aux_fig} ${aux_fls} ${aux_glg} ${aux_glo} ${aux_gls} ${aux_ist} ${aux_lg} ${aux_lob} ${aux_lof} ${aux_log} ${aux_lol} ${aux_lor} ${aux_los} ${aux_lot} ${aux_maf} ${aux_out} ${aux_lox} ${aux_make} ${aux_map} ${aux_mtc} ${aux_run} ${aux_slg} ${aux_slo} ${aux_sls} ${aux_stc} ${aux_tikz} ${aux_tdo} ${aux_toc} ${aux_xdy} ${aux_xref} ${aux_tmp}"
if [[ $show_menu == "false" ]]; then

Loading…
Cancel
Save