GeSHi library. After enabling this plugin visit the options page to configure code style.
Version: 0.8.3
Author: Dmytro Shteflyuk
Author URI: http://kpumuk.info/
*/
/*
Copyright 2006 - 2009 Dmytro Shteflyuk
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* Doesn't work if PHP version is not 4.0.6 or higher
*/
if (version_compare(phpversion(), '4.0.6', '<')) {
return;
}
/** Instance of plugin */
$CodeColorer = new CodeColorer();
/** Register plugin actions */
/** Regular pages */
add_action('wp_head', array (&$CodeColorer, 'addCSS'), 1);
/** Admin pages */
if (is_admin()) {
add_action('admin_head', array(&$CodeColorer, 'init'), 1);
add_action('admin_head', array(&$CodeColorer, 'addCssStyle'), 1);
add_action('admin_menu', array(&$CodeColorer, 'addPluginOptionsPage'), 1);
}
/** Filters */
add_filter('the_content', array(&$CodeColorer, 'highlightCode1'), -1000);
add_filter('the_content', array(&$CodeColorer, 'highlightCode2'), 1000);
add_filter('the_excerpt', array(&$CodeColorer, 'highlightCode1'), -1000);
add_filter('the_excerpt', array(&$CodeColorer, 'highlightCode2'), 1000);
add_filter('comment_text', array(&$CodeColorer, 'highlightCode1'), -1000);
add_filter('comment_text', array(&$CodeColorer, 'highlightCode2'), 1000);
add_filter('pre_comment_content', array(&$CodeColorer, 'protectCommentContent1'), -1000);
add_filter('pre_comment_content', array(&$CodeColorer, 'protectCommentContent2'), 1000);
unset ($CodeColorer);
/** CodeColorer plugin class */
class CodeColorer {
var $pluginLocation = '/wp-content/plugins/codecolorer/';
var $pluginPath;
var $libPath;
var $DEFAULT_LINES_TO_SCROLL = 20;
var $DEFAULT_WIDTH = 435;
var $DEFAULT_HEIGHT = 300;
var $blocks = array();
var $comments = array();
var $samplePhpCode = ' /**
* Comment
*/
function hello() {
echo "Hello!";
return null;
}
exit();';
/** Initialization of environment */
function init() {
$this->pluginPath = dirname(__FILE__) . DIRECTORY_SEPARATOR;
$this->libPath = $this->pluginPath . 'lib' . DIRECTORY_SEPARATOR;
require_once($this->libPath . 'geshi.php');
}
/** Add css references to page head */
function addCSS() {
$this->init();
$this->addCssStyle();
}
function addCssStyle() {
$styles = stripslashes(get_option('codecolorer_css_style'));
echo '', "\n";
if (!empty($styles)) {
echo '\n";
}
}
function sampleCodeFactory() {
$this->init();
$options = $this->parseOptions('lang="php"');
$html = $this->highlightGeshi($this->samplePhpCode, $options);
$num_lines = count(explode("\n", $this->samplePhpCode));
return $this->addContainer($html, $options, $num_lines);
}
function addPluginOptionsPage() {
add_options_page('CodeColorer', 'CodeColorer', 8, 'codecolorer/codecolorer-options.php');
}
/** Perform code highlighting using GESHi engine */
function highlightGeshi($content, $options) {
if (!class_exists('geshi')) $this->init();
/* Geshi configuration */
$geshi = new GeSHi($content, $options['lang'], $this->geshi_path);
$geshi->set_overall_class('codecolorer');
if (is_feed()) {
$geshi->set_overall_style('padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap');
} else {
$geshi->enable_classes();
if ($options['nowrap']) {
$geshi->set_overall_style('white-space:nowrap');
} else {
$geshi->set_overall_style('');
}
}
$geshi->set_tab_width($options['tab_size']);
if (!is_null($options['strict'])) $geshi->enable_strict_mode($options['strict']);
$geshi->enable_line_numbers(GESHI_NO_LINE_NUMBERS, 1);
if ($options['no_links']) $geshi->enable_keyword_links(false);
if ($options['inline']) {
$geshi->set_header_type(GESHI_HEADER_NONE);
} else {
$geshi->set_header_type(GESHI_HEADER_DIV);
}
if ($geshi->error()) {
return $geshi->error();
}
$result = $geshi->parse_code();
if ($options['line_numbers'] && !$options['inline']) {
$table = '