This is a helper file I made for PyroCMS/CodeIgniter that makes it easy to use GeSHi Highlighting on code snippets pulled from a database or on complete files read from the system. No need for hooks to pre-process everything, wasting valuable resources.
To use this helper script, you first need to follow these steps to setup GeSHi with PyroCMS/CodeIgniter. After you have GeSHi setup, place this file in your PyroCMS addons/helpers folder, or for CodeIgniter, copy it to your application/helpers folder, and you'll be all set to go
The file is actually a set of two functions that you can call from a controller, geshi() and geshi_code().
The geshi() function is for reading and highlighting code from a file on the system. It takes three variables - geshi($path, $name, $language):
- $path - the absolute path to the file
- $name - the name of the file (currently used in the code header)
- $language - the coding language - used by GeSHi
The geshi_code() function is for reading and highlighting code from a database(or other source). It takes three variables - geshi_code( $source, $language, $title):
- $source - the code from database
- $language - the coding language - used by GeSHi
- $title - what to display in the code header
Both functions return an array with two items, one is the css styles, and the other is the formatted code. That way you can place them in your view where you need them.
GeSHi Helper file:<?php defined('BASEPATH') OR exit('No direct script access allowed.'); /** * GeSHi Highlighter PyroCMS/CodeIgniter Helper File * @author Larry Price * @filename geshi_helper.php * @title Geshi Helper * @version 1.0 **/ /** * Usage: * For highlighting code straight from file * geshi($path, $name, $language) * @return string */ if ( ! function_exists('geshi')) { function geshi($path, $name, $language) { $source = read_file($path); $geshi = new GeSHi($source, $language); $geshi->enable_classes(); $geshi->set_overall_style('font-size:12px; padding:10px; color: #000000; border: 3px solid silver; width: 650px; background-color: #f9f9f9; margin: 0 0 1.5em 0; overflow: auto;', true); $geshi->set_header_content($name.' <LANGUAGE>' ); $geshi->set_header_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-bottom: 1px solid #d0d0d0; padding: 2px;'); $geshi->set_footer_content('Parsed in <TIME> seconds, using GeSHi <VERSION>'); $geshi->set_footer_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-top: 1px solid #d0d0d0; padding: 2px;'); $geshi->set_header_type(GESHI_HEADER_PRE_TABLE); $highlights['styles'] = $geshi->get_stylesheet(); $highlights['code'] = $geshi->parse_code(); return $highlights; } } /** * Usage: * For highlighting code in document * geshi_code( $source, $language, $title) * @return string */ if ( ! function_exists('geshi_code')) { function geshi_code( $source, $language, $title) { $geshi = new GeSHi($source, $language); $geshi->enable_classes(); $geshi->set_overall_style('font-size:12px; padding:10px; color: #000000; border: 3px solid #666; width: 650px; background-color: #f9f9f9; margin: 0 0 1.5em 0; overflow: auto;', true); $geshi->set_header_content($title); $geshi->set_header_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-bottom: 1px solid #d0d0d0; padding: 2px;'); $geshi->set_footer_content('Parsed in <TIME> seconds, using GeSHi <VERSION>'); $geshi->set_footer_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-top: 1px solid #d0d0d0; padding: 2px;'); $geshi->set_header_type(GESHI_HEADER_PRE_TABLE); $highlights['styles'] = $geshi->get_stylesheet(); $highlights['code'] = $geshi->parse_code(); return $highlights; } } /* End of file geshi_helper.php */
Hope you find this helpful! If you have any questions, please leave a comment.