<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom">
	<title>Words on display - David Högberg</title>
	<link rel="self" href="http://david.hgbrg.seatom.xml" type="application/atom+xml" />
	<updated>2011-11-12T18:13:35+01:00</updated>
	<id>http://david.hgbrg.seatom.xml</id>

	<author>
		<name>David Högberg</name>
		<uri>http://david.hgbrg.se</uri>
	</author>

	<entry>
		<title>back from the future</title>
		<link href="http://david.hgbrg.sereturn.html.html" rel="alternate" type="text/html" />
		<id>http://david.hgbrg.sereturn.html.html</id>
		<updated>2011-11-12T18:13:32+01:00</updated>
		<content type="html">
			&lt;p&gt;the future you think you know is never really it. oh i've lived in so many imaginary futures; misty landscapes of hopes and wishes. always inside the illusion, the fear of stepping outside strong enough to touch but too subtle to grasp. right there, feel it? just above your stomach. like a knot, curled so dense it's almost black. instinct keeping it tight, protecting. i've woken up after days inside ... years ... who knows? looking back at the hollowness of it.&lt;/p&gt;
&lt;p&gt;this permeating psychosis is sometimes real, sometimes unreal, often everything, subtly massive, deceptively embracing.&lt;/p&gt;
&lt;p&gt;and then: the moments of clarity, the braveness to stop, of letting go, one instinct pitched against another. expansion; a sense of strings unwinding, of strands breaking, tearing apart.&lt;/p&gt;
&lt;p&gt;but it never quite breaks; an entire universe just outside your reach. the feeling of being tangled up in a vast web of stories, the sense of those endless loops of recursion just outside your consciousness.&lt;/p&gt;
		</content>
	</entry>	
	<entry>
		<title>Building a minimal blogging engine</title>
		<link href="http://david.hgbrg.seexpress2.html.html" rel="alternate" type="text/html" />
		<id>http://david.hgbrg.seexpress2.html.html</id>
		<updated>2011-11-10T15:08:46+01:00</updated>
		<content type="html">
			&lt;p&gt;A short description of &lt;a class=&quot;reference external&quot; href=&quot;http://github.com/dfh/express2&quot;&gt;Express 2&lt;/a&gt;, an
