打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ckeditor

ckeditor-安插代码功能并高亮显示,结合syntaxhighlighter_2.1

ckeditor---插入代码功能并高亮显示,结合syntaxhighlighter_2.1

给自己做了个小博客网站:http://lc448986375.gicp.net,

我希望能和csdn的博客编辑一样,能够添加高亮显示代码的功能,看了网上很多文章,大部分都是抄袭的,一模一样,而且按照他们的方法有的也不能运行出来,所以我就自己摸索出来了一个方法:

当然我也是按照别人的方法做的,只不过我亲自做了出来,和大家分享一下,如有问题大家一起讨论


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

CKEDITOR.plugins.add('insertcode', {      requires: ['dialog'],      init: function(a){          var b = a.addCommand('insertcode', new CKEDITOR.dialogCommand('insertcode'));          a.ui.addButton('insertcode', {              label: a.lang.insertcode,              command: 'insertcode',              icon: this.path + 'images/code.jpg'          });          CKEDITOR.dialog.add('insertcode', this.path + 'dialogs/insertcode.js');      }  });  

2、在"insertcode"目录下增加"images"目录,放入一个"code.jpg"的图片,随便一个都可以(16*16)

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

CKEDITOR.dialog.add('insertcode', function(editor){      var escape = function(value){          return value;      };         return {          title: 'Insert Code Dialog',          resizable: CKEDITOR.DIALOG_RESIZE_BOTH,          minWidth: 720,          minHeight: 480,          contents: [{              id: 'cb',              name: 'cb',              label: 'cb',              title: 'cb',              elements: [{                  type: 'select',                  label: 'Language',                  id: 'lang',                  required: true,                  'default': 'csharp',                  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']]              }, {                  type: 'textarea',                  style: 'width:700px;height:420px',                  label: 'Code',                  id: 'code',                  rows: 31,                  'default': ''              }]          }],          onOk: function(){              code = this.getValueOf('cb', 'code');              lang = this.getValueOf('cb', 'lang');              html = '' + escape(code) + '';            	//注意这里的代码,网上很多教程都是因为这里组合的时候出错而不能运行            editor.insertHtml("<pre class='brush:"+ lang +"' >"+ html +"</pre>");        },          onLoad: function(){          }      };  });  

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了,还有最后一步就是在你需要高亮代码的页面引用:(注意,需要什么Brush就导如什么,或者直接全部导入)

这些文件在syntaxhighlighter里面的都有

<link type="text/css" rel="stylesheet" href="js/syntaxhighlighter/styles/shCore.css" />   <link type="text/css" rel="stylesheet" href="js/syntaxhighlighter/styles/shThemeDefault.css" />   <script type="text/javascript" src="js/syntaxhighlighter/scripts/shCore.js" > </script>   <script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushJava.js" ></script><script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushCSharp.js" ></script><script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushCss.js" ></script><script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushJScript.js" ></script><script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushPhp.js" ></script><script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushXml.js" ></script><script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushSql.js" ></script><script type="text/javascript" src="js/syntaxhighlighter/scripts/shBrushVB.js" ></script>

最后还要引入这个

这个文件在syntaxhighlighter3之前的版本中才有,我不知道现在的3里面用们代替它,有知道的可以告诉我

<script type= "text/javascript" >  SyntaxHighlighter.config.clipboardSwf= 'js/syntaxhighlighter/scripts/clipboard.swf' ;  SyntaxHighlighter.all();  </script>  



好了,这样就可以了,可以到我的博客中看看效果的: http://lc448986375.gicp.net

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
高亮显示代码syntaxhighlighter
SyntaxHighlighter使用方法[转]
How to Add and Use SyntaxHighlighter 3 for Bl...
ckeditor 使用、获取数据、设置数据
thinkphp下ckeditor+ckfinder的图片上传配置 - 美拓blog
ckeditor3.6.5+ckfinder2.0.2+jsp编辑器配置 支持服务器浏览/上传图片、Flash
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服