#!/usr/bin/env bash # Best practice options set -o errexit set -o nounset set -o pipefail if [[ "${TRACE-0}" == "1" ]]; then set -o xtrace; fi if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then echo 'Usage: ' exit fi INPUT_DIR=~/notes OUTPUT_DIR=~/public_html pushd ~ # Recursively iterate over directories, calling process_file on .md files traverse_dir() { local dir=$1 for d in $dir; do OUTPUT=$OUTPUT_DIR${d##$INPUT_DIR} if [ -d "$d" ]; then mkdir -p $OUTPUT traverse_dir "$d/*" elif [[ $d == *.md ]]; then OUTPUT=${OUTPUT%.md}.html #OUTPUT=${OUTPUT// /_} process_file "$d" "$OUTPUT" fi done } # Take a Markdown file and process it to HTML process_file() { local INPUT=$1 local OUTPUT=$2 local TITLE=${INPUT%.md} TITLE=${TITLE##*/} if [ "$TITLE" == 'index' ]; then TITLE='Home' fi echo $INPUT echo $OUTPUT pandoc -f markdown -t html -o "$OUTPUT" -i "$INPUT" --standalone --template ~/code/site/template.html --variable=pagetitle:"$TITLE" replace_links "$OUTPUT" } # Replace links in Markdown files with working links to the new HTML files replace_links() { local FILE=$1 # Add .html extensions # sed -i 's/\(href="\)\([^"]*\)/\1\2.html/g' "$FILE" sed -Ei.bak '/https|\.[a-z]+/!s/href="[^"]*/&.html/' "$FILE" } setup_files() { cp ~/code/site/style.css ~/public_html } traverse_dir $INPUT_DIR/\* setup_files popd