Skip to content

Commit

Permalink
Update md2pdf.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
darkoperator authored May 11, 2021
1 parent 0a47791 commit 925917d
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions Build/md2pdf.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
#!/bin/bash
# Script based on https://learnbyexample.github.io/tutorial/ebook-generation/customizing-pandoc/

Help()
{
# Display Help
echo "Generate guide PDF from a master MarkDown file."
echo
echo "Syntax: md2pdf.sh ./Build/<mastermarkdown> <output.pdf>"
echo
exit
}

while getopts ":h" option; do
case $option in
h | *) # display Help
Help
exit;;
esac
done

if [ -z "$1" ]
then
Help
exit
fi

SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "$SCRIPT")

pandoc "$tfl" \
echo -e "\e[1;32m Running Pandoc to generate the LaTex file. \e[0m"
pandoc "$1" \
-f gfm \
--toc \
--listings \
Expand All @@ -20,15 +45,16 @@ pandoc "$tfl" \
-V mainfont="DejaVu Serif" \
-V monofont="DejaVu Sans Mono" \
--pdf-engine=xelatex \
-o temp.tex
-o /tmp/temp.tex

echo -e "\e[1;32m Running Perl to format the output. \e[0m"
fn="${2%.*}"

perl -0777 -pe 's/begin\{document\}\n\n\K(.*?^\}$)(.+?)\n/$2\n\\thispagestyle{empty}\n\n$1\n/ms' temp.tex > ${fn}.tex

xelatex ${SCRIPTPATH}/${fn}.tex &> /dev/null
xelatex ${SCRIPTPATH}/${fn}.tex &> /dev/null
perl -0777 -pe 's/begin\{document\}\n\n\K(.*?^\}$)(.+?)\n/$2\n\\thispagestyle{empty}\n\n$1\n/ms' /tmp/temp.tex > ${SCRIPTPATH}/${fn}.tex

rm temp.tex "$fn".{tex,toc,aux,log}
echo -e "\e[1;32m Generating PDF. \e[0m"
xelatex ${SCRIPTPATH}/${fn}.tex > ${SCRIPTPATH}/pdfgen.log

echo -e "\e[1;32m Cleanning temp files. \e[0m"
rm /tmp/temp.tex "$fn".{tex,toc,aux,log}

0 comments on commit 925917d

Please sign in to comment.