Fork me on Bitbucket

Tacot

Generate easily your statics web sites

Purpose and features

This project is born from simple need : « I look for web site generator in Python, like Blogofile, Pelicon or Sphinx but with very low level feature » (ask mail on French Python mailing-list).

With Tacot you can generate easily static web sites (generate .html files) from templates files writed in Mako.

Why do that compared with writing raw .html files ?

With Tacot you can easily share common content betwen your pages (examples : header, footer, navigation...) without to duplicate this content in all your files.

There are already many Python static web site generator, like Sphinx, pelican... why reinvent the wheel ?

With Tacot I don’t reinventing the wheel… when I searched a tool like Tacot, I found nothing. I don’t need ReStructredText parser, I don’t want blog generator, I don’t want documentation generator, I don’t want theme feature…

I want only a very simple (low level) tool to convert simple html template files… and I found nothing.

Installation

$ pip install tacot

Quick start

Download demo1.tar.gz archive to demo1 folder.

$ mkdir demos; cd demos
$ curl -o demo1.tar.gz http://packages.python.org/tacot/en/_static/demo1.tar.gz
$ tar xfz demo1.tar.gz
$ cd demo1

This is the archive content :

$ tree
.
├── earth.html
├── includes
│   ├── layout.html
│   └── navigation.html
├── index.html
├── jupiter.html
├── mars.html
├── mercury.html
├── neptune.html
├── pluto.html
├── saturn.html
├── styles.css
├── uranus.html
└── venus.html

Web site generation :

$ tacot
Please wait, tacot process 11 files :

jupiter.html
pluto.html
neptune.html
styles.css
index.html
saturn.html
uranus.html
earth.html
mars.html
venus.html
mercury.html

The site is generated in _build folder :

$ ls _build/ -1
earth.html
index.html
jupiter.html
mars.html
mercury.html
neptune.html
pluto.html
saturn.html
styles.css
uranus.html
venus.html

You can see the web site with your browser :

$ firefox _build

Using

$ tacot --help
usage: tacot [-h] [-o OUTPUT] [-m MANIFEST] [path]

A tool to generate a static web site, with Mako templates.

positional arguments:
  path                  Path where to find the content files

optional arguments:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        Where to output the generated files. If not specified,
                        a directory will be created, named "_build" in the
                        current path (_build by default).
  -m MANIFEST, --manifest MANIFEST
                        Manifest config file (.manifest by default)

By default, Tacot read files in current directory and place the generated content in _build folder:

You can select source files and set output folder with this parameters :

$ tacot -o /var/www/demo/ /tmp/demo1

Here, the template files are in /tmp/demo1 and the web site is generated in /var/www/demo.

.manifest file

Use .manifest file to select files list to process. If empty, .manifest file content this configuration :

global-include *
prune _build/
prune includes/
exclude .manifest

_build and includes folders are ignored.

.manifest file syntax is the same that Python packages MANIFEST.in file, futher information, read this : documentation « The MANIFEST.in template »

Option --autoreload

Use --autoreload option to automatically restart the web site generation process when modification occur in source file tree :

$ tacot --autoreload

CTRL-C to stop the script.

Templates

Template engine

Tacot use Mako template engine.

What files are interpreted by the template engine ?

All files with .html (or .html.mako) extension are interpreted by template engine.

If your .html file don’t use template engine function, then your file will be simply copied to destionation folder, without transformation.

Variables exposed in templates

Tacot expose two variables in templates, root_path and current_page :

  • root_path : is a function to generate relative url to site root, according to current page
  • current_page : retourn current page url, this is useful to select item in navigation

Examples

In /saturn/index.html I use root_path like this :

<a href="${root_path("/")}">Accueil</a>

The root_path will return “../”.

This is particularly useful in include pages, example in navigation :

<ul>
    <li><a href="${ root_path("/mercury.html") }">Mercury</li>
    <li><a href="${ root_path("/venus.html") }">Venus</li>
    <li><a href="${ root_path("/earth.html") }">Earth</li>
    <li><a href="${ root_path("/mars.html") }">Mars</li>
    <li><a href="${ root_path("jupiter/") }">Jupiter</li>
    <li><a href="${ root_path("saturn/") }">Saturn</li>
    <li><a href="${ root_path("/uranus.html") }">Uranus</a></li>
    <li><a href="${ root_path("/neptune.html") }">Neptune</a></li>
    <li><a href="${ root_path("/pluto.html") }">Pluto</a></li>
</ul>

Here, “Jupiter” and “Saturn” are in sub folders. All pages contain navigation include… the navigation links are all relative from the current page.

The current_page variable return the current page url :

% if current_page == 'saturn/index.html':
<p>I'm in "Saturn" page</p>
% endif

Global variables

You can define global variables (example in your layout template file):

<%

g.project = u"My project"
g.author = u"Stéphane Klein"

%>

So, you can use this global like this in your templates :

<h1>${g.project}</h1>

Tips

Classical “layout” file

In includes/layout.html :

<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">

    <title><%block name="title"/></title>
    <link rel="stylesheet" href="${root_path('css/styles.css')}" type="text/css" media="screen" charset="utf-8" />
</head>
<body>
    <div class="sidebar">
        <%include file="navigation.html" />
    </div><!-- sidebar -->
    <div class="content">
        ${self.body()}
    </div><!-- content -->
</body>
</html>

Classical “navigation” file

In includes/navigation.html :

<%def name="item(url, label)">
% if current_page == url:
<span>${label}</span>
% else:
<a href="${root_path(url)}">${label}</a>
% endif
</%def>
<ul>
    <li>${item("index.html", "Solar system")}</li>
    <li>${item("mercury.html", "Mercury")}</li>
    <li>${item("venus.html", "Venus")}</li>
    <li>${item("earth.html", "Earth")}</li>
    <li>${item("mars.html", "Mars")}</li>
    <li>${item("jupiter.html", "Jupiter")}</li>
    <li>${item("saturn.html", "Saturn")}</li>
    <li>${item("uranus.html", "Uranus")}</li>
    <li>${item("neptune.html", "Neptune")}</li>
    <li>${item("pluto.html", "Pluto")}</li>
</ul>

A page that use “layout” file

Example index.html :

<%inherit file="includes/layout.html"/>

<%block name="title">Home</%block>

<h1>Home</h1>

<p>Foo bar…</p>

Contribution

Get source code :

$ hg clone http://bitbucket.org/harobed/tacot

Tickets… : Tacot

License

This is the MIT license: http://www.opensource.org/licenses/mit-license.php

Copyright (c) 2011-2012 Stéphane Klein.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Fork me on Bitbucket