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

Create functions for everything

parent b7efdd0a
#!/bin/bash #!/bin/bash
# Checks for the presence of markdown files # First replace all whitespaces from filenames with underscores
echo replace_whitespaces() {
md=$(ls -1 *.md 2>/dev/null | wc -l) # ls -1 list one file per line for f in *\ *; do
mdown=$(ls -1 *.mdown 2>/dev/null | wc -l) mv "$f" "${f// /_}";
mark=$(ls -1 *.markdown 2>/dev/null | wc -l) done
present=$(($md+$mdown+$mark)) }
if [ "$present" -eq 0 ]; then # Create array to store all md files for later
echo -e "No markdown files detected!\n" # readarray -d '' array < <(find . -iname "*.md" -o -iname "*.mdown" -o -iname "*.markdown")
exit create_filearray () {
fi mapfile -d $'\0' files < <(find . -iname "*.md" -print0)
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 find_markdown () {
echo -n "Would you like to add any LaTeX details(e.g. --variable mainfont:\"Gill Sans\")?: " if [ "${#files[@]}" -eq 0 ] ; then
read variable echo -e "No markdown files detected!\n"
exit
else
echo -e "${#files[@]} markdown files detected!\n"
fi
}
# Compiles documents # Checks for output directory and creates one if missing
echo -e "\nBeginning conversion...\n" 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
}
count=0 # 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
}
find . -iname "*.md" -o -iname "*.mdown" -o -iname "*.markdown" | while read -r i; do # find -iname finds <pattern> case insensitive extract_filename() {
y="$i" y="$file" # ./FILENAME.EXTENSION
x="${y%.*}" x="${y%.*}" # ./FILENAME
z="${x##*/}" z="${x##*/}" # FILENAME
echo "Extracted filename: ${z}"
}
pdf_filename="$z.pdf" create_date_string() {
pdf_path="./${pdf_dir}/${pdf_filename}" created_date=$(stat -c %y ${pdf_path}) # Get file last modified date and time
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) 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 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 .) 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 file_time=${ftime//:} # remove the colons from the time stamp
# echo "Constructed date string: $file_date and time string: $file_time"
}
construct_pdf_path() {
pdf_filename="${z}.pdf"
pdf_path="./${pdf_dir}/${pdf_filename}"
}
construct_bak_pdf_path() {
bak_pdf_filename="${z}_${file_date}-${file_time}.pdf" # create new filename bak_pdf_filename="${z}_${file_date}-${file_time}.pdf" # create new filename
bak_pdf_path="./${pdf_dir}/${bak_pdf_filename}" bak_pdf_path="./${pdf_dir}/${bak_pdf_filename}"
}
# END functions #
pdf_dir="PDFs"
if [ -f $pdf_path ]; then replace_whitespaces
echo "File $i was already converted." 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"
mv $pdf_path $bak_pdf_path mv $pdf_path $bak_pdf_path
echo "Existing file was renamed to $bak_pdf_filename" echo -e "\nExisting file was moved to ${bak_pdf_path}\n"
fi fi
pandoc -f markdown $i --pdf-engine=$option --variable font-family:sans-serif $variable -o $pdf_path echo -e "Continuing with new file creation\n"
pandoc -s --toc --template=my.latex -f markdown "$file" --pdf-engine=$option -o "$pdf_path" # could add --variable font-family:sans-serif $variable
# ((count++)) echo -e "File created\n"
# echo "$count file compiled."
done done
echo -e "\nAll files complete!" echo -e "\nAll files complete!"
\ No newline at end of file
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