Forum Index   -   Topic Index
   Preload
     Code Highlight3 Preload Hack ( New xcode version, for XC )
Register To Post

Threaded | Newest First Previous Topic | Next Topic | Bottom
Poster Thread
wanikoo
Posted on: 2006/10/30 0:09
Home away from home
Joined: 2006/3/1
From: third rock from the sun
Posts: 323
Code Highlight3 Preload Hack ( New xcode version, for XC )
---------------------------------------------------------
Code Highlight3 Preload Hack version 1.0 by wanikoo
MyTextSanitizer.XoopsCodePre Version
- [hcode][/hcode]
- [ghcode=lang][/ghcode]
(ex:lang => php, [ghcode=php][/ghcode])
based on phppp's "Xoops extended code hack" ( http://xoops.org.cn )
---------------------------------------------------------
( Tested on XOOPS Cube Legacy beta 2 )
---------------------------------------------------------
^^;;
Just a humble Preload hack for Code Syntax highlighting!!
This hack highlights code syntax of contents of [ hcode ][ /hcode ] and [ghcode=lang][/ghcode]
(<div class="xoopsCode"><pre><code>(.*)</code></pre></div> )

----------------
Feature
----------------
1) Two new xcodes for code highlight!
(a) [ hcode ][ /hcode ]
it uses highlight_string() of PHP
- How simple!!..^^;;
(b) [ghcode=lang][/ghcode]
it uses GeSHi(Generic Syntax Highlighter)
-If you want to use GESHi, you must download/install it!
--You can download GeSHi at its official site!
( http://qbnz.com/highlighter/ )
-How to install GESHi-
--Just upload files of GESHi package!
ex) /html/class/geshi/*.*(geshi.php...etc.)
--Modify define('XC_CODE_HIGHLIGHT3_GESHI', 0) in CodeHighlight3.class.php
( 0 => 1 )
--and customize related codes in CodeHighlight.class.php
( please customize GeSHi refering to http://qbnz.com/highlighter/documentation.php )

3) ^^;;
For detail, please read comments of CodeHighlight3.class.php


-------------
How to install
-------------
1) CodeHighlight3.class.php
Copy CodeHighlight3.class.php into /html/modules/legacy/preload/ like this.
( ex: /html/modules/legacy/preload/CodeHighlight3.class.php )
2) That's all^^;;
-----------------
How to uninstall
-----------------
1) Just delete CodeHighlight3.class.php

2) That's all^^;;


-------------------
CodeHighlight3.class.php (ver1.0)

Download it here!
http://www.wanisys.net/data/codehighlight3.zip

<?php
/**
 Code Highlight3 Preload Hack version 1.0 by wanikoo
 MyTextSanitizer.XoopsCodePre Version
[hcode][/hcode]
[ghcode=lang][/ghcode](ex:lang => php, [ghcode=php][/ghcode])
 ( http://www.wanisys.net/ )
*/

if (!defined('XOOPS_ROOT_PATH')) exit();

if (!defined('XC_CODE_HIGHLIGHT3')) define('XC_CODE_HIGHLIGHT3', 1);
if (!defined('XC_CODE_HIGHLIGHT3_GESHI')) define('XC_CODE_HIGHLIGHT3_GESHI', 1);

class Legacy_CodeHighlight3 extends XCube_ActionFilter
{
	function preBlockFilter()
	{
		//$this->mRoot->mDelegateManager->add("MyTextSanitizer.XoopsCodePostFilter", "Legacy_CodeHighlight3::decodeHighlightCode");
		$this->mRoot->mDelegateManager->add("MyTextSanitizer.XoopsCodePre", "Legacy_CodeHighlight3::decodeHighlightCode");
	}


	//function decodeHighlightCode(&$source, $allowimage="0")
	function decodeHighlightCode(&$patterns, &$replacements, $allowimage="0")
	{

	if(XC_CODE_HIGHLIGHT3 == 1){
            $patterns[] = "/\[hcode](.*)\[\/hcode\]/esU";
            $replacements[] = "'<div class=\"xoopsCode\"><pre>'.Legacy_CodeHighlight3::XC_CodeHighlighter(Legacy_CodeHighlight3::sanitizerHcode('$1'), 'hcode').'</pre></div>'";

	$patterns[] = "/\[ghcode([^\]]*?)\](.*)\[\/ghcode\]/esU";
	$replacements[] = "'<div class=\"xoopsCode\">'.Legacy_CodeHighlight3::XC_CodeHighlighter(Legacy_CodeHighlight3::sanitizerHcode('$2'), 'ghcode', '$1').'</div>'";

            //$source =  preg_replace($patterns, $replacements, $source);
	}
	else {
	}

	}


