SyHi: my minimalistic syntax highlighting plug-in for WordPress

I had been using Code Snippet for a year, or a couple of years (I can't really remember), and was more or less happy with it. Apart from the fact that it didn't preserve some stuff properly, like double dashes and quotes. So then I tried adding another plug-in to the mix, Preserve Code Formatting. But both at the same time didn't work out as I expected hoped.

The solution?

I made a new plug-in. It's very minimalistic, starting with its name: SyHi.

How does it work?

The problem with other plug-ins is that they apply their formatting and then let WordPress continue modifying the posts' text. That's a totally bad idea, and that's why quotes and dashes were systematically altered. On the other hand, this little clever plug-in takes the code blocks apart, sets them aside while leaving placeholder text where they were, and when WordPress has finished with all its filtering and texturizing, SyHi replaces the placeholder texts with the preserved, syntax highlighted beatiful pieces of code that the author entered.

The resulting code not only is nice to look at, but can be copied and pasted into a compiler and it will work with no modifications at all. No more please replace em dashes with double hyphens because WordPress modified the snippet's output...: your blog readers can now simply copy and paste!

Alphatesters needed

So far I have tested SyHi with a test blog and this blog too. It looks fine, but I would be more than happy if somebody else wants to have a go at testing it. I would even add you to the Thanks section!

Unfortunately, the plug-in is not yet available in the wp-plugins directory; I'm waiting for them to approve my request so that you can install it from within the plugins admin page.

Plug-in page at WordPress directory: SyHi. You can also clone the SyHi github repository or download the latest version from there.


So you don't believe me when I say this plug-in works great?

Using Python

noise_output ='noise.wav', 'w')
noise_output.setparams((2, 2, 44100, 0, 'NONE', 'not compressed'))

for i in range(0, SAMPLE_LEN):
        value = random.randint(-32767, 32767)
        packed_value = struct.pack('h', value)


What about php?

class SyHi
    protected $code_blocks;
    protected $geshi_instance = null;

    public function __construct()
        // Execute pre and post process functions before and after each post content is processed
        add_filter('the_content', array(&$this, 'pre_process'), 2);
        add_filter('the_content', array(&$this, 'post_process'), 1000);

        // Same for each comment
        add_filter('comment_text', array(&$this, 'pre_process'), 2);
        add_filter('comment_text', array(&$this, 'post_process'), 1000);

        // Add the css stylesheet link to the <head>
        add_action('wp_head', array(&$this, 'add_css'), 1);

OK and what about C/C++?

void luisita_reportErrors(lua_State *L, int st)
    if (st != 0)
        std::cerr << "ERROR -- " << lua_tostring(L, -1) << std::endl;
        lua_pop(L, 1); // removes error message

As you can see, double dashes are being respected, quotes are kept as they are, and code is nicely highlighted. And if you're still feeling skeptical, keep browsing posts in this blog, since all code snippets go through SyHi.

Update: added the link to the plug-in's repository :)

Update 2: let me clarify something: this plug-in can beautify snippets in pretty much every programming language you can think of, thanks to the huge language support provided by GeSHi. I have shown you only three examples as a simple demonstration, but you can even show Z80 assembler code!

The full list of supported languages is in the left column of the GeSHi website, under the Supported Languages header. Heck, it even has support for a language called BrainFuck!