Project Title


GeSHi for CodeIgniter

GeSHi is a syntax-highlighter that provides server-side highlighting for over 100 programming languages. This library provides a simple, clean GeSHi API tailored for use in CodeIgniter projects.

Features:

Installation

  1. Download the latest version from the Github page
  2. Copy the geshilib.php file and geshi folder into your application/libraries folder.

Loading the Library

Like any other library in CodeIgniter, use the $this->load->library function to load geshilib:

$this->load->library('geshilib');

Basic Syntax Highlighting

Once the library is loaded, you can use GeSHi to highlight any string by calling the $this->geshilib->highlight method.

For example, to highlight a string $mycode, which contains SQL code: $this->geshilib->highlight($mycode, 'sql');

The second argument ('sql') is the language you want to use for highlighting. Any language that GeSHi supports is valid. Refer to the GeSHi Homepage for a list of supported langauges (hint: they're pretty much all supported).

If you don't specify a second argument, GeSHiLib will highlight your code using simple BASH highlighting.

The $this->geshilib->filter method accepts a third argument, $params. See "Setting Paramters" below for more information regarding this argument.

Highlighting Code Blocks inside of HTML

GeSHi lib can parse HTML output and look for code to highlight. Place the code you wish to highlight between <sourcecode>...</sourcecode> tags within your HTML code, and then send the HTML to the $this->geshilib->filter method:

$my_html = "<p>Some regular content</p><sourcecode>$x = "Hello, World!";</sourcecode>";

$highlighted = $this->geshilib->filter($my_html);

To specify a language, add the language attribute to the sourcecode tag:

$my_html = "<p>Some regular content</p><sourcecode language='php'>$x = "Hello, World!";</sourcecode>";

$highlighted = $this->geshilib->filter($my_html);

Don't worry that <sourcecode>...</sourcecode> tags aren't valid HTML. GeSHi will conver them to <pre>...</pre> tags.

The $this->geshilib->filter method also accepts a second argument, $params. See "Setting Paramaters" below for more information regarding this argument.

Automating GeshiLib with a Output Hook

If you want Geshilib to automatically highlight all content contained within <sourcecode>...</sourcecode> tags inside of your views, you can create a hook.

  1. Ensure that hooks are enabled in the application/config/config.php file: $config['enable_hooks'] = TRUE;
  2. Add the following code to your application/config/hooks.php file:
    $hook['display_override'] = array(
    	'class' => 'Geshilib',
    	'function' => 'geshi_display_override',
    	'filename' => 'geshilib.php',
    	'filepath' => 'libraries',
    	'params' => array());

Manually Finding and Highlighting Code in CI View Files

To automatically find and highlight all <sourcecode>...</sourcecode> inside of a CodeIgniter view file, load the view into a string using $this->load->view, run $this->geshilib->filter on it, and manually output the results using $this->output->set_output:

$vdata = $this->load->view('some_view', $some_data);
$vdata = $this->geshilib->filter($vdata);
$this->output->set_output($vdata);

Setting Parameters

GeSHi provides several options for customizing highlighted code. You can specify these parameters by adding the $params array argument to the highlight or filter methods, or by specifying content for the params array in the display_override hook.

The $params variable is an associative array. The key is the parameter name, and the value is an array of options for the parameter. Parameters in the $params array are the same as methods in the GeSHi object. Almost any public method in a GeSHi object can be the a key in the $params array. Exceptions include the error(), parse_code(), and get_stylesheet() methods. Values in the $params array are the arguments that the GeSHi object method accepts.

For example, the GesHi object contains a public method, enable_line_numbers() that accepts two arguments: a GeSHi constant, and an optional integer. To use this method, you would set your parameters array as follows:

$params = array(
	'enable_line_numbers' => array(GESHI_NORMAL_LINE_NUMBERS, 15)
);

$this->geshilib->highlight($some_code, $params);

For a full documentation of methods that the GeSHi object supports, refer to the GeSHi Documentation.

Some Common Parameters

Note: This list is not comprehensive. Refer to the GeSHi Documentation for all methods/parameters.

Note: This parameters list refers to GeSHi v1.0.8.10 API. If you are using a different version of GeSHi with this library, this API may be different.

Parameter Options Description
set_header_type GESHI_HEADER_DIV
GESHI_HEADER_PRE
GESHI_HEADER_PRE_VALID
GESHI_HEADER_PRE_TABLE
GESHI_HEADER_NONE
Sets the header type for highlighted sourcecode.
enable_line_numbers GESHI_NORMAL_LINE_NUMBERS
GESHI_FANCY_LINE_NUMBERS
GESHI_NO_LINE_NUMBERS
set_line_style Any desired CSS code
start_line_numbers_at Any positive integer
enable_classes TRUE to enable classes
FALSE to disable classes
set_overall_class desired class name
set_overall_id desired ID
set_overall_style desired CSS code
set_line_style desired even line style, desired odd line style
set_tab_width any positive integer
enable_ids TRUE to enable ID's
FALSE to disable ID's

Other Parameters

The following parameters apply to the geshilib library, not GeSHi itself:

Parameter Default Options Description
fail_gracefully TRUE TRUE or FALSE If FALSE, GeSHi will throw exceptions if it encounters an error during highlighting. Useful for debugging.
default_language BASH Any GeSHi-supported Language Set the default language in case none is specified.

Examples of Setting Parameters

The following example enables line numbers and sets tabs to 3 characters:

$params = array('enable_line_numbers' => array(GESHI_NORMAL_LINE_NUMBERS), 'set_tab_width' => array('3'));

$this->geshilib->highlight("echo 'Hello World';", 'php', $params);

The following example enable line numbers and adds an overall class to GeSHi output for all <sourcecode>...</sourcecode> instances encountered when using the display_override hook:

$hook['display_override'] = array(
	'class' => 'Geshilib',
	'function' => 'geshi_display_override',
	'filename' => 'geshilib.php',
	'filepath' => 'libraries',
	'params' => array(
		'enable_line_numbers' => array('GESHI_FANCY_LINE_NUMBERS', 10), 'set_overall_class' => array('sourcecode')
	));

Note: Because of the way CodeIgniter loads libraries, you must refer to all GeSHi constants as strings inside your application/config/hooks.php file. For example, the constant GESHI_FANCY_LINE_NUMBERS would become the string 'GESHI_FANCY_LINE_NUMBERS' inside your parameters array.