Drupal 7 Syntax Highlighting

It seems like the GeSHi Filter is the most popular Drupal 7.x module for syntax highlighting. To get it up and running, first install the dependent Libraries API (2.x-dev) and download the required GeSHi PHP Library (1.0.x), then install the GeSHi Filter module. As per the GeSHi Filter README, extract the GeSHi Library to /sites/all/modules/geshifilter and then enable the module via the Drupal admin interface. If the library isn't detected, try clearing the cache.

Once the GeSHi module is running, you have to enable the filter on your preferred text format (e.g. Filtered HTML). You do this on the Admin >> Configuration >> Text Formats page. I also had to put GeSHi at the bottom of the processing order to get it to work. If you’re using a WYSIWYG editor (like CKEditor) for creating content, make sure it’s not escaping special characters like ‘>’.

Once you get the module fully functional, you should be able to highlight a wide variety of languages, as illustrated in the following examples.

Java class example.

 * Dummy Java class to illustrate GeSHi 
 * @author Pascal Brandt
public class Dummy {
    // Constants

    public static final DUMMY_STRING = "Dummy";

    // Members

    protected int dummyInt;
    protected List<Integer> dummyList;

    // Contructor

    public Dummy(int dInt, List<Integer> dList) {
        dummyInt = dInt;
        dummyList = dList;

PHP example from MediaWiki’s LocalSettings.php config file.

$wgUploadPath       = "$wgScriptPath/uploads";      ## Wiki 1.5 defaults to /images, but allows more than just images

$wgUploadDirectory  = "$IP/uploads";                ## Wiki 1.5 defaults to /images, but allows more than just images

## To enable image uploads, make sure the above '$wgUploadPath' directory is writable by Apache User or group.

## ''(i.e.  chmod og+w uploads images)''  then the following should be true:

$wgEnableUploads       = true;
$wgUseImageResize      = true;
$wgUseImageMagick      = true;
$wgImageMagickConvertCommand = "/usr/bin/convert";
## If you want to use image uploads under safe mode, create the directories images/archive, images/thumb and

## images/temp, and make them all writable. Then uncomment this, if it's not already uncommented:

$wgHashedUploadDirectory = false;

Python list comprehension example.

new_range = [i * i for i in range(5) if i % 2 == 0]

MySQL create table example.

CREATE TABLE client_firms (
    id   INT,
    name VARCHAR(35)
    PARTITION r0 VALUES IN (1, 5, 9, 13, 17, 21),
    PARTITION r1 VALUES IN (2, 6, 10, 14, 18, 22),
    PARTITION r2 VALUES IN (3, 7, 11, 15, 19, 23),
    PARTITION r3 VALUES IN (4, 8, 12, 16, 20, 24)

In theory, inline code snippets like

$var = 35

should also work.

EDIT: Since I migrated to Jekyll, the above code is actually highlighted using Pygments.

comments powered by Disqus