convert_to_pdf.sh 3.29 KB
Newer Older
1
2
#!/bin/bash

3
4
5
6
7
8
9
10
11
12
13
# First replace all whitespaces from filenames with underscores
replace_whitespaces() {
	for f in *\ *; do 
		mv "$f" "${f// /_}"; 
	done
}
# Create array to store all md files for later
# readarray -d '' array < <(find . -iname "*.md" -o -iname "*.mdown" -o -iname "*.markdown")
create_filearray () {
	mapfile -d $'\0' files < <(find . -iname  "*.md" -print0)
}
14

15
16
17
18
19
20
21
22
find_markdown () {
	if [ "${#files[@]}" -eq 0 ] ; then
		echo -e "No markdown files detected!\n"
		exit
	else 
		echo -e "${#files[@]} markdown files detected!\n"
	fi
}
23

24
25
26
27
28
29
30
31
32
# Checks for output directory and creates one if missing
create_outdir_if_not_exists () {
	if [ ! -d "$pdf_dir" ]; then
		mkdir $pdf_dir
		echo -e "Output folder created.\n"
	else
		echo -e "Selecting existing output folder.\n"
	fi
}
33

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Asks user for the LaTeX engine to be used
select_latex_engine() {
	echo -e "LaTeX engines\n------------\n"
	PS3=$'\n'"Please choose a LaTeX engine (type 1 for pdflatex): "
	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
}
54

55
56
57
58
59
60
extract_filename() {
	y="$file" 		# ./FILENAME.EXTENSION
	x="${y%.*}" 	# ./FILENAME
	z="${x##*/}" 	# FILENAME
	echo "Extracted filename: ${z}"
}
61

62
63
create_date_string() {
	created_date=$(stat -c %y ${pdf_path}) # Get file last modified date and time
64
65
66
67
	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
68
69
70
71
72
73
74
	# echo "Constructed date string: $file_date and time string: $file_time"
}

construct_pdf_path() {
	pdf_filename="${z}.pdf"
	pdf_path="./${pdf_dir}/${pdf_filename}"
}
75

76
construct_bak_pdf_path() {
77
78
	bak_pdf_filename="${z}_${file_date}-${file_time}.pdf" # create new filename
	bak_pdf_path="./${pdf_dir}/${bak_pdf_filename}"
79
80
81
82
}
# END functions #

pdf_dir="PDFs"
83

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
replace_whitespaces
create_filearray
find_markdown
create_outdir_if_not_exists
select_latex_engine

# # Allow user to add custom LaTeX variables 
# echo -n "Would you like to add any LaTeX details(e.g. --variable mainfont:\"Gill Sans\")?: "
# read variable

# Compile documents
echo -e "\nBeginning conversion...\n"

# find . -iname "*.md" -o -iname "*.mdown" -o -iname "*.markdown" | while read -r file; do # find -iname finds <pattern> case insensitive
for file in "${files[@]}"; do                                                      
    echo "File:" ${file}                                                                 
	extract_filename
	construct_pdf_path
	construct_bak_pdf_path
	echo "PDF Path: $pdf_path"
	if [ -f "$pdf_path" ]; then
		echo "File $file was already converted."
		create_date_string
		echo "Bak file name: $bak_pdf_filename"
108
		mv $pdf_path $bak_pdf_path
109
		echo -e "\nExisting file was moved to ${bak_pdf_path}\n"
110
	fi
111
	echo -e "Continuing with new file creation\n"
112
	pandoc -s --toc --template=my.latex -f gfm "$file" --pdf-engine=$option  -o "$pdf_path"  # could add --variable font-family:sans-serif $variable
113
	echo -e "File created\n"
114
115
done

116
echo -e "\nAll files complete!"