@madpilot rants

Indenting highlighted source code with WordPress 2.0

Ok, after going away and thinking about the problem, I came up with a quick and dirty (But seemingly effective and compliant) way of doing the indenting. It does require a small modification to the GeSHi code. I’m going to use an empty inline tag (ins tag – which stands for insert – how appropriate…)

Open the geshi.php file (See my previous post about code highlighting in WordPress 2.0 if you have no idea what I’m talking about) and find the indent function. Search for all the nbsp elements and replace with:

  1. < ins class=“in” > < /ins>

Now open the insertcode.css file and the style.css (in your theme) and add:

  1. ins.in {margin-left: 15px;}

Voila! indenting. The output looks like this:

  1. < ?php
  2. echo “hello” . world();
  3. function world() {
  4. return “world”;
  5. }
  6. ?>

A few more notes: html and xml highlighting is pretty well broken unless you add a erroneous space between the angle bracket and the tag (See this example) and don’t try to use special characters. I’m currently tweaking the colour coding classes to make them look nicer too. I’ll post these when they are done.

4 comments

  1. Pingback: Bloggy Hell » Blog Archive » WordPress 2.0 mod: Insert highlighted code.
  2. There's a little problem with that, at least in the current GeSHi version I use (1.0.7.14): It's not a simple matter of replacing non-breaking spaces with the code you provided, since that simply messes up the result.

    The affected code is this:



    $strs = array(0 => ' ', 1 => ' ');



    and further down:

    // Other whitespace

    $result = str_replace(' ', '  ', $result);

    $result = str_replace(' ', '  ', $result);

    $result = str_replace("n ", "n ", $result);



    Replacing blindly doesn't work, adding them to the $result does produce extremely odd effects, certainly not what I see on your blog.
  3. And of course all the nbsp's got stripped while posting...
  4. Nevermind, I figured it out. Sorry for the noise.

Leave a comment