-
Notifications
You must be signed in to change notification settings - Fork 78
/
syntaxhighlighter_mce.js
109 lines (86 loc) · 3.28 KB
/
syntaxhighlighter_mce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* SyntaxHighlighter shortcode plugin
* by Andrew Ozz of Automattic
*/
// Avoid JS errors
if ( typeof syntaxHLcodes == 'undefined' ) {
var syntaxHLcodes = 'sourcecode';
}
(function() {
tinymce.create('tinymce.plugins.SyntaxHighlighterPlugin', {
init : function(ed, url) {
var t = this;
ed.onBeforeSetContent.add(function(ed, o) {
o.content = t._htmlToVisual(o.content);
});
ed.onPostProcess.add(function(ed, o) {
if ( o.save ) {
o.content = t._visualToHtml(o.content);
}
});
},
getInfo : function() {
return {
longname : 'SyntaxHighlighter Assister',
author : 'Automattic',
authorurl : 'http://wordpress.com/',
infourl : 'http://wordpress.org/extend/plugins/syntaxhighlighter/',
version : tinymce.majorVersion + "." + tinymce.minorVersion
};
},
// Private methods
_visualToHtml : function(content) {
content = tinymce.trim(content);
// 2 <br> get converted to \n\n and are needed to preserve the next <p>
content = content.replace(new RegExp('(<pre>\\s*)?(\\[(' + syntaxHLcodes + ')[^\\]]*\\][\\s\\S]*?\\[\\/\\3\\])(\\s*<\\/pre>)?', 'gi'),
function(a) {
a = a.replace( /<br \/>([\t ])/g, '<br \/><%%KEEPWHITESPACE%%>$1' );
return a + '<br /><br />';
});
content = content.replace(/<\/pre>(<br \/><br \/>)?<pre>/gi, '\n');
return content;
},
_htmlToVisual : function(content) {
content = tinymce.trim(content);
content = content.replace(new RegExp('(<p>\\s*)?(<pre>\\s*)?(\\[(' + syntaxHLcodes + ')[^\\]]*\\][\\s\\S]*?\\[\\/\\4\\])(\\s*<\\/pre>)?(\\s*<\\/p>)?', 'gi'), '<pre>$3</pre>');
content = content.replace(/<\/pre><pre>/gi, '\n');
// Remove anonymous, empty paragraphs.
content = content.replace(/<p>(\s| )*<\/p>/mg, '');
// Look for <p> <br> in the [tag]s, replace with <br />
content = content.replace(new RegExp('\\[(' + syntaxHLcodes + ')[^\\]]*\\][\\s\\S]+?\\[\\/\\1\\]', 'gi'),
function(a) {
return a.replace(/<br ?\/?>[\r\n]*/g, '<br />').replace(/<\/?p( [^>]*)?>[\r\n]*/g, '<br />');
});
return content;
}
});
// Register plugin
tinymce.PluginManager.add('syntaxhighlighter', tinymce.plugins.SyntaxHighlighterPlugin);
})();
var syntaxHLlast = 0;
function pre_wpautop2(content) {
var d = new Date(), time = d.getTime();
if ( time - syntaxHLlast < 500 )
return content;
syntaxHLlast = time;
content = content.replace(new RegExp('<pre>\\s*\\[(' + syntaxHLcodes + ')', 'gi'), '[$1');
content = content.replace(new RegExp('\\[\\/(' + syntaxHLcodes + ')\\]\\s*<\\/pre>', 'gi'), '[/$1]');
content = this._pre_wpautop(content);
content = content.replace(new RegExp('\\[(' + syntaxHLcodes + ')[^\\]]*\\][\\s\\S]+?\\[\\/\\1\\]', 'gi'),
function(a) {
return a.replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&').replace(/<%%KEEPWHITESPACE%%>/g, '');
});
return content;
}
function wpautop2(content) {
// js htmlspecialchars
content = content.replace(new RegExp('\\[(' + syntaxHLcodes + ')[^\\]]*\\][\\s\\S]+?\\[\\/\\1\\]', 'gi'),
function(a) {
return a.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
});
return this._wpautop(content);
}
switchEditors._pre_wpautop = switchEditors.pre_wpautop;
switchEditors._wpautop = switchEditors.wpautop;
switchEditors.pre_wpautop = pre_wpautop2;
switchEditors.wpautop = wpautop2;