Commit a357c98a authored by Sabina Köfler's avatar Sabina Köfler
Browse files

Create shell script to convert .md to .pdf

parent 9f47c3eb
#!/bin/bash
# Checks for the presence of markdown files
echo
md=$(ls -1 *.md 2>/dev/null | wc -l) # ls -1 list one file per line
mdown=$(ls -1 *.mdown 2>/dev/null | wc -l)
mark=$(ls -1 *.markdown 2>/dev/null | wc -l)
present=$(($md+$mdown+$mark))
if [ "$present" -eq 0 ]; then
echo -e "No markdown files detected!\n"
exit
fi
pdf_dir="PDFs"
# Checks for output directory and creates one if missing
if [ ! -d $pdf_dir ]; then
mkdir $pdf_dir
echo -e "Output folder created.\n"
else
echo -e "Selecting existing output folder.\n"
fi
# Asks user for the LaTeX engine to be used
echo -e "LaTeX engines\n------------\n"
PS3=$'\n'"Please choose a LaTeX engine: "
select option in pdflatex lualatex xelatex
do
case $option in
pdflatex)
echo -e "\npdflatex engine selected";;
lualatex)
echo -e "\nlualatex engine selected";;
xelatex)
echo -e "\nxelatex";;
*)
echo "Please choose an option";;
esac
break
done
echo
# Allows user to add custom LaTeX variables
echo -n "Would you like to add any LaTeX details(e.g. --variable mainfont:\"Gill Sans\")?: "
read variable
# Compiles documents
echo -e "\nBeginning conversion...\n"
count=0
find . -iname "*.md" -o -iname "*.mdown" -o -iname "*.markdown" | while read -r i; do # find -iname finds <pattern> case insensitive
y="$i"
x="${y%.*}"
z="${x##*/}"
pdf_filename="$z.pdf"
pdf_path="./${pdf_dir}/${pdf_filename}"
created_date=$(stat -c %y $pdf_path) # Get file last modified date and time
IFS=" " read -r a b c <<< "$created_date" # split the string into 3 parts (date time timezone_offset)
file_date="$a" # the first part is the date
ftime="${b%.*}" # the second part is the time (but its too exact, so strip everything after the .)
file_time=${ftime//:} # remove the colons from the time stamp
bak_pdf_filename="${z}_${file_date}-${file_time}.pdf" # create new filename
bak_pdf_path="./${pdf_dir}/${bak_pdf_filename}"
if [ -f $pdf_path ]; then
echo "File $i was already converted."
mv $pdf_path $bak_pdf_path
echo "Existing file was renamed to $bak_pdf_filename"
fi
pandoc -f markdown $i --pdf-engine=$option --variable font-family:sans-serif $variable -o $pdf_path
# ((count++))
# echo "$count file compiled."
done
echo -e "\nAll files complete!"
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment