summaryrefslogtreecommitdiffstats
path: root/extensions/tinymce/jscripts/tiny_mce/plugins/mediawiki/editor_plugin.js
blob: e69dc5fb7d61881199d5d58bfa22a323a3535838 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
 * $Id$
 *
 * @author Bret McMillan <bretm@redhat.com>
 * @copyright Copyright © 2004-2008, Red Hat, Inc., All rights reserved.
 *
 * Adapted from:
 * - the "example" and "bbcode" plugins provided by Moxicode
 * - Remy Sharp's wiki2html code: http://remysharp.com/2008/04/01/wiki-to-html-using-javascript/
 * 
 * See also:  http://en.wikipedia.org/wiki/Wikipedia:Cheatsheet
 */

(function() {
	tinymce.create('tinymce.plugins.MediaWikiPlugin', {
		/**
		 * Initializes the plugin, this will be executed after the plugin has been created.
		 * This call is done before the editor instance has finished it's initialization so use the onInit event
		 * of the editor instance to intercept that event.
		 *
		 * @param {tinymce.Editor} ed Editor instance that the plugin is initialized in.
		 * @param {string} url Absolute URL to where the plugin is located.
		 */
		init : function(ed, url) {
                        var t = this;
                        // load wiki2html syncronous calls...
                        // might want to explore async later
                        tinymce.ScriptLoader.load(tinymce.PluginManager.urls.mediawiki + '/wiki2html.js');

			ed.onBeforeSetContent.add(function(ed, o) {
				o.content = t['_mw2html'](o.content);
			});

			ed.onPostProcess.add(function(ed, o) {
				if (o.set)
					o.content = t['_mw2html'](o.content);
				
				if (o.get)
					o.content = t['_html2mw'](o.content);
				
			});
		},

		/**
		 * Returns information about the plugin as a name/value array.
		 * The current keys are longname, author, authorurl, infourl and version.
		 *
		 * @return {Object} Name/value array containing information about the plugin.
		 */
		getInfo : function() {
			return {
				longname : 'MediaWiki plugin',
				author : 'Bret McMillan <bretm@redhat.com>',
				authorurl : 'http://www.redhat.com/',
				infourl : 'http://www.redhat.com/',
				version : "0.1"
			};
		},

		// Private methods

                convertlists : function(s) {
		    var t = this;

		    s = s.replace(/<(?:ol|ul)>(.*?)<\/(?:ol|ul)>/gim, function (m) {
			    var glyph = m.match(/ol/) ? '#' : '*';

			    m.replace(/<li>(.*?)<\/li>/gim, glyph + "$1");

			    // TODO:  fixme, this needs to be recursive...
			    //m = t['convertlists'](m);

			    return m;
			});

		    return s;
                },

		// HTML -> MediaWiki, it'd be nice to get this upstream into remy's code
		_html2mw : function(s) {
		        var t = this;

			function rep(re, str) {
				s = s.replace(re, str);
			};

			// WikiWord urls
			rep(/<a.*?href=\"([A-Z].*?)\".*?>\1<\/a>/gim, "[[$1]]");

			// handle where url's text is the same as the url
			rep(/<a.*?href=\"(.*?)\".*?>\1<\/a>/gim, "$1");


			// handle external urls with body text
			rep(/<a.*?href=\"(.+?)\".*?>(.+?)<\/a>/gim,"[$1 $2]");


			// images
			// XXXXXXXXXXXXXXXXXXXXXX


			// <em> to ''
			rep(/<em>(.*?)<\/em>/gim, "''$1''");

			// <strong> to '''
			rep(/<strong>(.*?)<\/strong>/gim, "'''$1'''");

			// headers
			rep(/<h1>(.*?)<\/h1>/gim, "=$1=");
			rep(/<h2>(.*?)<\/h2>/gim, "==$1==");
			rep(/<h3>(.*?)<\/h3>/gim, "===$1===");
			rep(/<h4>(.*?)<\/h4>/gim, "====$1====");
			rep(/<h5>(.*?)<\/h5>/gim, "=====$1=====");
			rep(/<h5>(.*?)<\/h6>/gim, "======$1======");

			// <p>
			rep(/<p>([\s\S]*?)<\/p>/gim, '\n$1');


			// <p align>
			rep(/<p.*?align\: left.*?>([\s\S]*?)<\/p>/gim, '\n$1');

			// various indents...
			// <p style="padding-left"...>
			rep(/<p .*?padding\-left.*?>([\s\S]*?)<\/p>/gim, '\n  $1');
			rep(/<pre>([\s\S]*?)<\/pre>/gim, '\n $1');
			rep(/<blockquote>([\s\S]*?)<\/blockquote>/gim, '\n  $1');

                        // <nbsp>
			rep(/\&nbsp\;/gim, ' ');

			// <br>
			rep(/<br[^>]*?>/gim, '\n');

			// ul,ol lists
			s = t['convertlists'](s);

			// tables
			// XXXXXXXXXXXXXXXXXXXXXX
			// this doesn't work great atm ... :/
                        rep(/<table.*?>.*?(?:<tbody>)([\s\S]*?)<\/tbody>.*?<\/table>/gim, "{|\n$1|}\n");
                        rep(/<tr.*?>\s*?([\s\S]*?)\s*?<\/tr>/gim, "|-\n$1");
                        rep(/<td.*?>[\s\n]*?([^\n]*?)[\n\s]*?<\/td>/gim, "|$1\n");
			// this last one 'cause my regex's seem to leave <td/>'s all over the place...
			// probably due to a tinymce cleanup function repairing bad html
			// rep(/\s*?<td\/>\s*?/gim, "");

			return s;
		},

		// MediaWiki -> HTML, delegate to remy's code
		_mw2html : function(s) {
		    //s = tinymce.trim(s);
			return s.wiki2html();
		}
	});

	// Register plugin
	tinymce.PluginManager.add('mediawiki', tinymce.plugins.MediaWikiPlugin);
})();