Harder than I thought

July 30, 2009  |  Posted in: Programming  |  0 comments

Tags: / / /

These days writing a simple blog app has become the ‘hello world’ of web programming. They always make it seem so simple, because hey, blogs are just posts with comments and tags right? Right?! Yes, I decided to write my own blogging engine for this site. At the end of the day it was (and still is) an excellent exercise in really learning a framework. But it certainly wasn’t a 20-minute exercise. 

Why would you do such a thing?

Yes I know, the world is littered with blogs of all shapes and sizes. But I have a need to really understand how things work, and what better way than to build something yourself? Plus, now I have an ongoing project that will need to be constantly tweaked, patched and optimized. I’ve done a few WordPress installs and mods, so I’m pretty familiar with what a blogging app should be.

And that was the first problem. Once you get past Posts/Comments/Tags you’re faced with a whole litany of other items. How do I handle comment spam? Implement search on the posts? What do I do with trolls? Caching? Oh yeah, I have to write administrative tools for this as well.

And so begins a software project.

I wrote this site using CakePHP. It’s the PHP MVC framework that I use for most of my projects. I’m already comfortable using it, and have used it for a number of sites already. Eventually I want to start playing more with Python/Django, but I wanted to build this now. Note: I am not interested in any holy wars pro or con about PHP as a language, so just don’t comment about it. Thanks.

Cake has a lot of functionality out of the box, and its easy to add what you need. Since it’s been around a while, lots of people have written additional plugin code for Cake 1.2 (and I hope a lot of those ideas will make their way into Cake 3’s core.) Since it’s currently my preferred framework I will probably be writing more than a few posts concerning hacks/tricks I’ve been using.

Just In Time Knowledge

Like Jeff Atwood mentioned in one the early Stack Overflow podcasts its not always about the language anymore, its about the framework. He was specifically talking about how jQuery had changed the way people used JavaScript. But I think it extends past that.

I’m not a programmer by trade and would have great difficulty writing a complex application by myself. However, I am very clever when it comes to manipulating frameworks and implementing plugins. I can craft enough ‘glue’ code and bang on the API just enough to get the results I usually want. (If I ever create the next Web 3.0 sensation I’ll be sure to hire some engineers to re-factor the app.)

I knew that I wanted to use Markdown as my syntax language for posts and comments, as I’ve used it for years and love it. So I grabbed PHP Markdown and plugged it into Cake.

There would be a lot of code snippets. So I needed a way to prettify the code. This one caused me a bit of a headache. At first I started with GeSHi but didn’t like that you had to explicitly state the language in your code block. It was too complex. I had the same problem with SyntaxHighlighter. I wanted something language agnostic and dead simple to use. After thrashing about for a while I stumbled across Google Code Prettify and more importantly Ryan Tomayko’s modifications to it. Combined with Markdown, Prettify would gussy up all my code blocks like so:

function quicksort($seq) 
{
    if(!count($seq)) return $seq;

    $k = $seq[0];
    $x = $y = array(); 
    $length = count($seq);

    for($i=1; $i < $length; $i++) 
    {   
        if($seq[$i] <= $k) 
        {
            $x[] = $seq[$i];
        } 
        else 
        {
            $y[] = $seq[$i];
        }
    } 
    return array_merge(quicksort($x), array($k), quicksort($y));
}

Note: Don’t ever use that Quicksort, in PHP it’s already implemented with C in the sort() function

To really seriously cleanup user input I grabbed a copy of HTMLPurifier and plugged that in to complement Cake’s Sanitize Class.

And so on. In short, I was able to quickly find excellent solutions written by good coders that I could use for my application. Yes, I have something of a Frankenstein code-base, but it works. With some clever caching I think it will be reasonably performant as well (knocking on wood now…)

Before the web frameworks showed up I wouldn’t even have considered trying to build an app like this. I wouldn’t have started learning to code, which would have been a shame as it turns out I really like doing it. But then, I’ve always been a late bloomer.

Make a comment

Privacy: I will never give/sell/share your email address with anyone, ever. I need it to help crack down on spam, and contact you if I have a question about your comment that would be best handled in private.

A red label and/or an asterisk (*) indicates a required field

this will never be made public
(optional)

Preview:

Recent posts

Git Down

0 Comments :: August 21, 2010

It's been a long time coming.

0 Comments :: February 01, 2010

Array.prototype for fun and profit

1 Comments :: January 14, 2010

Ruby to the rescue

0 Comments :: December 04, 2009

About that tablet

0 Comments :: August 30, 2009

Search the posts

Categories

Browse around

© 2010 Darren Newton, all rights reserved - Revision 334