3.1.8 高亮代码显示

八刀丁二
八刀丁二 这家伙很懒,还没有设置简介

6 人点赞了该文章 · 3390 浏览

亮图-可根据以下步骤修改,只改官方一个文件!

1.jpg
2.jpg

修改官方代码插件的标签

 
  1. 首先下载高亮js https://highlightjs.org/download/将下载好的文件放置             根目录/static/js/
  2. 打开 根目录/system/Services/BBCode.php
首先添加一个方法
	//添加一个类变量 用于首次加载
	protected $on_js = null;
    //添加一个载入js和高亮样式的方法 
    private function gaoliang(){
        if(!$this->on_js){
        TPL::import_js('js/highlight/highlight.pack.js'); //您保存js路径
        //高亮样式=styles路径下的文件名 官方有例子可参照!
        TPL::import_css('js/highlight/styles/monokai-sublime.css');
        //用于初始化,当然你有更好的办法可以更改
        $this->on_js = '<script>$(document).ready(function() {$(\'pre code\').each(function(i, block) {   hljs.highlightBlock( block); }); });</script>';
        return  $this->on_js; 
        }else{
            return  ''; 
        }
    }
    
添加完毕 修改 _code_callback() 方法
	private function _code_callback($match)
	{
	    //用于自定义指定样式! 如果需要可以根据下面方法修改! 
	    $css = $match[2]?'class="'.$match[2].'"':'';
	    //这里调用高亮的方法!
		return $this->gaoliang()."<pre><code ".$css.">" . str_replace('[', '<span>[</span>', $match[1]) . "</code></pre>";
	}
修改可自定义指定样式 在构造器增加代码,并非增加方法,(注意)
public function __construct()
    {
        //可指定脚本高亮样式
        $this->bbcode_table["/\[code-.*?\](.*?)\[\/code-(.*?)\]/is"] = '_code_callback';
        //其他代码 
    }
    

发布于 2016-03-27 14:48

免责声明:

本文由 八刀丁二 原创发布于 WeCenter ,著作权归作者所有。

登录一下,更多精彩内容等你发现,贡献精彩回答,参与评论互动

登录! 还没有账号?去注册

