First commit

master
Aadhavan Srinivasan 12 months ago
commit 141be2c483

@ -0,0 +1,104 @@
#!/bin/bash
#TODO: Figure out how to prevent pandoc from exporting the YAML header of the .md files (probably set up a temp dir).
BASE_PATH="/home/aadhavan/Programming/Bash/sitegen"
check_for_dirs() {
if [ ! -d "${BASE_PATH}/source" ]; then
echo "'source' folder does not exist."
exit
fi
if [ ! -d "${BASE_PATH}/output" ]; then
echo "'output' folder does not exist."
fi
# Check if temp dir exists, if it does, throw an error and exit.
# Check if header and footer.html exist.
}
setup_temp_dir() {
# Check if 'temp' already exists
mkdir $BASE_PATH/temp
}
setup_output_dir() {
rm -r output # Delete existing 'output' directory
cp -r source output #Copy directory structure from 'source' to 'output'
}
del_files_in_output() {
find $BASE_PATH/output -type f -name "*.md" -delete #Delete all .md files (which were copied over from 'source') in 'output'
}
read_metadata() {
# Read the metadata from the top of a .md file into a string
metadata=$(awk 'BEGIN{RS = "\n\n"} {print $0}; {exit}' $1) # Reads from the .md file until a double-newline is encountered
}
convert_to_array() {
# Converts the metadata into two arrays: one with the key, and the other with the value
readarray -t meta_key < <(echo -e "$1" | awk -F: '{print $1}')
readarray -t meta_value < <(echo -e "$1" | awk -F: '{st = index($0,":"); values = substr($0,st+1); print values}' | cut -c 2-)
}
add_date_to_array() {
meta_key+=("date")
meta_value+=("$(date -r $1 +'%b %d, %Y')")
}
add_header_and_footer() {
# Add header
cat $BASE_PATH/header.html | cat - $1 > $BASE_PATH/temp/temp.html
# Add footer
echo >> $BASE_PATH/temp/temp.html
cat $BASE_PATH/footer.html >> $BASE_PATH/temp/temp.html
# Move temp file to original location
mv $BASE_PATH/temp/temp.html $1
}
replace_vars() {
# Loop through 'meta_key' array, search for the first occurence of the values in the HTML doc, and replace them with corresponding values in 'meta_value'.
for index in $(seq 0 `expr "${#meta_key[@]}" - 1`); do
sed -i "0, /[\$][\$]${meta_key[$index]}[\$][\$]/ {s/[\$][\$]${meta_key[$index]}[\$][\$]/${meta_value[index]}/}" $1
done
}
md_to_html() {
# Convert .md files from 'source' and place them into the correct locations into 'output'
files=$(find $BASE_PATH/source -name "*.md")
for file in $files; do
read_metadata $file # Sets the 'metadata' variable
convert_to_array "$metadata" #Sets the 'meta_key' and 'meta_value' arrays
add_date_to_array "$file" #Uses 'meta_key' and 'meta_value' arrays
# Copy file to temp dir and strip metadata
cp $file $BASE_PATH/temp/
let num_lines=$(echo $metadata | wc -l)+1
sed -i "1,${num_lines}d" $BASE_PATH/temp/`basename $file`
pandoc -f markdown $BASE_PATH/temp/`basename $file` > $BASE_PATH/output/`basename $file .md`.html
rm $BASE_PATH/temp/*
html_file="$BASE_PATH/output/`basename $file .md`.html"
add_header_and_footer $html_file
replace_vars $html_file #Uses 'meta_key' and 'meta_value' arrays
unset metadata meta_key meta_value
done
}
check_for_dirs
setup_temp_dir
setup_output_dir
del_files_in_output
md_to_html
Loading…
Cancel
Save