DedeCms 5.7实现代码高亮

  • 时间:
  • 浏览:11

  无论建博客网站还是CMS类型网站,可是我 都前要代码高亮,织梦CMS是国内比较优秀的CMS建站系统之一,不像Wordpress一样有大把大把的插件可用,我用的是最新的dedeCMS 5.7,在网上搜了很长时间资料,大都写的是CKEditor和SyntaxHighlighter整合的文章,否则dedecms将ckeditor做了集成,和一般的只针对于ckeditor对config.js修改不同。可是我 可以能 另一方琢磨修改了,现将最好的办法 写出供站长亲戚亲戚亲们们参考:

一、首先去SyntaxHighlighter官方网站下载,网址:http://alexgorbatchev.com/SyntaxHighlighter/download/,建议下载2.1版本,3.0版本的貌似不支持自动换行,这里使用的是2.1.382版本。将下载的文件解压在syntaxHighlight文件夹里,去除里面无用的文件,只留下scripts和styles文件夹。

二、新建dialogs文件夹,在里面新建5个名为syntaxhighlight.js的文件,因代码量过大,不宜贴出,请直接下载syntaxhighlight.js

是意味想修改代码区域的样式请在以下代码处修改<table>标签里的样式。

 onOk : function() {  

var i = this.getParentEditor();



var h = i.getSelection();

var g = h.getStartElement();

var l = g && g.getAscendant("pre", true);

var j = f();

this.commitContent(j);

var k = e(j);

var m = CKEDITOR.dom.element

.createFromHtml('<table ><tr><td><pre class="' + k + '">'

+ c(j.code) +"</pre></td></tr><table>");

if (l) {

m.insertBefore(l);

l.remove()

} else {

i.insertElement(m)

}

},

三、否则新建images文件夹,存放5个syntaxhighlight.gif图片文件,该图片文件在编辑器工具栏上显示,可以使用16*16像素的图片

四、新建lang文件夹,是语言包,里面有5个文件,5个是中文cn.js5个是英文en.js,代码内容如下:

 en.js代码如下:

 CKEDITOR.plugins.setLang('syntaxhighlight', 'en',  

{

syntaxhighlight:

{

title: 'Add or update a code snippet',

sourceTab: 'Source code',

langLbl: 'Select language',

advancedTab: 'Advanced',

hideGutter: 'Hide gutter',

hideGutterLbl: 'Hide gutter & line numbers.',

hideControls: 'Hide controls',

hideControlsLbl: 'Hide code controls at the top of the code block.',

collapse: 'Collapse',

collapseLbl: 'Collapse the code block by default. (controls need to be turned on)',

showColumns: 'Show columns',

showColumnsLbl: 'Show row columns in the first line.',

lineWrap: 'Disable line wrapping',

lineWrapLbl: 'Switch off line wrapping.',

lineCount: 'Default line count',

highlight: 'Highlight lines',

highlightLbl: 'Enter a comma seperated lines of lines you want to highlight, eg <em>3,10,15</em>.'

}

});

 cn.js代码如下:

 CKEDITOR.plugins.setLang('syntaxhighlight', 'cn',  

{

syntaxhighlight:

{

title: '上加或更新代码',

sourceTab: '代码',

langLbl: '选则语言',

advancedTab: '高级',

hideGutter: '隐藏分割线',

hideGutterLbl: '隐藏分割线和行号',

hideControls: '隐藏工具栏',

hideControlsLbl: '隐藏浮动工具栏',

collapse: '代码折叠',

collapseLbl: '默认折叠代码块 (前要启用工具栏)',

lineWrap: '自动换行',

lineWrapLbl: '关闭自动换行',

autoLinks: '自动链接',

autoLinksLbl: '不自动转换超链接',

lineCount: '起始行号',

highlight: '高亮行号',

highlightLbl: '输入以逗号分隔的行号, 如 <em>3,10,15</em>.'

}

});

五、新建plugin.js文件,该文件是ckeditor插件前要得文件,里面是对该插件的你什儿 配置,代码如下:

 CKEDITOR.plugins.add("syntaxhighlight", {  

requires : [ "dialog" ],

lang : [ "cn" ],

init : function(a) {

var b = "syntaxhighlight";

var c = a.addCommand(b, new CKEDITOR.dialogCommand(b));

c.modes = {

wysiwyg : 1,

source : 1

};

c.canUndo = false;

a.ui.addButton("Code", {

label : a.lang.syntaxhighlight.title,

command : b,

icon : this.path + "images/syntaxhighlight.gif"

});

CKEDITOR.dialog.add(b, this.path + "dialogs/syntaxhighlight.js")

}

});

