Go Geshi Function

April 22, 2011
Use this function for easier and quicker GeSHi implementations. This is targeted to the non-OOP PHP programming community out there, but it's also useful for those who do use OOP and would rather a quick wrapper function.

In "industrial" terms, this is considered a recipe, because it contains all the ingredients to a successful cooking show (I mean production).

To get started, you're going to need to download GeSHi from SourceForge (I used version 1.0.8.10 when I developed this).

And you may find it beneficial to also view the author's GeSHi homepage, which contains a great deal of documentation and examples if you choose not to use this function.

Why the Go GeSHi function?



Configuring


There's not too much to configure by default, just a few variables:

One last note...


You must pass the source in BASE 64 ENCODING! I assume this makes it easier to store in the source of your other files without all kinds of escaping.

To make it easier, I put up a Base64 Online Encoder which will take care of that for you.

How to use the code


Way too easy...
Download Original
  1. <?PHP
  2. go_geshi("BaSe64EnCoDiNgHeRe==");
  3. ?>

The Code


(Which just happens to show this function in action!)

Download Original
  1. if (!function_exists(go_geshi))
  2. {
  3. function go_geshi($source)
  4. {
  5. //Configuration
  6. $language = "PHP";
  7. $path_to_geshi_languages = "geshi/geshi";
  8. $generate_downloads = TRUE;
  9. $width_in_px = 700;
  10.  
  11. $source = base64_decode($source);
  12. if ($generate_downloads) {
  13. if (!file_exists("src-" . md5($source) . ".src")) {
  14. $file = fopen("src-" . md5($source) . ".src","w");
  15. fwrite($file, $source);
  16. fclose($file);
  17. }
  18. }
  19. echo "<style type='text/css'>.geshi-rl-box>pre{white-space:pre-wrap;white-space:-moz-pre-wrap!important;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;width:{$width_in_px}px}</style>";
  20. require_once('geshi/geshi.php');
  21. $geshi = new GeSHi($source, $language, $path_to_geshi_languages);
  22. $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS,2);
  23. $geshi->set_link_target("_blank\" rel=\"nofollow");
  24. echo "<div class='centeredbox geshi-rl-box'>";
  25. if ($generate_downloads) echo "<a href='src-" . md5($source) . ".src'>Download Original</a><br />";
  26. echo $geshi->parse_code() . "</div>";
  27. }
  28. }


Name:

No comments yet! Be the first!