欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ckeditor syntaxhighlighter代码高亮插件配置分享

程序员文章站 2022-06-16 20:05:53
最近由于自己想做一个网站形式的代码库,自已写一个在线文本编辑器,对于现在的我来,确实是很不切实际,呵呵!再说了,现在有一个非常好的在线文本编辑器(ckeditor)了,我和...

最近由于自己想做一个网站形式的代码库,自已写一个在线文本编辑器,对于现在的我来,确实是很不切实际,呵呵!再说了,现在有一个非常好的在线文本编辑器(ckeditor)了,我和必再去费这等功夫呢!有现成的,拿过用就是的呗!正所谓的拿来主义!不过这个在线文本编辑器,对于我们程序员来说有一个算是缺陷吧!没有代码高亮的功能!这样把代码贴上去,很不好看!今天晚上,我总是把他给弄出来了。当然也采在别人的肩膀上做成的。在此感谢他们的分享!费话不多说了!咱们进入正题吧!

首先去官方网站下载个ckeditor

其次去官方网站下载个syntaxhighlighter ,这个是代码高亮插件。

还有就是请把我下面提供下载的demo里的syntaxhighlighter/shbrushes.js文件

下载以后,把他们解压,加入项目,如下所示:

ckeditor syntaxhighlighter代码高亮插件配置分享

然后在ckeditor下面新建一个文件夹,命名为: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.toolbar,
      command: 'insertcode',
      icon: this.path + 'images/code.jpg'
    });
    ckeditor.dialog.add('insertcode', this.path + 'dialogs/insertcode.js');
  }
});

目录结构如下图:图二

ckeditor syntaxhighlighter代码高亮插件配置分享

再新建一个images文件夹,放入一个"code.jpg"的图片,如上图所示,当然图片可以从google找一个,16*16大小的就好了。

再新建一个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 () {
    }
  };
});

接下来,我们就把高亮插件插入到ckeditor里来,找到ckeditor文件夹下的"ckeditor.js"。按ctrl+f查找"about",找到"fullpage:false,height:200,plugins:'about,basicstyles",我们在"about"后面增加",insertcode",这里就变成"plugins:'about,insertcode,basicstyles"。

如图:

ckeditor syntaxhighlighter代码高亮插件配置分享

 继续查找"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');}});"。

 如下图:

ckeditor syntaxhighlighter代码高亮插件配置分享

 接下来继续在ckeditor.js查找"i.toolbar_basic=",这就是ckeditor默认的工具栏了,我们在这里加上",insertcode",比如我的"['maximize','showblocks','-','insertcode']"

我添加在如下图选中的文本那个地方:

ckeditor syntaxhighlighter代码高亮插件配置分享

最后一步:进入"ckeditor\lang",请注意是分别在"en.js","zh.js","zh-cn.js"中增加",insertcode:'insert code'",",insertcode:'插入代碼'",",insertcode:'插入代码'",一定要按这个顺序加哦。

如下图是en.js中的,zh-cn.js,zh.js我就不一一截图了。

ckeditor syntaxhighlighter代码高亮插件配置分享

最后在页面上添加如下引用:

<head runat="server">
  <title></title>
  <link type="text/css" rel="stylesheet" href="syntaxhighlighter_3.0.83/styles/shcore.css" />
  <link type="text/css" rel="stylesheet" href="syntaxhighlighter_3.0.83/styles/shthemedefault.css" /> 
  <script type="text/javascript" src="syntaxhighlighter_3.0.83/scripts/shcore.js"></script>
  <script type="text/javascript" src="syntaxhighlighter_3.0.83/scripts/shbrushes.js"></script>
  <script type="text/javascript" src="ckeditor/ckeditor.js"></script>
  <link type="text/css" rel="stylesheet" href="syntaxhighlighter_3.0.83/styles/shcoredefault.css" />
  <script type="text/javascript">    syntaxhighlighter.all();</script>
</head>

页面的代码如下:

<form id="form1" runat="server">
  <div>
    <asp:textbox id="txtcontent" runat="server" textmode="multiline" height="310px" 
      width="100%"></asp:textbox>
    <script type="text/javascript">
      ckeditor.replace('<%= txtcontent.clientid %>', { skin: 'office2003' });
    </script> 
    </script>--%>
    <asp:button runat="server" text="button" onclick="unnamed1_click" />
  </div>
  <asp:literal id="literal1" runat="server"></asp:literal>
  </form>

后台代码:

复制代码 代码如下:

protected void unnamed1_click(object sender, eventargs e)
    {
        this.literal1.text = txtcontent.text;
    }

还有就是在页面的@ page指令中加入 validaterequest="false",加入后的@page指令如下:

复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" validaterequest="false" %>

否则会报错的。

效果图:

ckeditor syntaxhighlighter代码高亮插件配置分享

怎么获取这个文本编辑器里的文本今天白天再写吧!该睡觉了,1点多了,o my god!

(o ye!现在总是很完善了的)

我的demo下载:cheditor+syntaxhighlighter demo ,如果还有点不懂,请多多参考这个demo,或者给我留言吧!

参考博文:http://zhidao.baidu.com/question/302527067.html
http://blog.sina.com.cn/s/blog_5fdcf5c90100uo4r.html