The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

StaticVolt - Static website generator

VERSION

version 0.03

SYNOPSIS

    use StaticVolt;

    my $staticvolt = StaticVolt->new;  # Default configuration
    $staticvolt->compile;

METHODS

new

Accepts an optional hash with the following parametres:

    # Override configuration (parametres set explicitly)
    my $staticvolt = StaticVolt->new(
        'includes'    => '_includes',
        'layouts'     => '_layouts',
        'source'      => '_source',
        'destination' => '_destination',
    );
  • includes

    Specifies the directory in which to search for template files. By default, it is set to _includes.

  • layouts

    Specifies the directory in which to search for layouts or wrappers. By default, it is set to _layouts.

  • source

    Specifies the directory in which source files reside. Source files are files which will be compiled to HTML if they have a registered convertor and a YAML configuration in the beginning. By default, it is set to _source.

  • destination

    This directory will be created if it does not exist. Compiled and output files are placed in this directory. By default, it is set to _destination.

compile

Each file in the "source" directory is checked to see if it has a registered convertor as well as a YAML configuration at the beginning. All such files are compiled considering the "YAML Configuration Keys" and the compiled output is placed in the "destination" directory. The rest of the files are copied over to the "destination" without compiling.

YAML Configuration Keys

"YAML Configuration Keys" should be placed at the beginning of the file and should be enclosed within a pair of ---.

Example of using a layout along with a custom key and compiling a markdown "source" file:

"layout" file - main.html:

    <!DOCTYPE html>
    <html>
        <head>
            <title></title>
        </head>
        <body>
            [% content %]
        </body>
    </html>

"source" file - index.markdown:

    ---
    layout: main.html
    drink : water
    ---
    Drink **plenty** of [% drink %].

"destination" (output/compiled) file - index.html:

    <!DOCTYPE html>
    <html>
        <head>
            <title></title>
        </head>
        <body>
            <p>Drink <strong>plenty</strong> of water.</p>

        </body>
    </html>
  • layout

    Uses the corresponding layout or wrapper to wrap the compiled content. Note that content is a special variable used in Template Toolkit along with wrappers. This variable contains the processed wrapped content. In essence, the output/compiled file will have the content variable replaced with the compiled "source" file.

  • custom keys

    These keys will be available for use in the same page as well as in the layout. In the above example, drink is a custom key.

Walkthrough

Consider the source file index.markdown which contains:

    ---
    layout : main.html
    title  : Just an example title
    heading: StaticVolt Example
    ---

    StaticVolt Example
    ==================

    This is an **example** page.

Let main.html which is a wrapper or layout contain:

    <!DOCTYPE html>
    <html>
        <head>
            <title>[% title %]</title>
        </head>
        <body>
            [% content %]
        </body>
    </html>

During compilation, all variables defined as "YAML Configuration Keys" at the beginning of the file will be processed and be replaced by their values in the output file index.html. A registered convertor (StaticVolt::Convertor::Markdown) is used to convert the markdown text to HTML.

Compiled output file index.html contains:

    <!DOCTYPE html>
    <html>
        <head>
            <title>Just an example title</title>
        </head>
        <body>
            <h1>StaticVolt Example</h1>
            <p>This is an <strong>example</strong> page.</p>

        </body>
    </html>

Default Convertors

How to build a convertor?

The convertor should inherit from StaticVolt::Convertor. Define a subroutine named "convert" in StaticVolt::Convertor that takes a single argument. This argument should be converted to HTML and returned.

Register filename extensions by calling the register method inherited from StaticVolt::Convertor. register accepts a list of filename extensions.

A convertor template that implements conversion from a hypothetical format FooBar:

    package StaticVolt::Convertor::FooBar;

    use strict;
    use warnings;

    use base qw( StaticVolt::Convertor );

    use Foo::Bar qw( foobar );

    sub convert {
        my $content = shift;
        return foobar $content;
    }

    # Handle files with the extensions:
    #   .foobar, .fb, .fbar, .foob
    __PACKAGE__->register(qw/ foobar fb fbar foob /);

Inspiration

StaticVolt is inspired by Tom Preston-Werner's Jekyll.

Acknowledgements

Shlomi Fish for suggesting change of licence.

See Also

Template Toolkit

AUTHOR

Alan Haggai Alavi <alanhaggai@alanhaggai.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2011 by Alan Haggai Alavi.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)