GeSHify: a GeSHi syntax highlighting extension for Expression Engine

30 . January 2008
written by Clemens Lang at Jan 30th 2008, 21:51

I recently wanted to post some source code on this Expression Engine-powered blog - I thought about some syntax highlighting to improve readability. However, after asking Google and the Expression Engine forums search it turned out there had been several attempts to integrate a syntax highlighter into Expression Engine, but these plugins or extensions were either outdated or not available anymore. *sigh*

Update: GeSHify has moved! Visit it's new home http://geshify.com/. This page is probably outdated!



So I figured out it would end as it always does (notice how I exaggerate here to keep the pathos of the text) - I had to do it my way. And here it is: GeSHify for Expression Engine.
GeSHify supports the following features:



  • Customizable intelligent caching (this one is very important, since GeSHi is not the fastest code highlighter out there)

  • Supports GeSHi 1.0 and GeSHi 1.1 (currently alpha) - and you can even switch between both using some clicks in the control panel

  • Customizable tag (in case you want to keep the original functionality of [ code ] instead of overwriting it, which is the default setting)

  • HTML-argument-style settings: you can pass something like lang=actionscript to the code tag

  • Default settings manageable using the control panel, so you don't have to pass all arguments each time you're pasting code



Want to see an example? Sure, no problem:
  1. <?php
  2. function activate_extension()
  3. {
  4.  global $DB;
  5.  $DB->query($DB->insert_string('exp_extensions',
  6.   array(
  7.    'extension_id' => '',
  8.    'class' => 'Geshify',
  9.    'method' => 'pre_typography',
  10.    'hook' => 'typography_parse_type_start',
  11.    'settings' => serialize($this->settings_default),
  12.    'priority' => 8,
  13.    'version' => $DB->escape_str($this->version),
  14.    'enabled' => 'y'
  15.   )
  16.  ));
  17.  $DB->query($DB->insert_string('exp_extensions',
  18.   array(
  19.    'extension_id' => '',
  20.    'class' => 'Geshify',
  21.    'method' => 'post_typography',
  22.    'hook' => 'typography_parse_type_end',
  23.    'settings' => serialize($this->settings_default),
  24.    'priority' => 8,
  25.    'version' => $DB->escape_str($this->version),
  26.    'enabled' => 'y'
  27.   )
  28.  ));
  29. }
  30. ?>

If you know PHP and the Expression Engine API you might notice this is the place where the extension hooks into Expression Engine. If you don't you probably don't care either wink

Read on for Documentation and Download

Documentation

Please give me some feedback, whether you want me to include the original GeSHi package in the download and release a new version of GeSHify whenever GeSHi is updated or if you want to download GeSHi yourself, generating less updates to GeSHify where no GeSHify code actually changed.

This documentation expects your GeSHify tag name setting to be “code” and your delimiters to be “[” and “]”, which is the default. If you feel a need to do so, you can change these settings in your control panel by going to Admin » Utilities » Extensions Manager » GeSHify, where you will be provided a fancy settings manager.

code-tag syntax

To parse code with GeSHify, you need to wrap it in [code] and [/code]. You can optionally pass the following arguments

  • lang
    lang=[identifier of the GeSHi language file you want to use for parsing this code block]
    Default value can be changed in the control panel. Check the GeSHi website for possible values - note that these values might differ from GeSHi 1.0 to GeSHi 1.1 even for the same code-language
    Example: [code lang=php]<?php // some php code ?>[/code]
  • line
    line=[none|fancy99|normal]
    type of line numbering to use. Possible values are “none” for no line numbers, “normal” for standard line numbers and “fancy” followed by a number where each numberth line will be highlighted in a different color. The default value can be changed in the control panel.
    Example: [code line=none][/code] for no line numbers, [code line=fancy5][/code] for each 5th line number in a different color and [code line=normal][/code] for standard line numbers.
  • start
    start=[number]
    first line number to be used
    Example: [code start=5][/code] will start line numbering at 5; very useful, if you're trying to split a file in multiple parts, but still have consistent line numbers.
  • strict
    strict=[true|false|1|0]
    whether to use strict mode, where you'll have to include the start and end tag in interpreted languages like e.g. PHP. Defaults to false on GeSHi 1.0 and true on GeSHi 1.1.
    Example: [code strict=true][/code] or [code strict=1][/code] for strict handling, [code strict=false][/code] or [code start=0][/code] for non-strict handling.
  • keyword_links
    keyword_links=[true|false|1|0]
    whether to link special keywords with online documentation for the language used (e.g. PHP keywords will be linked to php.net when this is enabled). Default value can be changed in the control panel.
    Example: [code keyword_links=true][/code] or [code keyword_links=1][/code] for linked keywords, [code keyword_links=0] or [code keyword_links=false] to disable keyword links.
  • overall_id
    overall_id=[id string to use in the id attribute of the code container]
    ID attribute to assign to the HTML element containing the source code after hightlighting. You can use this to apply custom CSS or JavaScript.
    Example: [code overall_id=heapsort_in_php][/code]
  • overall_class
    overall_class=[class name to add to the class attribute of the code container]
    class name to add to the class list of the HTML element containing the source code after highlighting. You can use this to apply custom CSS or JavaScript.
    Example: [code overall_class=sorting_algorithms_in_php][/code]

Caching

GeSHify will cache your highlighted source code until it is being modified or until there hasn't been any hit on the page containing it for 24h (you can change that value using the control panel). GeSHify will clear old cache files based on a 10% chance with each hit (random number generators ftw!). If you'd like to clear your cache manually delete the contents of your system/cache/geshify_cache directory (if you changed the cache directory setting in the GeSHify extension settings this directory will be located somewhere else)

License

GeSHify is released under the GPL.

Bugs and known limitations

Please report any bugs you encounter or and features you'd like to see by commenting on this post or by mailing to .(JavaScript must be enabled to view this email address)

GeSHi 1.1 is alpha software - many code libraries are missing and neither line numbering nor disabling strict mode work with GeSHi 1.1 yet.

Download & Changelog

GeSHify has a new home at geshify.com. Please get it from there.