The default code highlighter plugin (which is based on AstonishMe code display) bundled with b2evolution is more or less ok. However, I wanted it to be able to highlight something else aside from XML and PHP so I hacked around the code for it to use GeSHi.

Update

The new version is now packaged as a complete plugin.

Download

code_highlight_plugin.zip v 0.3

Installation Steps:

  1. Download GeSHi
  2. Extract geshi.php and geshi folder to [blogroot]/plugins/code_highlight_plugin/
  3. Backup _code_highlight.plugin.php and replace it with this: _code_highlight.plugin.php. Alternatively, here’s also the diff file: _code_highlight.diff.txt.
  4. If you want, you could remove the code_highlight_plugin/highlighters directory.
  5. Reactivate Code Highlight (Go to App Settings->Plugins) if needed.

  1. Delete the previous code highlight plugin folder at [blogroot]/plugins/code_highlight.
  2. Unzip the entire folder to the plugins folder
  3. Reactivate if necessary

Follow up:

Usage

Its use is exactly like the code highlighter plugin, nothing changed. So if you want to highlight C++ code, do something like this:

HTML:

<codeblock lang="cpp"> cout << "hello world!";</codeblock>

Output:

C++:

cout << "hello world!";

More information can be seen in the documentation.

Extra Steps

Note that as of v0.3, this is not anymore needed because GeSHi is already bundled.
GeSHi doesn’t handle unknown languages too well. It spits out error messages which you wouldn’t want your visitors to see. You can make the following modification in geshi.php to prevent this:

Go to line 446, in function get_language_name(). And change this line:

PHP:

return $this->language_data[‘LANG_NAME’]. ‘ (Unknown Language)’;

to this:

PHP:

return $this->language. ‘ (Unknown Language)’;

Then go to line 987 and change this line:

PHP:

if ( GESHI_MAYBE == $this->language_data[‘STRICT_MODE_APPLIES’])

to this:

PHP:

if ( !empty($this->language_data[‘STRICT_MODE_APPLIES’]) && GESHI_MAYBE == $this->language_data[‘STRICT_MODE_APPLIES’])

Known Issues

  • The syntax highlighting itself doesn’t use CSS (though the main code layout does), relying on inline style defintions
  • The rendering code unescapes the escaped code and send it to GeSHi only to be escaped again. It’s inefficient but this may not be a major issuse since results are cached.

Feedback would be appreciated.

Trackback address for this post

Trackback URL (right click and copy shortcut/link location)

3 comments

# Maik Email on 08/09/07 at 11:07
The download of this plugin seems broken!
# Wil [Member] Email on 18/09/07 at 08:14
Sorry, I'll fix the link as soon as possible.
# Jay Email on 25/03/08 at 15:37
Nice plug in. Doesn't seem to highlight javascript correctly, but I think thats a Geshi problem not the plugin (which is working fine on C++.

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)