Since recent times have suggested to me that I better start putting a good collection of code online about how the things I create work, I need to start with a better syntax highlighter which is so important to explaining how things work in this world.
I started off using google’s system, but found it too cumbersome. I shot straight through three more plugins tonight and found they also have strange issues. This last one I’m working with today has found all the problems I have had and made it nice to use, besides one large issue with the WordPress TinyMCE Editor.
The winner is
CodeViewer by elasticdog – http://elasticdog.com/2004/09/code-viewer/
Below is a Test Program – this is a C program and you download here to it:
#include <stdio.h>#include <stdlib.h>void main() {return;}- Download this code: test.c
I found that formatting is more important than the actual syntax highlighting. So what I’m doing is using CodeViewer for the display and will use a child extension for doing actual syntax highlighting.
GeSHi has given a wonderful attempt at writing a variable syntax highlighter. It works while in chunk-based highlighting too. GeSHi is a O(n) based parser for syntax highlighting, yet it works pretty quickly as it keeps its use of substr and preg_replace at a minimum where doing character based descent to speed up the system.
I’ve partially implemented GeSHi for line-based highlighting inside CodeViewer. You can download and compliment your wordpress installation by grabbing this tarball – Code Viewer with GeSHi Support.
To write a proper geshi supported tag, you must use the lang attribute.
<viewcode src="cv_geshi_example.html" lang="html" />
If you would like to see the changes I’ve made to the plugin, please see this diff below:
--- code-viewer.txt Mon Aug 29 07:08:37 2005+++ code-viewer.php Sun Nov 18 09:33:15 2007@@ -9,14 +9,15 @@*//* Configuration Settings */-$default_path = "http://elasticdog.com/code/"; // the absolute path of your code folder+$default_path = "http://{$_SERVER['HTTP_HOST']}/code/"; // the absolute path of your code folder/* --- STOP EDITING --- */+require_once('geshi/geshi.php');function code_viewer($text) {global $default_path;- $count = preg_match_all('/<viewcode src="([^"]+)"(?: link="(?i:(yes|no))")?\s?\/>/', $text, $matches);+ $count = preg_match_all('/<viewcode src="([^"]+)"(?: link="(?i:(yes|no))")?(?: lang="(?i:([a-z]+))")?\s?\/>/', $text, $matches);for ($i = 0; $i < $count; $i++) {// Determine if the specified path is absolute, or relative to the root path@@ -29,6 +30,9 @@$path = $default_path . $matches[1][$i];}+ if(strtolower($matches[3][$i]) != "") {+ $language = $matches[3][$i];+ }// Open the file// If the file can't be found, print an error messageif ($lines = @file($path)) {@@ -44,8 +48,11 @@} else {$numtabs = strlen($line) - strlen(ltrim($line)); // determine the number of tabs$line = trim($line); // trim leading/trailing whitespace-- $codelist .= "\t" . '<li class="tab' . $numtabs . ' ' . $toggle . '"><code>' . htmlspecialchars($line) . '</code></li>' . "\n";+ if(isset($language)) {+ $codelist .= "\t" . '<li class="tab' . $numtabs . ' ' . $toggle . '"><code>' . geshi_highlight($line, $language, null, true) . '</code></li>' . "\n";+ } else {+ $codelist .= "\t" . '<li class="tab' . $numtabs . ' ' . $toggle . '"><code>' . htmlspecialchars($line) . '</code></li>' . "\n";+ }}}@@ -76,4 +83,4 @@add_filter('the_content', 'code_viewer', 9);add_filter('the_content', 'fix_bad_p');-?>\ No newline at end of file+?>- Download this code: code-viewer_geshi.diff