summaryrefslogtreecommitdiffstats
path: root/extensions/tinymce/jscripts/tiny_mce/plugins/mediawiki/editor_plugin.js
blob: 47f2770107884c765299d0da90fb8912ed70a218 (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
/**
 * $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

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

			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]");

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

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

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

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

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

                        // <nbsp>
			//rep(/&nbsp;/gi, ' ');

			// ul,ol lists
			// XXXXXXXXXXXXXXXXXXXXXX
			

			// images
			// XXXXXXXXXXXXXXXXXXXXXX

			// tables
			// XXXXXXXXXXXXXXXXXXXXXX

			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);
})();