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

3
4
5
6
7
8
# First replace all whitespaces from filenames with underscores
replace_whitespaces() {
	for f in *\ *; do 
		mv "$f" "${f// /_}"; 
	done
}
9

10
11
12
13
# Create array to store all md files for later
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

Sabina Köfler's avatar
Sabina Köfler committed
55
construct_filename() {
56
57
58
	y="$file" 		# ./FILENAME.EXTENSION
	x="${y%.*}" 	# ./FILENAME
	z="${x##*/}" 	# FILENAME
Sabina Köfler's avatar
Sabina Köfler committed
59
60
	pdf_filename="${z}.pdf"
	pdf_path="./${pdf_dir}/${pdf_filename}" 
61
}
62

63
create_date_string() {
Sabina Köfler's avatar
Sabina Köfler committed
64
65
66
67
68
	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
Sabina Köfler's avatar
Sabina Köfler committed
69
	echo "Constructed date string: $file_date and time string: $file_time"
70
}
Sabina Köfler's avatar
Sabina Köfler committed
71

Sabina Köfler's avatar
Sabina Köfler committed
72
73
construct_bak_path() {
	bak_pdf_filename="${z}_${file_date}-${file_time}.pdf"
74
	bak_pdf_path="./${pdf_dir}/${bak_pdf_filename}"
75
76
77
78
}
# END functions #

pdf_dir="PDFs"
79

80
81
82
83
replace_whitespaces
create_filearray
find_markdown
create_outdir_if_not_exists
Sabina Köfler's avatar
Sabina Köfler committed
84

85
86
87
88
89
90
91
92
93
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"

Sabina Köfler's avatar
Sabina Köfler committed
94
95
96
97
98
99
100
101
102
103
104
105
106
# search1="\[\[_TOC_\]\]"
# search2="## Inhalt"
# # 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}                                                                 
# 	# remove the line that renders the table of contents in markdown and copy file to /PDFs
# 	if ! grep -q "$search1" $file; then 
# 		continue
# 	else
# 		echo -e "Found search string in $file\n Will make a copy"
# 		sed "/$search1/d;/$search2/d" $file > ./$pdf_dir/$file
# 	fi
# done
Sabina Köfler's avatar
Sabina Köfler committed
107
108
109
110

find ./$pdf_dir -iname "*.md" | while read -r file; do
	echo -e "\nFound files to convert $file"
	construct_filename #$file
111
	if [ -f "$pdf_path" ]; then
Sabina Köfler's avatar
Sabina Köfler committed
112
		echo -e "File $file was already converted to $pdf_path."
113
		create_date_string
Sabina Köfler's avatar
Sabina Köfler committed
114
115
		construct_bak_path
		echo "Constructing back up path: $bak_pdf_path"
116
		mv $pdf_path $bak_pdf_path
117
		echo -e "\nExisting file was moved to ${bak_pdf_path}\n"
118
	else
Sabina Köfler's avatar
Sabina Köfler committed
119
		echo -e "File $file not the same as $pdf_path."
120
	fi
Sabina Köfler's avatar
Sabina Köfler committed
121
	echo -e "\nContinuing with new file creation\n"
Sabina Köfler's avatar
Sabina Köfler committed
122
123
	pandoc -s --toc --template=my.latex -f gfm "$file" --pdf-engine=$option  -o "$pdf_path"  # could add --variable font-family:sans-serif $variable
	echo -e "File created\n"
124
125
done

Sabina Köfler's avatar
Sabina Köfler committed
126
echo -e "\nAll files complete!"