    function sanitizerHcode($str){

        //$str = MyTextSanitizer::xoopsCodeDecode(MyTextSanitizer::htmlSpecialChars(str_replace('\"', '"', $str)),0);
        $str = MyTextSanitizer::htmlSpecialChars(str_replace('\"', '"', $str));
        return $str;

    }

/**#@+
 * Original author: phppp
 * Xoops extended code
 * http://xoops.org.cn
 * Modified by wanikoo
 **/

	function &XC_CodeHighlighter($source, $type, $language = "php")
	{
	if (empty($language)) {
	$language = "php";
	}
	else {
	$language = strtolower(str_replace('=', '', $language));
	}

	$source = Legacy_CodeHighlight3::undohschars3($source);
	$source = stripslashes($source);

	if( file_exists(XOOPS_ROOT_PATH . '/class/geshi/geshi.php') && (XC_CODE_HIGHLIGHT3_GESHI == 1) && ($type == 'ghcode') ) {
	//$source = $this->XC_geshihighlightcode( $source, $language );
	$source = Legacy_CodeHighlight3::XC_geshihighlightcode( $source, $language );
	}
	else if($type == 'hcode'){
	//$source =  $this->XC_phphighlightcode($source);
	$source = Legacy_CodeHighlight3::XC_phphighlightcode($source);
	}
	else {
	$source = addslashes($source);
	$source = MyTextSanitizer::htmlSpecialChars($source);
	}
	return $source;

	}

	function &XC_phphighlightcode($text)
	{

	if(floor(phpversion())<4) return $text;

	$addedtags = 0;
	if (!strpos($text,"<?") and (substr($text,0,2)!="<?")) {
		$text="<?\n".trim($text)."\n?>";
	}
	ob_start();
	$oldlevel=error_reporting(0);
	highlight_string($text);
	error_reporting($oldlevel);
	$text = ob_get_contents();
	ob_end_clean();
	if ($addedtags) {
		$openingpos = strpos($text,'&lt;?');
		$closingpos = strrpos($text, '?');
		$text=substr($text, 0, $openingpos).substr($text, $openingpos+5, $closingpos-($openingpos+5)).substr($text, $closingpos+5);
	}
	return $text;

	}


	function &XC_geshihighlightcode( $source, $language )
	{

	if( file_exists(XOOPS_ROOT_PATH . '/class/geshi/geshi.php') ) {
   	include_once(XOOPS_ROOT_PATH . '/class/geshi/geshi.php');
	}

	//please customize GeSHi refering to http://qbnz.com/highlighter/documentation.php
     	$geshi = new GeSHi($source, $language, XOOPS_ROOT_PATH . '/class/geshi/');
    	$geshi->set_encoding(_CHARSET);
    	$geshi->enable_classes();
	$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); 
	$code = $geshi->parse_code();
	return $code;
	}

    	function &undohschars3(&$text)
    	{
        	$ret = preg_replace(array("/&gt;/i", "/&lt;/i", "/&quot;/i", "/&#039;/i"), array(">", "<", "\"", "'"), $text);
	return $ret;
	}


}

?>


PS:
This hack is still unstable and may require some resource of your server!!
( T.T..anyway... )
PS1:
Don't use this hack with my other highlight hacks ( CodeHighlight.class.php, CodeHighlight2.class.php )

PS2:
This hack will not work properly on XOOPS Cube Legacy 2.1 Alpha because it uses new XC Core!

PS3:
Umm...
Still..I am not good at Xoops Cube.
So...I can't guarantee this hack..^^;;
( Tested on XOOPS Cube Legacy 2.1 Beta 2)

------------
^^;;


----------------
the most educational site, wanisys.net
XOOPS Cube Korean Support Site !!

minahito
Posted on: 2006/10/30 14:54
Home away from home
Joined: 2005/10/6
From: Japan
Posts: 398
Re: Code Highlight3 Preload Hack ( New xcode version, for XC )
Great!

