Skip to content

Instantly share code, notes, and snippets.

@thgie
Last active April 19, 2022 15:38
Show Gist options
  • Save thgie/c758e0d52fdc154d007afdcbb8e93365 to your computer and use it in GitHub Desktop.
Save thgie/c758e0d52fdc154d007afdcbb8e93365 to your computer and use it in GitHub Desktop.
git hook to compile and deploy a bunch of markdown files to a html website with the help of pandoc. used by https://github.com/thgie/jache.re
#!/bin/sh
INPUT_DIR="$(pwd)/"
OUTPUT_DIR="/var/www/jache.re/"
git diff-tree -r --name-only --no-commit-id HEAD@{1} HEAD | grep -E '.md' | while read file ; do
# prepare input file string
INPUT_FILE="${file#\"}"
# prepare relative output file path w/o extension
OUTPUT_RELATIVE=${INPUT_FILE%.*}
# prepare absolute output path with extension
OUTPUT_ABSOLUTE="$OUTPUT_DIR$OUTPUT_RELATIVE.html"
# prepare absolute output path to containing folder to create folder if necessary
OUTPUT_PATH="${OUTPUT_ABSOLUTE%/*}/"
#echo "converting /var/repositories/jache.re/$file to $OUTPUT_ABSOLUTE"
mkdir -p "$OUTPUT_PATH"
# if file contains [@xyz] citations, apply bibtex file and cite-proc
if grep -q "\[\@" "$INPUT_DIR$INPUT_FILE" ; then
pandoc "$INPUT_DIR$INPUT_FILE" -f markdown -o "$OUTPUT_ABSOLUTE" --template=files/template/main.html --bibliography files/jachere.bib
else
pandoc "$INPUT_DIR$INPUT_FILE" -f markdown -o "$OUTPUT_ABSOLUTE" --template=files/template/main.html
fi
# replace the .md link to .html
sed -i -e "s/\.md/.html/g" "$OUTPUT_ABSOLUTE"
# replace anchor hrefs to work with base href
ANCHOR_RELATIVE=$(echo "$OUTPUT_RELATIVE" | sed 's;/;\\/;g')
sed -i -e "s/href=\"#/href=\"${ANCHOR_RELATIVE}.html#/g" "$OUTPUT_ABSOLUTE"
done
# sync files to output dir
rsync -avu --delete "${INPUT_DIR}files/" "${OUTPUT_DIR}files"
# resize images
find "$OUTPUT_DIR" -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' -execdir mogrify -resize 820x {} +
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment