Syntax Highlighting for Drupal
You can now download the module from the GeSHi Filter project page
I am a comp sci student so I often post code on my website. I just happen to use a wonderful CMS/blogger called Drupal that supports filters and all kinds of other goodies. I wanted a filter that would take anything posted in <code> tags and automagically apply syntax highlighting.
GeSHi and Codefilter
My solution was to take the existing codefilter module that drupal provides and intergrate GeSHi into it. All in all it is very simple, you just have to install the codefilter patch and install genshi. Then go to admin -> settings -> codefilter to configure and enable genshi.
Because it is extremely difficult to determine the language from a code fragment you have to do it yourself. There is an option where a default language can be configured. But I wanted to make sure that I could use this for more than one language so I added some options to the <code> tag.
The have the code hightlighted in a language other than the default do this:
If you want to turn it off temporarily do this:
The path can be downloaded below. There is an alternative coloring module called VIM color which uses the coloring schemes from vim. This is arguably more powerful and more tested however it requries certain perl modules and access to perl which limits its usefulness on windows. My module uses a php library called GeSHi so it will run where ever drupal is running. In truth if I would have noticed vim color first I wouldn't have bothered writing this, as it is I may change to it at some point in the future.
Supported Languages
| Actionscript | ADA | Apache Log |
| AppleScript | ASM | ASP |
| Bash | Blitz | Basic |
| C | C for Macs | C# |
| C++ | CAD DCL | CadLisp |
| CSS | Delphi | DIV |
| DOS | Eiffel | FreeBasic |
| GML | HTML | Inno |
| Java | Javascript | Lisp |
| Lua | Microprocessor ASM | MySQL |
| NSIS | Objective C | OCaml |
| OpenOffice | BASIC | Oracle 8 SQL |
| Pascal | Perl | PHP |
| Python | Q(uick)BASIC | Ruby |
| Scheme | SDL | Basic |
| Smarty | SQL | VB.NET |
| Visual BASIC | Visual Fox Pro | XML |
Examples
C
/************************************
*
* error
*
* DESCRIPTION
* Generic output routine for errors.
*
* RETURNS
* void
*
*/
void error( char* file, int line, bool terminal, char *message, ... ) {
va_list ap;
char* temp = strrchr( file, '\\' );
if( temp != NULL )
file = temp+1;
fprintf( stderr, "E|%s:%d ", file, line );Python
# constructor
#
# DESCRIPTION
# Creates an empty set, or a set for a list of
# elements if the list is passed in
#
def __init__( self, list = { } ):
if list != None:
self.set = { }
for i in range( 0, len( list ) ):
self.set[ list[i] ] = ""
else:
selt.set = list
HTML 4 Strict
<div id="header">
<img id="logo" src="/themes/filbar-classic/px/filbar-logo.png" alt="filbar" />
<div id="headerbg">
<div id="date">
Saturday, August 06, 2005 </div>
<ul>
<li><a href="/gallery">Gallery</a></li>
<li><a href="/project">Projects</a></li>
<li><a href="/forum">Forum</a></li>
<li><a href="">Home</a></li>
</ul>
</div>
<div id="search">
<form action="search" method="post">
<input type="text" size="15" value="" name="edit[keys]" />
<input type="submit" value="Search" />
</form>
</div>
<div id="breadcrumb">
<div class="breadcrumb"><a href="">Home</a> » <a href="/node/add">create content</a> » <a href="/node/add/blog">Submit personal blog entry</a></div> </div>
</div>
Java
*
*/
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class ImagePanel extends JPanel {
Image image;
public ImagePanel( String path ) {
image = Toolkit.getDefaultToolkit().getImage( path );
setVisible( true );
}
public ImagePanel() {
setVisible( true );
}
}
| Attachment | Size |
|---|---|
| codefilter-geshi.patch | 8.04 KB |












Does your module work with TinyMCE?
When I read this it got me wondering.it's gaining popularity.Thanks for sharing.
Post new comment