r108255 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108254‎ | r108255 | r108256 >
Date:17:19, 6 January 2012
Author:gwicke
Status:deferred
Tags:
Comment:
Handle default for template arg expansion, add template fetch functionality
and tweak a few minor things in the grammar and QuoteTransformer.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/parser/ext.core.QuoteTransformer.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/ext.core.TemplateHandler.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/mediawiki.tokenizer.peg.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/parser/ext.core.QuoteTransformer.js
@@ -5,11 +5,13 @@
66 */
77
88 function QuoteTransformer ( ) {
9 - this.quoteAndNewlineRank = 2.1;
10 - this.anyRank = 2.101; // Just after regular quote and newline
119 this.reset();
1210 }
1311
 12+// constants
 13+QuoteTransformer.prototype.quoteAndNewlineRank = 2.1;
 14+QuoteTransformer.prototype.anyRank = 2.101; // Just after regular quote and newline
 15+
1416 QuoteTransformer.prototype.reset = function ( ) {
1517 // A chunk starts with a token context around a quote token and is
1618 // (optionally) followed by non-quote tokens. The quote token and its
Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.tokenizer.peg.js
@@ -29,6 +29,8 @@
3030 this.parser = PEG.buildParser(this.src);
3131 // add reference to this for event emission
3232 this.parser._tokenizer = this;
 33+ // Print the generated parser source
 34+ //console.log(this.parser.toSource());
3335 }
3436
3537 // some normalization
Index: trunk/extensions/VisualEditor/modules/parser/ext.core.TemplateHandler.js
@@ -2,8 +2,11 @@
33 * Template and template argument handling.
44 *
55 * @author Gabriel Wicke <gwicke@wikimedia.org>
 6+ * @author Brion Vibber <brion@wikimedia.org>
67 */
 8+var $ = require('jquery');
79
 10+
811 function TemplateHandler () {
912 this.reset();
1013 }
@@ -129,14 +132,62 @@
130133 };
131134
132135
 136+/**
 137+ * Fetch a template
 138+ */
 139+TemplateHandler.prototype._fetchTemplateAndTitle = function( title, frame, callback ) {
 140+ // @fixme normalize name?
 141+ if (title in this.pageCache) {
 142+ // @fixme should this be forced to run on next event?
 143+ callback( this.pageCache[title], title );
 144+ } else {
 145+ // whee fun hack!
 146+ console.log(title);
 147+ console.log(this.pageCache);
 148+ $.ajax({
 149+ url: frame.env.wgScriptPath + '/api' + frame.env.wgScriptExtension,
 150+ data: {
 151+ format: 'json',
 152+ action: 'query',
 153+ prop: 'revisions',
 154+ rvprop: 'content',
 155+ titles: name
 156+ },
 157+ success: function(data, xhr) {
 158+ var src = null, title = null;
 159+ $.each(data.query.pages, function(i, page) {
 160+ if (page.revisions && page.revisions.length) {
 161+ src = page.revisions[0]['*'];
 162+ title = page.title;
 163+ }
 164+ });
 165+ if (typeof src !== 'string') {
 166+ callback(null, null, 'Page not found');
 167+ } else {
 168+ callback(src, title);
 169+ }
 170+ },
 171+ error: function(msg) {
 172+ callback(null, null, 'Page/template fetch failure');
 173+ },
 174+ dataType: 'json',
 175+ cache: false // @fixme caching, versions etc?
 176+ }, 'json');
 177+ }
 178+};
 179+
 180+
133181 TemplateHandler.prototype.onTemplateArg = function ( token, cb, frame ) {
134182 var argName = token.attribs[0][1]; // XXX: do this properly!
135183 if ( argName in frame.args ) {
136184 // return tokens for argument
137185 return { tokens: frame.args[argName] };
138186 } else {
139 - // FIXME: support default value!
140 - return { token: { type: 'TEXT', value: '' } };
 187+ if ( token.attribs.length > 1 ) {
 188+ return token.attribs[1][1]; // default value, XXX: use key
 189+ } else {
 190+ return { token: { type: 'TEXT', value: '{{{' + argName + '}}}' } };
 191+ }
141192 }
142193 };
143194
Index: trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt
@@ -405,7 +405,7 @@
406406 //b.push({type: 'NEWLINE'});
407407 // Move this to a token stream transform!
408408 //console.log('about to emit' + pp(self));
409 - console.log( pp( result._tokenizer ));
 409+ //console.log( pp( result._tokenizer ));
410410 //console.log('emitted chunk' + pp(b));
411411 //return [];
412412 return b;
@@ -738,6 +738,7 @@
739739 };
740740 if (params && params.length) {
741741 // HACK, not final.
 742+ obj.attribs.push(['data-defaultvalue', params[0][1]]);
742743 obj.attribs.push(['data-json-args', JSON.stringify(params)]);
743744 }
744745 return obj;

Status & tagging log