extremely minimal blogging engine that took me like two years to build.&lt;/p&gt;
&lt;p&gt;As any computer geek or DIY:er can attest, there's something about making
things yourself. Sometimes it's a really bad idea, but in the end, at least you
come out of it learning something. In this case I have learnt that simplicity
is really hard and that it requires careful thinking up front. I.e., design
before implementation.&lt;/p&gt;
&lt;div class=&quot;section&quot; id=&quot;what-is-express-2&quot;&gt;
&lt;h1&gt;What is Express 2?&lt;/h1&gt;
&lt;p&gt;Express 2 is a small piece of software with one objective:&lt;/p&gt;
&lt;ul class=&quot;simple&quot;&gt;
&lt;li&gt;Publish timestamped articles with tags, in HTML and as an Atom feed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;That is, a simple blog. Express 2 does this by generating static HTML files
from a collection of entries stored in plain text files.&lt;/p&gt;
&lt;p&gt;It is written in PHP, because that is what I know (which is a pretty good
argument, if you ask me). The complete source code can be found at
&lt;a class=&quot;reference external&quot; href=&quot;http://github.com/dfh/express2&quot;&gt;http://github.com/dfh/express2&lt;/a&gt; and you are free to use it as you wish.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;section&quot; id=&quot;how-does-it-work&quot;&gt;
&lt;h1&gt;How does it work?&lt;/h1&gt;
&lt;p&gt;You put your entries in plain text files in &lt;tt class=&quot;docutils literal&quot;&gt;content/&lt;/tt&gt;, then run
&lt;tt class=&quot;docutils literal&quot;&gt;express2&lt;/tt&gt; (from the Express 2 directory):&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;
./express2
&lt;/pre&gt;
&lt;p&gt;This will build the blog and put the resulting HTML in &lt;tt class=&quot;docutils literal&quot;&gt;build/&lt;/tt&gt;, which you
then can put on your web server however you see fit.&lt;/p&gt;
&lt;p&gt;More details follow.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;section&quot; id=&quot;core-architecture&quot;&gt;
&lt;h1&gt;Core architecture&lt;/h1&gt;
&lt;p&gt;Let's begin with the directory structure. This is the Express 2 root
directory:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;
build/
content/
static/
templates/
express2
&lt;/pre&gt;
&lt;dl class=&quot;docutils&quot;&gt;
&lt;dt&gt;&lt;tt class=&quot;docutils literal&quot;&gt;build/&lt;/tt&gt;&lt;/dt&gt;
&lt;dd&gt;The generated content. This is what will be served by the web server.&lt;/dd&gt;
&lt;dt&gt;&lt;tt class=&quot;docutils literal&quot;&gt;content/&lt;/tt&gt;&lt;/dt&gt;
&lt;dd&gt;All entries, as plain text files. More on this later.&lt;/dd&gt;
&lt;dt&gt;&lt;tt class=&quot;docutils literal&quot;&gt;static/&lt;/tt&gt;&lt;/dt&gt;
&lt;dd&gt;Static resources like images, CSS and JavaScript. Will be copied to the
&lt;tt class=&quot;docutils literal&quot;&gt;build/&lt;/tt&gt; directory.&lt;/dd&gt;
&lt;dt&gt;&lt;tt class=&quot;docutils literal&quot;&gt;templates/&lt;/tt&gt;&lt;/dt&gt;
&lt;dd&gt;Templates for HTML/Atom.&lt;/dd&gt;
&lt;dt&gt;&lt;tt class=&quot;docutils literal&quot;&gt;express2&lt;/tt&gt;&lt;/dt&gt;
&lt;dd&gt;The Express 2 engine. Execute to generate content.&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;Express 2 stores entries in plain text files. This lets me use any text editor
to write and edit content (currently &lt;a class=&quot;reference external&quot; href=&quot;http://macvim.org/&quot;&gt;MacVim&lt;/a&gt;). Entries
consist of metadata and content, represented in an HTTP-style format. In
addition, there is support for writing the entry content in either plain HTML
or using ReST markup that will be automatically converted to HTML (this
conversion is made by what I call a &lt;em&gt;content parser&lt;/em&gt;).&lt;/p&gt;
&lt;p&gt;Let's illustrate this by creating an entry: &lt;tt class=&quot;docutils literal&quot;&gt;&lt;span class=&quot;pre&quot;&gt;content/1-hello_world.html&lt;/span&gt;&lt;/tt&gt;:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;
Title: Hello world
Tags: hello, world
Created: 2011-11-06 10:00
Updated: 2011-11-06 11:00
Parser: html
Template: default
Url: hello_world.html

&amp;lt;strong&amp;gt;Hello&amp;lt;/strong&amp;gt;, World!
&lt;/pre&gt;
&lt;p&gt;If you are familiar with the &lt;a class=&quot;reference external&quot; href=&quot;http://www.ietf.org/rfc/rfc2616.txt&quot;&gt;HTTP protocol&lt;/a&gt;, you will instantly recognize this.
Metadata is given as key-value pairs, followed by the content of the entry,
separated by exactly one blank line. Neat thing about this is that it allows me
to use an existing parser, in this case the &lt;tt class=&quot;docutils literal&quot;&gt;http_parse_message()&lt;/tt&gt; function
from the &lt;a class=&quot;reference external&quot; href=&quot;http://pecl.php.net/package/pecl_http&quot;&gt;PECL HTTP extension&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The different headers pretty much explain themselves. The &lt;tt class=&quot;docutils literal&quot;&gt;template&lt;/tt&gt; and
&lt;tt class=&quot;docutils literal&quot;&gt;parser&lt;/tt&gt; headers define what template to use and what parser to use for the
content. In this case, the template &lt;tt class=&quot;docutils literal&quot;&gt;templates/default.html.php&lt;/tt&gt; will be
used, and the content will be parsed by the function named &lt;tt class=&quot;docutils literal&quot;&gt;parse_html()&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;tt class=&quot;docutils literal&quot;&gt;Url&lt;/tt&gt; header define the &lt;em&gt;relative&lt;/em&gt; URL. That is, when I upload the
generated content to my blog, the URL of this entry will be
&lt;a class=&quot;reference external&quot; href=&quot;http://david.hgbrg.se/blog/hello_world.html&quot;&gt;http://david.hgbrg.se/blog/hello_world.html&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Some headers are given default values however, so the above could be shortened
to:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;
Title: Hello world
Tags: hello, world

Hello, World!
&lt;/pre&gt;
&lt;p&gt;The &lt;tt class=&quot;docutils literal&quot;&gt;Created&lt;/tt&gt; and &lt;tt class=&quot;docutils literal&quot;&gt;Updated&lt;/tt&gt; headers will default to the created/modified
timestamps of the file, &lt;tt class=&quot;docutils literal&quot;&gt;Parser&lt;/tt&gt; will be set according to the file extension
(&lt;tt class=&quot;docutils literal&quot;&gt;.html&lt;/tt&gt; -&amp;gt; the html parser in this case), and the &lt;tt class=&quot;docutils literal&quot;&gt;Url&lt;/tt&gt; will be set
from the filename using this regexp:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;
[0-9\-]([a-zA-Z0-9\-._]+)\.[a-z]+
&lt;/pre&gt;
&lt;p&gt;That is: in this case, the URL will be &lt;tt class=&quot;docutils literal&quot;&gt;hello_world.html&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Also, header names are case insensitive.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;section&quot; id=&quot;building&quot;&gt;
&lt;h1&gt;Building&lt;/h1&gt;
&lt;p&gt;Generating the static content is as easy as executing the &lt;tt class=&quot;docutils literal&quot;&gt;express2&lt;/tt&gt; file:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;
./express2
&lt;/pre&gt;
&lt;p&gt;It is made executable using the magic hashbang:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;
#!/bin/php
&lt;/pre&gt;
&lt;p&gt;If the &lt;tt class=&quot;docutils literal&quot;&gt;&lt;span class=&quot;pre&quot;&gt;-v&lt;/span&gt;&lt;/tt&gt; flag is provided, debugging info will be logged via syslog to
&lt;tt class=&quot;docutils literal&quot;&gt;/var/log/php.log&lt;/tt&gt; (I simply configured syslog to direct &lt;tt class=&quot;docutils literal&quot;&gt;LOG_LOCAL2&lt;/tt&gt;
messages to &lt;tt class=&quot;docutils literal&quot;&gt;/var/log/php.log&lt;/tt&gt;).&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;section&quot; id=&quot;templating&quot;&gt;
&lt;h1&gt;Templating&lt;/h1&gt;
&lt;p&gt;One of my requirements for the Express 2 engine was support for different
templates, to make it possible to give different entries different layouts
(might come in handy). As a plus, it is an adequate separation between logic
and presentation.&lt;/p&gt;
&lt;p&gt;As PHP basically is a fancy templating language, and I know it, there is no
need for an extra abstraction like &lt;a class=&quot;reference external&quot; href=&quot;http://www.smarty.net/&quot;&gt;Smarty&lt;/a&gt; or even
&lt;a class=&quot;reference external&quot; href=&quot;http://mustache.github.com/&quot;&gt;Mustache&lt;/a&gt;. Including a PHP file with HTML
content interspersed with simple logic and output of variables is sufficient.
(If you want to use a templating language mostly because it has prettier
syntax, get over it.)&lt;/p&gt;
&lt;p&gt;So anyway, the whole templating system consists of exactly one function:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;
function get_template_content( $filename, $context ) {
        ob_start();
        extract( $context );
        require $filename;
        $c = ob_get_contents();
        ob_end_clean();
        return $c;
}
&lt;/pre&gt;
&lt;p&gt;If you are unfamiliar with &lt;tt class=&quot;docutils literal&quot;&gt;extract()&lt;/tt&gt;, it means the elements in the
&lt;tt class=&quot;docutils literal&quot;&gt;$context&lt;/tt&gt; array will be available as variables in the template file. This is
illustrated by the &lt;tt class=&quot;docutils literal&quot;&gt;write_entry()&lt;/tt&gt; function:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;
function write_entry( $entry, $filename ) {
        dbg( sprintf( &amp;quot;Writing entry to '%s' ...&amp;quot;, $filename ) );

        # write by getting content through template file
        $ctxt = array( 'entry' =&amp;gt; $entry );
        file_put_contents( $filename,
                get_template_content( $entry-&amp;gt;template, $ctxt ) );
}
&lt;/pre&gt;
&lt;p&gt;Then, in the template file, the entry content can be displayed like this:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;
&amp;lt;h1 class=&amp;quot;title main-title&amp;quot;&amp;gt;
  &amp;lt;a href=&amp;quot;&amp;lt;?= $entry-&amp;gt;url ?&amp;gt;&amp;quot; title=&amp;quot;Permanent link to this entry.&amp;quot;&amp;gt;&amp;lt;?= $entry-&amp;gt;title ?&amp;gt;&amp;lt;/a&amp;gt;
&amp;lt;/h1&amp;gt;

&amp;lt;date class=&amp;quot;date&amp;quot;&amp;gt;&amp;lt;?= date( 'F d, Y', $entry-&amp;gt;created_on ) ?&amp;gt;&amp;lt;/date&amp;gt;

&amp;lt;section class=&amp;quot;body&amp;quot;&amp;gt;
  &amp;lt;?= $entry-&amp;gt;body ?&amp;gt;
&amp;lt;/section&amp;gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;div class=&quot;section&quot; id=&quot;static-data&quot;&gt;
&lt;h1&gt;Static data&lt;/h1&gt;
&lt;p&gt;Sometimes, it's nice to have an entry with images. Or with a movie, or with any
other type of file. Such things are just static files in the filesystem and are
thus treated as such. They are put in the &lt;tt class=&quot;docutils literal&quot;&gt;static/&lt;/tt&gt; directory.&lt;/p&gt;
&lt;p&gt;Also, any images, CSS and/or JavaScript needed for the templates are treated
the same way.&lt;/p&gt;
&lt;p&gt;When building, the entire content of the &lt;tt class=&quot;docutils literal&quot;&gt;static/&lt;/tt&gt; directory is copied to the
&lt;tt class=&quot;docutils literal&quot;&gt;build/&lt;/tt&gt; directory. So, for example &lt;tt class=&quot;docutils literal&quot;&gt;static/images/hello.png&lt;/tt&gt; ends up in
&lt;tt class=&quot;docutils literal&quot;&gt;build/images/hello.png&lt;/tt&gt;, and &lt;tt class=&quot;docutils literal&quot;&gt;static/css/screen.css&lt;/tt&gt; in
&lt;tt class=&quot;docutils literal&quot;&gt;build/css/screen.css&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Plus, if I ever want to add static pages to the blog, I can just put them in
&lt;tt class=&quot;docutils literal&quot;&gt;static/&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Technically, this copying is easily done by &lt;tt class=&quot;docutils literal&quot;&gt;rsync&lt;/tt&gt;:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;
function sync_images( $src_dir, $target_dir ) {
        $cmd = sprintf( 'rsync -r &amp;quot;%s&amp;quot; &amp;quot;%s&amp;quot;', $src_dir, $target_dir );
        dbg( sprintf( &amp;quot;Syncing images using '%s' ...&amp;quot;, $cmd ) );
        return system( $cmd );
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;div class=&quot;section&quot; id=&quot;implementation-details&quot;&gt;
&lt;h1&gt;Implementation details&lt;/h1&gt;
&lt;p&gt;Let's go over some details in &lt;tt class=&quot;docutils literal&quot;&gt;express2&lt;/tt&gt;.&lt;/p&gt;
&lt;div class=&quot;section&quot; id=&quot;oo-vs-functions&quot;&gt;
&lt;h2&gt;OO vs functions&lt;/h2&gt;
&lt;p&gt;Express 2 is not object oriented. Entries are represented by objects, but
contain only data, no behavior, and are manipulated by regular functions.&lt;/p&gt;
&lt;p&gt;There is no need for OO here. And in general, mixing data with behavior is
a bad idea (which is exactly what OO does). OO gives encapsulation, but so does
functions + namespaces. Thank you &lt;a class=&quot;reference external&quot; href=&quot;http://www.mefeedia.com/watch/24169588&quot;&gt;Rich Hickey&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Each core function in Express 2 takes care of a specific task:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;
build
write_entries
write_entry
write_atom
write_archive_page
sync_images
get_entries
read_entry
&lt;/pre&gt;
&lt;p&gt;I think their names explain their functionality. Then there are some helper
functions, of which the most important are:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;
get_related_entries
sort_entries_by_date
get_template_content
&lt;/pre&gt;
&lt;p&gt;In all, just a handful of functions, as you see. Actually, coming up with
a good set of functions – that is, constructing an adequate abstraction – was
the hardest part, and probably took me twice the time of implementing them.
This is a good thing though: time spent on design is ten times saved in
development. Actually, if you find yourself spending very little time on
design, your design probably sucks and you will pay for it eventually.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;section&quot; id=&quot;id1&quot;&gt;
&lt;h2&gt;Building&lt;/h2&gt;
&lt;p&gt;Building is done by the &lt;tt class=&quot;docutils literal&quot;&gt;build()&lt;/tt&gt; function, which is called when executing
&lt;tt class=&quot;docutils literal&quot;&gt;express2&lt;/tt&gt;:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;
# perform the build process
build( get_entries( __DIR__ . '/content/' ), __DIR__ . '/build/' );
&lt;/pre&gt;
&lt;p&gt;As straight-forward as can be. This is the &lt;tt class=&quot;docutils literal&quot;&gt;build()&lt;/tt&gt; function:&lt;/p&gt;
&lt;pre class=&quot;literal-block&quot;&gt;
function build( $entries, $target_dir ) {
        dbg( sprintf( &amp;quot;Building to '%s' ...&amp;quot;, $target_dir ) );

        $entries = sort_entries_by_date( $entries );

        write_entries( $entries, $target_dir );
        # write latest entry to index.html
        write_entry( $entries[0], path_join( $target_dir, 'index.html' ) );
        sync_images( __DIR__ . '/static/', $target_dir );
        write_atom( $entries, path_join( $target_dir, 'atom.xml' ) );
        write_archive_page( $entries, path_join( $target_dir, 'archive.html' ) );
}
&lt;/pre&gt;
&lt;p&gt;You can understand largely what is happening by just viewing the source above.
I cannot imagine how it can get much simpler than this.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;section&quot; id=&quot;performance&quot;&gt;
&lt;h1&gt;Performance&lt;/h1&gt;
&lt;p&gt;Express 2 is as fast as your webserver can serve static files. This means
a couple of thousand requests per second using nginx on a decent computer,
which is more than you need.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;section&quot; id=&quot;well-that-was-that&quot;&gt;
&lt;h1&gt;Well, that was that&lt;/h1&gt;
&lt;p&gt;That's all there is to it, really. &lt;em&gt;Um, but didn't you say it took two years to
make?&lt;/em&gt; Well, yeah, it only took a couple of days to actually design and build.
But to understand how to build it this simple and not fall in the trap of
over-engineering took me a long time. In comparison, the previous version,
&lt;a class=&quot;reference external&quot; href=&quot;https://github.com/dfh/express&quot;&gt;Express&lt;/a&gt;, is like two orders of magnitude
more complex. And unnecessary complexity is a bad, bad thing.&lt;/p&gt;
&lt;/div&gt;
		</content>
	</entry>	
</feed>

