convert_to_pdf.sh 2.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/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!"