convert_to_pdf.sh 3.79 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
14
# 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)
}
15

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

25
26
27
28
29
30
31
32
33
# 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
}
34

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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
}
55

Sabina Köfler's avatar
Sabina Köfler committed
56
construct_filename() {
57
58
59
	y="$file" 		# ./FILENAME.EXTENSION
	x="${y%.*}" 	# ./FILENAME
	z="${x##*/}" 	# FILENAME
Sabina Köfler's avatar
Sabina Köfler committed
60
61
62
63
	pdf_filename="${z}.pdf"
	pdf_path="./${pdf_dir}/${pdf_filename}" 
	# bak_pdf_filename="${z}_${file_date}-${file_time}.pdf"
	# bak_pdf_path="./${pdf_dir}/${bak_pdf_filename}"
64
}
65

66
67
create_date_string() {
	created_date=$(stat -c %y ${pdf_path}) # Get file last modified date and time
68
69
70
71
	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
72
	echo "Constructed date string: $file_date and time string: $file_time"
73
}
Sabina Köfler's avatar
Sabina Köfler committed
74
75
construct_bak_path() {
	bak_pdf_filename="${z}_${file_date}-${file_time}.pdf"
76
	bak_pdf_path="./${pdf_dir}/${bak_pdf_filename}"
77
78
79
80
}
# END functions #

pdf_dir="PDFs"
81

82
83
84
85
replace_whitespaces
create_filearray
find_markdown
create_outdir_if_not_exists
Sabina Köfler's avatar
Sabina Köfler committed
86
87
88

# exit

89
90
91
92
93
94
95
96
97
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
98
99
search1="\[\[_TOC_\]\]"
search2="## Inhalt"
100
101
# 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                                                      
Sabina Köfler's avatar
Sabina Köfler committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    echo "File:" ${file}                                                                 
	# construct_filename $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

find ./$pdf_dir -iname "*.md" | while read -r file; do
	echo -e "\nFound files to convert $file"
	construct_filename #$file
116
	if [ -f "$pdf_path" ]; then
Sabina Köfler's avatar
Sabina Köfler committed
117
		echo -e "File $file was already converted to $pdf_path."
118
		create_date_string
Sabina Köfler's avatar
Sabina Köfler committed
119
120
		construct_bak_path
		echo "Constructing back up path: $bak_pdf_path"
121
		mv $pdf_path $bak_pdf_path
122
		echo -e "\nExisting file was moved to ${bak_pdf_path}\n"
123
	else
Sabina Köfler's avatar
Sabina Köfler committed
124
		echo -e "File $file not the same as $pdf_path."
125
	fi
Sabina Köfler's avatar
Sabina Köfler committed
126
127
128
	echo -e "Continuing with new file creation\n"
	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"
129
130
done

131
echo -e "\nAll files complete!"