Project

General

Profile

1
/*
2
*   Syntax Highlighter 2.0 plugin for FCKEditor
3
*   ========================
4
*   Copyright (C) 2008  Darren James
5
*   Email : darren.james@gmail.com
6
*   URL : http://www.psykoptic.com/blog/
7
*
8
*   NOTE:
9
*   ========================
10
*   This plugin will add or edit a formatted <pre> tag for FCKEditor
11
*   To see results on the front end of your website
12
*   You will need to install SyntaxHighlighter 2.0.x from
13
*   http://alexgorbatchev.com/wiki/SyntaxHighlighter
14
*
15
*
16
*   This program is free software: you can redistribute it and/or modify
17
*   it under the terms of the GNU General Public License as published by
18
*   the Free Software Foundation, either version 3 of the License, or
19
*   (at your option) any later version.
20

    
21
*   This program is distributed in the hope that it will be useful,
22
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
23
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
*   GNU General Public License for more details.
25

    
26
*   You should have received a copy of the GNU General Public License
27
*   along with this program.  If not, see <http:*www.gnu.org/licenses/>.
28

    
29
*   This program comes with ABSOLUTELY NO WARRANTY.
30
*/
31

    
32
var version = "2.1.0";
33
var dialog = window.parent; // IE7 needs this
34
var oEditor = window.parent.InnerDialogLoaded();
35
var FCK = oEditor.FCK;
36
var FCKLang = oEditor.FCKLang;
37
var FCKConfig = oEditor.FCKConfig;
38
var FCKTools = oEditor.FCKTools;
39
var FCKBrowserInfo = oEditor.FCKBrowserInfo;
40

    
41

    
42
// default syntax object
43
function CodeSyntax() {
44
    var oCodeSyntax = new Object();
45
    oCodeSyntax.Code = oContainerPre.innerHTML;
46
    oCodeSyntax.Advanced = false;
47
    oCodeSyntax.Gutter = false;
48
    oCodeSyntax.NoControls = false;
49
    oCodeSyntax.Collapse = false;
50
    oCodeSyntax.Firstline = 0;
51
    oCodeSyntax.Showcolumns = false;
52
    oCodeSyntax.Highlight = null;
53

    
54
    return oCodeSyntax;
55
}
56

    
57
var oContainerPre = FCK.Selection.MoveToAncestorNode('PRE');
58
var oCodeSyntax = null;
59

    
60
// ----------------------
61
// populate our oCodeSyntax object
62
if (oContainerPre) {
63
    if (oContainerPre.tagName == 'PRE' && GetAttribute(oContainerPre, 'title') == 'code') {
64

    
65
        var CodeSettings = GetAttribute(oContainerPre, 'class', '');
66
        if (CodeSettings.length > 0) {
67

    
68
            // found valid code snippet, populate our CodeSyntax object
69
            oCodeSyntax = new CodeSyntax();
70

    
71
            if (CodeSettings.indexOf(";") > -1) {
72
                // advanced options set
73

    
74
                oCodeSyntax.Advanced = true;
75
                oCodeSyntax.Lang = CodeSettings.substring(CodeSettings.indexOf(":") + 1, CodeSettings.indexOf(";")).replace(/^\s+|\s+$/g, "");
76
                
77
                if (CodeSettings.indexOf("gutter") > -1)
78
                    oCodeSyntax.Gutter = true;
79

    
80
                if (CodeSettings.indexOf("toolbar") > -1)
81
                    oCodeSyntax.NoControls = true;
82

    
83
                if (CodeSettings.indexOf("collapse") > -1)
84
                    oCodeSyntax.Collapse = true;
85

    
86
                if (CodeSettings.indexOf("first-line") > -1) {
87

    
88
                    var match = /first-line: ([0-9]{1,4})/.exec(CodeSettings);
89
                    if (match != null && match.length > 0) {
90
                        oCodeSyntax.Firstline = match[1];
91
                    }
92
                    else {
93
                        oCodeSyntax.Firstline = 0;
94
                    }
95
                }
96
                
97
                // highlighted numbers
98
                if (CodeSettings.indexOf("highlight") > -1) {
99

    
100
                    // make sure we have a comma-seperated list
101
                    if (CodeSettings.match(/highlight: \[[0-9]+(,[0-9]+)*\]/)) {
102
                        // now grab the list
103
                        var match_hl = /highlight: \[(.*)\]/.exec(CodeSettings);
104
                        if (match_hl != null && match_hl.length > 0) {
105
                            oCodeSyntax.Highlight = match_hl[1];
106
                        }
107
                    }
108
                }
109

    
110

    
111
                if (CodeSettings.indexOf("ruler") > -1)
112
                    oCodeSyntax.Showcolumns = true;
113
            }
114
            else {
115
                oCodeSyntax.Lang = CodeSettings;
116
            }
117

    
118
        }
119

    
120
    } else {
121
        oContainerPre = null;
122
    }
123
}
124

    
125
// ----------------------
126
// config tabs
127
window.parent.AddTab('TabSourceCode', FCKLang.SyntaxHightlightTab1);
128
window.parent.AddTab('TabAdvanced', FCKLang.SyntaxHightlightTab2);
129

    
130
function OnDialogTabChange(tabCode) {
131
    ShowE('divSourceCode', (tabCode == 'TabSourceCode'));
132
    ShowE('divAdvanced', (tabCode == 'TabAdvanced'));
133
}
134
// ----------------------
135

    
136
window.onload = function() {
137

    
138
    // translate the dialog box texts
139
    oEditor.FCKLanguageManager.TranslatePage(document);
140
    // load current PRE block
141
    LoadSelected();
142
    // Show the "Ok" button.
143
    dialog.SetOkButton(true);
144
    // Select text field on load.
145
    SelectField('txtCode');
146

    
147
    // set version
148
    GetE('syntaxhighlight-version').innerHTML = version;
149

    
150
}
151

    
152
// ----------------------
153
// setup dialogue
154
function LoadSelected() {
155

    
156
    var ddLang = GetE('ddLang');
157

    
158
    if (!oCodeSyntax) {
159
        // creating new element
160
        if (FCKConfig.SyntaxHighlight2LangDefault != null) {
161

    
162
            for (count = 0; count < ddLang.length; count++) {
163

    
164
                if (ddLang.options[count].value == FCKConfig.SyntaxHighlight2LangDefault) {
165
                    ddLang.selectedIndex = count;
166
                    break;
167
                }
168
            }
169
        }
170

    
171
    }
172
    else {
173

    
174
        // editing existing element
175
        document.getElementById('txtCode').value = HTMLDecode(oCodeSyntax.Code);
176
        ddLang.value = oCodeSyntax.Lang;
177

    
178
        // set any advanced options
179
        if (oCodeSyntax.Advanced) {
180
            if (oCodeSyntax.Gutter)
181
                GetE('chkGutter').checked = true;
182

    
183
            if (oCodeSyntax.NoControls)
184
                GetE('chkNoControls').checked = true;
185

    
186
            if (oCodeSyntax.Collapse)
187
                GetE('chkCollapse').checked = true;
188

    
189
            if (oCodeSyntax.Firstline > 0) {
190
                GetE('chkLineCount').checked = true;
191
                GetE('txtLineCount').disabled = false;
192
                GetE('txtLineCount').value = oCodeSyntax.Firstline
193

    
194
            }
195

    
196
            if (oCodeSyntax.Highlight != null) {
197
                GetE('chkHighlightLine').checked = true;
198
                GetE('txtHighlightLines').disabled = false;
199
                GetE('txtHighlightLines').value = oCodeSyntax.Highlight
200

    
201
            }
202
            
203
            if (oCodeSyntax.Showcolumns)
204
                GetE('chkShowColumns').checked = true;
205

    
206
        }
207

    
208
    }
209
}
210

    
211
// ----------------------
212
// action on dialogue submit
213
function Ok() {
214
    var sCode = GetE('txtCode').value;
215
    var ddLang = GetE('ddLang').value + ";";
216
    var advanced = '';
217

    
218
    oEditor.FCKUndo.SaveUndoStep();
219

    
220
    if (!oContainerPre) {
221
        oContainerPre = FCK.CreateElement('PRE');
222
    }
223

    
224

    
225
    if (GetE('chkGutter').checked)
226
        advanced += "gutter: false; ";
227

    
228
    if (GetE('chkNoControls').checked)
229
        advanced += "toolbar: false; ";
230

    
231
    if (GetE('chkCollapse').checked)
232
        advanced += "collapse: true; ";
233

    
234
    // start line count from custom
235
    if (GetE('chkLineCount').checked)
236
        advanced += "first-line: " + GetE('txtLineCount').value + "; ";
237

    
238
    // highlight lines (strip all spaces)
239
    if (GetE('chkHighlightLine').checked)
240
        advanced += "highlight: [" + GetE('txtHighlightLines').value.replace(/\s/gi, "") + "]; ";
241

    
242
    // show ruler/column tool
243
    if (GetE('chkShowColumns').checked)
244
        advanced += "ruler: true; ";
245
   
246

    
247
    if (FCKBrowserInfo.IsIE) {
248
        // a bug in IE removes linebreaks in innerHTML, so lets use outerHTML instead
249
        oContainerPre.outerHTML = '<pre title="code" class="brush: ' + ddLang + advanced + '">' + HTMLEncode(sCode) + '</pre>';
250
    }
251
    else {
252
        oContainerPre.setAttribute("title", "code");
253
        oContainerPre.setAttribute("class", "brush: " + ddLang + advanced);
254
        oContainerPre.innerHTML = HTMLEncode(sCode);       
255
    }
256

    
257
    return true;
258
}
259

    
260
// ----------------------
261
// Helper functions
262
// ----------------------
263
function HTMLEncode(text) {
264
    if (!text)
265
        return '';
266

    
267
    text = text.replace(/&/g, '&amp;');
268
    text = text.replace(/</g, '&lt;');
269
    text = text.replace(/>/g, '&gt;');
270

    
271
    return text;
272
}
273

    
274
function HTMLDecode(text) {
275
    if (!text)
276
        return '';
277

    
278
    text = text.replace(/&gt;/g, '>');
279
    text = text.replace(/&lt;/g, '<');
280
    text = text.replace(/&amp;/g, '&');
281
    text = text.replace(/<br>/g, '\n');
282
    text = text.replace(/&quot;/g, '"');
283

    
284
    return text;
285
}
286

    
287
function changechk(checkbox, textfield) {
288

    
289
    if (checkbox.checked == true) {
290
        GetE(textfield).disabled = false;
291
    }
292
    else {
293
        GetE(textfield).disabled = true;
294
    }
295

    
296
}
297

    
(3-3/3)