2011-11-02 21:07:51 +00:00
|
|
|
var MWParserEnvironment = function(opts) {
|
|
|
|
var options = {
|
|
|
|
tagHooks: {},
|
|
|
|
parserFunctions: {},
|
|
|
|
pageCache: {}, // @fixme use something with managed space
|
2012-01-18 19:38:32 +00:00
|
|
|
debug: false,
|
|
|
|
wgScriptPath: "http://en.wikipedia.org/w",
|
|
|
|
wgScriptExtension: ".php",
|
|
|
|
fetchTemplates: false
|
2011-11-02 21:07:51 +00:00
|
|
|
};
|
|
|
|
$.extend(options, opts);
|
2012-01-18 19:38:32 +00:00
|
|
|
$.extend(this, options);
|
2011-11-02 21:07:51 +00:00
|
|
|
};
|
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
MWParserEnvironment.prototype.lookupKV = function ( kvs, key ) {
|
2012-01-18 01:42:56 +00:00
|
|
|
if ( ! kvs ) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-01-10 01:09:50 +00:00
|
|
|
var kv;
|
|
|
|
for ( var i = 0, l = kvs.length; i < l; i++ ) {
|
|
|
|
kv = kvs[i];
|
|
|
|
if ( kv[0] === key ) {
|
|
|
|
// found, return it.
|
2012-01-18 01:42:56 +00:00
|
|
|
return kv;
|
2011-11-02 21:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-10 01:09:50 +00:00
|
|
|
// nothing found!
|
|
|
|
return null;
|
|
|
|
};
|
2011-11-02 21:07:51 +00:00
|
|
|
|
2012-01-17 22:29:26 +00:00
|
|
|
MWParserEnvironment.prototype.KVtoHash = function ( kvs ) {
|
|
|
|
var res = {};
|
|
|
|
for ( var i = 0, l = kvs.length; i < l; i++ ) {
|
|
|
|
var kv = kvs[i],
|
2012-01-17 23:18:33 +00:00
|
|
|
key = this.tokensToString( kv[0] ).trim();
|
2012-01-17 22:29:26 +00:00
|
|
|
if( res[key] === undefined ) {
|
|
|
|
res[key] = kv[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//console.log( 'KVtoHash: ' + JSON.stringify( res ));
|
|
|
|
return res;
|
|
|
|
}
|
2011-11-02 21:07:51 +00:00
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
// Does this need separate UI/content inputs?
|
|
|
|
MWParserEnvironment.prototype.formatNum = function( num ) {
|
|
|
|
return num + '';
|
|
|
|
};
|
2011-11-02 21:07:51 +00:00
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
MWParserEnvironment.prototype.getVariable = function( varname, options ) {
|
2011-11-02 21:07:51 +00:00
|
|
|
//
|
2012-01-10 01:09:50 +00:00
|
|
|
};
|
2011-11-02 21:07:51 +00:00
|
|
|
|
|
|
|
/**
|
2012-01-10 01:09:50 +00:00
|
|
|
* @return MWParserFunction
|
2011-11-02 21:07:51 +00:00
|
|
|
*/
|
2012-01-10 01:09:50 +00:00
|
|
|
MWParserEnvironment.prototype.getParserFunction = function( name ) {
|
|
|
|
if (name in this.parserFunctions) {
|
|
|
|
return new this.parserFunctions[name]( this );
|
|
|
|
} else {
|
|
|
|
return null;
|
2011-11-02 21:07:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-01-10 01:09:50 +00:00
|
|
|
* @return MWParserTagHook
|
2011-11-02 21:07:51 +00:00
|
|
|
*/
|
2012-01-10 01:09:50 +00:00
|
|
|
MWParserEnvironment.prototype.getTagHook = function( name ) {
|
|
|
|
if (name in this.tagHooks) {
|
|
|
|
return new this.tagHooks[name](this);
|
|
|
|
} else {
|
|
|
|
return null;
|
2011-11-02 21:07:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-01-14 00:58:20 +00:00
|
|
|
MWParserEnvironment.prototype.normalizeTitle = function( name ) {
|
|
|
|
if (typeof name !== 'string') {
|
|
|
|
throw new Error('nooooooooo not a string');
|
|
|
|
}
|
2012-01-17 23:18:33 +00:00
|
|
|
name = name.trim().replace(/[\s_]+/g, '_');
|
2012-01-14 00:58:20 +00:00
|
|
|
function upperFirst( s ) { return s.substr(0, 1).toUpperCase() + s.substr(1); }
|
|
|
|
name = name.split(':').map( upperFirst ).join(':');
|
|
|
|
//if (name === '') {
|
|
|
|
// throw new Error('Invalid/empty title');
|
|
|
|
//}
|
|
|
|
return name;
|
|
|
|
};
|
|
|
|
|
2011-11-02 21:07:51 +00:00
|
|
|
/**
|
2012-01-10 01:09:50 +00:00
|
|
|
* @fixme do this for real eh
|
2011-11-02 21:07:51 +00:00
|
|
|
*/
|
2012-01-10 01:09:50 +00:00
|
|
|
MWParserEnvironment.prototype.resolveTitle = function( name, namespace ) {
|
|
|
|
// hack!
|
|
|
|
if (name.indexOf(':') == -1 && typeof namespace ) {
|
|
|
|
// hack hack hack
|
2012-01-14 00:58:20 +00:00
|
|
|
name = namespace + ':' + this.normalizeTitle( name );
|
2012-01-10 01:09:50 +00:00
|
|
|
}
|
|
|
|
return name;
|
2011-11-02 21:07:51 +00:00
|
|
|
};
|
|
|
|
|
2012-01-11 19:48:49 +00:00
|
|
|
MWParserEnvironment.prototype.tokensToString = function ( tokens ) {
|
|
|
|
var out = [];
|
2012-01-14 00:58:20 +00:00
|
|
|
//console.log( 'MWParserEnvironment.tokensToString, tokens: ' + JSON.stringify( tokens ) );
|
2012-01-11 19:48:49 +00:00
|
|
|
// XXX: quick hack, track down non-array sources later!
|
|
|
|
if ( ! $.isArray( tokens ) ) {
|
|
|
|
tokens = [ tokens ];
|
|
|
|
}
|
|
|
|
for ( var i = 0, l = tokens.length; i < l; i++ ) {
|
|
|
|
var token = tokens[i];
|
2012-01-14 00:58:20 +00:00
|
|
|
//console.log( 'MWParserEnvironment.tokensToString, token: ' + JSON.stringify( token ) );
|
2012-01-11 19:48:49 +00:00
|
|
|
if ( token.type === 'TEXT' ) {
|
|
|
|
out.push( token.value );
|
|
|
|
} else {
|
|
|
|
var tstring = JSON.stringify( token );
|
2012-01-14 00:58:20 +00:00
|
|
|
//console.log ( 'MWParserEnvironment.tokensToString, non-text token: ' + tstring );
|
|
|
|
//out.push( tstring );
|
2012-01-11 19:48:49 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-14 00:58:20 +00:00
|
|
|
//console.log( 'MWParserEnvironment.tokensToString result: ' + out.join('') );
|
2012-01-11 19:48:49 +00:00
|
|
|
return out.join('');
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-01-10 01:09:50 +00:00
|
|
|
|
2011-11-02 21:07:51 +00:00
|
|
|
if (typeof module == "object") {
|
|
|
|
module.exports.MWParserEnvironment = MWParserEnvironment;
|
|
|
|
}
|