2012-01-20 01:46:16 +00:00
|
|
|
/**
|
|
|
|
* Simple noinclude / onlyinclude implementation. Strips all tokens in
|
|
|
|
* noinclude sections.
|
|
|
|
*
|
|
|
|
* @author Gabriel Wicke <gwicke@wikimedia.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
var TokenCollector = require( './ext.util.TokenCollector.js' ).TokenCollector;
|
|
|
|
|
2012-02-28 13:24:35 +00:00
|
|
|
/**
|
|
|
|
* 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' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-12 13:42:09 +00:00
|
|
|
OnlyInclude.prototype.rank = 0.01; // Before any further processing
|
2012-02-28 13:24:35 +00:00
|
|
|
|
|
|
|
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 );
|
2012-03-07 20:06:54 +00:00
|
|
|
if ( token.constructor === EOFTk ) {
|
2012-02-28 13:24:35 +00:00
|
|
|
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 { };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-11 16:43:25 +00:00
|
|
|
|
2012-01-20 22:02:23 +00:00
|
|
|
function NoInclude( manager, isInclude ) {
|
2012-01-20 01:46:16 +00:00
|
|
|
new TokenCollector(
|
|
|
|
manager,
|
|
|
|
function ( tokens ) {
|
2012-01-20 22:02:23 +00:00
|
|
|
if ( isInclude ) {
|
2012-01-31 16:50:16 +00:00
|
|
|
manager.env.tp( 'noinclude stripping' );
|
2012-01-20 22:02:23 +00:00
|
|
|
return {};
|
|
|
|
} else {
|
|
|
|
tokens.shift();
|
|
|
|
if ( tokens.length &&
|
2012-03-07 20:06:54 +00:00
|
|
|
tokens[tokens.length - 1].constructor !== EOFTk ) {
|
2012-01-20 22:02:23 +00:00
|
|
|
tokens.pop();
|
|
|
|
}
|
|
|
|
return { tokens: tokens };
|
|
|
|
}
|
2012-01-20 01:46:16 +00:00
|
|
|
}, // just strip it all..
|
|
|
|
true, // match the end-of-input if </noinclude> is missing
|
2012-04-12 13:42:09 +00:00
|
|
|
0.02, // very early in stage 1, to avoid any further processing.
|
2012-01-20 01:46:16 +00:00
|
|
|
'tag',
|
|
|
|
'noinclude'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-02-11 16:43:25 +00:00
|
|
|
// XXX: Preserve includeonly content in meta tag (data attribute) for
|
|
|
|
// round-tripping!
|
2012-01-20 22:02:23 +00:00
|
|
|
function IncludeOnly( manager, isInclude ) {
|
2012-01-20 01:46:16 +00:00
|
|
|
new TokenCollector(
|
|
|
|
manager,
|
2012-01-20 22:02:23 +00:00
|
|
|
function ( tokens ) {
|
|
|
|
if ( isInclude ) {
|
|
|
|
tokens.shift();
|
|
|
|
if ( tokens.length &&
|
2012-03-07 20:06:54 +00:00
|
|
|
tokens[tokens.length - 1].constructor !== EOFTk ) {
|
2012-01-20 22:02:23 +00:00
|
|
|
tokens.pop();
|
|
|
|
}
|
|
|
|
return { tokens: tokens };
|
|
|
|
} else {
|
2012-01-31 16:50:16 +00:00
|
|
|
manager.env.tp( 'includeonly stripping' );
|
2012-01-20 22:02:23 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
},
|
2012-01-20 01:46:16 +00:00
|
|
|
true, // match the end-of-input if </noinclude> is missing
|
2012-04-12 13:42:09 +00:00
|
|
|
0.03, // very early in stage 1, to avoid any further processing.
|
2012-01-20 01:46:16 +00:00
|
|
|
'tag',
|
2012-01-20 22:02:23 +00:00
|
|
|
'includeonly'
|
2012-01-20 01:46:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof module == "object") {
|
|
|
|
module.exports.NoInclude = NoInclude;
|
2012-01-20 22:02:23 +00:00
|
|
|
module.exports.IncludeOnly = IncludeOnly;
|
2012-02-28 13:24:35 +00:00
|
|
|
module.exports.OnlyInclude = OnlyInclude;
|
2012-01-20 01:46:16 +00:00
|
|
|
}
|