Enhance BBcode with Delegate
I have used WordPress Plugin for PHP Code Highliting ,until I understood XOOPS Cube Delegate can do same thing.
From a few weeks ago, I'm using PHP Code Highliting Enhanced XCODE(BBCode) adding [phpsrc] tag.
If you make a CustomSanitizer.class.php in /preload folder and fill following code in this file, you can use 3 more XCODE(BBCode) Tags.
-[siteimg] : simlally to [siteurl] tag
-[phpsrc] : for whole PHP source file displaying (including <?php tag)
-[phpcode] : for PHP source snipplet
<?php
class CustomSanitizer extends XCube_ActionFilter
{
function preBlockFilter() {
$this->mController->mRoot->mDelegateManager->add("MyTextSanitizer.XoopsCodePre",array(&$this,"BBCodePre"));
}
function BBCodePre(&$patterns, &$replacements, $allowimage) {
// Replacement rules for [siteimg] tag
$patterns[] = "/\[siteimg align=(['\"]?)(left|center|right)\\1]([^\"\(\)\?\&'<>]*)\[\/siteimg\]/sU";
$patterns[] = "/\[siteimg]([^\"\(\)\?\&'<>]*)\[\/siteimg\]/sU";
if( $allowimage ) {
$replacements[] = '<img src="'.XOOPS_URL.'/\\3" align="\\2" alt="" />';
$replacements[] = '<img src="'.XOOPS_URL.'/\\1" alt="" />';
} else {
$replacements[] = '<a href"'.XOOPS_URL.'/\\3" target="_blank">'.XOOPS_URL.'/\\3</a>';
$replacements[] = '<a href"'.XOOPS_URL.'/\\1" target="_blank">'.XOOPS_URL.'/\\1</a>';
}
$patterns[] = '/\[phpcode\](.*?)\[\/phpcode\]/es';
$replacements[] = "CustomSanitizer::phpSource('\\1',true);";
$patterns[] = '/\[phpsrc\](.*?)\[\/phpsrc\]/es';
$replacements[] = "CustomSanitizer::phpSource('\\1',false);";
}
function phpSource($text,$tag=false) {
$maxlines = 30;
$maxwidth = "85%";
$text =str_replace(array('\"'),array('"'),$text);
if (!preg_match('/[<>"\']+/',$text)) {
$text =str_replace(array('>','<','"','''),array('>','<','"',"'"),$text);
}
$text = trim($text);
$linecount = count(explode("\n",$text))+1;
$text = preg_replace("/\r\n/","\n",$text);
if ($linecount > $maxlines) {
$linecount = $maxlines + 0.5;
}
if ($tag) {
$text = highlight_string("<?php\n".$text."\n?>", true);
$text = preg_replace('/(<font color="#0000BB">)<\?php<br \/>/','$1',$text);
$text = str_replace("<font color=\"#0000BB\">?></font>\n",'',$text);
} else {
$text = highlight_string($text, true);
}
$text = str_replace("<code><font color=\"#000000\">\n",'<code><font color="#000000">',$text);
return '<div class="phpsource" style="padding:0.0em 10px 0.1em 10px;margin: 5px 10px 5px 20px;font-size:12px;line-height:1.2;height:'.$linecount*1.2.'em;width:'.$maxwidth.';max-width:'.$maxwidth.';overflow:auto;">'.$text.'</div>';
}
}
?>
Simllar Enhancement is published by wanikoo in xoopscubeorg.
-Code Highlight2 Preload Hack ( XCCodeHighlight Version, for XC )
-Code Highlight3 Preload Hack ( New xcode version, for XC )
Comments
RSS feed for comments on this post.
No comments yet.
Leave a Comment
Sorry, the comment form is closed at this time.