Skip to content

Instantly share code, notes, and snippets.

@plugnburn
Last active December 3, 2025 21:04
Show Gist options
  • Select an option

  • Save plugnburn/c2f7cc3807e8934b179e to your computer and use it in GitHub Desktop.

Select an option

Save plugnburn/c2f7cc3807e8934b179e to your computer and use it in GitHub Desktop.

Revisions

  1. plugnburn revised this gist Jul 4, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion statix.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    #!/bin/bash

    #Statix - the simplest static website generator in bash
    #(c) Revoltech 2015

  2. plugnburn revised this gist Jul 4, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions statix.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    #!/bin/bash

    #Statix - the simplest static website generator in bash
    #(c) Revoltech 2015

    #stubs

  3. plugnburn revised this gist Jul 4, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,8 @@ Statix is a stand-alone Bash script aimed at generating full-featured, routable
    - Route configuration: a file that maps each publicly accessible template to a SEO-friendly URL
    - Assets: a directory with optional files copied to the output website directory with no processing.

    This script is also lightweight. Aside from some standard file management commands such as `cp`, `mkdir` and `rm`, the only serious dependency for Statix is GNU Grep compiled with PCRE support (i.e. the version that supports `-P` flag, included in most Linux distributions).

    Templates
    ---------

  4. plugnburn revised this gist Jul 4, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -12,9 +12,9 @@ Templates

    In Statix, a template is a simple HTML file (or its partial) where also several special directives are allowed:

    - Include block (`<!--#include:other-template.html-->`) - a block that allows including another template to reuse existing HTML code.
    - Set block (`<!--#set:variable=some new value-->`) - a block that allows setting a variable to the specific string value
    - Use block (`<!--@variable-->`) - a block that inserts a previously set variable value.
    - **Include block** (`<!--#include:other-template.html-->`) is a block that allows including another template to reuse existing HTML code.
    - **Set block** (`<!--#set:variable=some new value-->`) is a block that allows setting a variable to the specific string value.
    - **Use block** (`<!--@variable-->`) is a block that inserts a previously set variable value.

    Note that if a variable is set twice, the first set block occurence overrides any others. So if you want to set some page-specific variables and want to be sure they will not be overwritten by any included templates, please put the appropriate set blocks at the very top of the page.

  5. plugnburn revised this gist Jul 4, 2015. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ Templates

    In Statix, a template is a simple HTML file (or its partial) where also several special directives are allowed:

    - Include block (`<!--#include:other-template.html-->`) - a block that allows including other template to reuse existing HTML code.
    - Include block (`<!--#include:other-template.html-->`) - a block that allows including another template to reuse existing HTML code.
    - Set block (`<!--#set:variable=some new value-->`) - a block that allows setting a variable to the specific string value
    - Use block (`<!--@variable-->`) - a block that inserts a previously set variable value.

    @@ -23,8 +23,7 @@ Route configuration

    To let Statix know the entire structure of your website, it's mandatory to specify all routing in a separate file (say, `routes.conf`). This file contains the mapping of a physical template name (relative to your templates directory) and logical URL (relative to the supposed website root). Note that in order to avoid any building errors all URLs **must** end in `/` (forward slash). Physical names and URLs are separated with a colon (`:`). Each mapping pair is on a new line.

    Example of a typical `route.conf` file:

    Example of a typical `routes.conf` file:
    ```
    index.html:/
    about.html:/about-us/
  6. plugnburn revised this gist Jul 4, 2015. No changes.
  7. plugnburn revised this gist Jul 4, 2015. 1 changed file with 43 additions and 0 deletions.
    43 changes: 43 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    Statix - the simplest static website generator in Bash
    ======================================================

    Statix is a stand-alone Bash script aimed at generating full-featured, routable static websites from reusable HTML snippets. It features the most basic templating engine ever possible but allows to organize your content in a SEO-friendly way. All Statix-based websites contain these parts:

    - Templates: a directory where all HTML templates are stored
    - Route configuration: a file that maps each publicly accessible template to a SEO-friendly URL
    - Assets: a directory with optional files copied to the output website directory with no processing.

    Templates
    ---------

    In Statix, a template is a simple HTML file (or its partial) where also several special directives are allowed:

    - Include block (`<!--#include:other-template.html-->`) - a block that allows including other template to reuse existing HTML code.
    - Set block (`<!--#set:variable=some new value-->`) - a block that allows setting a variable to the specific string value
    - Use block (`<!--@variable-->`) - a block that inserts a previously set variable value.

    Note that if a variable is set twice, the first set block occurence overrides any others. So if you want to set some page-specific variables and want to be sure they will not be overwritten by any included templates, please put the appropriate set blocks at the very top of the page.

    Route configuration
    -------------------

    To let Statix know the entire structure of your website, it's mandatory to specify all routing in a separate file (say, `routes.conf`). This file contains the mapping of a physical template name (relative to your templates directory) and logical URL (relative to the supposed website root). Note that in order to avoid any building errors all URLs **must** end in `/` (forward slash). Physical names and URLs are separated with a colon (`:`). Each mapping pair is on a new line.

    Example of a typical `route.conf` file:

    ```
    index.html:/
    about.html:/about-us/
    contact.html:/contact/
    work.html:/portfolio/
    ```

    Build process
    -------------

    Building the website out of the source materials (templates, routes and assets) is as simple as calling the script with all necessary parameters:

    `/path/to/statix.sh -r <route config> -t <template directory> -o <output directory> [-a <asset directory>]`

    Everything but asset directory is mandatory here. If `<asset directory>` is not specified, output will contain just the generated HTML tree and nothing else will be copied. If `<output directory>` doesn't exist, it will be created, **but if it does and is not empty, it will be completely overwritten, so be careful!** After the build completes, you can transfer the output directory contents wherever you want to host it.

  8. plugnburn created this gist Jul 4, 2015.
    88 changes: 88 additions & 0 deletions statix.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    #!/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"