Highlighting source code in WordPress with Pygments

So I was playing around with the rather nice SyntaxHighlighter WordPress plugin. It worked pretty nicely. But it had some issues, IMO:

  1. It repurposes the class attribute to act like a pseudo-CSS ruleset with custom rules. To me that’s just ugly.
  2. It does all its work client-side. For some things that’s a nice touch, but for a blog with mostly static content it’s wasted time.
  3. It does almost everything in JavaScript. I have a personal distaste of working with JavaScript for no especial reason whatosever.
  4. It’s LGPLv3. I prefer less restrictive licenses for OSS.

On the other hand, I know the Pygments syntax highlighter pretty well, and I already had a written style plugin for it to get the syntax coloring I like. But there’s no mature Pygments plugin for WordPress that I could find. So I did what any good programmer would do and set out to write my own. I have the working knowledge of PHP (I’m a core dev, for mercy’s sake) and Python to do it with, after all. Not so much WordPress’ plugin API, though, so I took the SyntaxHighlighter plugin as a starting point.

The SyntaxHighlighter plugin was written to support WordPress versions back to something like 2.3, so it did absolutely everything the hard way. It does all manner of arcane hacks to work around issues I don’t understand at all. It hardcoded support for all its “brushes” and “themes”; adding a new one was annoying. The coding style made me cringe, but that’s not much of a complaint; everyone likes their own style. There was a screen or so worth of code to deal with backwards compatibility for an earlier version of the plugin. It used JavaScript to attach its stylesheets to the document instead of outputting them normally, mainly because all the JavaScripts had to process client-side before it knew what stylesheets were needed, so it had to be done in the footer. Still, all that embedded JS in PHP… There was the matter of repeating the attribute map three times in the shortcode processor function. And the hardcoding of the HTML for settings when WP has had an API for at least partially auto-generating them since 2.7. I can understand the need to support PHP 5.1, but PHP 4? I’ll have no truck with that.

So in I dug… ripped out all the JS, rewrote it to use the modern Settings API, tore out the backwards-compat stuff, added the ability to call out to Pygments (using the pygmentize commandline interface), cleaned up the shortcode parser a little… well, long story short, I got it to work decently okay. Not quite as elegant as the original, but it works. I still don’t know how much of the remaining code from the original plugin is nothing but cruft, since there are too many WordPress features I just don’t use (such as the visual post editor)!

I also added an option to disable the automatic use of the wpautop filter in WordPress. This means that finally I’m writing truly raw HTML in the HTML editor. No more creating paragraph and line break tags for me instead of letting me do it myself. Ha! Of course, this meant that a couple of hacks the code does to avoid wpautop processing were making things worse instead of better, but that was easily fixed.

I am not releasing this plugin’s code in any form whatsoever until I can contact the author of the SyntaxHighlighter plugin and resolve any potential licensing issues. There’s enough of his code left in the plugin for there to be a significant issue; I didn’t use his code as an example, I took his code and heavily modified it. If by some strange coincidence Alex Gorbatchev, Andrew Ozz, or Viper007Bond, the original authors named by the plugin, are reading this, I invite comment from them.

Here are some screenshots of the plugin’s settings screen:

Pygments Highlighter Settings, Page 1

Pygments Highlighter Settings, Page 1


Pygments Highlighter Settings, Page 2

Pygments Highlighter Settings, Page 2

8 thoughts on “Highlighting source code in WordPress with Pygments

    1. Gwynne Raskind Post author

      I’m afraid not; I never got around to working out whether I was able to do so under the terms of the license of the original author of SyntaxHighlighter, the plugin I based it on. But given the interest that’s been shown, I’ll look into it sometime soon :).

      Reply
  1. Mike

    The licensing information is at the bottom of this page, where it has been for years. There’s nothing stopping you as far as licensing is concerned:

    http://alexgorbatchev.com/SyntaxHighlighter/about.html

    “License
    You may use any SyntaxHighlighter project under the terms of either the MIT License or the GNU General Public License (GPL) Version 3.

    The MIT License is recommended for most projects. It is simple and easy to understand and it places almost no restrictions on what you can do with a SyntaxHighlighter project.

    If the GPL suits your project better you are also free to use a SyntaxHighlighter project under that license.

    You don’t have to do anything special to choose one license or the other and you don’t have to notify anyone which license you are using. You are free to use a SyntaxHighlighter project in commercial projects as long as the copyright header is left intact.”

    Reply

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>