-
-
Save plugnburn/c2f7cc3807e8934b179e to your computer and use it in GitHub Desktop.
| #!/bin/bash | |
| #Statix - the simplest static website generator in bash | |
| #stubs | |
| ROUTEFILE='' | |
| TPLDIR='' | |
| OUTDIR='' | |
| ASSETDIR='' | |
| #routines | |
| function showUsage { | |
| echo "Usage: $0 -r <route config> -t <template directory> -o <output directory> [-a <asset directory>]" 1>&2 | |
| exit 1 | |
| } | |
| function prerenderTemplate { | |
| local TPLFILE="${TPLDIR}/$1" | |
| local TPLCONTENT="$(<$TPLFILE)" | |
| local L='' | |
| local INCLUDES=$(grep -Po '<!--\s*#include:.*?-->' "$TPLFILE") | |
| OLDIFS="$IFS" | |
| IFS=$'\n' | |
| for L in $INCLUDES; do | |
| local INCLFNAME=$(echo -n "$L"|grep -Po '(?<=#include:).*?(?=-->)') | |
| local INCLFCONTENT="$(prerenderTemplate ${INCLFNAME})" | |
| TPLCONTENT="${TPLCONTENT//$L/$INCLFCONTENT}" | |
| done | |
| IFS="$OLDIFS" | |
| echo -n "$TPLCONTENT" | |
| } | |
| function renderTemplate { | |
| local TPLTEXT="$(prerenderTemplate $1)" | |
| local SETS=$(echo -n "$TPLTEXT"|grep -Po '<!--#set:.*?-->') | |
| local L='' | |
| OLDIFS="$IFS" | |
| IFS=$'\n' | |
| for L in $SETS; do | |
| local SET=$(echo -n "$L"|grep -Po '(?<=#set:).*?(?=-->)') | |
| local SETVAR="${SET%%=*}" | |
| local SETVAL="${SET#*=}" | |
| TPLTEXT="${TPLTEXT//$L/}" | |
| TPLTEXT="${TPLTEXT//<!--@${SETVAR}-->/${SETVAL}}" | |
| done | |
| IFS="$OLDIFS" | |
| echo -n "$TPLTEXT" | |
| } | |
| #read all option variables | |
| while getopts ':r:t:o:a:' OPT; do | |
| case "$OPT" in | |
| r) ROUTEFILE="$OPTARG" ;; | |
| t) TPLDIR="$OPTARG" ;; | |
| o) OUTDIR="$OPTARG" ;; | |
| a) ASSETDIR="$OPTARG" ;; | |
| *) showUsage ;; | |
| esac | |
| done | |
| shift $((OPTIND-1)) | |
| if [[ -z "$ROUTEFILE" ]] || [[ -z "$TPLDIR" ]] || [[ -z "$OUTDIR" ]] ; then | |
| showUsage | |
| fi | |
| #run main action | |
| mkdir -p "$OUTDIR" | |
| rm -rf "${OUTDIR}"/* | |
| if [[ "$ASSETDIR" ]]; then cp -rd "$ASSETDIR" "${OUTDIR}/";fi | |
| ROUTELIST="$(<$ROUTEFILE)" | |
| OLDIFS="$IFS" | |
| IFS=$'\n' | |
| for ROUTE in $ROUTELIST; do | |
| TPLNAME="${ROUTE%%:*}" | |
| TPLPATH="${ROUTE#*:}" | |
| if [[ "$TPLNAME" && "$TPLPATH" ]]; then | |
| mkdir -p "${OUTDIR}${TPLPATH}" | |
| renderTemplate "$TPLNAME" > "${OUTDIR}${TPLPATH}index.html" | |
| fi | |
| done | |
| IFS="$OLDIFS" | |
| echo "Website saved at $OUTDIR" |
I concur with @joeluis - would be great for a https://spdx.org/licenses/ license mentioned
@joselius, You can use any of my gists in your projects unless explicitly said otherwise.
Thanks @plugnburn,
I finally ended up creating a static website generator from scratch and named it webera.
I believe it's already sufficiently dissimilar from yours so that the inspiration wont suppose a problem. And as a work in progress it will also keep diverging more and more. My intention is to make it more versatile than simple, without making it complicated.
Anyhow I'll be happy to receive any comments. Or none. Thank you again.
Wow, I made something that is so close to this you'd think it's plagiarism. Even some of the tokens and keywords!
edit: okay, mines been updated so that you can embed arbitrary bash commands into any file- that's it! would love it if you took a look at it, since we both think alike github.com/circuitandcode/contate
i continued this a bit: https://github.com/g--o/xstatix
i continued this a bit: https://github.com/g--o/xstatix
Cool, because I even forgot about this project altogether.
Hey, I really love your simple script for static web generation. I found it very inspiring.
I want to make my own version, and I'd prefer to start by forking yours and then add several modifications and improvements I have in mind. But I don't see a license that explicitly allows it, and I don't see any other means of contact, so I can only ask you in the comments.
Would you be ok with that, or do you prefer that I don't use your code as a base, meaning I'll have to start from scratch?
Thank you!