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

Revision: 112593
Author:   gwicke
Date:     2012-02-28 13:24:35 +0000 (Tue, 28 Feb 2012)
Log Message:
-----------
Actually commit onlyinclude, as already announced in r112592.

Modified Paths:
--------------
    trunk/extensions/VisualEditor/modules/parser/ext.core.NoIncludeOnly.js
    trunk/extensions/VisualEditor/modules/parser/ext.util.TokenCollector.js
    
trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformManager.js
    trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.js
    trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt

Modified: trunk/extensions/VisualEditor/modules/parser/ext.core.NoIncludeOnly.js
===================================================================
--- trunk/extensions/VisualEditor/modules/parser/ext.core.NoIncludeOnly.js      
2012-02-28 13:21:01 UTC (rev 112592)
+++ trunk/extensions/VisualEditor/modules/parser/ext.core.NoIncludeOnly.js      
2012-02-28 13:24:35 UTC (rev 112593)
@@ -7,7 +7,81 @@
 
 var TokenCollector = require( './ext.util.TokenCollector.js' ).TokenCollector;
 
+/**
+ * OnlyInclude sadly forces synchronous template processing, as it needs to
+ * hold onto all tokens in case an onlyinclude block is encountered later.
+ */
+function OnlyInclude( manager, isInclude ) {
+       this.manager = manager;
+       if ( isInclude ) {
+               this.accum = [];
+               this.inOnlyInclude = false;
+               this.foundOnlyInclude = false;
+               // register for 'any' token, collect those
+               this.manager.addTransform( this.onAnyInclude.bind( this ), 
this.rank, 'any' );
+       } else {
+               // just convert onlyinclude tokens into meta tags with rt info
+               this.manager.addTransform( this.onOnlyInclude.bind( this ), 
this.rank, 
+                               'tag', 'onlyinclude' );
+       }
+}
 
