~sircmpwn/wersh

bcc3fbaf8d1e19c262290916967a524cb2111d37 — Drew DeVault 3 years ago
Initial commit
A  => .gitignore +1 -0
@@ 1,1 @@
_site

A  => LICENSE +24 -0
@@ 1,24 @@
Copyright (c) 2017 Drew DeVault, All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.
   
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

A  => README +31 -0
@@ 1,31 @@
wersh

Static site generation with POSIX shell scripts, loosely inspired by werc[1].

USAGE

wersh [-w] [input] [output]

Sources [input]/**.sh and writes the output to [output]/**. Input defaults to ./
and output defaults to _site/.

Files that don't match *.sh are copied directly. Files and directories that
match _* are not used.

When your script executes, the working directory is set to input.

The special file _config.sh will be sourced for all scripts if present.

-w will use inotifywait to watch for changes, then automatically recompile your
site. This is only supported on Linux.

EXAMPLES

See example/ for an example wersh site. Run the following:

    ./wersh example
    lighttpd -Df lighttpd.conf

Then browse to http://localhost:8080 to check out the example site.

[1] http://werc.cat-v.org/

A  => example/_config.sh +16 -0
@@ 1,16 @@
function hoedown() {
    /usr/bin/hoedown --tables --fenced-code
}

pages="page-one.html.sh page-two.html.sh"

function page_title() {
    case $1 in
        page-one.html.sh)
            echo "Page one"
            ;;
        page-two.html.sh)
            echo "Page two"
            ;;
    esac
}

A  => example/_templates/layout.html.sh +14 -0
@@ 1,14 @@
cat <<EOF
<!doctype html>
<html>
  <head>
    <title>${title:-Test site}</title>
    <link rel="stylesheet" type="text/css" href="/css/main.css" />
  </head>
  <body>
EOF
content
cat <<EOF
  </body>
</html>
EOF

A  => example/css/main.css.sh +19 -0
@@ 1,19 @@
cat <<EOF | sassc -s -t compressed
html
body {
  max-width: 500px;
  margin: 0 auto;
  font-family: sans-serif;
  font-size: 0.95rem;
}

h1, h2, h3 {
    text-transform: uppercase;
}

img {
    display: block;
    margin: 0 auto;
    max-height: 300px;
}
EOF

A  => example/images/mio.png +0 -0

A  => example/index.html.sh +33 -0
@@ 1,33 @@
function content() {
    cat <<EOF | hoedown
# wersh

Static site generation with POSIX shell scripts, loosely inspired by
[werc](http://werc.cat-v.org).

This page is a shell script: [example/index.html.sh](/index.html.sh). Files
ending with *.sh are sourced and their stdout is written to the corresponding output file.

## Pages

Since it's a shell script, you can do pretty much anything you want. This list
of pages is dynamically generated:

EOF
    for page in $pages
    do
        echo "* [$(page_title "$page")](${page%%.sh})" | hoedown
    done
    cat <<EOF | hoedown

## Static content

Files that don't end in *.sh are automatically copied into the output
directory, like this one:

![](/images/mio.png)
EOF
}

. _templates/layout.html.sh
cp index.html.sh "$outputdir"/index.html.sh

A  => example/page-one.html.sh +10 -0
@@ 1,10 @@
function content() {
  cat <<EOF | hoedown
# Page one

This is a test page.

[back](/)
EOF
}
. _templates/layout.html.sh

A  => example/page-two.html.sh +10 -0
@@ 1,10 @@
function content() {
  cat <<EOF | hoedown
# Page two

This is a test page.

[back](/)
EOF
}
. _templates/layout.html.sh

A  => lighttpd.conf +15 -0
@@ 1,15 @@
server.port		        = 8080
server.document-root    = "_site"
index-file.names	= ( "index.html" )
mimetype.assign		= (
				".html" => "text/html",
                ".html.sh" => "text/plain",
				".txt" => "text/plain",
				".css" => "text/css",
				".js" => "application/x-javascript",
				".jpg" => "image/jpeg",
				".jpeg" => "image/jpeg",
				".gif" => "image/gif",
				".png" => "image/png",
				"" => "application/octet-stream"
			)

A  => wersh +80 -0
@@ 1,80 @@
#!/bin/sh
set -e

function usage() {
    echo "Usage: $0 input/ output/"
    exit 1
}

_single=
_watch=
_n=0
for arg in "$@"
do
    case $arg in
        -s)
            _single=t
            _n=$(($_n + 1))
            ;;
        -w)
            _watch=t
            _n=$(($_n + 1))
            ;;
        -*)
            usage
            ;;
    esac
done
[ $_n -ne 0 ] && shift $_n

function escape() {
    sed -e 's/</\&lt;/g' -e 's/>/\&gt;/g'
}

if [ -n "$_single" ]
then
    [ $# -ne 4 ] && usage
    root=$(readlink -f $1)
    outputdir=$(readlink -f $2)
    output=$3
    _input=$(readlink -f "$4")
    cd $root
    [ -e "$root"/_config.sh ] && . "$root"/_config.sh
    . "$_input"
elif [ -n "$_watch" ]
then
    [ $# -gt 2 ] && usage
    root=${1:-.}
    $0 "$@"
    while inotifywait -qqre close_write "$root"
    do
        sleep 1
        $0 "$@"
    done
else
    [ $# -gt 2 ] && usage
    root=${1:-.}
    _outputdir=${2:-_site}
    if [ ! -d $_outputdir ] && [ -e $_outputdir ]
    then
        echo "$_outputdir exists and is not a directory"
        exit 1
    fi
    find "$root" -type f -name "*.sh" -not -path "*/_*/*" -not -name "_*" | \
    while IFS= read -r _input
    do
        _output=$_outputdir${_input#$root}
        _output=${_output%.sh}
        echo "Generating $_output from $_input"
        mkdir -p $(dirname "$_output")
        $0 -s "$root" "$_outputdir" "$_output" "$_input" > "$_output"
    done
    find "$root" -type f -not -name "*.sh" -not -path "*/_*/*" -not -name "_*" | \
    while IFS= read -r _input
    do
        _output=$_outputdir${_input#$root}
        echo "Copying $_input to $_output"
        mkdir -p $(dirname "$_output")
        cp "$_input" "$_output"
    done
fi