Thursday, May 21, 2009

» php-source-highlight

I spent an hour or two yesterday to write my first PHP extension in C++. It is a very simple wrapper around GNU source-highlight that also ships its functionality as a shared library and (C++) API since version 3.0. The whole point about that extension is that you can perform syntax highlighting using source-highlight's capabilities and broad support for many programming languages directly and naturally using a PHP function, instead of having to resort to executing a subprocess and fetching its output, or using the source-highlight CGI. Using the extension, source-highlight is preloaded as a shared library and hence, the performance is optimal. For example:
<?php
echo sourcehighlight("
public interface SomeInterface {
    void doSomething(String a, String b) throws IOException;
}
", "java");
?>
The above would result in this:
public interface SomeInterface {
    void doSomething(String a, String b) throws IOException;
}
It is currently a very first version, and I suppose that I will add more features in a near future (discussing a bit with Lorenzo Bettini, the author of GNU source-highlight), but the next step will be to implement a Mediawiki extension that uses php-source-highlight, which is pretty simple (I already wrote a few, once you know the API, it's trivial to do), in order to highlight Mediawiki markup, e.g.
<code lang="java">
public class Foo {
  public static final void main(String[] args) {
    System.out.println("Hello world");
  }
}
</code>
Resulting in this on rendered Mediawiki pages:
public class Foo {
  public static final void main(String[] args) {
    System.out.println("Hello world");
  }
}

Labels:

1 Comments:

Blogger betto said...

Well done Pascal :-)

21:04  

Post a Comment

<< Home