Saturday, April 4, 2015

wheezy template: static website generator

Sometimes you need a quick way to generate a static web site and put it up on a server. Here is how you can generate a static website with wheezy.template. Let's suppose the following structure of the site:
 content/
 `- index.html
  - main.html
  shared/
  `- master.html
   snippet/
   `- script.html
    - widget.html
shared/master.html:
<!DOCTYPE html>
<html lang="en">
<head>
    @def title():
    No Title
    @end
    <title>@title() - My Site</title>
</head>
<body>
    <div id="container">
        <div id="placeholder">
            @def content():
            @end
            @content()\
        </div>
    </div>
    @include('shared/snippet/script.html')
</body>
</html>
shared/snippet/script.html:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
shared/snippet/widget.html:
@def header(msg):
<h1>
    @msg!h
</h1>
@end
index.html:
@extends('shared/master.html')
@import 'shared/snippet/widget.html' as widget

@def title():
Hello, World!
@end

@def content():
    @widget.header('Welcome')
@end

Environment

You need to install wheezy.template package and optional dependency wheezy.html (for whitespace removal and optimized HTML escape):
virtualenv env
env/bin/pip install wheezy.template wheezy.html

Generate

A single page can be generated like this:
env/bin/wheezy.template content/index.html
The result will be displayed on screen and you can redirect it to some file. Here is an example how to do that in a batch:
cd content
mkdir /tmp/site
find . -name '*.html' -not -path './shared/*' \
  -exec sh -c '../env/bin/wheezy.template -w {} \
  > /tmp/site/{}' \;

Context

Optionally you can specify a generation context which can be a file or JSON string:
@require(name)
Hello, @name!
{ "name": "World" }
wheezy.template hello.txt hello.json
You can specify several contexts to override some values:
wheezy.template hello.txt hello.json '{"name": "John"}'

2 comments :

  1. Your project looks quite promising and deserves more attention. But if you want to spread your job and attract fans, you need to do a lot of things. I am glad to help if you want.

    ReplyDelete