Index: trunk/parsers/wikidom/tests/wikidom/index.html |
— | — | @@ -3,21 +3,26 @@ |
4 | 4 | <html> |
5 | 5 | <head> |
6 | 6 | <title>Wikidom Tests</title> |
7 | | - <link rel="stylesheet" href="../../lib/qunit.css" type="text/css" /> |
| 7 | + <link rel="stylesheet" href="../../lib/qunit.css" /> |
8 | 8 | </head> |
9 | 9 | <body> |
10 | 10 | <h1 id="qunit-header">Wikidom Tests</h1> |
11 | 11 | <h2 id="qunit-banner"></h2> |
12 | 12 | <h2 id="qunit-userAgent"></h2> |
13 | 13 | <ol id="qunit-tests"></ol> |
14 | | - <script src="../../lib/jquery.js" type="text/javascript"></script> |
15 | | - <script src="../../lib/qunit.js" type="text/javascript"></script> |
16 | | - <script src="../../lib/wiki.js" type="text/javascript"></script> |
17 | | - <script src="../../lib/wiki.util.js" type="text/javascript"></script> |
18 | | - <script src="../../lib/wiki.Context.js" type="text/javascript"></script> |
19 | | - <script src="../../lib/wiki.AnnotationRenderer.js" type="text/javascript"></script> |
20 | | - <script src="../../lib/wiki.HtmlRenderer.js" type="text/javascript"></script> |
21 | | - <script src="../../lib/wiki.WikitextRenderer.js" type="text/javascript"></script> |
22 | | - <script src="wiki.test.js" type="text/javascript"></script> |
| 14 | + <script src="../../lib/jquery.js"></script> |
| 15 | + <script src="../../lib/qunit.js"></script> |
| 16 | + <script src="../../lib/es/es.js"></script> |
| 17 | + <script src="../../lib/es/es.EventEmitter.js"></script> |
| 18 | + <script src="../../lib/es/es.Container.js"></script> |
| 19 | + <script src="../../lib/es/es.AnnotationSerializer.js"></script> |
| 20 | + <script src="../../lib/es/es.Document.js"></script> |
| 21 | + <script src="../../lib/es/es.Document.Context.js"></script> |
| 22 | + <script src="../../lib/es/es.Document.Serializer.js"></script> |
| 23 | + <script src="../../lib/es/es.Document.WikitextSerializer.js"></script> |
| 24 | + <script src="../../lib/es/es.Document.HtmlSerializer.js"></script> |
| 25 | + <script src="../../lib/es/es.Document.JsonSerializer.js"></script> |
| 26 | + |
| 27 | + <script src="wiki.test.js"></script> |
23 | 28 | </body> |
24 | 29 | </html> |
Index: trunk/parsers/wikidom/tests/wikidom/wiki.test.js |
— | — | @@ -1,20 +1,20 @@ |
2 | 2 | module( 'Wiki DOM Serialization' ); |
3 | 3 | |
4 | | -var context = new wiki.Context(); |
| 4 | +var context = new es.Document.Context(), |
| 5 | + htmlSerializer = new es.Document.HtmlSerializer( context ), |
| 6 | + wikitextSerializer = new es.Document.WikitextSerializer( context ); |
5 | 7 | |
6 | 8 | function assertSerializations( tests ) { |
7 | | - var htmlRenderer = new wiki.HtmlRenderer( context ); |
8 | | - var wikitextRenderer = new wiki.WikitextRenderer( context ); |
9 | 9 | for ( var i = 0; i < tests.length; i++ ) { |
10 | 10 | equals( |
11 | | - htmlRenderer.render( tests[i].dom ), |
| 11 | + htmlSerializer.serializeDocument( tests[i].dom ), |
12 | 12 | tests[i].html, |
13 | 13 | 'Serialize ' + tests[i].subject + ' to HTML' |
14 | 14 | ); |
15 | 15 | } |
16 | 16 | for ( var i = 0; i < tests.length; i++ ) { |
17 | 17 | equals( |
18 | | - wikitextRenderer.render( tests[i].dom ), |
| 18 | + wikitextSerializer.serializeDocument( tests[i].dom ), |
19 | 19 | tests[i].wikitext, |
20 | 20 | 'Serialize ' + tests[i].subject + ' to Wikitext' |
21 | 21 | ); |
Index: trunk/parsers/wikidom/tests/annotations/index.html |
— | — | @@ -10,12 +10,12 @@ |
11 | 11 | <h2 id="qunit-banner"></h2> |
12 | 12 | <h2 id="qunit-userAgent"></h2> |
13 | 13 | <ol id="qunit-tests"></ol> |
14 | | - <script src="../../lib/es/es.js" type="text/javascript"></script> |
15 | | - <script src="../../lib/es/es.EventEmitter.js" type="text/javascript"></script> |
16 | | - <script src="../../lib/es/es.Content.js" type="text/javascript"></script> |
17 | | - <script src="../../lib/es/es.Range.js" type="text/javascript"></script> |
18 | | - <script src="../../lib/jquery.js" type="text/javascript"></script> |
19 | | - <script src="../../lib/qunit.js" type="text/javascript"></script> |
20 | | - <script src="test.js" type="text/javascript"></script> |
| 14 | + <script src="../../lib/es/es.js"></script> |
| 15 | + <script src="../../lib/es/es.EventEmitter.js"></script> |
| 16 | + <script src="../../lib/es/es.Content.js"></script> |
| 17 | + <script src="../../lib/es/es.Range.js"></script> |
| 18 | + <script src="../../lib/jquery.js"></script> |
| 19 | + <script src="../../lib/qunit.js"></script> |
| 20 | + <script src="test.js"></script> |
21 | 21 | </body> |
22 | 22 | </html> |
Index: trunk/parsers/wikidom/lib/wiki.Context.js |
— | — | @@ -1,81 +0,0 @@ |
2 | | -wiki.Context = function( options ) { |
3 | | - |
4 | | - /* Private Members */ |
5 | | - |
6 | | - // Constants are used for behavioral switches, or other non-parametric inclusions |
7 | | - var constants = options && options.constants ? options.constants : {}; |
8 | | - // Parameters are used during transclusion for template expansion |
9 | | - var parameters = options && options.parameters ? options.parameters : {}; |
10 | | - |
11 | | - /* Methods */ |
12 | | - |
13 | | - /** |
14 | | - * Checks if a page exists, used for rendering "new" links. |
15 | | - * |
16 | | - * This method delegates to the pageExists option passed through the constructor. If no such |
17 | | - * option was given, this method will always return false. |
18 | | - * |
19 | | - * @param name String: Page namespace |
20 | | - * @param name String: Page title |
21 | | - * @return Boolean: True if page exists |
22 | | - */ |
23 | | - this.pageExists = function( namespace, title ) { |
24 | | - return typeof options.pageExists === 'function' |
25 | | - ? options.pageExists( namespace, title ) : false; |
26 | | - }; |
27 | | - |
28 | | - /** |
29 | | - * Gets the Document Object Model of a page. |
30 | | - * |
31 | | - * This method delegates to the getPageDom option passed through the constructor. If no such |
32 | | - * option was given, this method will always return null. |
33 | | - * |
34 | | - * @param name String: Page namespace |
35 | | - * @param name String: Page title |
36 | | - * @return Object: Page DOM (document object) |
37 | | - */ |
38 | | - this.getPageDom = function( namespace, title ) { |
39 | | - return typeof options.getPageDom === 'function' |
40 | | - ? options.getPageDom( namespace, title ) : null; |
41 | | - }; |
42 | | - |
43 | | - /** |
44 | | - * Sets a constant. |
45 | | - * |
46 | | - * @param name String: Constant name |
47 | | - * @param value String: Constant value |
48 | | - */ |
49 | | - this.setConstant = function( name, value ) { |
50 | | - constants[name] = value; |
51 | | - }; |
52 | | - |
53 | | - /** |
54 | | - * Gets a constant. |
55 | | - * |
56 | | - * @param name String: Constant name |
57 | | - * @return String: Constant value |
58 | | - */ |
59 | | - this.getConstant = function( name ) { |
60 | | - return name in constants ? constants[name] : null; |
61 | | - }; |
62 | | - |
63 | | - /** |
64 | | - * Sets a parameter. |
65 | | - * |
66 | | - * @param name String: Parameter name |
67 | | - * @param value Object: Parameter value (document object) |
68 | | - */ |
69 | | - this.setParameter = function( name, value ) { |
70 | | - parameters[name] = value; |
71 | | - }; |
72 | | - |
73 | | - /** |
74 | | - * Gets a parameter. |
75 | | - * |
76 | | - * @param name String: Parameter name |
77 | | - * @return Object: Parameter value (document object) |
78 | | - */ |
79 | | - this.getParameter = function( name ) { |
80 | | - return name in parameters ? parameters[name] : null; |
81 | | - }; |
82 | | -}; |
Index: trunk/parsers/wikidom/lib/wiki.HtmlRenderer.js |
— | — | @@ -1,160 +0,0 @@ |
2 | | -/** |
3 | | - * Serializes a WikiDom into HTML. |
4 | | - */ |
5 | | -wiki.HtmlRenderer = function( context ) { |
6 | | - |
7 | | - /* Private Members */ |
8 | | - |
9 | | - var that = this; |
10 | | - var blockRenderers = { |
11 | | - 'comment': renderComment, |
12 | | - 'horizontal-rule': renderHorizontalRule, |
13 | | - 'heading': renderHeading, |
14 | | - 'paragraph': renderParagraph, |
15 | | - 'list': renderList, |
16 | | - 'table': renderTable, |
17 | | - 'transclusion': renderTransclusion, |
18 | | - 'parameter': renderParameter |
19 | | - }; |
20 | | - |
21 | | - /* Private Methods */ |
22 | | - |
23 | | - function renderDocument( doc, rawFirstParagraph ) { |
24 | | - var out = []; |
25 | | - for ( var b = 0, bMax = doc.blocks.length; b < bMax; b++ ) { |
26 | | - var block = doc.blocks[b]; |
27 | | - if ( block.type in blockRenderers ) { |
28 | | - if ( block.type === 'paragraph' ) { |
29 | | - out.push( |
30 | | - renderParagraph( block, rawFirstParagraph && b === 0 ) |
31 | | - ); |
32 | | - } else { |
33 | | - out.push( blockRenderers[block.type]( block ) ); |
34 | | - } |
35 | | - } |
36 | | - } |
37 | | - return out.join( '\n' ); |
38 | | - } |
39 | | - |
40 | | - function renderComment( comment ) { |
41 | | - return '<!--' + comment.text + '-->'; |
42 | | - } |
43 | | - |
44 | | - function renderHorizontalRule( rule ) { |
45 | | - return wiki.util.xml.tag( 'hr', {}, false ); |
46 | | - } |
47 | | - |
48 | | - function renderHeading( heading ) { |
49 | | - return wiki.util.xml.tag( 'h' + heading.level, {}, renderLine( heading.line ) ); |
50 | | - } |
51 | | - |
52 | | - function renderParagraph( paragraph, raw ) { |
53 | | - var out = []; |
54 | | - for ( var l = 0, lMax = paragraph.lines.length; l < lMax; l++ ) { |
55 | | - out.push( renderLine( paragraph.lines[l] ) ); |
56 | | - } |
57 | | - if ( raw ) { |
58 | | - return out.join( '\n' ); |
59 | | - } else { |
60 | | - return wiki.util.xml.tag( 'p', {}, out.join( '\n' ) ); |
61 | | - } |
62 | | - } |
63 | | - |
64 | | - function renderList( list ) { |
65 | | - var tags = { |
66 | | - 'bullet': 'ul', |
67 | | - 'number': 'ol' |
68 | | - }; |
69 | | - var out = []; |
70 | | - out.push( wiki.util.xml.open( tags[list.style] ) ); |
71 | | - for ( var i = 0, iMax = list.items.length; i < iMax; i++ ) { |
72 | | - out.push( renderItem( list.items[i] ) ); |
73 | | - } |
74 | | - out.push( wiki.util.xml.close( tags[list.style] ) ); |
75 | | - return out.join( '\n' ); |
76 | | - } |
77 | | - |
78 | | - function renderTable( table ) { |
79 | | - var out = []; |
80 | | - var types = { |
81 | | - 'heading': 'th', |
82 | | - 'data': 'td' |
83 | | - }; |
84 | | - out.push( wiki.util.xml.open( 'table', table.attributes ) ); |
85 | | - for ( var r = 0, rMax = table.rows.length; r < rMax; r++ ) { |
86 | | - out.push( wiki.util.xml.open( 'tr' ) ); |
87 | | - var row = table.rows[r]; |
88 | | - for ( var c = 0, cMax = row.length; c < cMax; c++ ) { |
89 | | - var type = types[row[c].type || 'data']; |
90 | | - out.push( wiki.util.xml.tag( |
91 | | - type, |
92 | | - row[c].attributes, |
93 | | - renderDocument( row[c].document, true ) |
94 | | - ) ); |
95 | | - } |
96 | | - out.push( wiki.util.xml.close( 'tr' ) ); |
97 | | - } |
98 | | - out.push( wiki.util.xml.close( 'table' ) ); |
99 | | - return out.join( '\n' ); |
100 | | - } |
101 | | - |
102 | | - function renderTransclusion( transclusion ) { |
103 | | - var title = []; |
104 | | - if ( transclusion.namespace !== 'Main' ) { |
105 | | - title.push( transclusion.namespace ) |
106 | | - } |
107 | | - title.push( transclusion.title ); |
108 | | - title = title.join( ':' ); |
109 | | - return wiki.util.xml.tag( 'a', { 'href': '/wiki/' + title }, title ); |
110 | | - } |
111 | | - |
112 | | - function renderParameter( parameter ) { |
113 | | - return '{{{' + parameter.name + '}}}'; |
114 | | - } |
115 | | - |
116 | | - function renderItem( item ) { |
117 | | - if ( 'lists' in item && item.lists.length ) { |
118 | | - var out = []; |
119 | | - out.push( wiki.util.xml.open( 'li' ) + renderLine( item.line ) ); |
120 | | - for ( var l = 0, lMax = item.lists.length; l < lMax; l++ ) { |
121 | | - out.push( renderList( item.lists[l] ) ); |
122 | | - } |
123 | | - out.push( wiki.util.xml.close( 'li' ) ) |
124 | | - return out.join( '\n' ); |
125 | | - } else { |
126 | | - return wiki.util.xml.tag( 'li', {}, renderLine( item.line ) ); |
127 | | - } |
128 | | - } |
129 | | - |
130 | | - function renderLine( line ) { |
131 | | - if ( 'annotations' in line && line.annotations.length ) { |
132 | | - var ar = new wiki.AnnotationRenderer(); |
133 | | - for ( var a = 0, aMax = line.annotations.length; a < aMax; a++ ) { |
134 | | - var an = line.annotations[a]; |
135 | | - switch ( an.type ) { |
136 | | - case 'bold': |
137 | | - ar.wrapWithXml( an.range, 'strong' ); |
138 | | - break; |
139 | | - case 'italic': |
140 | | - ar.wrapWithXml( an.range, 'em' ); |
141 | | - break; |
142 | | - case 'xlink': |
143 | | - ar.wrapWithXml( an.range, 'a', { 'href': an.data.url } ); |
144 | | - break; |
145 | | - case 'ilink': |
146 | | - ar.wrapWithXml( an.range, 'a', { 'href': '/wiki/' + an.data.title } ); |
147 | | - break; |
148 | | - } |
149 | | - } |
150 | | - return ar.apply( line.text ); |
151 | | - } else { |
152 | | - return line.text; |
153 | | - } |
154 | | - } |
155 | | - |
156 | | - /* Methods */ |
157 | | - |
158 | | - this.render = function( doc ) { |
159 | | - return renderDocument( doc ); |
160 | | - }; |
161 | | -}; |
Index: trunk/parsers/wikidom/lib/wiki.util.js |
— | — | @@ -1,54 +0,0 @@ |
2 | | -/** |
3 | | - * Utilities used by WikiDom renderers and parsers. |
4 | | - */ |
5 | | -wiki.util = { |
6 | | - 'str': { |
7 | | - 'repeat': function( pattern, count ) { |
8 | | - if ( count < 1 ) { return ''; } |
9 | | - var result = ''; |
10 | | - while ( count > 0 ) { |
11 | | - if ( count & 1 ) { result += pattern; } |
12 | | - count >>= 1; |
13 | | - pattern += pattern; |
14 | | - } |
15 | | - return result; |
16 | | - } |
17 | | - }, |
18 | | - 'xml': { |
19 | | - 'esc': function( text ) { |
20 | | - return text |
21 | | - .replace( /&/g, '&' ) |
22 | | - .replace( /</g, '<' ) |
23 | | - .replace( />/g, '>' ) |
24 | | - .replace( /"/g, '"' ) |
25 | | - .replace( /'/g, ''' ); |
26 | | - }, |
27 | | - 'attr': function( attributes, prespace ) { |
28 | | - var attr = []; |
29 | | - var name; |
30 | | - if ( attributes ) { |
31 | | - for ( name in attributes ) { |
32 | | - attr.push( name + '="' + attributes[name] + '"' ); |
33 | | - } |
34 | | - } |
35 | | - return ( prespace && attr.length ? ' ' : '' ) + attr.join( ' ' ); |
36 | | - }, |
37 | | - 'open': function( tag, attributes ) { |
38 | | - return '<' + tag + wiki.util.xml.attr( attributes, true ) + '>'; |
39 | | - }, |
40 | | - 'close': function( tag ) { |
41 | | - return '</' + tag + '>'; |
42 | | - }, |
43 | | - 'tag': function( tag, attributes, value, escape ) { |
44 | | - if ( value === false ) { |
45 | | - return '<' + tag + wiki.util.xml.attr( attributes, true ) + ' />'; |
46 | | - } else { |
47 | | - if ( escape ) { |
48 | | - value = wiki.util.xml.esc( value ); |
49 | | - } |
50 | | - return '<' + tag + wiki.util.xml.attr( attributes, true ) + '>' + value |
51 | | - + '</' + tag + '>'; |
52 | | - } |
53 | | - } |
54 | | - } |
55 | | -}; |
Index: trunk/parsers/wikidom/lib/wiki.WikitextRenderer.js |
— | — | @@ -1,163 +0,0 @@ |
2 | | -/** |
3 | | - * Serializes a WikiDom into Wikitext. |
4 | | - */ |
5 | | -wiki.WikitextRenderer = function( context ) { |
6 | | - |
7 | | - /* Private Members */ |
8 | | - |
9 | | - var that = this; |
10 | | - var blockRenderers = { |
11 | | - 'comment': renderComment, |
12 | | - 'horizontal-rule': renderHorizontalRule, |
13 | | - 'heading': renderHeading, |
14 | | - 'paragraph': renderParagraph, |
15 | | - 'list': renderList, |
16 | | - 'table': renderTable, |
17 | | - 'transclusion': renderTransclusion, |
18 | | - 'parameter': renderParameter |
19 | | - }; |
20 | | - |
21 | | - /* Private Methods */ |
22 | | - |
23 | | - function renderDocument( doc, rawFirstParagraph ) { |
24 | | - var out = []; |
25 | | - for ( var b = 0, bMax = doc.blocks.length; b < bMax; b++ ) { |
26 | | - var block = doc.blocks[b]; |
27 | | - if ( block.type in blockRenderers ) { |
28 | | - if ( block.type === 'paragraph' ) { |
29 | | - out.push( |
30 | | - renderParagraph( block, rawFirstParagraph && b === 0 ) |
31 | | - ); |
32 | | - if ( b + 1 < bMax /* && doc.blocks[b + 1].type === 'paragraph' */ ) { |
33 | | - out.push( '' ); |
34 | | - } |
35 | | - } else { |
36 | | - out.push( blockRenderers[block.type]( block ) ); |
37 | | - } |
38 | | - } |
39 | | - } |
40 | | - return out.join( '\n' ); |
41 | | - } |
42 | | - |
43 | | - function renderComment( comment ) { |
44 | | - return '<!--' + comment.text + '-->'; |
45 | | - } |
46 | | - |
47 | | - function renderHorizontalRule( rule ) { |
48 | | - return '----'; |
49 | | - } |
50 | | - |
51 | | - function renderHeading( heading ) { |
52 | | - var symbols = wiki.util.str.repeat( '=', heading.level ); |
53 | | - return symbols + renderLine( heading.line ) + symbols; |
54 | | - } |
55 | | - |
56 | | - function renderParagraph( paragraph ) { |
57 | | - var out = []; |
58 | | - for ( var l = 0, lMax = paragraph.lines.length; l < lMax; l++ ) { |
59 | | - out.push( renderLine( paragraph.lines[l] ) ); |
60 | | - } |
61 | | - return out.join( '\n' ); |
62 | | - } |
63 | | - |
64 | | - function renderList( list, path ) { |
65 | | - if ( typeof path === 'undefined' ) { |
66 | | - path = ''; |
67 | | - } |
68 | | - var symbols = { |
69 | | - 'bullet': '*', |
70 | | - 'number': '#' |
71 | | - }; |
72 | | - path += symbols[list.style]; |
73 | | - var out = []; |
74 | | - for ( var i = 0, iMax = list.items.length; i < iMax; i++ ) { |
75 | | - out.push( renderItem( list.items[i], path ) ); |
76 | | - } |
77 | | - return out.join( '\n' ); |
78 | | - } |
79 | | - |
80 | | - function renderTable( table ) { |
81 | | - var out = []; |
82 | | - var types = { |
83 | | - 'heading': '!', |
84 | | - 'data': '|' |
85 | | - }; |
86 | | - out.push( '{|' + wiki.util.xml.attr( table.attributes ) ); |
87 | | - for ( var r = 0, rMax = table.rows.length; r < rMax; r++ ) { |
88 | | - var row = table.rows[r]; |
89 | | - if ( r ) { |
90 | | - out.push( '|-' ); |
91 | | - } |
92 | | - for ( var c = 0, cMax = row.length; c < cMax; c++ ) { |
93 | | - var type = types[row[c].type || 'data']; |
94 | | - out.push( |
95 | | - type |
96 | | - + ( row[c].attributes ? wiki.util.xml.attr( row[c].attributes ) + '|' : '' ) |
97 | | - + renderDocument( row[c].document, true ) |
98 | | - ); |
99 | | - } |
100 | | - } |
101 | | - out.push( '|}' ); |
102 | | - return out.join( '\n' ); |
103 | | - } |
104 | | - |
105 | | - function renderTransclusion( transclusion ) { |
106 | | - var title = []; |
107 | | - if ( transclusion.namespace === 'Main' ) { |
108 | | - title.push( '' ); |
109 | | - } else if ( transclusion.namespace !== 'Template' ) { |
110 | | - title.push( transclusion.namespace ) |
111 | | - } |
112 | | - title.push( transclusion.title ); |
113 | | - return '{{' + title.join( ':' ) + '}}'; |
114 | | - } |
115 | | - |
116 | | - function renderParameter( parameter ) { |
117 | | - return '{{{' + parameter.name + '}}}'; |
118 | | - } |
119 | | - |
120 | | - function renderItem( item, path ) { |
121 | | - if ( 'lists' in item && item.lists.length ) { |
122 | | - var out = []; |
123 | | - out.push( path + ' ' + renderLine( item.line ) ); |
124 | | - for ( var l = 0, lMax = item.lists.length; l < lMax; l++ ) { |
125 | | - out.push( renderList( item.lists[l], path ) ); |
126 | | - } |
127 | | - return out.join( '\n' ); |
128 | | - } else { |
129 | | - return path + ' ' + renderLine( item.line ); |
130 | | - } |
131 | | - } |
132 | | - |
133 | | - function renderLine( line ) { |
134 | | - if ( 'annotations' in line && line.annotations.length ) { |
135 | | - var ar = new wiki.AnnotationRenderer(); |
136 | | - for ( var a = 0, aMax = line.annotations.length; a < aMax; a++ ) { |
137 | | - var an = line.annotations[a]; |
138 | | - switch ( an.type ) { |
139 | | - case 'bold': |
140 | | - ar.wrapWithText( an.range, '\'\'\'', '\'\'\'' ); |
141 | | - break; |
142 | | - case 'italic': |
143 | | - ar.wrapWithText( an.range, '\'\'', '\'\'' ); |
144 | | - break; |
145 | | - case 'xlink': |
146 | | - ar.wrapWithText( an.range, '[' + an.data.href + ' ', ']' ); |
147 | | - break; |
148 | | - case 'ilink': |
149 | | - ar.wrapWithText( an.range, '[[' + an.data.title + '|', ']]' ); |
150 | | - break; |
151 | | - } |
152 | | - } |
153 | | - return ar.apply( line.text ); |
154 | | - } else { |
155 | | - return line.text; |
156 | | - } |
157 | | - } |
158 | | - |
159 | | - /* Methods */ |
160 | | - |
161 | | - this.render = function( doc ) { |
162 | | - return renderDocument( doc ); |
163 | | - }; |
164 | | -}; |
Index: trunk/parsers/wikidom/lib/wiki.AnnotationRenderer.js |
— | — | @@ -1,74 +0,0 @@ |
2 | | -/** |
3 | | - * Serializes offset-based annotations. |
4 | | - */ |
5 | | -wiki.AnnotationRenderer = function() { |
6 | | - |
7 | | - /* Private Members */ |
8 | | - |
9 | | - var that = this; |
10 | | - var insertions = {}; |
11 | | - |
12 | | - /* Methods */ |
13 | | - |
14 | | - /** |
15 | | - * Adds a set of insertions around a range of text. |
16 | | - * |
17 | | - * Insertions for the same range will be nested in order of declaration. |
18 | | - * @example |
19 | | - * ar = new wiki.AnnotationRenderer(); |
20 | | - * ar.wrapWithText( { 'start': 1, 'end': 2 }, '[', ']' ); |
21 | | - * ar.wrapWithText( { 'start': 1, 'end': 2 }, '{', '}' ); |
22 | | - * // Outputs: "a[{b}]c" |
23 | | - * console.log( ar.apply( 'abc' ) ); |
24 | | - * |
25 | | - * @param range Object: Range to insert text around |
26 | | - * @param pre String: Text to insert before range |
27 | | - * @param post String: Text to insert after range |
28 | | - */ |
29 | | - this.wrapWithText = function( range, pre, post ) { |
30 | | - if ( !( range.start in insertions ) ) { |
31 | | - insertions[range.start] = [pre]; |
32 | | - } else { |
33 | | - insertions[range.start].push( pre ); |
34 | | - } |
35 | | - if ( !( range.end in insertions ) ) { |
36 | | - insertions[range.end] = [post]; |
37 | | - } else { |
38 | | - insertions[range.end].unshift( post ); |
39 | | - } |
40 | | - }; |
41 | | - |
42 | | - /** |
43 | | - * Adds a set of opening and closing XML tags around a range of text. |
44 | | - * |
45 | | - * This is a convenience function, and has the same nesting behavior as wrapWithText. |
46 | | - * |
47 | | - * @param range Object: Range to insert XML tags around |
48 | | - * @param tag String: XML tag name |
49 | | - * @param attributes Object: XML tag attributes (optional) |
50 | | - */ |
51 | | - this.wrapWithXml = function( range, tag, attributes ) { |
52 | | - that.wrapWithText( |
53 | | - range, wiki.util.xml.open( tag, attributes ), wiki.util.xml.close( tag ) |
54 | | - ); |
55 | | - }; |
56 | | - |
57 | | - /** |
58 | | - * Applies insertions to text. |
59 | | - * |
60 | | - * @param text String: Text to apply insertions to |
61 | | - * @return String: Wrapped text |
62 | | - */ |
63 | | - this.apply = function( text ) { |
64 | | - var out = '', i; |
65 | | - for ( i = 0, iMax = text.length; i <= iMax; i++ ) { |
66 | | - if ( i in insertions ) { |
67 | | - out += insertions[i].join( '' ); |
68 | | - } |
69 | | - if ( i < iMax ) { |
70 | | - out += text[i]; |
71 | | - } |
72 | | - } |
73 | | - return out; |
74 | | - }; |
75 | | -}; |
Index: trunk/parsers/wikidom/lib/wiki.js |
— | — | @@ -1,52 +0,0 @@ |
2 | | -/* |
3 | | - * Wikitext document object models |
4 | | - * |
5 | | - * document |
6 | | - * blocks: Array |
7 | | - * attributes: Plain object |
8 | | - * |
9 | | - * // Blocks |
10 | | - * |
11 | | - * comment |
12 | | - * text: String |
13 | | - * horizontal-rule |
14 | | - * heading |
15 | | - * level: Integer (1..6) |
16 | | - * line: Line object |
17 | | - * paragraph |
18 | | - * lines: Array of line objects |
19 | | - * list |
20 | | - * style: String ("bullet" or "number") |
21 | | - * items: Array of item objects |
22 | | - * table |
23 | | - * rows: Array of arrays of cell objects |
24 | | - * attributes: Plain object |
25 | | - * transclusion |
26 | | - * namespace: String |
27 | | - * title: String |
28 | | - * parameters: Array of documents |
29 | | - * parameter |
30 | | - * name: String or integer |
31 | | - * default: Document object |
32 | | - * |
33 | | - * // Components |
34 | | - * |
35 | | - * line |
36 | | - * text: String |
37 | | - * item |
38 | | - * line: Line object |
39 | | - * lists: Array of list objects |
40 | | - * range |
41 | | - * start: Integer |
42 | | - * end: Integer |
43 | | - * annotation |
44 | | - * type: String |
45 | | - * range: Range object |
46 | | - * data: Plain object |
47 | | - * cell |
48 | | - * document: Object |
49 | | - * attributes: Plain object |
50 | | - */ |
51 | | - |
52 | | -// Global object - other modules will attach to this |
53 | | -var wiki = {}; |
Index: trunk/parsers/wikidom/lib/es/es.Document.Context.js |
— | — | @@ -0,0 +1,91 @@ |
| 2 | +/** |
| 3 | + * Creates a document context. |
| 4 | + * |
| 5 | + * @class |
| 6 | + * @constructor |
| 7 | + * @property options |
| 8 | + * @property options.constants {Object} Behavioral switches, or other non-parametric inclusions |
| 9 | + * @property options.parameters {Object} Used during transclusion for template expansion |
| 10 | + * @property options.isPage {Function} Callback for checking if a page exists |
| 11 | + * @property options.getWikiDom {Function} Callback for getting the WikiDom of for a page |
| 12 | + */ |
| 13 | +es.Document.Context = function( options ) { |
| 14 | + this.options = $.extend({ |
| 15 | + 'constants': {}, |
| 16 | + 'parameters': {}, |
| 17 | + 'isPage': null, |
| 18 | + 'getWikiDom': null |
| 19 | + }, options); |
| 20 | +}; |
| 21 | + |
| 22 | +/* Methods */ |
| 23 | + |
| 24 | +/** |
| 25 | + * Checks if a page exists, used for rendering "new" links. |
| 26 | + * |
| 27 | + * This method delegates to the isPage option passed through the constructor. If no such |
| 28 | + * option was given, this method will always return false. |
| 29 | + * |
| 30 | + * @param name {String} Page namespace |
| 31 | + * @param name {String} Page title |
| 32 | + * @returns {Boolean} True if page exists |
| 33 | + */ |
| 34 | +es.Document.Context.prototype.isPage = function( namespace, title ) { |
| 35 | + return typeof this.options.isPage === 'function' |
| 36 | + ? this.options.isPage( namespace, title ) : false; |
| 37 | +}; |
| 38 | + |
| 39 | +/** |
| 40 | + * Gets the Document Object Model of a page. |
| 41 | + * |
| 42 | + * This method delegates to the getWikiDom option passed through the constructor. If no such |
| 43 | + * option was given, this method will always return null. |
| 44 | + * |
| 45 | + * @param name {String} Page namespace |
| 46 | + * @param name {String} Page title |
| 47 | + * @returns {Object} Page DOM (document object) |
| 48 | + */ |
| 49 | +es.Document.Context.prototype.getWikiDom = function( namespace, title ) { |
| 50 | + return typeof this.options.getWikiDom === 'function' |
| 51 | + ? this.options.getWikiDom( namespace, title ) : null; |
| 52 | +}; |
| 53 | + |
| 54 | +/** |
| 55 | + * Sets a constant. |
| 56 | + * |
| 57 | + * @param name {String} Constant name |
| 58 | + * @param value {String} Constant value |
| 59 | + */ |
| 60 | +es.Document.Context.prototype.setConstant = function( name, value ) { |
| 61 | + this.options.constants[name] = value; |
| 62 | +}; |
| 63 | + |
| 64 | +/** |
| 65 | + * Gets a constant. |
| 66 | + * |
| 67 | + * @param name {String} Constant name |
| 68 | + * @returns {String} Constant value |
| 69 | + */ |
| 70 | +es.Document.Context.prototype.getConstant = function( name ) { |
| 71 | + return name in this.options.constants ? this.options.constants[name] : null; |
| 72 | +}; |
| 73 | + |
| 74 | +/** |
| 75 | + * Sets a parameter. |
| 76 | + * |
| 77 | + * @param name {String} Parameter name |
| 78 | + * @param value {Object} Parameter value (document object) |
| 79 | + */ |
| 80 | +es.Document.Context.prototype.setParameter = function( name, value ) { |
| 81 | + this.options.parameters[name] = value; |
| 82 | +}; |
| 83 | + |
| 84 | +/** |
| 85 | + * Gets a parameter. |
| 86 | + * |
| 87 | + * @param name {String} Parameter name |
| 88 | + * @returns {Object} Parameter value (document object) |
| 89 | + */ |
| 90 | +es.Document.Context.prototype.getParameter = function( name ) { |
| 91 | + return name in this.options.parameters ? this.options.parameters[name] : null; |
| 92 | +}; |
Property changes on: trunk/parsers/wikidom/lib/es/es.Document.Context.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 93 | + native |
Added: svn:mime-type |
2 | 94 | + text/plain |
Index: trunk/parsers/wikidom/lib/es/es.AnnotationSerializer.js |
— | — | @@ -0,0 +1,58 @@ |
| 2 | +/** |
| 3 | + * Creates an annotation renderer object. |
| 4 | + * |
| 5 | + * @class |
| 6 | + * @constructor |
| 7 | + * @property annotations {Object} List of annotations to be applied |
| 8 | + */ |
| 9 | +es.AnnotationSerializer = function() { |
| 10 | + this.annotations = {}; |
| 11 | +}; |
| 12 | + |
| 13 | +/** |
| 14 | + * Adds a set of annotations to be inserted around a range of text. |
| 15 | + * |
| 16 | + * Insertions for the same range will be nested in order of declaration. |
| 17 | + * @example |
| 18 | + * stack = new es.AnnotationSerializer(); |
| 19 | + * stack.wrapWithText( { 'from': 1, 'to': 2 }, '[', ']' ); |
| 20 | + * stack.wrapWithText( { 'from': 1, 'to': 2 }, '{', '}' ); |
| 21 | + * // Outputs: "a[{b}]c" |
| 22 | + * console.log( stack.apply( 'abc' ) ); |
| 23 | + * |
| 24 | + * @param range {es.Range} Range to insert text around |
| 25 | + * @param pre {String} Text to insert before range |
| 26 | + * @param post {String} Text to insert after range |
| 27 | + */ |
| 28 | +es.AnnotationSerializer.prototype.add = function( range, pre, post ) { |
| 29 | + // TODO: Once we are using Range objects, we should do a range.normalize(); here |
| 30 | + if ( !( range.start in this.annotations ) ) { |
| 31 | + this.annotations[range.start] = [pre]; |
| 32 | + } else { |
| 33 | + this.annotations[range.start].push( pre ); |
| 34 | + } |
| 35 | + if ( !( range.end in this.annotations ) ) { |
| 36 | + this.annotations[range.end] = [post]; |
| 37 | + } else { |
| 38 | + this.annotations[range.end].unshift( post ); |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +/** |
| 43 | + * Renders annotations into text. |
| 44 | + * |
| 45 | + * @param text {String} Text to apply annotations to |
| 46 | + * @returns {String} Wrapped text |
| 47 | + */ |
| 48 | +es.AnnotationSerializer.prototype.render = function( text ) { |
| 49 | + var out = ''; |
| 50 | + for ( var i = 0, length = text.length; i <= length; i++ ) { |
| 51 | + if ( i in this.annotations ) { |
| 52 | + out += this.annotations[i].join( '' ); |
| 53 | + } |
| 54 | + if ( i < length ) { |
| 55 | + out += text[i]; |
| 56 | + } |
| 57 | + } |
| 58 | + return out; |
| 59 | +}; |
Property changes on: trunk/parsers/wikidom/lib/es/es.AnnotationSerializer.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 60 | + native |
Added: svn:mime-type |
2 | 61 | + text/plain |
Index: trunk/parsers/wikidom/lib/es/es.Document.HtmlSerializer.js |
— | — | @@ -0,0 +1,177 @@ |
| 2 | +/** |
| 3 | + * Serializes a WikiDom into HTML. |
| 4 | + * |
| 5 | + * @class |
| 6 | + * @constructor |
| 7 | + * @extends {es.Document.Serializer} |
| 8 | + * @param context {es.Document.Context} Context of the wiki the document is a part of |
| 9 | + * @property serializers {Object} List of serializing methods indexed by symbolic object names |
| 10 | + */ |
| 11 | +es.Document.HtmlSerializer = function( context ) { |
| 12 | + es.Document.Serializer.call( this, context ); |
| 13 | + this.serializers = { |
| 14 | + 'comment': this.serializeComment, |
| 15 | + 'horizontal-rule': this.serializeHorizontalRule, |
| 16 | + 'heading': this.serializeHeading, |
| 17 | + 'paragraph': this.serializeParagraph, |
| 18 | + 'list': this.serializeList, |
| 19 | + 'table': this.serializeTable, |
| 20 | + 'transclusion': this.serializeTransclusion, |
| 21 | + 'parameter': this.serializeParameter |
| 22 | + }; |
| 23 | +}; |
| 24 | + |
| 25 | +/* Methods */ |
| 26 | + |
| 27 | +es.Document.HtmlSerializer.prototype.serializeDocument = function( doc, rawFirstParagraph ) { |
| 28 | + var out = []; |
| 29 | + for ( var b = 0, bMax = doc.blocks.length; b < bMax; b++ ) { |
| 30 | + var block = doc.blocks[b]; |
| 31 | + if ( block.type in this.serializers ) { |
| 32 | + if ( block.type === 'paragraph' ) { |
| 33 | + out.push( this.serializeParagraph( block, rawFirstParagraph && b === 0 ) ); |
| 34 | + } else { |
| 35 | + out.push( this.serializers[block.type].call( this, block ) ); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + return out.join( '\n' ); |
| 40 | +}; |
| 41 | + |
| 42 | +es.Document.HtmlSerializer.prototype.serializeComment = function( comment ) { |
| 43 | + return '<!--' + comment.text + '-->'; |
| 44 | +}; |
| 45 | + |
| 46 | +es.Document.HtmlSerializer.prototype.serializeHorizontalRule = function( rule ) { |
| 47 | + return es.Document.Serializer.buildXmlTag( 'hr', {}, false ); |
| 48 | +}; |
| 49 | + |
| 50 | +es.Document.HtmlSerializer.prototype.serializeHeading = function( heading ) { |
| 51 | + return es.Document.Serializer.buildXmlTag( |
| 52 | + 'h' + heading.level, {}, this.serializeLine( heading.line ) |
| 53 | + ); |
| 54 | +}; |
| 55 | + |
| 56 | +es.Document.HtmlSerializer.prototype.serializeParagraph = function( paragraph, raw ) { |
| 57 | + var out = []; |
| 58 | + for ( var l = 0, lMax = paragraph.lines.length; l < lMax; l++ ) { |
| 59 | + out.push( this.serializeLine( paragraph.lines[l] ) ); |
| 60 | + } |
| 61 | + if ( raw ) { |
| 62 | + return out.join( '\n' ); |
| 63 | + } else { |
| 64 | + return es.Document.Serializer.buildXmlTag( 'p', {}, out.join( '\n' ) ); |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +es.Document.HtmlSerializer.prototype.serializeList = function( list ) { |
| 69 | + var tags = { |
| 70 | + 'bullet': 'ul', |
| 71 | + 'number': 'ol' |
| 72 | + }; |
| 73 | + var out = []; |
| 74 | + out.push( es.Document.Serializer.buildXmlOpeningTag( tags[list.style] ) ); |
| 75 | + for ( var i = 0, iMax = list.items.length; i < iMax; i++ ) { |
| 76 | + out.push( this.serializeItem( list.items[i] ) ); |
| 77 | + } |
| 78 | + out.push( es.Document.Serializer.buildXmlClosingTag( tags[list.style] ) ); |
| 79 | + return out.join( '\n' ); |
| 80 | +}; |
| 81 | + |
| 82 | +es.Document.HtmlSerializer.prototype.serializeTable = function( table ) { |
| 83 | + var out = []; |
| 84 | + var types = { |
| 85 | + 'heading': 'th', |
| 86 | + 'data': 'td' |
| 87 | + }; |
| 88 | + out.push( es.Document.Serializer.buildXmlOpeningTag( 'table', table.attributes ) ); |
| 89 | + for ( var r = 0, rMax = table.rows.length; r < rMax; r++ ) { |
| 90 | + out.push( es.Document.Serializer.buildXmlOpeningTag( 'tr' ) ); |
| 91 | + var row = table.rows[r]; |
| 92 | + for ( var c = 0, cMax = row.length; c < cMax; c++ ) { |
| 93 | + var type = types[row[c].type || 'data']; |
| 94 | + out.push( es.Document.Serializer.buildXmlTag( |
| 95 | + type, |
| 96 | + row[c].attributes, |
| 97 | + this.serializeDocument( row[c].document, true ) |
| 98 | + ) ); |
| 99 | + } |
| 100 | + out.push( es.Document.Serializer.buildXmlClosingTag( 'tr' ) ); |
| 101 | + } |
| 102 | + out.push( es.Document.Serializer.buildXmlClosingTag( 'table' ) ); |
| 103 | + return out.join( '\n' ); |
| 104 | +}; |
| 105 | + |
| 106 | +es.Document.HtmlSerializer.prototype.serializeTransclusion = function( transclusion ) { |
| 107 | + var title = []; |
| 108 | + if ( transclusion.namespace !== 'Main' ) { |
| 109 | + title.push( transclusion.namespace ) |
| 110 | + } |
| 111 | + title.push( transclusion.title ); |
| 112 | + title = title.join( ':' ); |
| 113 | + return es.Document.Serializer.buildXmlTag( 'a', { 'href': '/wiki/' + title }, title ); |
| 114 | +}; |
| 115 | + |
| 116 | +es.Document.HtmlSerializer.prototype.serializeParameter = function( parameter ) { |
| 117 | + return '{{{' + parameter.name + '}}}'; |
| 118 | +}; |
| 119 | + |
| 120 | +es.Document.HtmlSerializer.prototype.serializeItem = function( item ) { |
| 121 | + if ( 'lists' in item && item.lists.length ) { |
| 122 | + var out = []; |
| 123 | + out.push( |
| 124 | + es.Document.Serializer.buildXmlOpeningTag( 'li' ) + this.serializeLine( item.line ) |
| 125 | + ); |
| 126 | + for ( var l = 0, lMax = item.lists.length; l < lMax; l++ ) { |
| 127 | + out.push( this.serializeList( item.lists[l] ) ); |
| 128 | + } |
| 129 | + out.push( es.Document.Serializer.buildXmlClosingTag( 'li' ) ) |
| 130 | + return out.join( '\n' ); |
| 131 | + } else { |
| 132 | + return es.Document.Serializer.buildXmlTag( 'li', {}, this.serializeLine( item.line ) ); |
| 133 | + } |
| 134 | +}; |
| 135 | + |
| 136 | +es.Document.HtmlSerializer.prototype.serializeLine = function( line ) { |
| 137 | + if ( 'annotations' in line && line.annotations.length ) { |
| 138 | + var as = new es.AnnotationSerializer(); |
| 139 | + function addXml( range, tag, attributes ) { |
| 140 | + as.add( |
| 141 | + range, |
| 142 | + es.Document.Serializer.buildXmlOpeningTag( tag, attributes ), |
| 143 | + es.Document.Serializer.buildXmlClosingTag( tag ) |
| 144 | + ); |
| 145 | + }; |
| 146 | + for ( var a = 0, aMax = line.annotations.length; a < aMax; a++ ) { |
| 147 | + var an = line.annotations[a]; |
| 148 | + switch ( an.type ) { |
| 149 | + case 'bold': |
| 150 | + addXml( an.range, 'strong' ); |
| 151 | + break; |
| 152 | + case 'italic': |
| 153 | + addXml( an.range, 'em' ); |
| 154 | + break; |
| 155 | + case 'xlink': |
| 156 | + addXml( an.range, 'a', { 'href': an.data.url } ); |
| 157 | + break; |
| 158 | + case 'ilink': |
| 159 | + addXml( an.range, 'a', { 'href': '/wiki/' + an.data.title } ); |
| 160 | + break; |
| 161 | + } |
| 162 | + } |
| 163 | + return as.render( line.text ); |
| 164 | + } else { |
| 165 | + return line.text; |
| 166 | + } |
| 167 | +}; |
| 168 | + |
| 169 | +/* Registration */ |
| 170 | + |
| 171 | +es.Document.serializers.html = function( doc, context ) { |
| 172 | + var serializer = new es.Document.HtmlSerializer( context ) |
| 173 | + return serializer.serializeDocument( doc ); |
| 174 | +}; |
| 175 | + |
| 176 | +/* Inheritance */ |
| 177 | + |
| 178 | +es.extend( es.Document.HtmlSerializer, es.Document.Serializer ); |
Property changes on: trunk/parsers/wikidom/lib/es/es.Document.HtmlSerializer.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 179 | + native |
Added: svn:mime-type |
2 | 180 | + text/plain |
Index: trunk/parsers/wikidom/lib/es/es.Document.js |
— | — | @@ -13,6 +13,13 @@ |
14 | 14 | this.width = null; |
15 | 15 | }; |
16 | 16 | |
| 17 | +/* Static Members */ |
| 18 | + |
| 19 | +/** |
| 20 | + * List of registered document serializers. |
| 21 | + */ |
| 22 | +es.Document.serializers = {}; |
| 23 | + |
17 | 24 | /* Static Methods */ |
18 | 25 | |
19 | 26 | /** |
— | — | @@ -34,6 +41,19 @@ |
35 | 42 | |
36 | 43 | /* Methods */ |
37 | 44 | |
| 45 | +es.Document.prototype.serialize = function( serializer, context ) { |
| 46 | + if ( context === undefined ) { |
| 47 | + context = new es.Document.Context(); |
| 48 | + } |
| 49 | + if ( serializer in es.Document.serializers ) { |
| 50 | + return es.Document.serializers[serializer]( this.getWikiDomDocument(), context ); |
| 51 | + } |
| 52 | +}; |
| 53 | + |
| 54 | +es.Document.prototype.getSerializers = function() { |
| 55 | + return es.Document.serializers; |
| 56 | +}; |
| 57 | + |
38 | 58 | /** |
39 | 59 | * Forces all blocks in the document to render. |
40 | 60 | * |
Index: trunk/parsers/wikidom/lib/es/es.Document.WikitextSerializer.js |
— | — | @@ -0,0 +1,169 @@ |
| 2 | +/** |
| 3 | + * Serializes a WikiDom into Wikitext. |
| 4 | + * |
| 5 | + * @class |
| 6 | + * @constructor |
| 7 | + * @extends {es.Document.Serializer} |
| 8 | + * @param context {es.WikiContext} Context of the wiki the document is a part of |
| 9 | + * @property serializers {Object} List of serializing methods indexed by symbolic object names |
| 10 | + */ |
| 11 | +es.Document.WikitextSerializer = function( context ) { |
| 12 | + es.Document.Serializer.call( this, context ); |
| 13 | + this.serializers = { |
| 14 | + 'comment': this.serializeComment, |
| 15 | + 'horizontal-rule': this.serializeHorizontalRule, |
| 16 | + 'heading': this.serializeHeading, |
| 17 | + 'paragraph': this.serializeParagraph, |
| 18 | + 'list': this.serializeList, |
| 19 | + 'table': this.serializeTable, |
| 20 | + 'transclusion': this.serializeTransclusion, |
| 21 | + 'parameter': this.serializeParameter |
| 22 | + }; |
| 23 | +}; |
| 24 | + |
| 25 | +/* Methods */ |
| 26 | + |
| 27 | +es.Document.WikitextSerializer.prototype.serializeDocument = function( doc, rawFirstParagraph ) { |
| 28 | + var out = []; |
| 29 | + for ( var b = 0, bMax = doc.blocks.length; b < bMax; b++ ) { |
| 30 | + var block = doc.blocks[b]; |
| 31 | + if ( block.type in this.serializers ) { |
| 32 | + if ( block.type === 'paragraph' ) { |
| 33 | + out.push( |
| 34 | + this.serializeParagraph( block, rawFirstParagraph && b === 0 ) |
| 35 | + ); |
| 36 | + if ( b + 1 < bMax /* && doc.blocks[b + 1].type === 'paragraph' */ ) { |
| 37 | + out.push( '' ); |
| 38 | + } |
| 39 | + } else { |
| 40 | + out.push( this.serializers[block.type].call( this, block ) ); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + return out.join( '\n' ); |
| 45 | +}; |
| 46 | + |
| 47 | +es.Document.WikitextSerializer.prototype.serializeComment = function( comment ) { |
| 48 | + return '<!--' + comment.text + '-->'; |
| 49 | +}; |
| 50 | + |
| 51 | +es.Document.WikitextSerializer.prototype.serializeHorizontalRule = function( rule ) { |
| 52 | + return '----'; |
| 53 | +}; |
| 54 | + |
| 55 | +es.Document.WikitextSerializer.prototype.serializeHeading = function( heading ) { |
| 56 | + var symbols = es.Document.Serializer.repeatString( '=', heading.level ); |
| 57 | + return symbols + this.serializeLine( heading.line ) + symbols; |
| 58 | +}; |
| 59 | + |
| 60 | +es.Document.WikitextSerializer.prototype.serializeParagraph = function( paragraph ) { |
| 61 | + var out = []; |
| 62 | + for ( var l = 0, lMax = paragraph.lines.length; l < lMax; l++ ) { |
| 63 | + out.push( this.serializeLine( paragraph.lines[l] ) ); |
| 64 | + } |
| 65 | + return out.join( '\n' ); |
| 66 | +}; |
| 67 | + |
| 68 | +es.Document.WikitextSerializer.prototype.serializeList = function( list, path ) { |
| 69 | + if ( typeof path === 'undefined' ) { |
| 70 | + path = ''; |
| 71 | + } |
| 72 | + var symbols = { |
| 73 | + 'bullet': '*', |
| 74 | + 'number': '#' |
| 75 | + }; |
| 76 | + path += symbols[list.style]; |
| 77 | + var out = []; |
| 78 | + for ( var i = 0, iMax = list.items.length; i < iMax; i++ ) { |
| 79 | + out.push( this.serializeItem( list.items[i], path ) ); |
| 80 | + } |
| 81 | + return out.join( '\n' ); |
| 82 | +}; |
| 83 | + |
| 84 | +es.Document.WikitextSerializer.prototype.serializeTable = function( table ) { |
| 85 | + var out = []; |
| 86 | + var types = { |
| 87 | + 'heading': '!', |
| 88 | + 'data': '|' |
| 89 | + }; |
| 90 | + out.push( '{|' + es.Document.Serializer.buildXmlAttributes( table.attributes ) ); |
| 91 | + for ( var r = 0, rMax = table.rows.length; r < rMax; r++ ) { |
| 92 | + var row = table.rows[r]; |
| 93 | + if ( r ) { |
| 94 | + out.push( '|-' ); |
| 95 | + } |
| 96 | + for ( var c = 0, cMax = row.length; c < cMax; c++ ) { |
| 97 | + var type = types[row[c].type || 'data'], |
| 98 | + attr = row[c].attributes |
| 99 | + ? es.Document.Serializer.buildXmlAttributes( row[c].attributes ) + '|' : '' |
| 100 | + out.push( type + attr + this.serializeDocument( row[c].document, true ) ); |
| 101 | + } |
| 102 | + } |
| 103 | + out.push( '|}' ); |
| 104 | + return out.join( '\n' ); |
| 105 | +}; |
| 106 | + |
| 107 | +es.Document.WikitextSerializer.prototype.serializeTransclusion = function( transclusion ) { |
| 108 | + var title = []; |
| 109 | + if ( transclusion.namespace === 'Main' ) { |
| 110 | + title.push( '' ); |
| 111 | + } else if ( transclusion.namespace !== 'Template' ) { |
| 112 | + title.push( transclusion.namespace ) |
| 113 | + } |
| 114 | + title.push( transclusion.title ); |
| 115 | + return '{{' + title.join( ':' ) + '}}'; |
| 116 | +}; |
| 117 | + |
| 118 | +es.Document.WikitextSerializer.prototype.serializeParameter = function( parameter ) { |
| 119 | + return '{{{' + parameter.name + '}}}'; |
| 120 | +}; |
| 121 | + |
| 122 | +es.Document.WikitextSerializer.prototype.serializeItem = function( item, path ) { |
| 123 | + if ( 'lists' in item && item.lists.length ) { |
| 124 | + var out = []; |
| 125 | + out.push( path + ' ' + this.serializeLine( item.line ) ); |
| 126 | + for ( var l = 0, lMax = item.lists.length; l < lMax; l++ ) { |
| 127 | + out.push( this.serializeList( item.lists[l], path ) ); |
| 128 | + } |
| 129 | + return out.join( '\n' ); |
| 130 | + } else { |
| 131 | + return path + ' ' + this.serializeLine( item.line ); |
| 132 | + } |
| 133 | +}; |
| 134 | + |
| 135 | +es.Document.WikitextSerializer.prototype.serializeLine = function( line ) { |
| 136 | + if ( 'annotations' in line && line.annotations.length ) { |
| 137 | + var as = new es.AnnotationSerializer(); |
| 138 | + for ( var a = 0, aMax = line.annotations.length; a < aMax; a++ ) { |
| 139 | + var an = line.annotations[a]; |
| 140 | + switch ( an.type ) { |
| 141 | + case 'bold': |
| 142 | + as.add( an.range, '\'\'\'', '\'\'\'' ); |
| 143 | + break; |
| 144 | + case 'italic': |
| 145 | + as.add( an.range, '\'\'', '\'\'' ); |
| 146 | + break; |
| 147 | + case 'xlink': |
| 148 | + as.add( an.range, '[' + an.data.href + ' ', ']' ); |
| 149 | + break; |
| 150 | + case 'ilink': |
| 151 | + as.add( an.range, '[[' + an.data.title + '|', ']]' ); |
| 152 | + break; |
| 153 | + } |
| 154 | + } |
| 155 | + return as.render( line.text ); |
| 156 | + } else { |
| 157 | + return line.text; |
| 158 | + } |
| 159 | +}; |
| 160 | + |
| 161 | +/* Registration */ |
| 162 | + |
| 163 | +es.Document.serializers.wikitext = function( doc, context ) { |
| 164 | + var serializer = new es.Document.WikitextSerializer( context ) |
| 165 | + return serializer.serializeDocument( doc ); |
| 166 | +}; |
| 167 | + |
| 168 | +/* Inheritance */ |
| 169 | + |
| 170 | +es.extend( es.Document.WikitextSerializer, es.Document.Serializer ); |
Property changes on: trunk/parsers/wikidom/lib/es/es.Document.WikitextSerializer.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 171 | + native |
Added: svn:mime-type |
2 | 172 | + text/plain |
Index: trunk/parsers/wikidom/lib/es/es.Document.JsonSerializer.js |
— | — | @@ -0,0 +1,28 @@ |
| 2 | +/** |
| 3 | + * Serializes a WikiDom into JSON. |
| 4 | + * |
| 5 | + * @class |
| 6 | + * @constructor |
| 7 | + * @extends {es.Document.Serializer} |
| 8 | + * @param context {es.WikiContext} Context of the wiki the document is a part of |
| 9 | + */ |
| 10 | +es.Document.JsonSerializer = function( context ) { |
| 11 | + es.Document.Serializer.call( this, context ); |
| 12 | +}; |
| 13 | + |
| 14 | +/* Methods */ |
| 15 | + |
| 16 | +es.Document.JsonSerializer.prototype.serializeDocument = function( doc ) { |
| 17 | + return FormatJSON( doc ); |
| 18 | +}; |
| 19 | + |
| 20 | +/* Registration */ |
| 21 | + |
| 22 | +es.Document.serializers.json = function( doc, context ) { |
| 23 | + var serializer = new es.Document.JsonSerializer( context ) |
| 24 | + return serializer.serializeDocument( doc ); |
| 25 | +}; |
| 26 | + |
| 27 | +/* Inheritance */ |
| 28 | + |
| 29 | +es.extend( es.Document.JsonSerializer, es.Document.Serializer ); |
Property changes on: trunk/parsers/wikidom/lib/es/es.Document.JsonSerializer.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 30 | + native |
Added: svn:mime-type |
2 | 31 | + text/plain |
Index: trunk/parsers/wikidom/lib/es/es.Document.Serializer.js |
— | — | @@ -0,0 +1,69 @@ |
| 2 | +/** |
| 3 | + * Creates content serializer. |
| 4 | + * |
| 5 | + * Base object for all serializers, providing basic shared functionality and stubs for required |
| 6 | + * implementations. |
| 7 | + * |
| 8 | + * @class |
| 9 | + * @constructor |
| 10 | + * @param context {es.WikiContext} Context of the wiki the document is a part of |
| 11 | + * @property context {es.WikiContext} Context of the wiki the document is a part of |
| 12 | + */ |
| 13 | +es.Document.Serializer = function( context ) { |
| 14 | + this.context = context; |
| 15 | +}; |
| 16 | + |
| 17 | +/* Static Methods */ |
| 18 | + |
| 19 | +es.Document.Serializer.repeatString = function( pattern, count ) { |
| 20 | + if ( count < 1 ) { |
| 21 | + return ''; |
| 22 | + } |
| 23 | + var result = ''; |
| 24 | + while ( count > 0 ) { |
| 25 | + if ( count & 1 ) { result += pattern; } |
| 26 | + count >>= 1; |
| 27 | + pattern += pattern; |
| 28 | + } |
| 29 | + return result; |
| 30 | +}; |
| 31 | + |
| 32 | +es.Document.Serializer.escapeXmlText = function( text ) { |
| 33 | + return text |
| 34 | + .replace( /&/g, '&' ) |
| 35 | + .replace( /</g, '<' ) |
| 36 | + .replace( />/g, '>' ) |
| 37 | + .replace( /"/g, '"' ) |
| 38 | + .replace( /'/g, ''' ); |
| 39 | +}; |
| 40 | + |
| 41 | +es.Document.Serializer.buildXmlAttributes = function( attributes, prespace ) { |
| 42 | + var attr = []; |
| 43 | + var name; |
| 44 | + if ( attributes ) { |
| 45 | + for ( name in attributes ) { |
| 46 | + attr.push( name + '="' + attributes[name] + '"' ); |
| 47 | + } |
| 48 | + } |
| 49 | + return ( prespace && attr.length ? ' ' : '' ) + attr.join( ' ' ); |
| 50 | +}; |
| 51 | + |
| 52 | +es.Document.Serializer.buildXmlOpeningTag = function( tag, attributes ) { |
| 53 | + return '<' + tag + es.Document.Serializer.buildXmlAttributes( attributes, true ) + '>'; |
| 54 | +}; |
| 55 | + |
| 56 | +es.Document.Serializer.buildXmlClosingTag = function( tag ) { |
| 57 | + return '</' + tag + '>'; |
| 58 | +}; |
| 59 | + |
| 60 | +es.Document.Serializer.buildXmlTag = function( tag, attributes, value, escape ) { |
| 61 | + if ( value === false ) { |
| 62 | + return '<' + tag + es.Document.Serializer.buildXmlAttributes( attributes, true ) + ' />'; |
| 63 | + } else { |
| 64 | + if ( escape ) { |
| 65 | + value = wiki.util.xml.esc( value ); |
| 66 | + } |
| 67 | + return '<' + tag + es.Document.Serializer.buildXmlAttributes( attributes, true ) + '>' |
| 68 | + + value + '</' + tag + '>'; |
| 69 | + } |
| 70 | +}; |
Property changes on: trunk/parsers/wikidom/lib/es/es.Document.Serializer.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 71 | + native |
Added: svn:mime-type |
2 | 72 | + text/plain |
Index: trunk/parsers/wikidom/demos/es/index.html |
— | — | @@ -51,24 +51,31 @@ |
52 | 52 | <div id="es-editor"></div> |
53 | 53 | |
54 | 54 | <!-- EditSurface --> |
55 | | - <script type="text/javascript" src="../../lib/jquery.js"></script> |
56 | | - <script type="text/javascript" src="../../lib/es/es.js"></script> |
57 | | - <script type="text/javascript" src="../../lib/es/es.EventEmitter.js"></script> |
58 | | - <script type="text/javascript" src="../../lib/es/es.Position.js"></script> |
59 | | - <script type="text/javascript" src="../../lib/es/es.Location.js"></script> |
60 | | - <script type="text/javascript" src="../../lib/es/es.Range.js"></script> |
61 | | - <script type="text/javascript" src="../../lib/es/es.Selection.js"></script> |
62 | | - <script type="text/javascript" src="../../lib/es/es.Content.js"></script> |
63 | | - <script type="text/javascript" src="../../lib/es/es.Container.js"></script> |
64 | | - <script type="text/javascript" src="../../lib/es/es.Block.js"></script> |
65 | | - <script type="text/javascript" src="../../lib/es/es.Document.js"></script> |
66 | | - <script type="text/javascript" src="../../lib/es/es.Surface.js"></script> |
67 | | - <script type="text/javascript" src="../../lib/es/es.Flow.js"></script> |
68 | | - <script type="text/javascript" src="../../lib/es/es.ParagraphBlock.js"></script> |
69 | | - <script type="text/javascript" src="../../lib/es/es.ListBlockList.js"></script> |
70 | | - <script type="text/javascript" src="../../lib/es/es.ListBlockItem.js"></script> |
71 | | - <script type="text/javascript" src="../../lib/es/es.ListBlock.js"></script> |
72 | | - <script type="text/javascript" src="../../lib/es/es.Cursor.js"></script> |
| 55 | + <script src="../../lib/jquery.js"></script> |
| 56 | + <script src="../../lib/FormatJSON.js"></script> |
| 57 | + <script src="../../lib/es/es.js"></script> |
| 58 | + <script src="../../lib/es/es.EventEmitter.js"></script> |
| 59 | + <script src="../../lib/es/es.Position.js"></script> |
| 60 | + <script src="../../lib/es/es.Location.js"></script> |
| 61 | + <script src="../../lib/es/es.Range.js"></script> |
| 62 | + <script src="../../lib/es/es.Selection.js"></script> |
| 63 | + <script src="../../lib/es/es.Content.js"></script> |
| 64 | + <script src="../../lib/es/es.Container.js"></script> |
| 65 | + <script src="../../lib/es/es.Block.js"></script> |
| 66 | + <script src="../../lib/es/es.Document.js"></script> |
| 67 | + <script src="../../lib/es/es.AnnotationSerializer.js"></script> |
| 68 | + <script src="../../lib/es/es.Document.Context.js"></script> |
| 69 | + <script src="../../lib/es/es.Document.Serializer.js"></script> |
| 70 | + <script src="../../lib/es/es.Document.WikitextSerializer.js"></script> |
| 71 | + <script src="../../lib/es/es.Document.HtmlSerializer.js"></script> |
| 72 | + <script src="../../lib/es/es.Document.JsonSerializer.js"></script> |
| 73 | + <script src="../../lib/es/es.Surface.js"></script> |
| 74 | + <script src="../../lib/es/es.Flow.js"></script> |
| 75 | + <script src="../../lib/es/es.ParagraphBlock.js"></script> |
| 76 | + <script src="../../lib/es/es.ListBlockList.js"></script> |
| 77 | + <script src="../../lib/es/es.ListBlockItem.js"></script> |
| 78 | + <script src="../../lib/es/es.ListBlock.js"></script> |
| 79 | + <script src="../../lib/es/es.Cursor.js"></script> |
73 | 80 | |
74 | 81 | <!-- Demo --> |
75 | 82 | <script> |
Index: trunk/parsers/wikidom/demos/es/index2.html |
— | — | @@ -78,31 +78,32 @@ |
79 | 79 | </tr>
|
80 | 80 | </table>
|
81 | 81 |
|
82 | | - <script type="text/javascript" src="../../lib/jquery.js"></script>
|
83 | | - <script type="text/javascript" src="../../lib/es/es.js"></script>
|
84 | | - <script type="text/javascript" src="../../lib/es/es.EventEmitter.js"></script>
|
85 | | - <script type="text/javascript" src="../../lib/es/es.Position.js"></script>
|
86 | | - <script type="text/javascript" src="../../lib/es/es.Location.js"></script>
|
87 | | - <script type="text/javascript" src="../../lib/es/es.Range.js"></script>
|
88 | | - <script type="text/javascript" src="../../lib/es/es.Selection.js"></script>
|
89 | | - <script type="text/javascript" src="../../lib/es/es.Content.js"></script>
|
90 | | - <script type="text/javascript" src="../../lib/es/es.Container.js"></script>
|
91 | | - <script type="text/javascript" src="../../lib/es/es.Block.js"></script>
|
92 | | - <script type="text/javascript" src="../../lib/es/es.Document.js"></script>
|
93 | | - <script type="text/javascript" src="../../lib/es/es.Surface.js"></script>
|
94 | | - <script type="text/javascript" src="../../lib/es/es.Flow.js"></script>
|
95 | | - <script type="text/javascript" src="../../lib/es/es.ParagraphBlock.js"></script>
|
96 | | - <script type="text/javascript" src="../../lib/es/es.ListBlockList.js"></script>
|
97 | | - <script type="text/javascript" src="../../lib/es/es.ListBlockItem.js"></script>
|
98 | | - <script type="text/javascript" src="../../lib/es/es.ListBlock.js"></script>
|
99 | | - <script type="text/javascript" src="../../lib/es/es.Cursor.js"></script>
|
100 | | - <script type="text/javascript" src="../../lib/FormatJSON.js"></script>
|
101 | | - <script src="../../lib/wiki.js" type="text/javascript"></script>
|
102 | | - <script src="../../lib/wiki.util.js" type="text/javascript"></script>
|
103 | | - <script src="../../lib/wiki.AnnotationRenderer.js" type="text/javascript"></script>
|
104 | | - <script src="../../lib/wiki.HtmlRenderer.js" type="text/javascript"></script>
|
105 | | - <script src="../../lib/wiki.WikitextRenderer.js" type="text/javascript"></script>
|
106 | | -
|
| 82 | + <script src="../../lib/jquery.js"></script>
|
| 83 | + <script src="../../lib/FormatJSON.js"></script>
|
| 84 | + <script src="../../lib/es/es.js"></script>
|
| 85 | + <script src="../../lib/es/es.EventEmitter.js"></script>
|
| 86 | + <script src="../../lib/es/es.Position.js"></script>
|
| 87 | + <script src="../../lib/es/es.Location.js"></script>
|
| 88 | + <script src="../../lib/es/es.Range.js"></script>
|
| 89 | + <script src="../../lib/es/es.Selection.js"></script>
|
| 90 | + <script src="../../lib/es/es.Content.js"></script>
|
| 91 | + <script src="../../lib/es/es.Container.js"></script>
|
| 92 | + <script src="../../lib/es/es.Block.js"></script>
|
| 93 | + <script src="../../lib/es/es.Document.js"></script>
|
| 94 | + <script src="../../lib/es/es.AnnotationSerializer.js"></script>
|
| 95 | + <script src="../../lib/es/es.Document.Context.js"></script>
|
| 96 | + <script src="../../lib/es/es.Document.Serializer.js"></script>
|
| 97 | + <script src="../../lib/es/es.Document.WikitextSerializer.js"></script>
|
| 98 | + <script src="../../lib/es/es.Document.HtmlSerializer.js"></script>
|
| 99 | + <script src="../../lib/es/es.Document.JsonSerializer.js"></script>
|
| 100 | + <script src="../../lib/es/es.Surface.js"></script>
|
| 101 | + <script src="../../lib/es/es.Flow.js"></script>
|
| 102 | + <script src="../../lib/es/es.ParagraphBlock.js"></script>
|
| 103 | + <script src="../../lib/es/es.ListBlockList.js"></script>
|
| 104 | + <script src="../../lib/es/es.ListBlockItem.js"></script>
|
| 105 | + <script src="../../lib/es/es.ListBlock.js"></script>
|
| 106 | + <script src="../../lib/es/es.Cursor.js"></script>
|
| 107 | +
|
107 | 108 | <!-- Demo -->
|
108 | 109 | <script>
|
109 | 110 | $(document).ready( function() {
|
— | — | @@ -314,15 +315,13 @@ |
315 | 316 | } );
|
316 | 317 |
|
317 | 318 | var previewTimeout = null;
|
318 | | - var wikitextRenderer = new wiki.WikitextRenderer();
|
319 | 319 | doc.on( 'update', function() {
|
320 | 320 | if ( previewTimeout !== null ) {
|
321 | 321 | clearTimeout( previewTimeout );
|
322 | 322 | }
|
323 | 323 | previewTimeout = setTimeout( function () {
|
324 | | - var data = doc.getWikiDomDocument();
|
325 | | - $( '#wikitext-source' ).text( wikitextRenderer.render( data ) );
|
326 | | - $( '#json-source' ).text( FormatJSON( data ) );
|
| 324 | + $( '#wikitext-source' ).text( doc.serialize( 'wikitext' ) );
|
| 325 | + $( '#json-source' ).text( doc.serialize( 'json' ) );
|
327 | 326 | }, 100 );
|
328 | 327 | } );
|
329 | 328 |
|
Index: trunk/parsers/wikidom/demos/renderers/index.html |
— | — | @@ -58,26 +58,28 @@ |
59 | 59 | <div id="json-source" class="source"></div> |
60 | 60 | <script src="../../lib/jquery.js"></script> |
61 | 61 | <script src="../../lib/FormatJSON.js"></script> |
62 | | - <script src="../../lib/wiki.js" type="text/javascript"></script> |
63 | | - <script src="../../lib/wiki.util.js" type="text/javascript"></script> |
64 | | - <script src="../../lib/wiki.AnnotationRenderer.js" type="text/javascript"></script> |
65 | | - <script src="../../lib/wiki.HtmlRenderer.js" type="text/javascript"></script> |
66 | | - <script src="../../lib/wiki.WikitextRenderer.js" type="text/javascript"></script> |
| 62 | + <script src="../../lib/es/es.js"></script> |
| 63 | + <script src="../../lib/es/es.EventEmitter.js"></script> |
| 64 | + <script src="../../lib/es/es.Container.js"></script> |
| 65 | + <script src="../../lib/es/es.AnnotationSerializer.js"></script> |
| 66 | + <script src="../../lib/es/es.Document.js"></script> |
| 67 | + <script src="../../lib/es/es.Document.Context.js"></script> |
| 68 | + <script src="../../lib/es/es.Document.Serializer.js"></script> |
| 69 | + <script src="../../lib/es/es.Document.WikitextSerializer.js"></script> |
| 70 | + <script src="../../lib/es/es.Document.HtmlSerializer.js"></script> |
| 71 | + <script src="../../lib/es/es.Document.JsonSerializer.js"></script> |
67 | 72 | <script> |
68 | 73 | $( document ).ready( function() { |
69 | 74 | $.getJSON( 'document.js', function( data ) { |
70 | | - // HTML |
71 | | - var htmlRenderer = new wiki.HtmlRenderer(); |
72 | | - var html = htmlRenderer.render( data ); |
| 75 | + var context = new es.Document.Context(), |
| 76 | + htmlSerializer = new es.Document.HtmlSerializer( context ), |
| 77 | + wikitextSerializer = new es.Document.HtmlSerializer( context ), |
| 78 | + jsonSerializer = new es.Document.JsonSerializer( context ), |
| 79 | + html = htmlSerializer.serializeDocument( data ); |
73 | 80 | $( '#html-rendering' ).append( html ); |
74 | 81 | $( '#html-source' ).text( html ); |
75 | | - // Wikitext |
76 | | - var wikitextRenderer = new wiki.WikitextRenderer(); |
77 | | - var wikitext = wikitextRenderer.render( data ); |
78 | | - $( '#wikitext-source' ).text( wikitext ); |
79 | | - // JSON |
80 | | - var json = FormatJSON( data ); |
81 | | - $( '#json-source' ).text( json ); |
| 82 | + $( '#wikitext-source' ).text( wikitextSerializer.serializeDocument( data ) ); |
| 83 | + $( '#json-source' ).text( jsonSerializer.serializeDocument( data ) ); |
82 | 84 | } ); |
83 | 85 | } ); |
84 | 86 | </script> |