diff options
Diffstat (limited to 'gen.sh')
-rwxr-xr-x | gen.sh | 124 |
1 files changed, 124 insertions, 0 deletions
@@ -0,0 +1,124 @@ +#!/usr/bin/env sh + +WORKDIR=$PWD +OUTDIR=out +verbose=0 # Variables to be evaluated as shell arithmetic should be initialized to a default or validated beforehand. + +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 <<EOF +<!DOCTYPE html> +<html> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>Glue Code Journal</title> + <style type="text/css"> + body { + margin: 40px + auto; + max-width: 650px; + line-height: 1.6; + font-size: 18px; + color: #444; + padding: 0 + 10px + } + + h1, h2, h3 { + line-height: 1.2 + } + </style> +</head> + +EOF +) + +#### FOOTER #### +FOOTER=$(cat <<EOF + +</body> +</html> +EOF +) +### PROCESS Files #### +# Make out dir +if [ ! -d "${OUTDIR}" ]; then mkdir ${OUTDIR} ; fi + +MDLIST=$(eval "find ${WORKDIR} -type f -name \"*.md\" ") +for m in $MDLIST; do + # shortpath trims the working directory prefix and the '.git' suffix from the directory + shortpath=$(eval "echo $m | sed -e 's,${WORKDIR}/,,' ") + dirpath=$(eval "echo $m | sed -e 's,${WORKDIR}/,,' -e 's,/.*.md$,,' ") + filename=$(eval "echo $m | sed -e 's,${WORKDIR}/,,' -e 's,${dirpath}/,,' -e 's,\.md,,' ") + if [ $verbose -gt 0 ]; then + echo "Processing: ${shortpath}, ${dirpath} ${filename}" + fi + if [ ! -d "${OUTDIR}/${dirpath}" ]; then mkdir ${OUTDIR}/${dirpath} ; fi + + echo $HEADER > "${OUTDIR}/${dirpath}/${filename}.html" + cmark --safe --validate-utf8 -t html --width 80 $m >> "${OUTDIR}/${dirpath}/${filename}.html" + echo $FOOTER >> "${OUTDIR}/${dirpath}/${filename}.html" + + if [ $verbose -gt 0 ]; then + echo "Created: ${OUTDIR}/${dirpath}/${filename}.html" + fi +done |