PHPCMS V9使用 syntax-highlighter 进行代码高亮

新增一个函数库文件,放到phpcms自动加载目录下

phpcms\libs\functions\autoload\highlighter.func.php

[php]

<?php
/**
*  代码高亮函数库
*
* @copyright            (C) 2005-2011 4wei
* @license                http://www.4wei.cn/
* @lastmodify            2012-2-3
*/

function highlighter($content, $lang="java")
{
preg_match_all("@(\[($lang)\][\s\S]+\[/$lang\])@is", $content, $codes);

$codes = $codes[1];

if(empty($codes)) return $content;

foreach ($codes as $code)
{
$content = str_replace($code, highlighter_format($code, $lang), $content);
}

return $content;
}

function highlighter_format($code, $lang)
{
$code = strip_tags(preg_replace(‘/<br\\s*?\/??>/i’, ”, $code));

$code = str_replace("[$lang]", "<pre class=\"brush: {$lang}; auto-links: false; \">", $code);
$code = str_replace("[/$lang]", "</pre>", $code);
$code = str_replace(array("“", "”"), ‘"’, $code);

return $code;
}

?>

[/php]

模板中调用需要格式化的数据:

[html]

<script type="text/javascript" src="{JS_PATH}syntax-highlighter/scripts/brush.js"></script>
<link type="text/css" rel="stylesheet" href="{JS_PATH}syntax-highlighter/styles/shCore.css"/>
<link type="text/css" rel="stylesheet" href="{JS_PATH}syntax-highlighter/styles/shThemeDefault.css"/>
<script type=’text/javascript’>
<!–
$(document).ready(function(){
SyntaxHighlighter.config.clipboardSwf = ‘{JS_PATH}syntax-highlighter/scripts/clipboard.swf’;
SyntaxHighlighter.all();
});
//–>
</script>

{highlighter($content, "php")}

[/html]

并将相关js文件上传到phpcms的 static目录下

statics