2012-02-09 13:44:20 +00:00
|
|
|
/**
|
2012-02-09 22:27:45 +00:00
|
|
|
* Generic attribute expansion handler.
|
2012-02-09 13:44:20 +00:00
|
|
|
*
|
|
|
|
* @author Gabriel Wicke <gwicke@wikimedia.org>
|
|
|
|
*/
|
|
|
|
var $ = require('jquery'),
|
|
|
|
request = require('request'),
|
|
|
|
events = require('events'),
|
|
|
|
qs = require('querystring'),
|
|
|
|
ParserFunctions = require('./ext.core.ParserFunctions.js').ParserFunctions,
|
|
|
|
AttributeTransformManager = require('./mediawiki.TokenTransformManager.js')
|
|
|
|
.AttributeTransformManager,
|
|
|
|
defines = require('./mediawiki.parser.defines.js');
|
|
|
|
|
|
|
|
|
|
|
|
function AttributeExpander ( manager ) {
|
|
|
|
this.reset();
|
|
|
|
this.register( manager );
|
|
|
|
}
|
|
|
|
|
|
|
|
AttributeExpander.prototype.reset = function ( token ) {
|
|
|
|
return {token: token};
|
|
|
|
};
|
|
|
|
|
|
|
|
// constants
|
|
|
|
AttributeExpander.prototype.rank = 1.11;
|
|
|
|
|
|
|
|
AttributeExpander.prototype.register = function ( manager ) {
|
|
|
|
this.manager = manager;
|
|
|
|
// Register for template and templatearg tag tokens
|
|
|
|
manager.addTransform( this.onToken.bind(this),
|
|
|
|
this.rank, 'any' );
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Token handler
|
|
|
|
*
|
|
|
|
* Expands target and arguments (both keys and values) and either directly
|
|
|
|
* calls or sets up the callback to _expandTemplate, which then fetches and
|
|
|
|
* processes the template.
|
|
|
|
*/
|
|
|
|
AttributeExpander.prototype.onToken = function ( token, frame, cb ) {
|
2012-02-09 16:36:29 +00:00
|
|
|
if ( token.constructor === TagTk && token.attribs && token.attribs.length ) {
|
2012-02-09 13:44:20 +00:00
|
|
|
var expandData = {
|
|
|
|
token: token,
|
|
|
|
cb: cb
|
|
|
|
};
|
|
|
|
var atm = new AttributeTransformManager(
|
|
|
|
this.manager,
|
|
|
|
this._returnAttributes.bind( this, expandData )
|
|
|
|
);
|
|
|
|
if( atm.process( token.attribs ) ) {
|
|
|
|
// Attributes were transformed synchronously
|
|
|
|
this.manager.env.dp (
|
2012-02-21 18:49:43 +00:00
|
|
|
'sync attribs for ', token
|
2012-02-09 13:44:20 +00:00
|
|
|
);
|
|
|
|
// All attributes are fully expanded synchronously (no IO was needed)
|
|
|
|
return { token: token };
|
|
|
|
} else {
|
|
|
|
// Async attribute expansion is going on
|
2012-02-21 18:49:43 +00:00
|
|
|
this.manager.env.dp( 'async return for ', token );
|
2012-02-09 13:44:20 +00:00
|
|
|
expandData.async = true;
|
|
|
|
return { async: true };
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return { token: token };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for argument (including target) expansion in AttributeTransformManager
|
|
|
|
*/
|
|
|
|
AttributeExpander.prototype._returnAttributes = function ( expandData,
|
|
|
|
attributes )
|
|
|
|
{
|
2012-02-21 18:49:43 +00:00
|
|
|
this.manager.env.dp( 'AttributeExpander._returnAttributes: ',attributes );
|
2012-02-09 13:44:20 +00:00
|
|
|
// Remove the target from the attributes
|
|
|
|
expandData.token.attribs = attributes;
|
|
|
|
if ( expandData.async ) {
|
|
|
|
expandData.cb( [expandData.token], false );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof module == "object") {
|
|
|
|
module.exports.AttributeExpander = AttributeExpander;
|
|
|
|
}
|