ckeditor+代碼高亮

來源:互聯網
上載者:User

更新提示:現在大家可以不必像我這樣為了實現代碼高亮的功能,去修改ckeditor編輯器,大家可以去使用百度編輯器(Ueditor)他有代碼高亮的功能,還蠻好用的,我的個人網站就是的百度編輯器的。歡迎大家去我的部落格看看。

最近由於自己想做一個網站形式的程式碼程式庫,自已寫一個線上文字編輯器,對於現在的我來,確實是很不切實際,呵呵!再說了,現在有一個非常好的線上文字編輯器(ckeditor)了,我和必再去費這等功夫呢!有現成的,拿過用就是的唄!正所謂的拿來主義!不過這個線上文字編輯器,對於我們程式員來說有一個算是缺陷吧!沒有代碼高亮的功能!這樣把代碼貼上去,很不好看!今天晚上,我總是把他給弄出來了。當然也採在別人的肩膀上做成的。在此感謝他們的分享!費話不多說了!咱們進入正題吧!

首先去官方網站下載個ckeditor

其次去官方網站下載個syntaxhighlighter ,這個是代碼高亮外掛程式。

還有就是請把我下面提供下載的Demo裡的syntaxhighlighter/shBrushes.js檔案

下載以後,把他們解壓,加入項目,如下所示:

 

然後在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');
}
});

 

目錄結構如:圖二

再建立一個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"。

 繼續尋找"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.js尋找"i.toolbar_Basic=",這就是CKEditor預設的工具列了,我們在這裡加上",insertcode",比如我的"['Maximize','ShowBlocks','-','insertcode']"

我添加在如選中的文本那個地方:

最後一步:進入"ckeditor\lang",請注意是分別在"en.js","zh.js","zh-cn.js"中增加",insertcode:'Insert Code'",",insertcode:'插入代碼'",",insertcode:'插入代碼'",一定要按這個順序加哦。

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

最後在頁面上添加如下引用:

View Code

<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>

 頁面的代碼如下:

View Code

<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>

後台代碼:

View Code

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" %>

否則會報錯的。 

怎麼擷取這個文字編輯器裡的文本今天白天再寫吧!該睡覺了,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

http://www.cnblogs.com/html8/archive/2011/07/29/2121427.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.