#!/usr/bin/env sh WORKDIR=$PWD OUTDIR=/var/www/blog verbose=0 # Variables to be evaluated as shell arithmetic should be initialized to a default or validated beforehand. if [ ! "$(command -v lowdown)" ]; then echo "lowdown not installed in $PATH" fi usage="$(basename "$0") [-h] [-v -w WORKDIR -f FILE] where: -w set the working directory (default: ${WORKDIR}) -o set the output directory (default: ${OUTDIR}) -h show this help text -v verbose output (multiple flags increase verbosity) (ex: '-v -v') " while :; do case $1 in -h | -\? | --help) # Call a "show_help" function to display a synopsis, then exit. echo "$usage" exit ;; -v) verbose=$((verbose + 1)) # Each -v argument adds 1 to verbosity. ;; -w) # Takes an option argument, ensuring it has been specified. if [ -n "$2" ]; then WORKDIR=$2 shift fi ;; -w=?*) WORKDIR=${1#*=} # Delete everything up to "=" and assign the remainder. ;; -w=) # Handle the case of an empty --file= printf 'ERROR: "-w" requires a non-empty option argument.\n' >&2 exit 1 ;; -o) # Takes an option argument, ensuring it has been specified. if [ -n "$2" ]; then OUTDIR=$2 shift fi ;; -o=?*) OUTDIR=${1#*=} # Delete everything up to "=" and assign the remainder. ;; -o=) # Handle the case of an empty --file= printf 'ERROR: "-o" requires a non-empty option argument.\n' >&2 exit 1 ;; --) # End of all options. shift break ;; -?*) printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2 ;; *) # Default case: If no more options then break out of the loop. break ;; esac shift done #### HEADER #### HEADER=$(cat < Glue Code Journal EOF ) #### FOOTER #### FOOTER=$(cat < EOF ) ### PROCESS Files #### # Make out dir if [ ! -d "${OUTDIR}" ]; then mkdir ${OUTDIR} ; fi # Remove posts and drafts if present if [ -d "${OUTDIR}/posts" ]; then rm -rf "${OUTDIR}/posts" ; fi if [ -d "${OUTDIR}/draft" ]; then rm -rf "${OUTDIR}/draft" ; fi MDLIST=$(eval "find ${WORKDIR} -type f -name \"*.md\" ") for m in $MDLIST; do # shortpath trims the working directory prefix shortpath=$(eval "echo $m | sed -e 's,${WORKDIR}/,,' ") BNAME=$(eval "basename $m") dirpath=$(eval "dirname $shortpath") filename=$(eval "echo ${BNAME} | sed -e 's,\.md,,' ") if [ $verbose -gt 0 ]; then echo "Processing: ${shortpath}" fi OUTPATH="${OUTDIR}/${dirpath}/${filename}.html" if [ ! -d "${OUTDIR}/${dirpath}" ]; then mkdir -p "${OUTDIR}/${dirpath}" ; fi # Output directly to file if $dirpath ends in ".md" for root-level files case $dirpath in *.md) OUTPATH="${OUTDIR}/${filename}.html" ;; *) # Make the outdir if not ending in ".md" esac echo $HEADER > "${OUTPATH}" lowdown -thtml --out-no-smarty $m >> "${OUTPATH}" echo $FOOTER >> "${OUTPATH}" if [ $verbose -gt 0 ]; then echo "Created: ${OUTPATH}" fi done