https://www.mediawiki.org/wiki/Special:Code/MediaWiki/108255

Revision: 108255
Author:   gwicke
Date:     2012-01-06 17:19:14 +0000 (Fri, 06 Jan 2012)
Log Message:
-----------
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
    trunk/extensions/VisualEditor/modules/parser/ext.core.TemplateHandler.js
    trunk/extensions/VisualEditor/modules/parser/mediawiki.tokenizer.peg.js
    trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt

Modified: 
trunk/extensions/VisualEditor/modules/parser/ext.core.QuoteTransformer.js
===================================================================
--- trunk/extensions/VisualEditor/modules/parser/ext.core.QuoteTransformer.js   
2012-01-06 17:03:51 UTC (rev 108254)
+++ trunk/extensions/VisualEditor/modules/parser/ext.core.QuoteTransformer.js   
2012-01-06 17:19:14 UTC (rev 108255)
@@ -5,11 +5,13 @@
  */
 
 function QuoteTransformer ( ) {
-       this.quoteAndNewlineRank = 2.1;
-       this.anyRank = 2.101; // Just after regular quote and newline
        this.reset();
 }
 
+// constants
+QuoteTransformer.prototype.quoteAndNewlineRank = 2.1;
+QuoteTransformer.prototype.anyRank = 2.101; // Just after regular quote and 
newline
+
 QuoteTransformer.prototype.reset = function ( ) {
        // A chunk starts with a token context around a quote token and is
        // (optionally) followed by non-quote tokens. The quote token and its

Modified: 
trunk/extensions/VisualEditor/modules/parser/ext.core.TemplateHandler.js
===================================================================
--- trunk/extensions/VisualEditor/modules/parser/ext.core.TemplateHandler.js    
2012-01-06 17:03:51 UTC (rev 108254)
+++ trunk/extensions/VisualEditor/modules/parser/ext.core.TemplateHandler.js    
2012-01-06 17:19:14 UTC (rev 108255)
@@ -2,8 +2,11 @@
  * Template and template argument handling.
  *
  * @author Gabriel Wicke <[email protected]>
+ * @author Brion Vibber <[email protected]>
  */
+var $ = require('jquery');
 
+
 function TemplateHandler () {
        this.reset();
 }
@@ -129,14 +132,62 @@
 };
 
 
+/**
+ * Fetch a template
+ */
+TemplateHandler.prototype._fetchTemplateAndTitle = function( title, frame, 
callback ) {
+       // @fixme normalize name?
+       if (title in this.pageCache) {
+               // @fixme should this be forced to run on next event?
+               callback( this.pageCache[title], title );
+       } else {
+               // whee fun hack!
+               console.log(title);
+               console.log(this.pageCache);
+               $.ajax({
+                       url: frame.env.wgScriptPath + '/api' + 
frame.env.wgScriptExtension,
+                       data: {
+                               format: 'json',
+                       action: 'query',
+                       prop: 'revisions',
+                       rvprop: 'content',
+                       titles: name
+                       },
+                       success: function(data, xhr) {
+                               var src = null, title = null;
+                               $.each(data.query.pages, function(i, page) {
+                                       if (page.revisions && 
page.revisions.length) {
+                                               src = page.revisions[0]['*'];
+                                               title = page.title;
+                                       }
+                               });
+                               if (typeof src !== 'string') {
+                                       callback(null, null, 'Page not found');
+                               } else {
+                                       callback(src, title);
+                               }
+                       },
+                       error: function(msg) {
+                               callback(null, null, 'Page/template fetch 
failure');
+                       },
+                       dataType: 'json',
+                       cache: false // @fixme caching, versions etc?
+               }, 'json');
+       }
+};
+
+
 TemplateHandler.prototype.onTemplateArg = function ( token, cb, frame ) {
        var argName = token.attribs[0][1]; // XXX: do this properly!
        if ( argName in frame.args ) {
                // return tokens for argument
                return { tokens: frame.args[argName] };
        } else {
-               // FIXME: support default value!
-               return { token: { type: 'TEXT', value: '' } };
+               if ( token.attribs.length > 1 ) {
+                       return token.attribs[1][1]; // default value, XXX: use 
key
+               } else {
+                       return { token: { type: 'TEXT', value: '{{{' + argName 
+ '}}}' } };
+               }
        }
 };
 

Modified: 
trunk/extensions/VisualEditor/modules/parser/mediawiki.tokenizer.peg.js
===================================================================
--- trunk/extensions/VisualEditor/modules/parser/mediawiki.tokenizer.peg.js     
2012-01-06 17:03:51 UTC (rev 108254)
+++ trunk/extensions/VisualEditor/modules/parser/mediawiki.tokenizer.peg.js     
2012-01-06 17:19:14 UTC (rev 108255)
@@ -29,6 +29,8 @@
                this.parser = PEG.buildParser(this.src);
                // add reference to this for event emission
                this.parser._tokenizer = this;
+               // Print the generated parser source
+               //console.log(this.parser.toSource());
        }
 
        // some normalization

Modified: trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt
===================================================================
--- trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt 
2012-01-06 17:03:51 UTC (rev 108254)
+++ trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt 
2012-01-06 17:19:14 UTC (rev 108255)
@@ -405,7 +405,7 @@
     //b.push({type: 'NEWLINE'});
        // Move this to a token stream transform!
     //console.log('about to emit' + pp(self));
-    console.log( pp( result._tokenizer ));
+    //console.log( pp( result._tokenizer ));
     //console.log('emitted chunk' + pp(b));
     //return [];
     return b;
@@ -738,6 +738,7 @@
       };
       if (params && params.length) {
           // HACK, not final.
+          obj.attribs.push(['data-defaultvalue', params[0][1]]);
           obj.attribs.push(['data-json-args', JSON.stringify(params)]);
       }
       return obj;


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to