09
Mar
08

My code look pretty one day (or turning your GeSHi install into a service and using it with AS3)

File this one under “when I could have been creating something useful, I created this”

Hmmmm

I’ve been using PrivatePaste.com quite often of late and thought hmm… it would be simple to create a quick as3 library for using the service. Unfortunately PrivatePaste isn’t intended to be used as a web service. This means we can post and get a result, but it will be html. The site also lacks a crossdomain.xml file. While parsing the HTML using a regular expression, it occurred to me that privatepaste just uses GeSHi. A php open source library for syntax highlighting.

Since I already have the GeSHi wordpress plugin setup on this blog, I figured why mess around with parsing an HTML page when I can just make a quick syntax highlighting service myself. All you need to do is add the following file to the directory containing GeSHi. If you’ve installed the wp-syntax plugin (or any highlighting plugin that uses GeSHi) for your wordpress blog then its under /wp-content/plugins.

include('geshi.php');
 
if ( get_magic_quotes_gpc() ) $_POST['source'] = stripslashes($_POST['source']);
 
$source = $_POST['source'];
$language = $_POST['format'];
$path = 'geshi/';
 
$geshi = new GeSHi($source, $language, $path);
 
if($_POST['linenumbers'] == "true")
	$lineoption = GESHI_NORMAL_LINE_NUMBERS;
else
	$lineoption = GESHI_NO_LINE_NUMBERS;
 
 
$geshi->enable_line_numbers($lineoption);

echo $geshi->parse_code();

Thats it, you now have a GeSHi service. To use your service in ActionScript 3 you can use as3geshilib that I’ve included in the download:
GeSHi AIR

private function parseCode():void{
	var geshiService:GeshiService = new GeshiService();
 
	geshiService.addEventListener(GeshiResultEvent.ON_GET_PARSE_CODE, function(event:GeshiResultEvent):void{
			trace(event);
			htmlDisplay.htmlText = event.data as String;
		});
 
	geshiService.parseCode("SELECT * FROM user_table", GeshiFormatType.SQL, true);
}
geshiservice.zip Includes geshiservice.php, as3geshilib - source included

In the end, what we really have here is an example of how to make a quick php service and use it in ActionScript. Which I guess is not a complete waste of time…

If you just need syntax highlighting in your AS3 project then I would recommend checking out this AS3 code highlighting library http://labs.searchcoders.com/text/. I haven’t checked it out yet, but plan to the next time my code runs off on a tangent.


0 Responses to “My code look pretty one day (or turning your GeSHi install into a service and using it with AS3)”


  1. No Comments

flickr