Index: trunk/extensions/VisualEditor/modules/parser/ext.Cite.js |
— | — | @@ -1,16 +1,20 @@ |
2 | 2 | /** |
3 | | - * The ref / references tags don't do any fancy HTML, so we can actually |
4 | | - * implement this in terms of parse tree manipulations, skipping the need |
5 | | - * for renderer-specific plugins as well. |
| 3 | + * Simple token transform version of the Cite extension. |
6 | 4 | * |
7 | | - * Pretty neat huh! |
| 5 | + * @class |
| 6 | + * @constructor |
8 | 7 | */ |
9 | | - |
10 | 8 | function Cite () { |
11 | 9 | this.refGroups = {}; |
12 | 10 | this.refTokens = []; |
13 | 11 | } |
14 | 12 | |
| 13 | +/** |
| 14 | + * Register with dispatcher. |
| 15 | + * |
| 16 | + * @method |
| 17 | + * @param {Object} TokenTransformDispatcher to register to |
| 18 | + */ |
15 | 19 | Cite.prototype.register = function ( dispatcher ) { |
16 | 20 | // Register for ref and references tag tokens |
17 | 21 | var self = this; |
— | — | @@ -27,9 +31,17 @@ |
28 | 32 | }; |
29 | 33 | |
30 | 34 | |
31 | | -// Convert list of key-value pairs to object, with first entry for a key |
32 | | -// winning. |
33 | | -// XXX: Move to general util module |
| 35 | +/** |
| 36 | + * Convert list of key-value pairs to object, with first entry for a |
| 37 | + * key. |
| 38 | + * |
| 39 | + * XXX: Move to general utils |
| 40 | + * |
| 41 | + * @static |
| 42 | + * @method |
| 43 | + * @param {Array} List of [key, value] pairs |
| 44 | + * @returns {Object} Object with key/values set, first entry wins. |
| 45 | + */ |
34 | 46 | Cite.prototype.attribsToObject = function ( attribs ) { |
35 | 47 | if ( attribs === undefined ) { |
36 | 48 | return {}; |
— | — | @@ -44,7 +56,13 @@ |
45 | 57 | return obj; |
46 | 58 | }; |
47 | 59 | |
48 | | - |
| 60 | +/** |
| 61 | + * Handle ref tag tokens. |
| 62 | + * |
| 63 | + * @method |
| 64 | + * @param {Object} TokenContext |
| 65 | + * @returns {Object} TokenContext |
| 66 | + */ |
49 | 67 | Cite.prototype.onRef = function ( tokenCTX ) { |
50 | 68 | |
51 | 69 | var refGroups = this.refGroups; |
— | — | @@ -131,19 +149,21 @@ |
132 | 150 | { |
133 | 151 | type: 'TAG', |
134 | 152 | name: 'span', |
135 | | - attribs: [['id', linkback], |
136 | | - ['class', 'reference'], |
137 | | - // ignore element when serializing back to wikitext |
138 | | - ['data-nosource', '']] |
| 153 | + attribs: [ |
| 154 | + ['id', linkback], |
| 155 | + ['class', 'reference'], |
| 156 | + // ignore element when serializing back to wikitext |
| 157 | + ['data-nosource', ''] |
| 158 | + ] |
139 | 159 | }, |
140 | 160 | { |
141 | 161 | type: 'TAG', |
142 | 162 | name: 'a', |
143 | | - attribs: |
144 | | - [['data-type', 'hashlink'], |
| 163 | + attribs: [ |
| 164 | + ['data-type', 'hashlink'], |
145 | 165 | ['href', '#' + ref.target] |
146 | 166 | // XXX: Add round-trip info here? |
147 | | - ] |
| 167 | + ] |
148 | 168 | }, |
149 | 169 | { |
150 | 170 | type: 'TEXT', |
— | — | @@ -161,6 +181,13 @@ |
162 | 182 | return tokenCTX; |
163 | 183 | }; |
164 | 184 | |
| 185 | +/** |
| 186 | + * Handle references tag tokens. |
| 187 | + * |
| 188 | + * @method |
| 189 | + * @param {Object} TokenContext |
| 190 | + * @returns {Object} TokenContext |
| 191 | + */ |
165 | 192 | Cite.prototype.onReferences = function ( tokenCTX ) { |
166 | 193 | |
167 | 194 | var refGroups = this.refGroups; |
— | — | @@ -174,36 +201,41 @@ |
175 | 202 | attribs: [['id', ref.target]] |
176 | 203 | }]; |
177 | 204 | if (ref.linkbacks.length == 1) { |
178 | | - out = out.concat([{ |
179 | | - type: 'TAG', |
180 | | - name: 'a', |
181 | | - attribs: |
182 | | - [['data-type', 'hashlink'], |
183 | | - ['href', '#' + ref.linkbacks[0]] |
184 | | - ] |
185 | | - }, |
186 | | - {type: 'TEXT', value: arrow}, |
187 | | - {type: 'ENDTAG', name: 'a'} |
188 | | - ], |
189 | | - ref.tokens // The original content tokens |
| 205 | + out = out.concat([ |
| 206 | + { |
| 207 | + type: 'TAG', |
| 208 | + name: 'a', |
| 209 | + attribs: [ |
| 210 | + ['data-type', 'hashlink'], |
| 211 | + ['href', '#' + ref.linkbacks[0]] |
| 212 | + ] |
| 213 | + }, |
| 214 | + {type: 'TEXT', value: arrow}, |
| 215 | + {type: 'ENDTAG', name: 'a'} |
| 216 | + ], |
| 217 | + ref.tokens // The original content tokens |
190 | 218 | ); |
191 | 219 | } else { |
192 | 220 | out.content.push({type: 'TEXT', value: arrow}); |
193 | 221 | $.each(ref.linkbacks, function(i, linkback) { |
194 | | - out = out.concat([{ |
195 | | - type: 'TAG', |
196 | | - name: 'a', |
197 | | - attribs: |
198 | | - [['data-type', 'hashlink'], |
199 | | - ['href', '#' + ref.linkbacks[0]] |
200 | | - ] |
201 | | - }, |
202 | | - // XXX: make formatNum available! |
203 | | - //{type: 'TEXT', value: env.formatNum( ref.groupIndex + '.' + i)}, |
204 | | - {type: 'TEXT', value: ref.groupIndex + '.' + i}, |
205 | | - {type: 'ENDTAG', name: 'a'} |
206 | | - ], |
207 | | - ref.tokens // The original content tokens |
| 222 | + out = out.concat([ |
| 223 | + { |
| 224 | + type: 'TAG', |
| 225 | + name: 'a', |
| 226 | + attribs: [ |
| 227 | + ['data-type', 'hashlink'], |
| 228 | + ['href', '#' + ref.linkbacks[0]] |
| 229 | + ] |
| 230 | + }, |
| 231 | + // XXX: make formatNum available! |
| 232 | + //{ |
| 233 | + // type: 'TEXT', |
| 234 | + // value: env.formatNum( ref.groupIndex + '.' + i) |
| 235 | + //}, |
| 236 | + {type: 'TEXT', value: ref.groupIndex + '.' + i}, |
| 237 | + {type: 'ENDTAG', name: 'a'} |
| 238 | + ], |
| 239 | + ref.tokens // The original content tokens |
208 | 240 | ); |
209 | 241 | }); |
210 | 242 | } |
— | — | @@ -220,22 +252,35 @@ |
221 | 253 | if (options.group in refGroups) { |
222 | 254 | var group = refGroups[options.group]; |
223 | 255 | var listItems = $.map(group.refs, renderLine); |
224 | | - tokenCTX.token = [{ |
225 | | - type: 'TAG', |
226 | | - name: 'ol', |
227 | | - attribs: [['class', 'references']] |
228 | | - }].concat(listItems, {type: 'ENDTAG', name: 'ol'}); |
| 256 | + tokenCTX.token = [ |
| 257 | + { |
| 258 | + type: 'TAG', |
| 259 | + name: 'ol', |
| 260 | + attribs: [ |
| 261 | + ['class', 'references'] |
| 262 | + ] |
| 263 | + } |
| 264 | + ].concat( listItems, { type: 'ENDTAG', name: 'ol' } ); |
229 | 265 | } else { |
230 | 266 | tokenCTX.token = { |
231 | 267 | type: 'SELFCLOSINGTAG', |
232 | 268 | name: 'placeholder', |
233 | | - attribs: [['data-origNode', JSON.stringify(token)]] |
| 269 | + attribs: [ |
| 270 | + ['data-origNode', JSON.stringify(token)] |
| 271 | + ] |
234 | 272 | }; |
235 | 273 | } |
236 | 274 | |
237 | 275 | return tokenCTX; |
238 | 276 | }; |
239 | 277 | |
| 278 | +/** |
| 279 | + * Handle end token. |
| 280 | + * |
| 281 | + * @method |
| 282 | + * @param {Object} TokenContext |
| 283 | + * @returns {Object} TokenContext |
| 284 | + */ |
240 | 285 | Cite.prototype.onEnd = function ( tokenCTX ) { |
241 | 286 | // XXX: Emit error messages if references tag was missing! |
242 | 287 | // Clean up |