1/*
2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
4 *
5 * == BEGIN LICENSE ==
6 *
7 * Licensed under the terms of any of the following licenses at your
8 * choice:
9 *
10 *  - GNU General Public License Version 2 or later (the "GPL")
11 *    http://www.gnu.org/licenses/gpl.html
12 *
13 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14 *    http://www.gnu.org/licenses/lgpl.html
15 *
16 *  - Mozilla Public License Version 1.1 or later (the "MPL")
17 *    http://www.mozilla.org/MPL/MPL-1.1.html
18 *
19 * == END LICENSE ==
20 *
21 * This is a sample implementation for a custom Data Processor for basic BBCode.
22 */
23
24FCK.DataProcessor =
25{
26	/*
27	 * Returns a string representing the HTML format of "data". The returned
28	 * value will be loaded in the editor.
29	 * The HTML must be from <html> to </html>, eventually including
30	 * the DOCTYPE.
31	 *     @param {String} data The data to be converted in the
32	 *            DataProcessor specific format.
33	 */
34	ConvertToHtml : function( data )
35	{
36		// Convert < and > to their HTML entities.
37        data = data.replace( /</g, '&lt;' ) ;
38        data = data.replace( />/g, '&gt;' ) ;
39
40        // Convert line breaks to <br>.
41        data = data.replace( /(?:\r\n|\n|\r)/g, '<br>' ) ;
42
43        // [url]
44        data = data.replace( /\[url\](.+?)\[\/url]/gi, '<a href="$1">$1</a>' ) ;
45        data = data.replace( /\[url\=([^\]]+)](.+?)\[\/url]/gi, '<a href="$1">$2</a>' ) ;
46
47        // [b]
48        data = data.replace( /\[b\](.+?)\[\/b]/gi, '<b>$1</b>' ) ;
49
50        // [i]
51        data = data.replace( /\[i\](.+?)\[\/i]/gi, '<i>$1</i>' ) ;
52
53        // [u]
54        data = data.replace( /\[u\](.+?)\[\/u]/gi, '<u>$1</u>' ) ;
55
56		return '<html><head><title></title></head><body>' + data + '</body></html>' ;
57	},
58
59	/*
60	 * Converts a DOM (sub-)tree to a string in the data format.
61	 *     @param {Object} rootNode The node that contains the DOM tree to be
62	 *            converted to the data format.
63	 *     @param {Boolean} excludeRoot Indicates that the root node must not
64	 *            be included in the conversion, only its children.
65	 *     @param {Boolean} format Indicates that the data must be formatted
66	 *            for human reading. Not all Data Processors may provide it.
67	 */
68	ConvertToDataFormat : function( rootNode, excludeRoot, ignoreIfEmptyParagraph, format )
69	{
70		var data = rootNode.innerHTML ;
71
72		// Convert <br> to line breaks.
73		data = data.replace( /<br(?=[ \/>]).*?>/gi, '\r\n') ;
74
75		// [url]
76		data = data.replace( /<a .*?href=(["'])(.+?)\1.*?>(.+?)<\/a>/gi, '[url=$2]$3[/url]') ;
77
78		// [b]
79		data = data.replace( /<(?:b|strong)>/gi, '[b]') ;
80		data = data.replace( /<\/(?:b|strong)>/gi, '[/b]') ;
81
82		// [i]
83		data = data.replace( /<(?:i|em)>/gi, '[i]') ;
84		data = data.replace( /<\/(?:i|em)>/gi, '[/i]') ;
85
86		// [u]
87		data = data.replace( /<u>/gi, '[u]') ;
88		data = data.replace( /<\/u>/gi, '[/u]') ;
89
90		// Remove remaining tags.
91		data = data.replace( /<[^>]+>/g, '') ;
92
93		return data ;
94	},
95
96	/*
97	 * Makes any necessary changes to a piece of HTML for insertion in the
98	 * editor selection position.
99	 *     @param {String} html The HTML to be fixed.
100	 */
101	FixHtml : function( html )
102	{
103		return html ;
104	}
105} ;
106
107// This Data Processor doesn't support <p>, so let's use <br>.
108FCKConfig.EnterMode = 'br' ;
109
110// To avoid pasting invalid markup (which is discarded in any case), let's
111// force pasting to plain text.
112FCKConfig.ForcePasteAsPlainText	= true ;
113
114// Rename the "Source" buttom to "BBCode".
115FCKToolbarItems.RegisterItem( 'Source', new FCKToolbarButton( 'Source', 'BBCode', null, FCK_TOOLBARITEM_ICONTEXT, true, true, 1 ) ) ;
116
117// Let's enforce the toolbar to the limits of this Data Processor. A custom
118// toolbar set may be defined in the configuration file with more or less entries.
119FCKConfig.ToolbarSets["Default"] = [
120	['Source'],
121	['Bold','Italic','Underline','-','Link'],
122	['About']
123] ;