Tuesday 26th September, 2006

Keep it under your hat, old man.

posted by Rob Mitchelmore, 00:18 (anchor)
Saturday 16th September, 2006


posted by Rob Mitchelmore, 20:02 (anchor)
Friday 8th September, 2006
The Emo Pirate boards the ship via an improvised grappling hook made from an electric guitar. His men then climb up his fringe onto your deck. He takes out his iPod headphones and paces up and down dramatically.

He thinks aloud about killing you, but then decides that it is more of a punishment to let you continue to live in this vale of tears. Especially on a boat. He hates boats.

He thinks aloud also about robbing you of your treasure, but he knows that this would not fill his empty and misunderstood soul, so it isn't worth the effort.

He cries a bit, and then wanders off.

Abigail pointed out the reason for Emo Pirate being so miserable; he is inefficient, and wears his fringe over his left eye, while wearing his eyepatch over his right eye. This makes it almost impossible for him to see where he is going.

After colliding painfully with the mast twenty-three times in five days, one is bound to acquire a somewhat melancholy view on the question of free will versus determinism.

posted by Rob Mitchelmore, 15:02 (anchor)
Monday 4th September, 2006
It is possible to indent code sensibly using CSS and still have lines wrap in a moderately readable fashion. This example will emulate the following style of ccode formatting:

if ((a long expression) &&
(a long expression)) {
test test test;
test test test;

That is - continuations of lines are indented by a fixed amount relative to the 'main' line, and blocks are indented more (usually with a tab).

This is trivially implementable using the CSS text-indent property. An algorithm to format code is shown below:

  1. for each line do:
    1. replace tabs in line with spaces
    2. count initial spaces in line
    3. add indent to it
    4. convert special characters in code to html entities
    5. trim leading spaces and put code in div with style="padding-left: indent + depth em; text-indent: -indent em;
  2. wrap indented code in a div with overall formatting and output

In PHP this could be coded something along the lines of:

$tabwidth = 4;
$indent = 2;

function indenttastic($code) {
        global $tabwidth, $indent;

        $ar = explode("\n", str_replace("\t",
                str_repeat(" ", $tabwidth), $code));
        $newcode = "";

        foreach ($ar as $line) {
                $ln = ltrim($line);
                $depth = strlen($line) - strlen($ln);
                $nl = sprintf('<div style="padding-left: %d em;' .
                        ' text-indent: %d em">%s</div>' . "\n",
                        $depth + $indent, -$indent,
                $newcode .= $nl;

        return "<div class=\"code\">" . $newcode . "</div>";

(please note this code is untested, and does not deal with the case of having multiple contiguous spaces in the middle of lines, which HTML will compress into invidual spaces).

If some knowledge of language structure is held at layout-time, then the indent length could be altered to be the same length as the first lexical token in the line followed by any whitespace.

Also note that 1em indent may not correspond to one space-width. Experimentation may be necessary.

posted by Rob Mitchelmore, 02:30 (anchor)
Saturday 2nd September, 2006


posted by Rob Mitchelmore, 21:16 (anchor)
Friday 1st September, 2006
It is possible to add support for emoticons (also known as smileys) to a Blogger weblog, so long as it is hosted on a webhost that supports PHP (using the FTP or SFTP upload options provided by Blogger). This technique has not been tested using the Blogger beta. It uses the PHP str_replace function and the "here document" syntax.

Always take a backup of your blog template before editing it. This is important.

Please note that it is important to roughly understand what the code presented here is doing before using it; it is a bad habit to import code entirely blindly. In addition, this technique is not very efficient. It is merely adequate.

To begin with, it is important that the pages are being published as .php files. To check this, look under Settings, then under Publishing, and look for the Blog Filename option. The file name should end in ".php":


Also look under Settings, Archives; the archive file name should also end in .php:


If these needed to be changed, then republish the blog and check that the PHP pages work. If the server returns the source code rather than a formatted page, then PHP is set up incorrectly. Once this is working, remove and archive the old .html blog and archive files.

Once this has been done and the blog is behaving as it was before, create a file emote.php. This should contain the following:

        $emotes = array();

        function emotify($post) {
                global $emotes;
                $p = $post;
                foreach($emotes as $txt => $url) {
                        $p = str_replace($txt,
                                '<img src="' . $url . '" alt="' .
                                htmlentities($txt) . '" />', $p);
                return $p;

Upload this to, or create this in, the root directory of the blog, with the main blog file (index.php or whatever was entered in the file name box earlier). Then edit the template and add the following line to the very top of the template:

<?php include("/full/path/to/emote.php"); ?>

replacing the /full/path/to with the actual filesystem path of emote.php. Then republish the blog to ensure that it is still working. If errors are raised, they are probably in emote.php; make sure that your copy of emote.php is correct.

If all is correct up to this stage, edit the template of the weblog again. Find the line that contains <$BlogItemBody$>. If the tag is not on a line on its own, then insert linebreaks before and after it so that it is.

Then add the following line before it:

<?php $p = <<< EOP<$BlogItemNumber$>

and the following lines after it:

print emotify($p);

It is very important that the line beginning EOP has no spaces or other characters before it.

Once this is done, republish the blog again, and ensure that it works exactly as it did before.

Create an emoticons directory in the root of the blog with emote.php. Upload the emoticon images into this directory. To activate them, edit the first line of emote.php. Each emoticon should have an entry consisting of a piece of text and an image url, which should point to your emoticons directory. For example:

":)" => "/emoticons/smiley.png"

These definitions should be separated by commas; thus, the first lines of emote.php might look like:

        $emotes = array(":)" => "/emoticons/smiley.png",
                        ";)" => "/emoticons/wink.png",
                        "<3" => "/emoticons/heart.png");

These emoticons will then be substituted into posts when used.

posted by Rob Mitchelmore, 17:02 (anchor)
June 2015May 2015April 2015June 2014
January 2014November 2013October 2013July 2013
April 2013March 2013January 2013November 2012
older posts