+OnlyInclude.prototype.rank = 0.001;
+
+OnlyInclude.prototype.onOnlyInclude = function ( token, manager ) {
+       var meta = new TagTk( 'meta' );
+       meta.dataAttribs = { strippedTokens: [token] };
+       return { token: meta };
+};
+
+OnlyInclude.prototype.onAnyInclude = function ( token, manager ) {
+       //this.manager.env.dp( 'onAnyInclude', token, this );
+       if ( token.type === 'END' ) {
+               this.inOnlyInclude = false;
+               if ( this.accum.length && ! this.foundOnlyInclude ) {
+                       var res = this.accum;
+                       res.push( token );
+                       this.accum = [];
+                       this.manager.setTokensRank( res, this.rank + 0.001 );
+                       return { tokens: res };
+               } else {
+                       this.foundOnlyInclude = false;
+                       this.accum = [];
+                       return { token: token };
+               }
+       } else if ( ( token.constructor === TagTk ||
+                               token.constructor === EndTagTk ||
+                               token.constructor === SelfclosingTagTk ) &&
+                       token.name === 'onlyinclude' ) {
+               var meta;
+               if ( ! this.inOnlyInclude ) {
+                       this.foundOnlyInclude = true;
+                       this.inOnlyInclude = true;
+                       // wrap collected tokens into meta tag for 
round-tripping
+                       meta = new TagTk( 'meta' );
+                       this.accum.push( token );
+                       meta.dataAttribs = { strippedTokens: this.accum };
+                       this.accum = [];
+                       return meta;
+               } else {
+                       this.inOnlyInclude = false;
+                       meta = new TagTk( 'meta' );
+                       meta.dataAttribs = { strippedTokens: [token] };
+               }
+               meta.rank = this.rank;
+               return { token: meta };
+       } else {
+               if ( this.inOnlyInclude ) {
+                       token.rank = this.rank;
+                       return { token: token };
+               } else {
+                       this.accum.push( token );
+                       return { };
+               }
+       }
+};
+
+
 function NoInclude( manager, isInclude ) {
        new TokenCollector( 
                        manager,
@@ -60,4 +134,5 @@
 if (typeof module == "object") {
        module.exports.NoInclude = NoInclude;
        module.exports.IncludeOnly = IncludeOnly;
+       module.exports.OnlyInclude = OnlyInclude;
 }

Modified: 
trunk/extensions/VisualEditor/modules/parser/ext.util.TokenCollector.js
===================================================================
--- trunk/extensions/VisualEditor/modules/parser/ext.util.TokenCollector.js     
2012-02-28 13:21:01 UTC (rev 112592)
+++ trunk/extensions/VisualEditor/modules/parser/ext.util.TokenCollector.js     
2012-02-28 13:24:35 UTC (rev 112593)
@@ -9,6 +9,10 @@
  *
  * Calls the passed-in callback with the collected tokens.
  *
+ * XXX: optionally support nested delimiters using a stack?
+ */
+
+/**
  * @class
  * @constructor
  * @param {Object} SyncTokenTransformManager to register with

Modified: 
trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformManager.js
===================================================================
--- 
trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformManager.js 
    2012-02-28 13:21:01 UTC (rev 112592)
+++ 
trunk/extensions/VisualEditor/modules/parser/mediawiki.TokenTransformManager.js 
    2012-02-28 13:24:35 UTC (rev 112593)
@@ -162,6 +162,18 @@
        }
 };
 
+TokenTransformManager.prototype.setTokensRank = function ( tokens, rank ) {
+       for ( var i = 0, l = tokens.length; i < l; i++ ) {
+               var token = tokens[i];
+               // convert string literal to string object
+               if ( token.constructor === String && token.rank === undefined ) 
{
+                       tokens[i] = new String( token );
+                       token = tokens[i];
+               }
+               token.rank = rank;
+       }
+};
+
 /**
  * Comparison for sorting transformations by ascending rank.
  */
@@ -251,6 +263,7 @@
  */
 TokenTransformManager.prototype._transformToken = function ( token, 
phaseEndRank, ts, cbOrPrevToken ) {
        // prepend 'any' transformers
+       //this.env.dp('_transformToken', token);
        var anyTrans = this.transformers.any;
        if ( anyTrans.length ) {
                ts = this.transformers.any.concat(ts);
@@ -264,6 +277,7 @@
                        transformer = ts[i];
                        if ( res.token.rank && transformer.rank <= 
res.token.rank ) {
                                // skip transformation, was already applied.
+                               //console.warn( 'skipping transform');
                                continue;
                        }
                        // Transform the token.
@@ -291,6 +305,9 @@
                        }
                        res.token.rank = phaseEndRank; // need phase passed in!
                }
+               //else {
+               //      this.env.dp( '_transformToken aborted', res );
+               //}
 
        }
        return res;

Modified: trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.js
===================================================================
--- trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.js    
2012-02-28 13:21:01 UTC (rev 112592)
+++ trunk/extensions/VisualEditor/modules/parser/mediawiki.parser.js    
2012-02-28 13:24:35 UTC (rev 112593)
@@ -19,6 +19,7 @@
        NoIncludeOnly                           = 
require('./ext.core.NoIncludeOnly.js'),
        IncludeOnly                                     = 
NoIncludeOnly.IncludeOnly,
        NoInclude                                       = 
NoIncludeOnly.NoInclude,
+       OnlyInclude                                     = 
NoIncludeOnly.OnlyInclude,
        QuoteTransformer            = 
require('./ext.core.QuoteTransformer.js').QuoteTransformer,
        PostExpandParagraphHandler  = 
require('./ext.core.PostExpandParagraphHandler.js')
                                                                                
                                                .PostExpandParagraphHandler,
@@ -144,6 +145,7 @@
                // Synchronous in-order per input
                sync01: 
                        [ 
+                               OnlyInclude,
                                IncludeOnly, 
                                NoInclude
                                // Insert TokenCollectors for extensions here 
(don't expand

Modified: trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt
===================================================================
--- trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt 
2012-02-28 13:21:01 UTC (rev 112592)
+++ trunk/extensions/VisualEditor/modules/parser/pegTokenizer.pegjs.txt 
2012-02-28 13:24:35 UTC (rev 112593)
@@ -1523,6 +1523,7 @@
 // plain-text content.
 directive
   = comment
+  / nowiki
   / tplarg_or_template
   / htmlentity 
 


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to