sachleenblogprojectsabout me

back My 4KB Blog Script

Posterous has become really difficult to work with. It's so slow! So I left. Oddly enough, this was the same reason I switched from Tumblr to Posterous in the first place.

Since my first ever PHP project of Kubix Blog1, I haven't done much with blogging until now. It's weird how all of my ideas come to me when I don't have any time to work on them, in this case, the week before finals.

What I have here is a PHP script that takes posts written in Markdown and page templates and outputs a static HTML website. It's small (under 4KB and about 90 lines of code, excluding PHP Markdown) and only includes very basic functionality, but that's all I need it to. Functionality wise, it is nowhere near what other static site generators like Phrozn or Jekyll can do. I'm not even trying to compare them. It's just a simple little thing I wrote for myself.

How it works

Post Structure

All of the posts are written in Markdown. The post structure is as follows:

Title
Date
tag1,tag2,tag3...

Post content

The script works by iterating over all the text files in the posts/ directory and parsing the title, date, tags, and content out of each file. The date is converted into a more friendly format (mmm dd, yyyy) for display so it can be in any format in the file. The tags are simply comma separated and converted into an array by the script. A post slug is also generated from the title. This is used as the filename for the post's generated HTML file.

Any extra media included in posts (images, for example) are in posts/content/. After the script has generated all of the output HTML, it copies the content directory into the publish directory (the directory that will contain all of the published files). My blog script is located outside of the public_html/ directory so I have to copy all of the files into public_html/blog/content/ for it to work.

Templates

Originally I was using RainTPL for the template but decided to get rid of it and just use PHP. I'm using the output control functions to load a template and get the generated HTML. The script then writes this HTML into a file in the publish directory.

Using PHP as the template engine means I don't have to rely on any 3rd party libraries and it's much faster. The template code doesn't look as nice, having to write <? echo($post['title']); ?> as opposed to {$post.title} with RainTPL, but I think it's a small price to pay.

When generating the output files for individual posts, all post data is available to the template. When generating the index, I have it only display a default of three full-text posts and the titles for all older posts.

The template directory is also copied into the publish directory so the CSS, images, and Javascript can be referenced.

Running The Script

I learned about PHP-CLI while working on this project. I knew I could run php scripts using php -f file.php but had no idea they could accept command line parameters and get input from STDIN. Initially I had made this a CLI script but decided against it later on. I do still run it from the command line when I want to publish a new post, but I read the arguments as GET parameters.

The script only has one parameter, anyway, noindex. When this parameter is set, it will do everything but generate a new index.html file. I have this so I can preview posts before publishing them. I simply run php -f publish.php noindex, browse to the URL of the newly created post, and if all is well, run php -f publish.php to generate the site again, this time, with an updated index.

Final Thoughts

I don't really like the idea of having two copies of the content/ directory but I think I'm going to leave it the way it is. I could clear all of the files in the local directory after they've been moved into the publish folder, but then I wouldn't be able to delete the publish folder and regenerate the entire site.

As I said earlier, it's nowhere near what some other scripts can do, but it was a fun project to work on and I learned a few new things.

Download

Download

You're free to do whatever you want with this script. If you find it useful, shoot me an email and let me know.

Update: I'm finally learning Git and put this project up on GitHub! Check it out https://github.com/sachleen/Blog.

  1. Although I wasn't able to find any screenshots of Kubix, it was nice reading about all of the vulnerabilities and exploits people had come up with. It was an ambitious first project, and looking back, I am not surprised it went nowhere. I guess I can say it is a good example of what not to do.