Skip to content

Instantly share code, notes, and snippets.

@merlos
Created July 13, 2023 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merlos/4eaf6e2635cf82456052cbc765d9506c to your computer and use it in GitHub Desktop.
Save merlos/4eaf6e2635cf82456052cbc765d9506c to your computer and use it in GitHub Desktop.
Shell script to convert markdown files to PDF
#!/bin/bash
# TODO improve documentation
OUTPUT_DIR='build'
SKIP_FILES=(README.md Home.md)
# This script exports Markdown files into different formats (HTML, PDF, etc..)
#
# Check if pandoc exists
if ! [ -x "$(command -v pandoc)" ]; then
echo 'Error: pandoc is not installed.' >&2
exit 1
fi
# Chech if pdflatex exists
if ! [ -x "$(command -v pdflatex)" ]; then
echo 'Error: pdflatex is not installed.' >&2
exit 1
fi
#cd ..
FILES_DIR=`pwd`
echo Current path: $FILES_DIR
MARKDOWN_FILES=`ls *.md`
echo Markdown files in folder:
echo $MARKDOWN_FILES
if [ ! -d $OUTPUT_DIR ]; then
echo "Output dir [$OUTPUT_DIR] does not exist. Will create it"
mkdir $OUTPUT_DIR
fi
for FILE in $MARKDOWN_FILES
do
if [[ " ${SKIP_FILES[@]} " =~ " ${FILE} " ]]; then
# whatever you want to do when arr contains value
echo "Skipping $FILE..."
continue
fi
#remove extension from file name
FILENAME=` echo "$FILE" | cut -d'.' -f1`
# Available output formats
# https://pandoc.org/MANUAL.html#general-options
# To PDF
echo Processing $FILENAME.pdf
pandoc --from markdown --toc $FILES_DIR/$FILE --to latex --metadata-file $FILENAME.yaml --lua-filter $FILES_DIR/bin/filter.lua -o $OUTPUT_DIR/$FILENAME.pdf
# To Word
echo Processing $FILENAME.docx
pandoc --from markdown --toc $FILES_DIR/$FILE --lua-filter $FILES_DIR/bin/filter.lua -o $OUTPUT_DIR/$FILENAME.docx
# To HTML
echo Processing $FILENAME.html
pandoc --from markdown --toc $FILES_DIR/$FILE --lua-filter $FILES_DIR/bin/filter.lua -o $OUTPUT_DIR/$FILENAME.html
# To ePub
echo Processing $FILENAME.epub
pandoc --from markdown --metadata title=$FILES_DIR/$FILENAME --toc $FILE --lua-filter $FILES_DIR/bin/filter.lua -o $OUTPUT_DIR/$FILENAME.epub
# Export doc with a template
# https://pandoc.org/demo/twocolumns.docx
#pandoc --reference-doc twocolumns.docx -o UsersGuide.docx MANUAL.txt
#echo Processing $FILENAME.pptx
#pandoc --from markdown --toc $FILE -o build/$FILENAME.pptx
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment