refact(links-package): clean up example tag
This commit is contained in:
parent
af3aacde19
commit
26a1405e40
|
@ -1,16 +1,14 @@
|
|||
var path = require('canonical-path');
|
||||
var yargs = require('yargs');
|
||||
|
||||
var INLINE_EXAMPLE = /(\S+)(\s+\S*)?(\s+\S*=(?:".*?"|'.*?'))?(\s+\S*=(?:".*?"|'.*?'))?(\s+\S*=(?:".*?"|'.*?'))?/;
|
||||
var KEY_VALUE_RX = /\s*(\S*)\s*=\s*(?:"(.*)?"|'(.*)?')/;
|
||||
/**
|
||||
* @dgService exampleInlineTagDef
|
||||
* @description
|
||||
* Process inline example tags (of the form {@example some/uri region -title='some title' -stylePattern='{some style pattern}' }), replacing them with HTML anchors
|
||||
* Process inline example tags (of the form {@example relativePath region -title='some title' -stylePattern='{some style pattern}' }),
|
||||
* replacing them with a jade makeExample mixin call.
|
||||
* @kind function
|
||||
* @param {Object} path The relative path to example
|
||||
* @param {Function} docs error message
|
||||
* @return {String} The html link information
|
||||
* @return {String} The jade makeExample mixin call
|
||||
*
|
||||
* @property {boolean} relativeLinks Whether we expect the links to be relative to the originating doc
|
||||
*/
|
||||
|
@ -21,20 +19,20 @@ module.exports = function exampleInlineTagDef(getLinkInfo, createDocMessage, log
|
|||
handler: function(doc, tagName, tagDescription) {
|
||||
|
||||
var tagArgs = parseArgs(tagDescription);
|
||||
var args = yargs.parse(tagArgs);
|
||||
var unnamedArgs = args._;
|
||||
var unnamedArgs = tagArgs._;
|
||||
var relativePath = unnamedArgs[0];
|
||||
var region = unnamedArgs.length > 1 && unnamedArgs[1];
|
||||
var title = args.title;
|
||||
var stylePattern = args.stylePattern;
|
||||
if (region) {
|
||||
var dir = path.join("_api", path.dirname(relativePath));
|
||||
var extn = path.extname(relativePath);
|
||||
var baseNameNoExtn = path.basename(relativePath, extn);
|
||||
var baseName = baseNameNoExtn + "-" + region + extn;
|
||||
}
|
||||
var title = tagArgs.title;
|
||||
// TODO: not yet implemented here
|
||||
var stylePattern = tagArgs.stylePattern;
|
||||
|
||||
var dir = path.join("_api", path.dirname(relativePath));
|
||||
var extn = path.extname(relativePath);
|
||||
var baseNameNoExtn = path.basename(relativePath, extn);
|
||||
var baseName = region ? baseNameNoExtn + "-" + region + extn : baseNameNoExtn + extn;
|
||||
|
||||
var comma = ', '
|
||||
var res = [ "+makeExample(", quote(dir), comma, quote(baseName), comma, title || 'null', ")" ].join('');
|
||||
var res = [ "+makeExample(", quote(dir), comma, quote(baseName), comma, title ? quote(title) : 'null', ")" ].join('');
|
||||
return res;
|
||||
}
|
||||
|
||||
|
@ -42,11 +40,16 @@ module.exports = function exampleInlineTagDef(getLinkInfo, createDocMessage, log
|
|||
};
|
||||
|
||||
function quote(str) {
|
||||
if (str == null || str.length === 0) return str;
|
||||
str = str.replace("'","'\'");
|
||||
return "'" + str + "'";
|
||||
}
|
||||
|
||||
// copied with some mods/fixes from npm string-argv
|
||||
|
||||
// processes an arg string in 'almost' the same fashion that the command processor does
|
||||
// and returns an args object in yargs format.
|
||||
function parseArgs(str) {
|
||||
// regex from npm string-argv
|
||||
//[^\s'"] Match if not a space ' or "
|
||||
|
||||
//+|['] or Match '
|
||||
|
@ -58,17 +61,32 @@ function parseArgs(str) {
|
|||
//["] Close match if "
|
||||
var rx = /[^\s'"]+|[']([^']*?)[']|["]([^"]*?)["]/gi;
|
||||
var value = str;
|
||||
var args = [];
|
||||
var match;
|
||||
var unnammedArgs = [];
|
||||
var args = { _: unnammedArgs };
|
||||
var match, key;
|
||||
do {
|
||||
//Each call to exec returns the next regex match as an array
|
||||
match = rx.exec(value);
|
||||
if (match !== null) {
|
||||
//Index 1 in the array is the captured group if it exists
|
||||
//Index 0 is the matched text, which we use if no captured group exists
|
||||
args.push(match[2] ? match[2] : (match[1]?match[1]:match[0]));
|
||||
var arg = match[2] ? match[2] : (match[1]?match[1]:match[0]);
|
||||
if (key) {
|
||||
args[key] = arg;
|
||||
key = null;
|
||||
} else {
|
||||
if (arg.substr(arg.length-1) === '=') {
|
||||
key = arg.substr(0, arg.length-1);
|
||||
// remove leading '-' if it exists.
|
||||
if (key.substr(0,1)=='-') {
|
||||
key = key.substr(1);
|
||||
}
|
||||
} else {
|
||||
unnammedArgs.push(arg)
|
||||
key = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (match !== null);
|
||||
|
||||
return args;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue