Index: trunk/extensions/VisualEditor/modules/parser/ext.core.TemplateHandler.js |
— | — | @@ -0,0 +1,146 @@ |
| 2 | +/** |
| 3 | + * Template and template argument handling. |
| 4 | + * |
| 5 | + * @author Gabriel Wicke <gwicke@wikimedia.org> |
| 6 | + */ |
| 7 | + |
| 8 | +function TemplateHandler () { |
| 9 | + this.reset(); |
| 10 | +} |
| 11 | + |
| 12 | +TemplateHandler.prototype.reset = function () { |
| 13 | + this.outstanding = 0; |
| 14 | +}; |
| 15 | + |
| 16 | +// constants |
| 17 | +TemplateHandler.prototype.rank = 1.1; |
| 18 | + |
| 19 | +TemplateHandler.prototype.register = function ( dispatcher ) { |
| 20 | + this.dispatcher = dispatcher; |
| 21 | + // Register for template and templatearg tag tokens |
| 22 | + dispatcher.addTransform( this.onTemplate.bind(this), |
| 23 | + this.rank, 'tag', 'template' ); |
| 24 | + dispatcher.addTransform( this.onTemplateArg.bind(this), |
| 25 | + this.rank, 'tag', 'templatearg' ); |
| 26 | + |
| 27 | + // Reset internal state when the parser pipeline is done |
| 28 | + dispatcher.addTransform( this.reset.bind(this), |
| 29 | + this.rank, 'end' ); |
| 30 | +}; |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * Main template token handler |
| 35 | + * |
| 36 | + * Expands target and arguments (both keys and values) and either directly |
| 37 | + * calls or sets up the callback to _expandTemplate, which then fetches and |
| 38 | + * processes the template. |
| 39 | + */ |
| 40 | +TemplateHandler.prototype.onTemplate = function ( token, cb, frame ) { |
| 41 | + // check for 'subst:' |
| 42 | + // check for variable magic names |
| 43 | + // check for msg, msgnw, raw magics |
| 44 | + // check for parser functions |
| 45 | + |
| 46 | + // create a new frame |
| 47 | + var newFrame = { |
| 48 | + args: {}, |
| 49 | + env: frame.env, |
| 50 | + target: token.attribs[0][1], // XXX: use data-target key instead! |
| 51 | + // Also handle templates and args in |
| 52 | + // target! |
| 53 | + outstanding: 0, |
| 54 | + cb: cb |
| 55 | + }, |
| 56 | + argcb, |
| 57 | + i = 0, |
| 58 | + kvs = [], |
| 59 | + res, |
| 60 | + kv; |
| 61 | + // XXX: transform the target |
| 62 | + |
| 63 | + |
| 64 | + // transform each argument (key and value), and handle asynchronous returns |
| 65 | + for ( var key in token.args ) { |
| 66 | + if ( token.hasOwnProperty( key ) ) { |
| 67 | + kv = { key: [], value: [] }; |
| 68 | + // transform the value |
| 69 | + argCB = this._returnArgValue.bind( this, { index: i, frame: newFrame } ); |
| 70 | + res = frame.transformPhase( frame, args[key], argCB ); |
| 71 | + if ( res.async ) { |
| 72 | + newFrame.outstanding++; |
| 73 | + } |
| 74 | + kv.value = res.tokens; |
| 75 | + |
| 76 | + // XXX: transform key too, and store it in the token's value for |
| 77 | + // the original key |
| 78 | + // For now, we assume the key to be a string. |
| 79 | + kv.key = key; |
| 80 | + |
| 81 | + // finally, append to kvs |
| 82 | + kvs.push( kv ); |
| 83 | + i++; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if ( newFrame.outstanding === 0 ) { |
| 88 | + return this._expandTemplate ( newFrame ); |
| 89 | + } else { |
| 90 | + return { async: true }; |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +/** |
| 95 | + * Callback for async argument value expansions |
| 96 | + */ |
| 97 | +TemplateHandler.prototype._returnArgValue = function ( ref, tokens, notYetDone ) { |
| 98 | + var frame = ref.frame, |
| 99 | + res; |
| 100 | + frame.args[ref.index].push( tokens ); |
| 101 | + if ( ! notYetDone ) { |
| 102 | + frame.outstanding--; |
| 103 | + if ( frame.outstanding === 0 ) { |
| 104 | + // this calls back to frame.cb, so no return here. |
| 105 | + this._expandTemplate( frame ); |
| 106 | + } |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +/** |
| 111 | + * Fetch, tokenize and token-transform a template after all arguments and the |
| 112 | + * target were expanded in frame. |
| 113 | + */ |
| 114 | +TemplateHandler.prototype._expandTemplate = function ( frame ) { |
| 115 | + // Set up a pipeline: |
| 116 | + // fetch template source -> tokenizer |
| 117 | + // -> TokenTransformDispatcher (phase 1/2 only, with frame passed in) |
| 118 | + // -> frame.cb( tokens ) |
| 119 | + |
| 120 | + |
| 121 | + // XXX: notes from brion's mediawiki.parser.environment |
| 122 | + // resolve template name |
| 123 | + // load template w/ canonical name |
| 124 | + // load template w/ variant names |
| 125 | + // recursion depth check |
| 126 | + // fetch from DB or interwiki |
| 127 | + // infinte loop check |
| 128 | + // |
| 129 | + // TODO: template fetching is already implemented there, copy this over! |
| 130 | +}; |
| 131 | + |
| 132 | + |
| 133 | +TemplateHandler.prototype.onTemplateArg = function ( token, cb, frame ) { |
| 134 | + var argName = token.attribs[0][1]; // XXX: do this properly! |
| 135 | + if ( argName in frame.args ) { |
| 136 | + // return tokens for argument |
| 137 | + return { tokens: frame.args[argName] }; |
| 138 | + } else { |
| 139 | + // FIXME: support default value! |
| 140 | + return { token: { type: 'TEXT', value: '' } }; |
| 141 | + } |
| 142 | +}; |
| 143 | + |
| 144 | + |
| 145 | +if (typeof module == "object") { |
| 146 | + module.exports.TemplateHandler = TemplateHandler; |
| 147 | +} |
Property changes on: trunk/extensions/VisualEditor/modules/parser/ext.core.TemplateHandler.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 148 | + native |
Index: trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt |
— | — | @@ -405,7 +405,7 @@ |
406 | 406 | //b.push({type: 'NEWLINE'}); |
407 | 407 | // Move this to a token stream transform! |
408 | 408 | //console.log('about to emit' + pp(self)); |
409 | | - //self._tokenizer.emit('chunk', b); |
| 409 | + console.log( pp( result._tokenizer )); |
410 | 410 | //console.log('emitted chunk' + pp(b)); |
411 | 411 | //return []; |
412 | 412 | return b; |
— | — | @@ -718,6 +718,7 @@ |
719 | 719 | return obj; |
720 | 720 | } |
721 | 721 | |
| 722 | +// XXX: support template and args in target! |
722 | 723 | template_target |
723 | 724 | = h:( !"}}" x:([^|\n]) { return x } )* { return h.join(''); } |
724 | 725 | |
— | — | @@ -742,6 +743,7 @@ |
743 | 744 | return obj; |
744 | 745 | } |
745 | 746 | |
| 747 | +// FIXME: handle template args and templates in key! (or even parser functions?) |
746 | 748 | template_param_name |
747 | 749 | = h:( !"}}" x:([^=|\n]) { return x } )* { return h.join(''); } |
748 | 750 | |