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.manager = manager;
|
2012-04-25 14:35:59 +00:00
|
|
|
// XXX: only register for tag tokens?
|
2012-02-09 13:44:20 +00:00
|
|
|
manager.addTransform( this.onToken.bind(this),
|
|
|
|
this.rank, 'any' );
|
2012-04-12 13:42:09 +00:00
|
|
|
}
|
2012-02-09 13:44:20 +00:00
|
|
|
|
2012-04-12 13:42:09 +00:00
|
|
|
// constants
|
|
|
|
AttributeExpander.prototype.rank = 1.11;
|
2012-02-09 13:44:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-04-12 13:42:09 +00:00
|
|
|
//console.warn( 'AttributeExpander.onToken', JSON.stringify( token ) );
|
|
|
|
if ( (token.constructor === TagTk ||
|
|
|
|
token.constructor === SelfclosingTagTk) &&
|
2012-03-06 18:02:35 +00:00
|
|
|
token.attribs &&
|
|
|
|
token.attribs.length ) {
|
2012-05-10 08:04:24 +00:00
|
|
|
// clone the token
|
2012-04-18 15:53:04 +00:00
|
|
|
token = $.extend( {}, token );
|
|
|
|
token.attribs = token.attribs.slice();
|
2012-02-09 13:44:20 +00:00
|
|
|
var atm = new AttributeTransformManager(
|
|
|
|
this.manager,
|
2012-04-25 14:35:59 +00:00
|
|
|
this._returnAttributes.bind( this, token, cb )
|
2012-02-09 13:44:20 +00:00
|
|
|
);
|
2012-04-25 14:35:59 +00:00
|
|
|
cb( { async: true } );
|
|
|
|
atm.process( token.attribs );
|
2012-02-09 13:44:20 +00:00
|
|
|
} else {
|
2012-05-10 08:04:24 +00:00
|
|
|
cb ( { tokens: [token] } );
|
2012-02-09 13:44:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-04-18 15:53:04 +00:00
|
|
|
* Callback for attribute expansion in AttributeTransformManager
|
2012-02-09 13:44:20 +00:00
|
|
|
*/
|
2012-04-25 14:35:59 +00:00
|
|
|
AttributeExpander.prototype._returnAttributes = function ( token, cb,
|
2012-02-09 13:44:20 +00:00
|
|
|
attributes )
|
|
|
|
{
|
2012-02-21 18:49:43 +00:00
|
|
|
this.manager.env.dp( 'AttributeExpander._returnAttributes: ',attributes );
|
2012-04-25 14:35:59 +00:00
|
|
|
token.attribs = attributes;
|
2012-05-10 08:04:24 +00:00
|
|
|
cb( { tokens: [token] } );
|
2012-02-09 13:44:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof module == "object") {
|
|
|
|
module.exports.AttributeExpander = AttributeExpander;
|
|
|
|
}
|