Index: trunk/extensions/VisualEditor/modules/es/es.Document.HtmlSerializer.js |
— | — | @@ -0,0 +1,204 @@ |
| 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 options {Object} List of options for serialization |
| 10 | + * @property serializers {Object} List of serializing methods indexed by symbolic object names |
| 11 | + */ |
| 12 | +es.Document.HtmlSerializer = function( context, options ) { |
| 13 | + es.Document.Serializer.call( this, context ); |
| 14 | + this.options = $.extend( { |
| 15 | + // defaults |
| 16 | + }, options || {} ); |
| 17 | + this.serializers = { |
| 18 | + 'comment': this.serializeComment, |
| 19 | + 'horizontal-rule': this.serializeHorizontalRule, |
| 20 | + 'heading': this.serializeHeading, |
| 21 | + 'paragraph': this.serializeParagraph, |
| 22 | + 'list': this.serializeList, |
| 23 | + 'table': this.serializeTable, |
| 24 | + 'transclusion': this.serializeTransclusion, |
| 25 | + 'parameter': this.serializeParameter |
| 26 | + }; |
| 27 | +}; |
| 28 | + |
| 29 | +/* Methods */ |
| 30 | + |
| 31 | +es.Document.HtmlSerializer.prototype.serializeDocument = function( doc, rawFirstParagraph ) { |
| 32 | + var out = []; |
| 33 | + for ( var b = 0, bMax = doc.blocks.length; b < bMax; b++ ) { |
| 34 | + var block = doc.blocks[b]; |
| 35 | + if ( block.type in this.serializers ) { |
| 36 | + if ( block.type === 'paragraph' ) { |
| 37 | + out.push( this.serializeParagraph( block, rawFirstParagraph && b === 0 ) ); |
| 38 | + } else { |
| 39 | + out.push( this.serializers[block.type].call( this, block ) ); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + return out.join( '\n' ); |
| 44 | +}; |
| 45 | + |
| 46 | +es.Document.HtmlSerializer.prototype.serializeComment = function( comment ) { |
| 47 | + return '<!--' + comment.text + '-->'; |
| 48 | +}; |
| 49 | + |
| 50 | +es.Document.HtmlSerializer.prototype.serializeHorizontalRule = function( rule ) { |
| 51 | + return es.Document.Serializer.buildXmlTag( 'hr', {}, false ); |
| 52 | +}; |
| 53 | + |
| 54 | +es.Document.HtmlSerializer.prototype.serializeHeading = function( heading ) { |
| 55 | + return es.Document.Serializer.buildXmlTag( |
| 56 | + 'h' + heading.level, {}, this.serializeLine( heading.line ) |
| 57 | + ); |
| 58 | +}; |
| 59 | + |
| 60 | +es.Document.HtmlSerializer.prototype.serializeParagraph = function( paragraph, raw ) { |
| 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 | + if ( raw ) { |
| 66 | + return out.join( '\n' ); |
| 67 | + } else { |
| 68 | + return es.Document.Serializer.buildXmlTag( 'p', {}, out.join( '\n' ) ); |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +es.Document.HtmlSerializer.prototype.serializeList = function( list ) { |
| 73 | + var tags = { |
| 74 | + 'bullet': 'ul', |
| 75 | + 'number': 'ol' |
| 76 | + }; |
| 77 | + var out = []; |
| 78 | + out.push( es.Document.Serializer.buildXmlOpeningTag( tags[list.style] ) ); |
| 79 | + for ( var i = 0, iMax = list.items.length; i < iMax; i++ ) { |
| 80 | + out.push( this.serializeItem( list.items[i] ) ); |
| 81 | + } |
| 82 | + out.push( es.Document.Serializer.buildXmlClosingTag( tags[list.style] ) ); |
| 83 | + return out.join( '\n' ); |
| 84 | +}; |
| 85 | + |
| 86 | +es.Document.HtmlSerializer.prototype.serializeTable = function( table ) { |
| 87 | + var out = []; |
| 88 | + var types = { |
| 89 | + 'heading': 'th', |
| 90 | + 'data': 'td' |
| 91 | + }; |
| 92 | + out.push( es.Document.Serializer.buildXmlOpeningTag( 'table', table.attributes ) ); |
| 93 | + for ( var r = 0, rMax = table.rows.length; r < rMax; r++ ) { |
| 94 | + out.push( es.Document.Serializer.buildXmlOpeningTag( 'tr' ) ); |
| 95 | + var row = table.rows[r]; |
| 96 | + for ( var c = 0, cMax = row.length; c < cMax; c++ ) { |
| 97 | + var type = types[row[c].type || 'data']; |
| 98 | + out.push( es.Document.Serializer.buildXmlTag( |
| 99 | + type, |
| 100 | + row[c].attributes, |
| 101 | + this.serializeDocument( row[c].document, true ) |
| 102 | + ) ); |
| 103 | + } |
| 104 | + out.push( es.Document.Serializer.buildXmlClosingTag( 'tr' ) ); |
| 105 | + } |
| 106 | + out.push( es.Document.Serializer.buildXmlClosingTag( 'table' ) ); |
| 107 | + return out.join( '\n' ); |
| 108 | +}; |
| 109 | + |
| 110 | +es.Document.HtmlSerializer.prototype.serializeTransclusion = function( transclusion ) { |
| 111 | + var title = []; |
| 112 | + if ( transclusion.namespace !== 'Main' ) { |
| 113 | + title.push( transclusion.namespace ); |
| 114 | + } |
| 115 | + title.push( transclusion.title ); |
| 116 | + title = title.join( ':' ); |
| 117 | + return es.Document.Serializer.buildXmlTag( 'a', { 'href': '/wiki/' + title }, title ); |
| 118 | +}; |
| 119 | + |
| 120 | +es.Document.HtmlSerializer.prototype.serializeParameter = function( parameter ) { |
| 121 | + return '{{{' + parameter.name + '}}}'; |
| 122 | +}; |
| 123 | + |
| 124 | +es.Document.HtmlSerializer.prototype.serializeItem = function( item ) { |
| 125 | + if ( 'lists' in item && item.lists.length ) { |
| 126 | + var out = []; |
| 127 | + out.push( |
| 128 | + es.Document.Serializer.buildXmlOpeningTag( 'li' ) + this.serializeLine( item.line ) |
| 129 | + ); |
| 130 | + for ( var l = 0, lMax = item.lists.length; l < lMax; l++ ) { |
| 131 | + out.push( this.serializeList( item.lists[l] ) ); |
| 132 | + } |
| 133 | + out.push( es.Document.Serializer.buildXmlClosingTag( 'li' ) ); |
| 134 | + return out.join( '\n' ); |
| 135 | + } else { |
| 136 | + return es.Document.Serializer.buildXmlTag( 'li', {}, this.serializeLine( item.line ) ); |
| 137 | + } |
| 138 | +}; |
| 139 | + |
| 140 | +es.Document.HtmlSerializer.prototype.serializeLine = function( line ) { |
| 141 | + var as = new es.AnnotationSerializer(); |
| 142 | + function addXml( range, tag, attributes ) { |
| 143 | + as.add( |
| 144 | + range, |
| 145 | + es.Document.Serializer.buildXmlOpeningTag( tag, attributes ), |
| 146 | + es.Document.Serializer.buildXmlClosingTag( tag ) |
| 147 | + ); |
| 148 | + } |
| 149 | + if ( 'annotations' in line && line.annotations.length ) { |
| 150 | + for ( var a = 0, aMax = line.annotations.length; a < aMax; a++ ) { |
| 151 | + var an = line.annotations[a]; |
| 152 | + switch ( an.type ) { |
| 153 | + case 'bold': |
| 154 | + addXml( an.range, 'strong' ); |
| 155 | + break; |
| 156 | + case 'italic': |
| 157 | + addXml( an.range, 'em' ); |
| 158 | + break; |
| 159 | + case 'size': |
| 160 | + switch ( an.data.type ) { |
| 161 | + case 'big': |
| 162 | + addXml( an.range, 'big' ); |
| 163 | + break; |
| 164 | + case 'small': |
| 165 | + addXml( an.range, 'small' ); |
| 166 | + break; |
| 167 | + } |
| 168 | + break; |
| 169 | + case 'script': |
| 170 | + switch ( an.data.type ) { |
| 171 | + case 'super': |
| 172 | + addXml( an.range, 'sup' ); |
| 173 | + break; |
| 174 | + case 'sub': |
| 175 | + addXml( an.range, 'sub' ); |
| 176 | + break; |
| 177 | + } |
| 178 | + break; |
| 179 | + case 'xlink': |
| 180 | + addXml( an.range, 'a', { 'href': an.data.url } ); |
| 181 | + break; |
| 182 | + case 'ilink': |
| 183 | + addXml( an.range, 'a', { 'href': '/wiki/' + an.data.title } ); |
| 184 | + break; |
| 185 | + case 'template': |
| 186 | + as.add( an.range, an.data.html, '' ); |
| 187 | + break; |
| 188 | + } |
| 189 | + } |
| 190 | + return as.render( line.text ); |
| 191 | + } else { |
| 192 | + return line.text; |
| 193 | + } |
| 194 | +}; |
| 195 | + |
| 196 | +/* Registration */ |
| 197 | + |
| 198 | +es.Document.serializers.html = function( doc, context, options ) { |
| 199 | + var serializer = new es.Document.HtmlSerializer( context, options ); |
| 200 | + return serializer.serializeDocument( doc ); |
| 201 | +}; |
| 202 | + |
| 203 | +/* Inheritance */ |
| 204 | + |
| 205 | +es.extend( es.Document.HtmlSerializer, es.Document.Serializer ); |
Property changes on: trunk/extensions/VisualEditor/modules/es/es.Document.HtmlSerializer.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 206 | + native |
Added: svn:mime-type |
2 | 207 | + text/plain |