#!/bin/bash _help() { echo "Pass arguments to create a static version of the index, printed on stdout Templating will replace config values config.style and config.source to null -l [snippets.txt] Path to snippets.txt. must be passed -c [config.js] *Path to configuration js replacing config values -s [styles.css] *Path to extra styles * can be left out " exit } _checkenv() { if [[ ! -e "$SNIPPETS" ]]; then echo "Snippets file '$SNIPPETS' not found." _help exit 1 fi if [[ -n "$CONFIG" ]]; then if [[ ! -e "$CONFIG" ]]; then echo "Config file '$CONFIG' not found." exit 1 fi fi if [[ -n "$STYLE" ]]; then if [[ ! -e "$STYLE" ]]; then echo "Styles file '$STYLE' not found." exit 1 fi fi echo " Snippets file: $SNIPPETS Config file: $CONFIG Style file: $STYLE " >&2 } _replace_snippets() { if [[ -e "$SNIPPETS" ]]; then sniptmp=$( mktemp ) sed 's/`/\\`/g' "$SNIPPETS" > "$sniptmp" sed \ -e '/TEMPLATED:SNIPPETS/a config.source=null;' \ -e '/TEMPLATED:SNIPPETS/a config.snippets=`' \ -e "/TEMPLATED:SNIPPETS/r $sniptmp" \ -e '/TEMPLATED:SNIPPETS/a `;' #-e "/TEMPLATED:SNIPPETS/d" rm -f "$sniptmp" else cat - fi } _replace_config() { if [[ -e "$CONFIG" ]]; then sed \ -e "/TEMPLATED:CONFIG/r $CONFIG" \ -e "/^.script language.*config.js.*script.*/d" #-e "/TEMPLATED:CONFIG/d" else cat - fi } _replace_style() { if [[ -e "$STYLE" ]]; then sed \ -e "/TEMPLATED:STYLE/r $STYLE" \ -e '/TEMPLATED:CONFIG/a config.style=null;' \ #-e "/TEMPLATED:STYLE/d" else cat - fi } _inject_dependencies() { sed \ -e "/TEMPLATED:HIGHLIGHT_STYLE/r $SELFDIR/default.min.css" \ -e "/TEMPLATED:HIGHLIGHT_STYLE/r $SELFDIR/dark.min.css" \ -e "/TEMPLATED:HIGHLIGHT_CODE/r $SELFDIR/highlight.min.js" \ -e "/stylesheet.*default.min.css/d" \ -e "/stylesheet.*dark.min.css/d" \ -e "/script.*highlight.min.js/d" } _replace() { cat "$SELFDIR"/index.html | _replace_snippets | _replace_style | _replace_config | _inject_dependencies } SELFDIR=$( dirname $( readlink -f "$0") ) CONFIG= STYLE= SNIPPETS= for (( i=1; i<=$#; i++ )); do j=$(( i + 1 )) [[ "${!i}" = "-h" ]] && _help [[ "${!i}" = "--help" ]] && _help [[ "${!i}" = "-l" ]] && { SNIPPETS="${!j}"; shift 1; continue; } [[ "${!i}" = "-c" ]] && { CONFIG="${!j}"; shift 1; continue; } [[ "${!i}" = "-s" ]] && { STYLE="${!j}"; shift 1; continue; } done _checkenv _replace