A simple static site generator.
Static is a command line tool that takes a child document and inserts it into a parent document, and it will do it recursively.
A parent document looks like this:
<html>
<head>
<title>[[title]]</title>
</head>
<body>
<h1>[[bigheader]]</h1>
[[content]]
</body>
</html>And this is a child document:
[[parent:template_default.html]]
[[block:title]]Interesting Page[[/block]]
[[block:bigheader]]Hello Everybody[[/block]]
[[block:content]]
<p>This is some <emphasis>very</emphasis> interesting content.</p>
<p>Blah, blah blah. I love the sound of my own voice.</p>
[[/block]]
<p>Anything outside of a block tag will be ignored.</p>In the parent document, fields are marked with two square brackets. The names inside of the square brackets correspond with the name after the : inside of the template tag.
For example, [[block:somename]] Some content [[/block]], will have
“Some content” inserted into the parent’s [[somename]] field.
All [[block:somename]]...[[/block]] tags will have their value inseted
into the parent’s matching [[somename]], and anything outside of
the tags will be ignored.
An additional feature is that external documents can be
inserted at any point by using a field like this:
[[insert:../dir/somefile.html]].
Also, values can be passed from the command line. By using
--var=KEY::VALUE, value will be inserted into a field that
looks like this: %%KEY%%. This can be used multiple times.
For example:
static child.html ../dist --var='date::10/10/10' --var='myname::Toby Flenderson'
The date and myname values will be inserted into %%date%% and
%%myname%% anywhere those fields appear.
If the filename is a dir, static will compile all the files
there, including any in sub dirs. It defaults to excluding
any file or directory that start with ‘template’, but that
can be changed by using the --ignore-prefix flag. This means
that you can have a directory structure that might look like
this and everything will be compiled but the template* files
will be ignored:
static ./html ../dist
That will compile all the files in html and put them in
../dist, and not iterating over the template* files (but the
non template files can still be referencing the template
files as parents).
html/
home.html
contact.html
products.html
products/
product-a.html
product-b.html
product-c.html
templates/
main.html
footer.html
header.html
or
html/ home.html contact.html products.html product-a.html product-b.html product-c.html template_main.html template_footer.html template_header.html
Usage:
static FILENAME DESTINATION [--var=KEY::VALUE...] [-i IGNORE] [-d]
Options:
--var=KEY::VALUE... This can be specified multiple times. Note that
double colons must be used.
-i PREFIX --ignore-prefix PREFIX
Static will ignore any file starting with this
prefix, whether it is a file or directory
-d --do-not-write Output result to screen instead of writing the
file to the filesystem
Example:
html-builder --var='name::John Brown' --var=phone::1234567 \
content.html ~/projects/site ~/projects/src/html/
Overview:
Compile one or all the files in a dir (excluding any set with the -i
switch). If the filename is a single file, compile only one, if
its a dir, compile all. The default is to ignore any files starting
with 'template_'.
This takes a child document and inserts it into a parent document.
The child document is the FILENAME and the parent is set in the child
document using [[parent:some/file/name]]
Values are pass from the child document to the parent by writing
template tags like this:
[[block:some_name]]value[[/block]]
In the parent the value of some_name is put into this field:
[[some_name]]
The --var=some_name::value field on the command line will be sent to
the %%some_name%% field in the parent.
FILENAME and DESTINATION are required.
var is an optional key:value formated string that contains replacement
values for placeholder strings in the html file. The placeholder
looks like this: %%placeholder%%
FILENAME: Source filename (the child document).
DESTINATION: Destination directory for compiled html if not specified
by the <template:destination> tag in the child document.
If you use Emacs, you’ll find this code useful.
This function automatically runs static on any child documents when saved, and runs static on all the files when a template file is saved.
(defun static/compile()
"If a buffer is using web-mode, it will call static to compile it"
(interactive)
(if (string= (format "%s" mode-name) "Web")
(let* ((this-file
(buffer-file-name
(current-buffer)))
;If file name starts with this it's a template
(template-marker "template_")
;Get the length of marker
(end-compare (length template-marker))
;Test if this file is a template
(is-template
(compare-strings
(file-name-nondirectory this-file) 0 end-compare
template-marker 0 end-compare)))
(if (booleanp is-template)
(setq this-file (file-name-directory this-file)))
(setq cmd (format "~/bin/static %s" this-file))
(shell-command cmd)
(message cmd)
)))
(add-hook 'after-save-hook static/compile)