Recent Topics

1 Mar 19, 2005 15:37    

Hello,

I wasn't able to find a GeSHi plugin for b2evolution. [url=http://qbnz.com/highlighter/]GeSHi[/url] is a PHP based source code syntax highlighter.

Is there such a plugin?

2 Mar 19, 2005 15:52

Nope. Would be a good tool though if anyone did it.

3 Mar 19, 2005 16:07

OK - This is what I got so far (it is nearly a 1:1 port of the Serendipity plugin). Unfortunatly the inline source code of a blog post won't be highlighted - Don't know what's wrong :(


<?php
/**
 * This file implements is a simple wrapper to the GeSHi syntax highlighting engine.
 *
 * b2evolution - {@link http://b2evolution.net/}
 * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
 * @copyright (c)2003-2004 by Francois PLANQUE - b2evolution - {@link http://fplanque.net/}
 * @copyright (c)2004 by Nigel McNie - GeSHi - {@link http://qbnz.com/highlighter/}
 * @copyright (c)2004 by Daniel S. Haischt - b2evo renderer- {@link http://www.foobar.com/}
 *
 * @package plugins
 */
if( !defined('EVO_CONFIG_LOADED') ) die( 'Please, do not access this page directly.' );
// if( !defined('DB_USER') ) die( 'Please, do not access this page directly.' );

/**
 * @package plugins
 */
class geshi_plugin extends Plugin
{
   var $code = 'b2evGeshi';
   var $name = 'Geshi';
   var $priority = 80;

   var $apply_when = 'opt-out';
   var $apply_to_html = true;
   var $apply_to_xml = false;
   var $short_desc;
   var $long_desc;

   /**
    * Constructor
    *
    * {@internal geshi_plugin::geshi_plugin(-)}}
    */
   function geshi_plugin()
   {
      $this->short_desc = T_('Provides source code syntax highlighting');
      $this->long_desc = T_('No description available');
   }


   /**
    * Perform rendering
    *
    * {@internal geshi_plugin::Render(-)}}
    *
    * @param array Associative array of parameters
    *                      (Output format, see {@link format_to_output()})
    * @return boolean true if we can render something for the required output format
    */
   function Render( & $params )
   {
      if( ! parent::Render( $params ) )
      {   // We cannot render the required format
         return false;
      }

 	require_once(dirname(__FILE__). '/geshi.php');
      $content = & $params['data'];
	$content = preg_replace_callback(
		"/\[geshi(\s)+lang=([A-Za-z]+)\](.*?)\[\/geshi\]/is",
		"geshicallback",
		$content);
      
      return true;
   }

   function geshicallback($matches) {
	$geshilang = strtolower($matches[2]);
	//echo "\n<!-- lang: $geshilang -->\n";
	$pathtogeshi = dirname(__FILE__). '/geshi';
	//echo "\n<!-- PathtoGeshi: $pathtogeshi -->\n";
	$geshi = new GeSHi($matches[3], $geshilang, 'geshi');
	$geshi->set_header_type(GESHI_HEADER_DIV);
	$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
	// Have to get rid of newlines.
	return str_replace("\n", '', $geshi->parse_code());
   }

}

?>

I put the geshi.php file into the plugins directory. The GeSHi language files are located within a directory called geshi under plugins.

The following markup within a post for example, will be considered to be transformed by GeSHi:


[geshi lang=php]var $code = 'b2evMrkdwn';[/geshi]


Form is loading...