JSON renderer for Text_Highlight
Text_Highlighter is one of my favourite PEAR packages, seems like I'm addicted to highlighting source code. After adding BB code and simple HTML renderers and an ABAP code syntax definition, today I played with adding a JSON renderer. Useful in case you want to get highlighted source code in your new shiny AJAX app.
Array renderer
After I did the JSON renderer, I said, OK, what if I want to tweak the JSON output just a bit (or the output from any renderer for that matter)? Add more options? Nah, I had a better idea, I scrapped the whole thing and did an Array renderer first. If you have the array output from the renderer, it's trivial to format it as JSON, or XML, or HTML, or anything. I believe even the exisitng Text_Highlighter renderers should be rewritten, to extend an Array renderer. Anyway, back to JSON.
Demo
To see the JSON renderer in action, you can go to my hiliteme.com site and check the JSON tab.
Source
The source code is available here - JSON.phps which extends Array.phps. To test, you need to add the two renderers to your PEAR repository under Text/Highlighter/Renderer
Example
So let's say you need to highlight the PHP code
<?php
echo "Hello Highlighted World!";
?>
You create an instance of Text_Highlighter and Text_Highlighter_Renderer_JSON and call the highlight() method, assuming that the code you need highlighted is in $source
// dependencies require_once 'Text/Highlighter.php'; require_once 'Text/Highlighter/Renderer/JSON.php'; // instance $json_options = array(); $json_options['tabsize'] = 4; $json_options['enumerated'] = true; $renderer =& new Text_Highlighter_Renderer_JSON($json_options); $highlighter =& Text_Highlighter::factory($_POST['language']); $highlighter->setRenderer($renderer); // do the highlighting $json_result = $highlighter->highlight($source);
Now $json_result will look like:
[["inlinetags","<?php"],["code"," \\n "],["reserved","echo"],["code"," "],["quotes","""],["string","Hello Highlighted World!"],["quotes","""],["code","; \\n"],["inlinetags","?>"]]
As you see the JSON output is an array, one element per highlighted keyword, and in this array there is a sub array - class/keyword. If you want to display this in your page (let's say you got it from an AJAX call), you can do a loop through the array and surround the keywords with span tags of the selected style:
// say your ajax call returned var src // var src = xmlhttp.responseText; var data = eval(src); var res = ''; for (var i in data) { res += '<span class="hl-' + data[i][0] + '">'; res += data[i][1]; res += '</span>'; } var el = document.getElementById('some-div'). el.innerHTML = '<pre>' + res + '</pre>';
Here I used innerHTML, you can also use DOM, but in this case you need a special case for the "\n" so that you can create a br element to address IE's habit of ignoring line feeds in a DOM-generated pre tag.
BTW, if you don't set the enumerated option to true, you'll get objects inside the main array, check hiliteme.com's JSON tab for an idea of how this would look like.
This entry was posted on Friday, October 27th, 2006 and is filed under Ajax, JavaScript, JSON, PEAR, tools. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Get notification for future posts: follow me on Twitter or subscribe to my RSS feed

October 30th, 2006 at 1:53 pm
PHPied.com: JSON renderer for Text_Highlight…
…
November 2nd, 2006 at 11:46 am
[...] phpied.com It’s your responsibility to die() if necessary… – PHP Manual « JSON renderer for Text_Highlight [...]
November 6th, 2011 at 6:52 pm
Definitely consider that which you stated. Your favorite reason appeared to be at the net the simplest factor to be mindful of. I say to you, I definitely get irked at the same time as other people think about worries that they plainly do not realize about. You managed to hit the nail upon the highest and also outlined out the entire thing without having side-effects , folks could take a signal. Will probably be again to get more. Thanks