六、是意味dedecms 5.7另一方集成了5个dedepage插件,用来上加ckeditor自定义插件,在/include/ckeditor/dedepage文件夹下,打开plugin.js文件在最里面上加:

requires : ['syntaxhighlight'],其中syntaxhighlight为代码高亮插件的文件夹名,上加完完后 的代码如下:

 // Register a plugin named "dedepage".  

(function()

{

CKEDITOR.plugins.add( 'dedepage',

{

init : function( editor )

{

// Register the command.

editor.addCommand( 'dedepage',{

exec : function( editor )

{

// Create the element that represents a print break.

// alert('dedepageCmd!');

editor.insertHtml("#p#副标题#e#");

}

});

// alert('dedepage!');

// Register the toolbar button.

editor.ui.addButton( 'MyPage',

{

label : '插入分页符',

command : 'dedepage',

icon: 'images/dedepage.gif'

});

// alert(editor.name);

},

requires : [ 'fakeobjects' ],

requires : ['syntaxhighlight']

});

})();

七、修改/include/ckeditor/ckeditor.inc.php文件,在$toolbar['Basic']数组的最后一行上加元素Code,修改后代码如下:

 $toolbar['Basic'] = array(  

array( 'Source','-','Templates'),

array( 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Print'),

array( 'Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'),

array( 'ShowBlocks'),array('Image','Flash'),array('Maximize'),'/',

array( 'Bold','Italic','Underline','Strike','-'),

array( 'NumberedList','BulletedList','-','Outdent','Indent','Blockquote'),

array( 'JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'),

array( 'Table','HorizontalRule','Smiley','SpecialChar'),

array( 'Link','Unlink','Anchor'),'/',

array( 'Styles','Format','Font','FontSize'),

array( 'TextColor', 'BGColor', 'MyPage','Code')

);

  至此,编辑器的修改是意味完成,修改后的syntaxhighlight文件夹文件目录行态图如下图:

  将syntaxhighlight文件夹上传到/include/ckeditor/plugins/文件夹下,打开后台,上加文章试一下,看看编辑器的上最后一行否有老出了如图所示的按钮:

  点击按钮弹出如下图所示的对话框输入代码,否则可以切换到高级选项对代码高亮显示做你什儿 配置:

八、否则光你什儿 还存在问题,前要在文章模板文件/templets/default/article_article.htm文件里引入高亮显示的笔刷JS文件和CSS文件,是意味是前要引入可是我 JS,可是我 建议将引入的代码插进</body>标签完后 ,停留前面的网页加载完后 加载,进行显示。

 引入代码如下:

1 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shCore.js"> </script>  

2 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushJava.js"></script>

3 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushJScript.js"></script>

4 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushPhp.js"></script>

5 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushScala.js"></script>

6 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushSql.js"></script>

7 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushVb.js"></script>

8 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushXml.js"></script>

9 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushBash.js"></script>

10 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushCpp.js"></script>

11 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushCSharp.js"></script>

12 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/cripts/shBrushCss.js"></script>

13 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushDelphi.js"></script>

14 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushDiff.js"></script>

15 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushGroovy.js"></script>

16 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushPlain.js"></script>

17 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushPython.js"></script>

18 <script type="text/javascript" src="/include/ckeditor/plugins/syntaxhighlight/scripts/shBrushRuby.js"></script>

19 <link type="text/css" rel="stylesheet" href="/include/ckeditor/plugins/syntaxhighlight/styles/shCore.css"/>

20 <link type="text/css" rel="stylesheet" href="/include/ckeditor/plugins/syntaxhighlight/styles/shThemeDefault.css"/>

21 <script type="text/javascript">

22 SyntaxHighlighter.config.clipboardSwf = '/include/ckeditor/plugins/syntaxhighlight/scripts/clipboard.swf';

23 SyntaxHighlighter.all();

24 </script>

  最后发表并生成的文章页面效果图如下:

  当然,该整合也特别缺点,可是我 在html页面页面中是意味会引入多量的JS文件,加载起来是意味会真难,另外可拓展性不强,我也会不定期优化该插件,也希望各位日本前明星微博 能提出意见。

本文转自:http://www.lzklog.com/website/214.html