mediawiki-extensions-Visual.../modules/parser/ext.core.NoIncludeOnly.js
Gabriel Wicke c688b039de Collected tweaks
* less verbose logging in noinclude processing and template expansion
* Give priority to the processing of templates transcluded from transclusions
  to get closer to depth-first processing. This serves to minimize memory
  usage from queued-up tokens.
* Increase the maximum outstanding requests per template retrieval. 10000
  amazingly proved too low a limit on some big pages.
* Only process a single template request callback at a time for now
* Add a debug print in the treebuilder wrapper
* Don't treat multiple comments on a single line as a single comment to match
  the PHP parser's behavior

Change-Id: I9a86b6d7bec3b9e1f17415daf1bf74170240721a
2012-04-16 15:47:03 +02:00

141 lines
3.8 KiB
JavaScript

/**
* Simple noinclude / onlyinclude implementation. Strips all tokens in
* noinclude sections.
*
* @author Gabriel Wicke <gwicke@wikimedia.org>
*/
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.
* This can fortunately be worked around by caching the tokens after
* onlyinclude processing (which is a good idea anyway).
*/
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.01; // Before any further processing
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.constructor === EOFTk ) {
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,
function ( tokens ) {
if ( isInclude ) {
//manager.env.tp( 'noinclude stripping' );
return {};
} else {
tokens.shift();
if ( tokens.length &&
tokens[tokens.length - 1].constructor !== EOFTk ) {
tokens.pop();
}
return { tokens: tokens };
}
}, // just strip it all..
true, // match the end-of-input if </noinclude> is missing
0.02, // very early in stage 1, to avoid any further processing.
'tag',
'noinclude'
);
}
// XXX: Preserve includeonly content in meta tag (data attribute) for
// round-tripping!
function IncludeOnly( manager, isInclude ) {
new TokenCollector(
manager,
function ( tokens ) {
if ( isInclude ) {
tokens.shift();
if ( tokens.length &&
tokens[tokens.length - 1].constructor !== EOFTk ) {
tokens.pop();
}
return { tokens: tokens };
} else {
manager.env.tp( 'includeonly stripping' );
return {};
}
},
true, // match the end-of-input if </noinclude> is missing
0.03, // very early in stage 1, to avoid any further processing.
'tag',
'includeonly'
);
}
if (typeof module == "object") {
module.exports.NoInclude = NoInclude;
module.exports.IncludeOnly = IncludeOnly;
module.exports.OnlyInclude = OnlyInclude;
}