1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-24 18:47:20 +00:00
TiddlyWiki5/js/ArgParser.js

115 lines
3.3 KiB
JavaScript
Raw Normal View History

/*\
title: js/ArgParser.js
Parse a space-separated string of name:value parameters. Values can be quoted with single quotes, double quotes, double square brackets, or double curly braces.
2011-11-22 18:48:47 +00:00
The parameters are returned in a structure that can be referenced like this:
2011-11-22 14:29:29 +00:00
(return).byName["name"][0] - First occurance of parameter with a given name
(return).byPos[0].n - Name of parameter in first position
(return).byPos[0].v.string - Value of parameter in first position
(return).byPos[0].v.evaluated - True if the parameter is to be evaluated
2011-11-22 14:29:29 +00:00
Options and their defaults are:
defaultName: null,
defaultValue: null,
noNames: false,
cascadeDefaults: false,
allowEval: true
2011-11-22 14:29:29 +00:00
\*/
(function(){
2011-11-22 14:29:29 +00:00
/*jslint node: true */
"use strict";
2011-11-22 14:29:29 +00:00
var ArgParser = function(argString,options) {
2011-12-28 22:07:17 +00:00
options = options || {};
var defaultName = options.defaultName,
defaultValue = options.defaultValue;
2011-11-22 14:29:29 +00:00
var parseToken = function(match,p) {
var n;
if(match[p]) { // Double quoted
n = {string: match[p]};
} else if(match[p+1]) { // Single quoted
n = {string: match[p+1]};
} else if(match[p+2]) { // Double-square-bracket quoted
n = {string: match[p+2]};
} else if(match[p+3]) { // Double-brace quoted
n = {string: match[p+3], evaluated: true};
} else if(match[p+4]) { // Unquoted
n = {string: match[p+4]};
} else if(match[p+5]) { // empty quote
n = {string: ""};
}
return n;
};
2011-11-22 14:29:29 +00:00
this.byPos = [];
var dblQuote = "(?:\"((?:(?:\\\\\")|[^\"])+)\")",
sngQuote = "(?:'((?:(?:\\\\\')|[^'])+)')",
dblSquare = "(?:\\[\\[((?:\\s|\\S)*?)\\]\\])",
dblBrace = "(?:\\{\\{((?:\\s|\\S)*?)\\}\\})",
unQuoted = options.noNames ? "([^\"'\\s]\\S*)" : "([^\"':\\s][^\\s:]*)",
emptyQuote = "((?:\"\")|(?:''))",
skipSpace = "(?:\\s*)",
token = "(?:" + dblQuote + "|" + sngQuote + "|" + dblSquare + "|" + dblBrace + "|" + unQuoted + "|" + emptyQuote + ")",
re = options.noNames ? new RegExp(token,"mg") : new RegExp(skipSpace + token + skipSpace + "(?:(\\:)" + skipSpace + token + ")?","mg"),
2011-12-01 10:19:21 +00:00
match,n,v;
2011-11-22 14:29:29 +00:00
do {
match = re.exec(argString);
2011-11-22 14:29:29 +00:00
if(match) {
2011-12-01 10:19:21 +00:00
n = parseToken(match,1);
2011-11-22 14:29:29 +00:00
if(options.noNames) {
this.byPos.push({n:"", v:n});
} else {
2011-12-01 10:19:21 +00:00
v = parseToken(match,8);
if(v === undefined && defaultName) {
2011-11-22 14:29:29 +00:00
v = n;
n = defaultName;
} else if(v === undefined && defaultValue) {
v = defaultValue;
}
if(n.evaluated === true) {
n = "{{" + n.string + "}}";
} else if (typeof n === "object" && n.hasOwnProperty("string")) {
n = n.string;
2011-11-22 14:29:29 +00:00
}
this.byPos.push({n:n, v:v});
if(options.cascadeDefaults) {
defaultName = n;
defaultValue = v;
2011-11-22 14:29:29 +00:00
}
}
}
} while(match);
this.byName = {};
for(var t=0; t<this.byPos.length; t++) {
2011-12-01 10:19:21 +00:00
n = this.byPos[t].n;
v = this.byPos[t].v;
if(this.byName.hasOwnProperty("n"))
2011-11-22 14:29:29 +00:00
this.byName[n].push(v);
else
this.byName[n] = [v];
}
2011-12-01 10:19:21 +00:00
};
2011-11-22 14:29:29 +00:00
// Retrieve the first occurance of a named parameter, or the default if missing
ArgParser.prototype.getValueByName = function(n) {
2011-11-22 14:29:29 +00:00
var v = this.byName[n];
return v && v.length > 0 ? v[0] : null;
2011-12-01 10:19:21 +00:00
};
2011-11-22 14:29:29 +00:00
// Retrieve all the string values as an array
ArgParser.prototype.getStringValues = function() {
var result = [];
for(var t=0; t<this.byPos.length; t++) {
result.push(this.byPos[t].v.string);
}
return result;
2011-12-01 10:19:21 +00:00
};
2011-11-22 14:29:29 +00:00
2011-12-01 10:19:21 +00:00
exports.ArgParser = ArgParser;
})();