cooldev
2019-02-19 08:13
按照你的方法但是修改不成功,请问我改的文件对吗?附上自己的/system/Services/BBCode.php <?php class Services_BBCode { //添加一个类变量 用于首次加载 protected $on_js = null; //添加一个载入js和高亮样式的方法 private function gaoliang(){ if(!$this->on_js){ TPL::import_js('js/highlight/highlight.pack.js'); //您保存js路径 //高亮样式=styles路径下的文件名 官方有例子可参照! TPL::import_css('js/highlight/styles/monokai-sublime.css'); //用于初始化,当然你有更好的办法可以更改 $this->on_js = ''; return $this->on_js; }else{ return ''; } } protected $bbcode_table = array(); private function _code_callback($match) { //用于自定义指定样式! 如果需要可以根据下面方法修改! $css = $match[2]?'class="'.$match[2].'"':''; //这里调用高亮的方法! return $this->gaoliang()." " . str_replace('[', '[', $match[1]) . " "; } private function _b_callback($match) { return "<strong>$match[1]</strong>"; } private function _i_callback($match) { return "<em>$match[1]</em>"; } private function _quote_callback($match) { return "<blockquote><p>$match[1]</p></blockquote>"; } private function _size_callback($match) { return "<span style=\"font-size:$match[1]px\">$match[2]</span>"; } private function _s_callback($match) { return "<del>$match[1]</del>"; } private function _u_callback($match) { return '<span style="text-decoration:underline;">' . $match[1] . '</span>'; } private function _url_callback($match) { if (substr($match[1], 0, 4) != 'http') { return $match[1]; } return "<a href=\"$match[1]\" rel=\"nofollow\" target=\"_blank\">$match[1]</a>"; } private function _link_callback($match) { if (substr($match[1], 0, 4) != 'http') { return $match[2]; } return "<a href=\"$match[1]\" rel=\"nofollow\" target=\"_blank\">$match[2]</a>"; } private function _img_callback($match) { if (substr($match[1], 0, 4) != 'http') { return $match[1]; } return "<img src=\"$match[1]\" />"; } private function _list_callback($match) { $match[1] = preg_replace_callback("/\[\*\](.*?)\[\/\*\]/is", array(&$this, '_list_element_callback'), $match[1]); return "<ul>" . preg_replace("/[\n\r?]/", "", $match[1]) . "</ul>"; } private function _list_element_callback($match) { return "<li>" . preg_replace("/[\n\r?]$/", "", $match[1]) . "</li>"; } private function _video_callback($match) { return load_class('Services_VideoUrlParser')->parse($match[1]); } private function _list_advance_callback($match) { if ($match[1] == '1') { $list_type = 'ol'; } else { $list_type = 'ul'; } $match[2] = preg_replace_callback("/\[\*\](.*?)\[\/\*\]/is", array(&$this, '_list_element_callback'), $match[2]); return '<' . $list_type . '>' . preg_replace("/[\n\r?]/", "", $match[2]) . '</' . $list_type . '>'; } public function __construct() { //可指定脚本高亮样式 $this->bbcode_table["/\[code-.*?\](.*?)\[\/code-(.*?)\]/is"] = '_code_callback'; //其他代码 // // /* // Replace [code]...[/code] with <pre><code>...</code></pre> $this->bbcode_table["/\[code\](.*?)\[\/code\]/is"] = '_code_callback'; */ // Replace [b]...[/b] with <strong>...</strong> $this->bbcode_table["/\[b\](.*?)\[\/b\]/is"] = '_b_callback'; // Replace [i]...[/i] with <em>...</em> $this->bbcode_table["/\[i\](.*?)\[\/i\]/is"] = '_i_callback'; // Replace [quote]...[/quote] with <blockquote><p>...</p></blockquote> $this->bbcode_table["/\[quote\](.*?)\[\/quote\]/is"] = '_quote_callback'; // Replace [size=30]...[/size] with <span style="font-size:30%">...</span> $this->bbcode_table["/\[size=(\d+)\](.*?)\[\/size\]/is"] = '_size_callback'; // Replace [s] with <del> $this->bbcode_table["/\[s\](.*?)\[\/s\]/is"] = '_s_callback'; // Replace [u]...[/u] with <span style="text-decoration:underline;">...</span> $this->bbcode_table["/\[u\](.*?)\[\/u\]/is"] = '_u_callback'; // Replace [color=somecolor]...[/color] with <span style="color:somecolor">...</span> /*$this->bbcode_table["/\[color=([#a-z0-9]+)\](.*?)\[\/color\]/is"] = function ($match) { return '<span style="color:' . $match[1] . ';">' . $match[2] . '</span>'; };*/ // Replace [url]...[/url] with <a href="...">...</a> $this->bbcode_table["/\[url\](.*?)\[\/url\]/is"] = '_url_callback'; // Replace [url=http://www.google.com/]A link to google[/url] with <a href="http://www.google.com/">A link to google</a> $this->bbcode_table["/\[url=(.*?)\](.*?)\[\/url\]/is"] = '_link_callback'; // Replace [img]...[/img] with <img src="..."/> $this->bbcode_table["/\[img\](.*?)\[\/img\]/is"] = '_img_callback'; // Replace [video]...[/video] with swf video player $this->bbcode_table["/\[video\](.*?)\[\/video\]/is"] = '_video_callback'; // Replace [list]...[/list] with <ul><li>...</li></ul> $this->bbcode_table["/\[list\](.*?)\[\/list\]/is"] = '_list_callback'; // Replace [list=1|a]...[/list] with <ul|ol><li>...</li></ul|ol> $this->bbcode_table["/\[list=(1|a)\](.*?)\[\/list\]/is"] = '_list_advance_callback'; return $this; } public function parse($text, $escapeHTML = false, $nr2br = false) { if (! $text) { return false; } if ($escapeHTML) { $text = htmlspecialchars($text); } foreach ($this->bbcode_table AS $key => $val) { $text = preg_replace_callback($key, array(&$this, $val), $text); } if ($nr2br) { $text = nl2br($text); } return $text; } }
rayyang
2016-03-28 16:02
谢谢分享,顶下
八刀丁二
2016-03-27 20:27
样式级别和加载顺序要搞清楚
龙天
2016-03-27 20:06
就是code 中不能有其它样式,加粗也都不行。
八刀丁二
2016-03-27 19:09
不太理解你的思维。
龙天
2016-03-27 18:49
为什么BB码不支持嵌套呢? 一个内容只能用一个样式。
chenronghua
2016-03-27 18:41
已经使用 openskill.cn
龙天
2016-03-27 18:34
找到了,通过色号找到了。
龙天
2016-03-27 18:16
在那个位置?没找到位置
八刀丁二
2016-03-27 18:14
直接编辑官方样式文件 code 样式