I submitted your recent 4 works to xoopscube.jp in "xoopscube.org Note Thread". You always use delegates very well without detail documents. You're really great!


----------------
minahito
Homepage (poor English )

wanikoo
Posted on: 2006/11/1 3:14
Home away from home
Joined: 2006/3/1
From: third rock from the sun
Posts: 323
Re: Code Highlight3 Preload Hack ( New xcode version, for XC )
---------------------------------------------------------
Code Highlight3 Preload Hack version 1.5 by wanikoo
MyTextSanitizer.XoopsCodePre Version
- [hcode][/hcode]
- [ghcode=lang][/ghcode]
(ex:lang => php, [ghcode=php][/ghcode])
based on phppp's "Xoops extended code hack" ( http://xoops.org.cn )
---------------------------------------------------------
( Tested on XOOPS Cube Legacy beta 2 )
---------------------------------------------------------
----------------
change
----------------
ver1.5
------
1) My mistake fixed!
-I forgot to replace my test config(of GeSHi) with default config(ex:language-path)(of GeSHi)(T.T)!!
-Sorry!!(but if you read its manual,you might fix it easily! )
(default language-path => XOOPS_ROOT_PATH.'/class/geshi/geshi/'
( ex: html/class/geshi/geshi/php.php )
2) Code refined!

Download it here!
http://www.wanisys.net/data/codehighlight3.zip

^^


----------------
the most educational site, wanisys.net
XOOPS Cube Korean Support Site !!

nobunobu
Posted on: 2006/11/29 23:55
Just popping in
Joined: 2005/10/6
From:
Posts: 16
Re: Code Highlight3 Preload Hack ( New xcode version, for XC )
Hi! Wanikoo.

I also made my own Code Highlithing preload before.
But, I found odd conversion by MyTextSanitizer.htmlSpecialChars disturbs this preload displaying code exactly.
For example
Following line in your code

