CKEditor中加入代码高亮插件syntaxhighlighter

CKEditor是新一代的FCKeditor,是一个重新开发的版本。CKEditor是全球最优秀的网页在线文字编辑器之一,因其惊人的性能与可扩展性而广泛的被运用于各大网站。 

官网 下载ckeditor,我下载的是CKEditor 3.3.1 。CKEditor与原来的FCKeditor有太大的不同了,作为开发人员,在做自己的博客的时候总是需要贴代码的,只好给它先做一个插入代码的插件了。高亮代码用的是"SyntaxHighlighter "。 

1、在"ckeditor/plugins/"目录下新建一个"insertcode"目录,然后在"insertcode"目录下新建一个"plugin.js",输入以下代码:

Js代码
  1. CKEDITOR.plugins.add('insertcode', {  
  2.     requires: ['dialog'],  
  3.     init: function(a){  
  4.         var b = a.addCommand('insertcode'new CKEDITOR.dialogCommand('insertcode'));  
  5.         a.ui.addButton('insertcode', {  
  6.             label: a.lang.insertcode.toolbar,  
  7.             command: 'insertcode',  
  8.             icon: this.path + 'images/code.jpg'  
  9.         });  
  10.         CKEDITOR.dialog.add('insertcode'this.path + 'dialogs/insertcode.js');  
  11.     }  
  12. });  

 2、增加"images"目录,放入一个"code.jpg"的图片(附件中上传了一个code的jpg图片,大家可以直接使用)。

3、增加"dialogs"目录,新建一个"insertcode.js",输入如下代码:

Js代码
  1. CKEDITOR.dialog.add('insertcode'function(editor){  
  2.     var escape = function(value){  
  3.         return value;  
  4.     };  
  5.     return {  
  6.         title: 'Insert Code Dialog',  
  7.         resizable: CKEDITOR.DIALOG_RESIZE_BOTH,  
  8.         minWidth: 720,  
  9.         minHeight: 480,  
  10.         contents: [{  
  11.             id: 'cb',  
  12.             name: 'cb',  
  13.             label: 'cb',  
  14.             title: 'cb',  
  15.             elements: [{  
  16.                 type: 'select',  
  17.                 label: 'Language',  
  18.                 id: 'lang',  
  19.                 required: true,  
  20.                 'default''csharp',  
  21.                 items: [['ActionScript3''as3'], ['Bash/shell''bash'], ['C#''csharp'], ['C++''cpp'], ['CSS''css'], ['Delphi''delphi'], ['Diff''diff'], ['Groovy''groovy'], ['Html''xhtml'], ['JavaScript''js'], ['Java''java'], ['JavaFX''jfx'], ['Perl''perl'], ['PHP''php'], ['Plain Text''plain'], ['PowerShell''ps'], ['Python''py'], ['Ruby''rails'], ['Scala''scala'], ['SQL''sql'], ['Visual Basic''vb'], ['XML''xml']]  
  22.             }, {  
  23.                 type: 'textarea',  
  24.                 style: 'width:700px;height:420px',  
  25.                 label: 'Code',  
  26.                 id: 'code',  
  27.                 rows: 31,  
  28.                 'default'''  
  29.             }]  
  30.         }],  
  31.         onOk: function(){  
  32.             code = this.getValueOf('cb''code');  
  33.             lang = this.getValueOf('cb''lang');  
  34.             html = '' + escape(code) + '';  
  35.             editor.insertHtml("<pre class=/"brush:" + lang + ";/">" + html + "</pre>");  
  36.         },  
  37.         onLoad: function(){  
  38.         }  
  39.     };  
  40. });  

 我是用"syntaxhighlighter"来做代码高亮的,如果你不喜欢它也可以换成其它的。 

4、接下来就是把插件加入到CKEditor里了,我是直接修改CKEditor插件的核心文件的,因为我是把“插入代码”功能做为一个编辑器必要的功能来使用的。

     找到ckeditor目录下的"ckeditor.js",这里的代码是经过压缩的,我们用CKEditor原来的about插件做参考。查找"about",找到"fullPage:false,height:200,plugins:'about,basicstyles ",我们在"about"后面增加",insertcode",这里就变成"plugins:'about,insertcode ,basicstyles"。

    继续查找"about",找到"j.add('about',{init:function(l){var m=l.addCommand('about',new a.dialogCommand('about'));m.modes={wysiwyg:1,source:1};m.canUndo=false;l.ui.addButton('About',{label:l.lang.about.title,command:'about'});a.dialog.add('about',this.path+'dialogs/about.js');}}); ",我们在这个分号后面增加"j.add('insertcode', {requires: ['dialog'],init: function(l){l.addCommand('insertcode', new a.dialogCommand('insertcode'));l.ui.addButton('insertcode', {label: l.lang.insertcode.toolbar,command: 'insertcode',icon: this.path + 'images/code.jpg'});a.dialog.add('insertcode', this.path + 'dialogs/insertcode.js');}}); "。

    接下来查找"i.toolbar_Basic=",这就是CKEditor默认的工具栏了,我们在这里加上",insertcode ",你可以加在你想要的位置。比如我的"['Maximize','ShowBlocks','-','insertcode'] "。

    5、进入"ckeditor/lang",分别在"en.js","zh.js","zh-cn.js"中增加",insertcode:'Insert Code' ",",insertcode:'插入代碼' ",",insertcode:'插入代码' "。

    6、对CKEditor的修改已经OK了,还有最后一步就是在你需要高亮代码的页面引用:

Html代码
  1. < link   type = "text/css"   rel = "stylesheet"   href ="js/syntaxhighlighter/styles/shCore.css" />   
  2. < link   type = "text/css"   rel = "stylesheet"   href ="js/syntaxhighlighter/styles/shThemeDefault.css" />   
  3. < script   type = "text/javascript"   src = "js/syntaxhighlighter/scripts/shCore.js" > </script >   
  4. < script   type = "text/javascript"   src = "js/syntaxhighlighter/scripts/shBrushes.js" ></ script >   

 这四个文件在syntaxhighlighter的下载包里都有,最后,还在页面增加这段JS:

Js代码
  1. <script type= "text/javascript" >  
  2. SyntaxHighlighter.config.clipboardSwf= 'js/syntaxhighlighter/scripts/clipboard.swf' ;  
  3. SyntaxHighlighter.all();  
  4. </script>  

 CKEditor的"插入代码"插件就OK了。

转载于:https://www.cnblogs.com/html8/archive/2011/07/29/2121427.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值