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
# readarray -d '' array < <(find . -iname "*.md" -o -iname "*.mdown" -o -iname "*.markdown")
create_filearray () {
mapfile -d $'\0' files < <(find . -iname "*.md" -print0)
}
find_markdown () {
if [ "${#files[@]}" -eq 0 ] ; then
echo -e "No markdown files detected!\n" echo -e "No markdown files detected!\n"
exit exit
fi else
pdf_dir="PDFs" echo -e "${#files[@]} markdown files detected!\n"
fi
}
# Checks for output directory and creates one if missing # Checks for output directory and creates one if missing
if [ ! -d $pdf_dir ]; then create_outdir_if_not_exists () {
if [ ! -d "$pdf_dir" ]; then
mkdir $pdf_dir mkdir $pdf_dir
echo -e "Output folder created.\n" echo -e "Output folder created.\n"
else else
echo -e "Selecting existing output folder.\n" echo -e "Selecting existing output folder.\n"
fi fi
}
# Asks user for the LaTeX engine to be used # Asks user for the LaTeX engine to be used
echo -e "LaTeX engines\n------------\n" select_latex_engine() {
PS3=$'\n'"Please choose a LaTeX engine: " echo -e "LaTeX engines\n------------\n"
select option in pdflatex lualatex xelatex PS3=$'\n'"Please choose a LaTeX engine (type 1 for pdflatex): "
do select option in pdflatex lualatex xelatex
do
case $option in case $option in
pdflatex) pdflatex)
echo -e "\npdflatex engine selected";; echo -e "\npdflatex engine selected";;
...@@ -35,44 +48,69 @@ do ...@@ -35,44 +48,69 @@ do
echo "Please choose an option";; echo "Please choose an option";;
esac esac
break break
done done
echo echo
}
# Allows user to add custom LaTeX variables extract_filename() {
echo -n "Would you like to add any LaTeX details(e.g. --variable mainfont:\"Gill Sans\")?: " y="$file" # ./FILENAME.EXTENSION
read variable x="${y%.*}" # ./FILENAME
z="${x##*/}" # FILENAME
echo "Extracted filename: ${z}"
}
# Compiles documents create_date_string() {
echo -e "\nBeginning conversion...\n" created_date=$(stat -c %y ${pdf_path}) # Get file last modified date and time
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) 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