preg_replace(array("/&amp;gt;/i", "/&amp;lt;/i",...

will be convered to

preg_replace(array("/&gt;/i", "/&lt;/i",...

If visitor sees this and copy and paste to his file editor cause invalid result.

So, I'm now thinking about more reliable solution.
I may change displayTarea method sequence and, I'll add new delegate in codePreConv and codeConv.

Please wait a while
wanikoo
Posted on: 2007/6/26 4:25
Home away from home
Joined: 2006/3/1
From: third rock from the sun
Posts: 323
Re: Code Highlight3 Preload Hack ( New xcode version, for XC )
---------------------------------------------------------
Code Highlight3 Preload Hack version 2.0 by wanikoo
MyTextSanitizer.XoopsCodePre Version
- [hcode][/hcode]
- [ghcode=lang][/ghcode]
(ex:lang => php, [ghcode=php][/ghcode])
based on phppp's "Xoops extended code hack" ( http://xoops.org.cn )
---------------------------------------------------------
( Tested on XOOPS Cube Legacy 2.1.1 )
---------------------------------------------------------
^^;;
Just a humble Preload hack for Code Syntax highlighting!!
This hack highlights code syntax of contents of [ hcode ][ /hcode ] and [ghcode=lang][/ghcode]
(<div class="xoopsCode"><pre><code>(.*)</code></pre></div> )

----------------
Feature
----------------
1) Two new xcodes for code highlight!
(a) [ hcode ][ /hcode ]
it uses highlight_string() of PHP
- How simple!!..^^;;
(b) [ghcode=lang][/ghcode]
it uses GeSHi(Generic Syntax Highlighter)
-If you want to use GESHi, you must download/install it!
--You can download GeSHi at its official site!
( http://qbnz.com/highlighter/ )
-How to install GESHi-
--Just upload files of GESHi package!
ex) /html/class/geshi/*.*(geshi.php...etc.)
--Modify define('XC_CODE_HIGHLIGHT3_GESHI', 0) in CodeHighlight3.class.php
( 0 => 1 )
--and customize related codes in CodeHighlight3.class.php
( please customize GeSHi refering to http://qbnz.com/highlighter/documentation.php )
--Available lang(s)
Refer to /html/class/geshi/geshi directory )

3) ^^;;
For detail, please read comments of CodeHighlight3.class.php

----------------
change
----------------
ver2.0
------
1) Some code modified for compatibility with brand-new Legacy_TextFilter of XC Legacy 2.1.x
( /legacy/kernel/Legacy_TextFilter.class.php
Now...it works well on XC Legacy 2.1.x
( Tested on XOOPS Cube Legacy 2.1.1 )
------
ver1.5
------
1) My mistake fixed!
-I forgot to replace my test config(of GeSHi) with default config(ex:language-path)(of GeSHi)(T.T)!!
-Sorry!!(but if you read its manual,you might fix it easily! )
(default language-path => XOOPS_ROOT_PATH.'/class/geshi/geshi/'
( ex: html/class/geshi/geshi/php.php )
2) Code refined!

-------------
How to install
-------------
1) CodeHighlight3.class.php
Copy CodeHighlight3.class.php into /html/modules/legacy/preload/ like this.
( ex: /html/modules/legacy/preload/CodeHighlight3.class.php )
2) That's all^^;;
-----------------
How to uninstall
-----------------
1) Just delete CodeHighlight3.class.php

2) That's all^^;;


-------------------
CodeHighlight3.class.php (ver2.0)

Download it here!
http://www.wanisys.net/data/codehighlight3.zip

<?php
/**
 Code Highlight3 Preload Hack version 2.0 by wanikoo
 MyTextSanitizer.XoopsCodePre Version
[hcode][/hcode]
[ghcode=lang][/ghcode](ex:lang => php, [ghcode=php][/ghcode])
 ( http://www.wanisys.net/ )
*/

if (!defined('XOOPS_ROOT_PATH')) exit();

if (!defined('XC_CODE_HIGHLIGHT3')) define('XC_CODE_HIGHLIGHT3', 1);
if (!defined('XC_CODE_HIGHLIGHT3_PHP')) define('XC_CODE_HIGHLIGHT3_PHP', 1);
if (!defined('XC_CODE_HIGHLIGHT3_GESHI')) define('XC_CODE_HIGHLIGHT3_GESHI', 1);

class Legacy_CodeHighlight3 extends XCube_ActionFilter
{
	function preBlockFilter()
	{
		$this->mRoot->mDelegateManager->add("Legacy_TextFilter.MakePreXCodeConvertTable", "Legacy_CodeHighlight3::precodeHighlightCode");
		$this->mRoot->mDelegateManager->add("MyTextSanitizer.XoopsCodePre", "Legacy_CodeHighlight3::decodeHighlightCode");
	}

	function precodeHighlightCode(&$patterns, &$replacements)
	{

	if(XC_CODE_HIGHLIGHT3 == 1){
		$patterns[] = "/\[hcode](.*)\[\/hcode\]/esU";
		$replacements[] = "'[hcode]'.base64_encode('$1').'[/hcode]'";
		$patterns[] = "/\[ghcode([^\]]*?)\](.*)\[\/ghcode\]/esU";
		$replacements[] = "'[ghcode$1]'.base64_encode('$2').'[/ghcode]'";
	}
	else {
	return;
	}

	}


	function decodeHighlightCode(&$patterns, &$replacements, $allowimage="0")
	{

	if(XC_CODE_HIGHLIGHT3 == 1){
            $patterns[] = "/\[hcode](.*)\[\/hcode\]/esU";
            $replacements[] = "'<div class=\"xoopsCode\"><pre>'.Legacy_CodeHighlight3::XC_CodeHighlighter(Legacy_TextFilter::codeSanitizer('$1'), 'hcode').'</pre></div>'";

	$patterns[] = "/\[ghcode([^\]]*?)\](.*)\[\/ghcode\]/esU";
	$replacements[] = "'<div class=\"xoopsCode\"><pre><code>'.Legacy_CodeHighlight3::XC_CodeHighlighter(Legacy_TextFilter::codeSanitizer('$2'), 'ghcode', '$1').'</code></pre></div>'";
	}
	else {
	return;
	}

	}


/**#@+
 * Original author: phppp
 * Xoops extended code
 * http://xoops.org.cn
 * Modified by wanikoo
 **/

	function &XC_CodeHighlighter($source, $type, $language = "php")
	{
	if (empty($language)) {
	$language = "php";
	}
	else {
	$language = strtolower(str_replace('=', '', $language));
	}

	if( file_exists(XOOPS_ROOT_PATH . '/class/geshi/geshi.php') && (XC_CODE_HIGHLIGHT3_GESHI == 1) && ($type == 'ghcode') ) {
	$source = Legacy_CodeHighlight3::undohschars3($source);
	$source = Legacy_CodeHighlight3::XC_geshihighlightcode( $source, $language );
	}
	elseif( (XC_CODE_HIGHLIGHT3_PHP == 1) && ($type == 'hcode' ) ){
	$source = Legacy_CodeHighlight3::undohschars3($source);
	$source = Legacy_CodeHighlight3::XC_phphighlightcode($source);
	}
	else {
	$source = Legacy_TextFilter::toShow($source);
	}
	return $source;

	}

	function &XC_phphighlightcode($text)
	{

	if(floor(phpversion())<4) return $text;

	$addedtags = 0;
	if (!strpos($text,"<?") and (substr($text,0,2)!="<?")) {
		$text="<?\n".trim($text)."\n?>";
		$addedtags=1;
	}
	ob_start();
	$oldlevel=error_reporting(0);
	highlight_string($text);
	error_reporting($oldlevel);
	$text = ob_get_contents();
	ob_end_clean();
	if ($addedtags) {
		$openingpos = strpos($text,'&lt;?');
		$closingpos = strrpos($text, '?');
		$text=substr($text, 0, $openingpos).substr($text, $openingpos+5, $closingpos-($openingpos+5)).substr($text, $closingpos+5);
	}
	return $text;

	}


	function &XC_geshihighlightcode( $source, $language )
	{

	if( file_exists(XOOPS_ROOT_PATH . '/class/geshi/geshi.php') ) {
   	include_once(XOOPS_ROOT_PATH . '/class/geshi/geshi.php');
	}

	//please customize GeSHi refering to http://qbnz.com/highlighter/documentation.php
     	//$geshi = new GeSHi($source, $language, XOOPS_ROOT_PATH . '/class/geshi/geshi/');
     	$geshi = new GeSHi($source, $language);
	//If your charset is not supported by htmlentities(),
	//please comment out $geshi->set_encoding(_CHARSET);
	//ex) Warning [PHP]: htmlspecialchars(): charset `EUC-KR' not supported, assuming iso-8859-1
	//refer to http://www.php.net/htmlentities
    	$geshi->set_encoding(_CHARSET);
	//Don't change header_type!!
	$geshi->set_header_type(GESHI_HEADER_NONE);
    	$geshi->enable_classes(false);
	$geshi->set_link_target ("_blank");
	//$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); 
	$code = $geshi->parse_code();
	return $code;
	}

    	function &undohschars3(&$text)
    	{
        	$text = preg_replace(array("/&gt;/i", "/&lt;/i", "/&quot;/i", "/&#039;/i"), array(">", "<", "\"", "'"), $text);
	$ret = preg_replace(array("/&amp;/i"), array('&'), $text);
	return $ret;
	}


}

?>


PS:
This hack is still unstable and may require some resource of your server!!
( T.T..anyway... )

PS2:
Don't use this hack with my other highlight hacks ( CodeHighlight.class.php, CodeHighlight2.class.php )

PS3:
Umm...
Still..I am not good at Xoops Cube.
So...I can't guarantee this hack..^^;;
( Tested on XOOPS Cube Legacy 2.1.1)

------------
^^;;


----------------
the most educational site, wanisys.net
XOOPS Cube Korean Support Site !!

gigamaster
Posted on: 2007/6/26 6:48
Home away from home
Joined: 2005/12/30
From: Switzerland
Posts: 1227
Re: Code Highlight3 Preload Hack ( New xcode version, for XC )
I hope to get some free time to test this at xoopserver.com as i'm coloring manually the code syntax within Pico.

Thanks to keep this preload up to date!

Best Regards,


----------------
Xoops Cube Support

MAC OSX - Apache 2.2 - MySQL 5.5 - PHP 5.3
Free BSD - Apache 2.2 - MySQL 5.5 - PHP 5.2

Threaded | Newest First Previous Topic | Next Topic | Top

Register To Post
 

Who's Online
22 user(s) are online (9 user(s) are browsing Forums)

Members: 0
Guests: 22

more...

Welcome | News | Overview | Documentation | Forum | Tutorialstop
Brasilian | French | German | Greek | Japanese | Korean | Russian | T-Chinese
Powered by XOOPS Cube © 2001-2011 The XOOPS Cube Project