2012-02-01 17:03:08 +00:00
|
|
|
/**
|
|
|
|
* Constructors for different token types. Plain text is represented as simple
|
|
|
|
* strings or String objects (if attributes are needed).
|
|
|
|
*/
|
2012-02-13 17:02:23 +00:00
|
|
|
|
2012-07-18 20:23:12 +00:00
|
|
|
var async = require('async');
|
2012-07-24 18:07:53 +00:00
|
|
|
var Util = require('./mediawiki.Util.js').Util;
|
2012-05-10 08:04:24 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
/* -------------------- TagTk -------------------- */
|
2012-03-13 12:32:31 +00:00
|
|
|
function TagTk( name, attribs, dataAttribs ) {
|
2012-02-01 17:03:08 +00:00
|
|
|
this.name = name;
|
|
|
|
this.attribs = attribs || [];
|
2012-03-13 12:32:31 +00:00
|
|
|
this.dataAttribs = dataAttribs || {};
|
2012-02-01 17:03:08 +00:00
|
|
|
}
|
2012-07-11 15:18:16 +00:00
|
|
|
|
2012-07-25 00:38:45 +00:00
|
|
|
/**
|
|
|
|
* Generic token attribute accessors
|
|
|
|
*/
|
|
|
|
var genericTokenMethods = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic set attribute method. Expects the context to be set to a token.
|
|
|
|
*/
|
|
|
|
addAttribute: function ( attributeName, value ) {
|
|
|
|
this.attribs.push( new KV( attributeName, value ) );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic set attribute method with support for change detection. Expects the
|
|
|
|
* context to be set to a token.
|
|
|
|
*/
|
|
|
|
addNormalizedAttribute: function ( attributeName, normalizedValue, origValue ) {
|
|
|
|
this.addAttribute( attributeName, normalizedValue );
|
|
|
|
if ( ! this.dataAttribs.a ) {
|
|
|
|
this.dataAttribs.a = {};
|
|
|
|
}
|
|
|
|
this.dataAttribs.a[attributeName] = normalizedValue;
|
|
|
|
if ( origValue !== undefined ) {
|
|
|
|
if ( ! this.dataAttribs.sa ) {
|
|
|
|
this.dataAttribs.sa = {};
|
|
|
|
}
|
|
|
|
this.dataAttribs.sa[attributeName] = origValue;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic attribute accessor. Expects the context to be set to a token.
|
|
|
|
*/
|
|
|
|
getAttribute: function ( attributeName ) {
|
|
|
|
return Util.lookup( this.attribs, attributeName );
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attribute accessor for the wikitext serializer. Performs change
|
|
|
|
* detection and uses unnormalized attribute values if set. Expects the
|
|
|
|
* context to be set to a token.
|
|
|
|
*/
|
|
|
|
getAttributeSource: function ( attributeName ) {
|
|
|
|
var curVal = Util.lookup( this.attribs, attributeName );
|
|
|
|
if ( ! this.dataAttribs.a ||
|
|
|
|
this.dataAttribs.a[attributeName] !== curVal ||
|
|
|
|
this.dataAttribs.sa[attributeName] === undefined ) {
|
|
|
|
return curVal;
|
|
|
|
} else {
|
|
|
|
return this.dataAttribs.sa[attributeName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
TagTk.prototype = {};
|
2012-07-11 15:18:16 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
TagTk.prototype.constructor = TagTk;
|
2012-07-11 15:18:16 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
TagTk.prototype.toJSON = function () {
|
|
|
|
return $.extend( { type: 'TagTk' }, this );
|
|
|
|
};
|
2012-07-11 15:18:16 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
TagTk.prototype.defaultToString = function(t) {
|
|
|
|
return "<" + this.name + ">";
|
|
|
|
};
|
|
|
|
|
|
|
|
TagTk.prototype.tagToStringFns = {
|
|
|
|
"listItem": function() {
|
|
|
|
return "<li:" + this.bullets.join('') + ">";
|
|
|
|
},
|
|
|
|
"mw-quote": function() {
|
|
|
|
return "<mw-quote:" + this.value + ">";
|
|
|
|
},
|
|
|
|
"urllink": function() {
|
|
|
|
return "<urllink:" + this.attribs[0].v + ">";
|
2012-07-11 15:18:16 +00:00
|
|
|
},
|
2012-07-24 17:50:19 +00:00
|
|
|
"behavior-switch": function() {
|
|
|
|
return "<behavior-switch:" + this.attribs[0].v + ">";
|
|
|
|
}
|
|
|
|
};
|
2012-07-11 15:18:16 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
TagTk.prototype.toString = function() {
|
|
|
|
if (this.dataAttribs.stx && this.dataAttribs.stx === "html") {
|
|
|
|
return "<HTML:" + this.name + ">";
|
|
|
|
} else {
|
|
|
|
var f = this.tagToStringFns[this.name];
|
|
|
|
return f ? f.bind(this)() : this.defaultToString();
|
2012-07-11 15:18:16 +00:00
|
|
|
}
|
2012-02-03 13:09:01 +00:00
|
|
|
};
|
2012-07-25 00:38:45 +00:00
|
|
|
// add in generic token methods
|
|
|
|
$.extend( TagTk.prototype, genericTokenMethods );
|
2012-02-03 13:09:01 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
/* -------------------- EndTagTk -------------------- */
|
2012-03-13 12:32:31 +00:00
|
|
|
function EndTagTk( name, attribs, dataAttribs ) {
|
2012-02-01 17:03:08 +00:00
|
|
|
this.name = name;
|
|
|
|
this.attribs = attribs || [];
|
2012-03-13 12:32:31 +00:00
|
|
|
this.dataAttribs = dataAttribs || {};
|
2012-02-01 17:03:08 +00:00
|
|
|
}
|
2012-07-11 15:18:16 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
EndTagTk.prototype = {};
|
2012-07-11 15:18:16 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
EndTagTk.prototype.constructor = EndTagTk;
|
2012-07-11 15:18:16 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
EndTagTk.prototype.toJSON = function () {
|
|
|
|
return $.extend( { type: 'EndTagTk' }, this );
|
|
|
|
};
|
|
|
|
|
|
|
|
EndTagTk.prototype.toString = function() {
|
|
|
|
if (this.dataAttribs.stx && this.dataAttribs.stx === "html") {
|
|
|
|
return "</HTML:" + this.name + ">";
|
|
|
|
} else {
|
|
|
|
return "</" + this.name + ">";
|
2012-07-11 15:18:16 +00:00
|
|
|
}
|
2012-02-03 13:09:01 +00:00
|
|
|
};
|
2012-07-25 00:38:45 +00:00
|
|
|
// add in generic token methods
|
|
|
|
$.extend( EndTagTk.prototype, genericTokenMethods );
|
2012-02-03 13:09:01 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
/* -------------------- SelfclosingTagTk -------------------- */
|
2012-03-13 12:32:31 +00:00
|
|
|
function SelfclosingTagTk( name, attribs, dataAttribs ) {
|
2012-02-01 17:03:08 +00:00
|
|
|
this.name = name;
|
|
|
|
this.attribs = attribs || [];
|
2012-03-13 12:32:31 +00:00
|
|
|
this.dataAttribs = dataAttribs || {};
|
2012-02-01 17:03:08 +00:00
|
|
|
}
|
2012-07-11 15:18:16 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
SelfclosingTagTk.prototype = {};
|
2012-07-11 15:18:16 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
SelfclosingTagTk.prototype.constructor = SelfclosingTagTk;
|
2012-07-11 15:18:16 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
SelfclosingTagTk.prototype.toJSON = function () {
|
|
|
|
return $.extend( { type: 'SelfclosingTagTk' }, this );
|
|
|
|
};
|
2012-07-20 23:09:09 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
SelfclosingTagTk.prototype.multiTokenArgToString = function(key, arg, indent, indentIncrement) {
|
|
|
|
var newIndent = indent + indentIncrement;
|
|
|
|
var present = true;
|
|
|
|
var toks = Util.toStringTokens(arg, newIndent);
|
|
|
|
var str = toks.join("\n" + newIndent);
|
2012-07-20 23:09:09 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
if (toks.length > 1 || str[0] === '<') {
|
|
|
|
str = [key, ":{\n", newIndent, str, "\n", indent, "}"].join('');
|
|
|
|
} else {
|
|
|
|
present = (str !== '');
|
|
|
|
}
|
|
|
|
|
|
|
|
return {present: present, str: str};
|
|
|
|
},
|
2012-07-20 23:09:09 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
SelfclosingTagTk.prototype.attrsToString = function(indent, indentIncrement, startAttrIndex) {
|
|
|
|
var buf = [];
|
|
|
|
for (var i = startAttrIndex, n = this.attribs.length; i < n; i++) {
|
|
|
|
var a = this.attribs[i];
|
|
|
|
var kVal = this.multiTokenArgToString("k", a.k, indent, indentIncrement);
|
|
|
|
var vVal = this.multiTokenArgToString("v", a.v, indent, indentIncrement);
|
|
|
|
|
|
|
|
if (kVal.present && vVal.present) {
|
|
|
|
buf.push([kVal.str, "=", vVal.str].join(''));
|
|
|
|
} else {
|
|
|
|
if (kVal.present) buf.push(kVal.str);
|
|
|
|
if (vVal.present) buf.push(vVal.str);
|
2012-07-13 19:08:10 +00:00
|
|
|
}
|
2012-07-24 17:50:19 +00:00
|
|
|
}
|
2012-07-13 19:08:10 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
return buf.join("\n" + indent + "|");
|
|
|
|
};
|
2012-07-20 23:09:09 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
SelfclosingTagTk.prototype.defaultToString = function(compact, indent) {
|
|
|
|
if (compact) {
|
|
|
|
var buf = "<" + this.name + ">:";
|
|
|
|
var attr0 = this.attribs[0];
|
|
|
|
return attr0 ? buf + Util.toStringTokens(attr0.k, "\n") : buf;
|
|
|
|
} else {
|
|
|
|
if (!indent) indent = "";
|
|
|
|
var origIndent = indent;
|
|
|
|
var indentIncrement = " ";
|
|
|
|
indent = indent + indentIncrement;
|
|
|
|
return ["<", this.name, ">(\n", indent, this.attrsToString(indent, indentIncrement, 0), "\n", origIndent, ")"].join('');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
SelfclosingTagTk.prototype.tagToStringFns = {
|
|
|
|
"extlink": function(compact, indent) {
|
|
|
|
var href = Util.kvTokensToString(Util.lookupKV(this.attribs, 'href').v);
|
2012-07-12 22:37:43 +00:00
|
|
|
if (compact) {
|
2012-07-24 17:50:19 +00:00
|
|
|
return ["<extlink:", href, ">"].join('');
|
2012-07-11 15:18:16 +00:00
|
|
|
} else {
|
2012-07-13 19:08:10 +00:00
|
|
|
if (!indent) indent = "";
|
|
|
|
var origIndent = indent;
|
2012-07-20 23:09:09 +00:00
|
|
|
var indentIncrement = " ";
|
2012-07-13 19:08:10 +00:00
|
|
|
indent = indent + indentIncrement;
|
2012-07-24 17:50:19 +00:00
|
|
|
var content = Util.lookupKV(this.attribs, 'content').v;
|
|
|
|
content = this.multiTokenArgToString("v", content, indent, indentIncrement).str;
|
|
|
|
return ["<extlink>(\n", indent,
|
|
|
|
"href=", href, "\n", indent,
|
|
|
|
"content=", content, "\n", origIndent,
|
|
|
|
")"].join('');
|
2012-07-20 23:09:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
"wikilink": function(compact, indent) {
|
|
|
|
if (!indent) indent = "";
|
|
|
|
var href = Util.kvTokensToString(Util.lookupKV(this.attribs, 'href').v);
|
|
|
|
if (compact) {
|
|
|
|
return ["<wikilink:", href, ">"].join('');
|
|
|
|
} else {
|
2012-07-20 23:09:09 +00:00
|
|
|
if (!indent) indent = "";
|
2012-07-24 17:50:19 +00:00
|
|
|
var origIndent = indent;
|
|
|
|
var indentIncrement = " ";
|
|
|
|
indent = indent + indentIncrement;
|
|
|
|
var tail = Util.lookupKV(this.attribs, 'tail').v;
|
|
|
|
var content = this.attrsToString(indent, indentIncrement, 2);
|
|
|
|
return ["<wikilink>(\n", indent,
|
|
|
|
"href=", href, "\n", indent,
|
|
|
|
"tail=", tail, "\n", indent,
|
|
|
|
"content=", content, "\n", origIndent,
|
|
|
|
")"].join('');
|
2012-07-11 15:18:16 +00:00
|
|
|
}
|
2012-07-24 17:50:19 +00:00
|
|
|
}
|
|
|
|
};
|
2012-07-11 15:18:16 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
SelfclosingTagTk.prototype.toString = function(compact, indent) {
|
|
|
|
if (this.dataAttribs.stx && this.dataAttribs.stx === "html") {
|
|
|
|
return "<HTML:" + this.name + " />";
|
|
|
|
} else {
|
|
|
|
var f = this.tagToStringFns[this.name];
|
|
|
|
return f ? f.bind(this)(compact, indent) : this.defaultToString(compact, indent);
|
2012-07-11 15:18:16 +00:00
|
|
|
}
|
2012-02-03 13:09:01 +00:00
|
|
|
};
|
2012-07-25 00:38:45 +00:00
|
|
|
// add in generic token methods
|
|
|
|
$.extend( SelfclosingTagTk.prototype, genericTokenMethods );
|
2012-02-03 13:09:01 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
/* -------------------- NlTk -------------------- */
|
2012-07-11 15:18:16 +00:00
|
|
|
function NlTk( ) { }
|
|
|
|
|
|
|
|
NlTk.prototype = {
|
|
|
|
constructor: NlTk,
|
|
|
|
|
|
|
|
toJSON: function () {
|
|
|
|
return $.extend( { type: 'NlTk' }, this );
|
|
|
|
},
|
|
|
|
|
|
|
|
toString: function() {
|
|
|
|
return "\\n";
|
|
|
|
}
|
2012-02-03 13:09:01 +00:00
|
|
|
};
|
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
/* -------------------- CommentTk -------------------- */
|
2012-06-19 13:24:13 +00:00
|
|
|
function CommentTk( value, dataAttribs ) {
|
2012-02-01 17:03:08 +00:00
|
|
|
this.value = value;
|
2012-06-19 13:24:13 +00:00
|
|
|
// won't survive in the DOM, but still useful for token serialization
|
|
|
|
if ( dataAttribs !== undefined ) {
|
|
|
|
this.dataAttribs = dataAttribs;
|
|
|
|
}
|
2012-02-01 17:03:08 +00:00
|
|
|
}
|
2012-07-11 15:18:16 +00:00
|
|
|
|
|
|
|
CommentTk.prototype = {
|
|
|
|
constructor: CommentTk,
|
|
|
|
|
|
|
|
toJSON: function () {
|
|
|
|
return $.extend( { type: 'COMMENT' }, this );
|
|
|
|
},
|
|
|
|
|
|
|
|
toString: function() {
|
|
|
|
return "<!--" + this.value + "-->";
|
|
|
|
}
|
2012-02-03 13:09:01 +00:00
|
|
|
};
|
2012-02-13 17:02:23 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
/* -------------------- EOFTk -------------------- */
|
2012-03-07 20:06:54 +00:00
|
|
|
function EOFTk( ) { }
|
2012-07-11 15:18:16 +00:00
|
|
|
EOFTk.prototype = {
|
|
|
|
constructor: EOFTk,
|
2012-02-13 17:02:23 +00:00
|
|
|
|
2012-07-11 15:18:16 +00:00
|
|
|
toJSON: function () {
|
|
|
|
return $.extend( { type: 'EOFTk' }, this );
|
|
|
|
},
|
|
|
|
|
|
|
|
toString: function() {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
};
|
2012-02-13 17:02:23 +00:00
|
|
|
|
2012-02-01 17:03:08 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
/* -------------------- KV -------------------- */
|
2012-02-01 17:03:08 +00:00
|
|
|
// A key-value pair
|
|
|
|
function KV ( k, v ) {
|
|
|
|
this.k = k;
|
|
|
|
this.v = v;
|
|
|
|
}
|
|
|
|
|
2012-04-25 14:35:59 +00:00
|
|
|
|
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
/* -------------------- Params -------------------- */
|
2012-04-25 14:35:59 +00:00
|
|
|
/**
|
|
|
|
* A parameter object wrapper, essentially an array of key/value pairs with a
|
|
|
|
* few extra methods.
|
|
|
|
*
|
|
|
|
* It might make sense to wrap array results of array methods such as slice
|
|
|
|
* into a params object too, so that users are not surprised by losing the
|
|
|
|
* custom methods. Alternatively, the object could be made more abstract with
|
|
|
|
* a separate .array method that just returns the plain array.
|
|
|
|
*/
|
|
|
|
function Params ( env, params ) {
|
|
|
|
this.env = env;
|
|
|
|
this.push.apply( this, params );
|
|
|
|
}
|
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
Params.prototype = [];
|
2012-04-25 14:35:59 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
Params.prototype.constructor = Params;
|
2012-04-25 14:35:59 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
Params.prototype.toString = function () {
|
|
|
|
return this.slice(0).toString();
|
|
|
|
};
|
2012-04-25 14:35:59 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
Params.prototype.dict = function () {
|
|
|
|
var res = {};
|
|
|
|
for ( var i = 0, l = this.length; i < l; i++ ) {
|
|
|
|
var kv = this[i],
|
|
|
|
key = this.env.tokensToString( kv.k ).trim();
|
|
|
|
res[key] = kv.v;
|
|
|
|
}
|
|
|
|
//console.warn( 'KVtoHash: ' + JSON.stringify( res ));
|
|
|
|
return res;
|
|
|
|
};
|
2012-04-25 14:35:59 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
Params.prototype.named = function () {
|
|
|
|
var n = 1,
|
|
|
|
out = {};
|
|
|
|
for ( var i = 0, l = this.length; i < l; i++ ) {
|
|
|
|
// FIXME: Also check for whitespace-only named args!
|
|
|
|
var k = this[i].k;
|
|
|
|
if ( k.constructor === String ) {
|
|
|
|
k = k.trim();
|
|
|
|
}
|
|
|
|
if ( ! k.length ) {
|
|
|
|
out[n.toString()] = this[i].v;
|
|
|
|
n++;
|
|
|
|
} else if ( k.constructor === String ) {
|
|
|
|
out[k] = this[i].v;
|
|
|
|
} else {
|
|
|
|
out[this.env.tokensToString( k ).trim()] = this[i].v;
|
|
|
|
}
|
2012-07-12 22:37:43 +00:00
|
|
|
}
|
2012-07-24 17:50:19 +00:00
|
|
|
return out;
|
|
|
|
};
|
2012-05-10 08:04:24 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
/**
|
|
|
|
* Expand a slice of the parameters using the supplied get options.
|
|
|
|
*/
|
|
|
|
Params.prototype.getSlice = function ( options, start, end ) {
|
|
|
|
var args = this.slice( start, end ),
|
|
|
|
cb = options.cb;
|
|
|
|
//console.warn( JSON.stringify( args ) );
|
|
|
|
async.map(
|
|
|
|
args,
|
|
|
|
function( kv, cb2 ) {
|
|
|
|
if ( kv.v.constructor === String ) {
|
|
|
|
// nothing to do
|
|
|
|
cb2( null, kv );
|
|
|
|
} else if ( kv.v.constructor === Array &&
|
|
|
|
// remove String from Array
|
|
|
|
kv.v.length === 1 && kv.v[0].constructor === String ) {
|
|
|
|
cb2( null, new KV( kv.k, kv.v[0] ) );
|
|
|
|
} else {
|
|
|
|
// Expand the value
|
|
|
|
var o2 = $.extend( {}, options );
|
|
|
|
// add in the key
|
|
|
|
o2.cb = function ( v ) {
|
|
|
|
cb2( null, new KV( kv.k, v ) );
|
|
|
|
};
|
|
|
|
kv.v.get( o2 );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function( err, res ) {
|
|
|
|
if ( err ) {
|
|
|
|
console.trace();
|
|
|
|
throw JSON.stringify( err );
|
|
|
|
}
|
|
|
|
//console.warn( 'getSlice res: ' + JSON.stringify( res ) );
|
|
|
|
cb( res );
|
|
|
|
});
|
|
|
|
};
|
2012-05-10 08:04:24 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
/* -------------------- ParserValue -------------------- */
|
2012-05-10 08:04:24 +00:00
|
|
|
/**
|
|
|
|
* A chunk. Wraps a source chunk of tokens with a reference to a frame for
|
|
|
|
* lazy and shared transformations. Do not use directly- use
|
|
|
|
* frame.newParserValue instead!
|
|
|
|
*/
|
|
|
|
function ParserValue ( source, frame ) {
|
|
|
|
if ( source.constructor === ParserValue ) {
|
|
|
|
Object.defineProperty( this, 'source',
|
|
|
|
{ value: source.source, enumerable: false } );
|
|
|
|
} else {
|
|
|
|
Object.defineProperty( this, 'source',
|
|
|
|
{ value: source, enumerable: false } );
|
|
|
|
}
|
|
|
|
Object.defineProperty( this, 'frame',
|
|
|
|
{ value: frame, enumerable: false } );
|
2012-04-25 14:35:59 +00:00
|
|
|
}
|
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
ParserValue.prototype = {};
|
2012-05-10 08:04:24 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
ParserValue.prototype._defaultTransformOptions = {
|
|
|
|
type: 'text/x-mediawiki/expanded'
|
|
|
|
};
|
2012-05-10 08:04:24 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
ParserValue.prototype.toJSON = function() {
|
|
|
|
return this.source;
|
|
|
|
};
|
2012-05-10 08:04:24 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
ParserValue.prototype.get = function( options, cb ) {
|
|
|
|
if ( ! options ) {
|
|
|
|
options = $.extend({}, this._defaultTransformOptions);
|
|
|
|
} else if ( options.type === undefined ) {
|
|
|
|
options.type = this._defaultTransformOptions.type;
|
|
|
|
}
|
2012-05-10 08:04:24 +00:00
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
// convenience cb override for async-style functions that pass a cb as the
|
|
|
|
// last argument
|
|
|
|
if ( cb === undefined ) {
|
|
|
|
cb = options.cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
var maybeCached;
|
|
|
|
var source = this.source;
|
|
|
|
if ( source.constructor === String ) {
|
|
|
|
maybeCached = source;
|
|
|
|
} else {
|
|
|
|
// try the cache
|
|
|
|
maybeCached = source.cache && source.cache.get( this.frame, options );
|
|
|
|
}
|
|
|
|
if ( maybeCached !== undefined ) {
|
|
|
|
if ( cb ) {
|
|
|
|
cb ( maybeCached );
|
2012-04-25 14:35:59 +00:00
|
|
|
} else {
|
2012-07-24 17:50:19 +00:00
|
|
|
return maybeCached;
|
2012-04-25 14:35:59 +00:00
|
|
|
}
|
2012-07-24 17:50:19 +00:00
|
|
|
} else {
|
|
|
|
if ( ! options.cb ) {
|
|
|
|
console.trace();
|
|
|
|
throw "Chunk.get: Need to expand asynchronously, but no cb provided! " +
|
|
|
|
JSON.stringify( this, null, 2 );
|
2012-05-10 08:04:24 +00:00
|
|
|
}
|
2012-07-24 17:50:19 +00:00
|
|
|
options.cb = cb;
|
|
|
|
this.frame.expand( source, options );
|
2012-07-11 15:18:16 +00:00
|
|
|
}
|
2012-05-10 08:04:24 +00:00
|
|
|
};
|
|
|
|
|
2012-07-24 17:50:19 +00:00
|
|
|
ParserValue.prototype.length = function () {
|
|
|
|
return this.source.length;
|
|
|
|
};
|
|
|
|
|
2012-05-10 08:04:24 +00:00
|
|
|
|
|
|
|
// TODO: don't use globals!
|
2012-07-25 00:38:45 +00:00
|
|
|
if (typeof module === "object") {
|
2012-02-01 17:03:08 +00:00
|
|
|
module.exports = {};
|
|
|
|
global.TagTk = TagTk;
|
|
|
|
global.EndTagTk = EndTagTk;
|
|
|
|
global.SelfclosingTagTk = SelfclosingTagTk;
|
|
|
|
global.NlTk = NlTk;
|
|
|
|
global.CommentTk = CommentTk;
|
|
|
|
global.EOFTk = EOFTk;
|
|
|
|
global.KV = KV;
|
2012-04-25 14:35:59 +00:00
|
|
|
global.Params = Params;
|
2012-05-10 08:04:24 +00:00
|
|
|
global.ParserValue = ParserValue;
|
2012-02-01 17:03:08 +00:00
|
|
|
}
|