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 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
|
* @dgService exampleInlineTagDef
|
||||||
* @description
|
* @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
|
* @kind function
|
||||||
* @param {Object} path The relative path to example
|
* @param {Object} path The relative path to example
|
||||||
* @param {Function} docs error message
|
* @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
|
* @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) {
|
handler: function(doc, tagName, tagDescription) {
|
||||||
|
|
||||||
var tagArgs = parseArgs(tagDescription);
|
var tagArgs = parseArgs(tagDescription);
|
||||||
var args = yargs.parse(tagArgs);
|
var unnamedArgs = tagArgs._;
|
||||||
var unnamedArgs = args._;
|
|
||||||
var relativePath = unnamedArgs[0];
|
var relativePath = unnamedArgs[0];
|
||||||
var region = unnamedArgs.length > 1 && unnamedArgs[1];
|
var region = unnamedArgs.length > 1 && unnamedArgs[1];
|
||||||
var title = args.title;
|
var title = tagArgs.title;
|
||||||
var stylePattern = args.stylePattern;
|
// TODO: not yet implemented here
|
||||||
if (region) {
|
var stylePattern = tagArgs.stylePattern;
|
||||||
var dir = path.join("_api", path.dirname(relativePath));
|
|
||||||
var extn = path.extname(relativePath);
|
var dir = path.join("_api", path.dirname(relativePath));
|
||||||
var baseNameNoExtn = path.basename(relativePath, extn);
|
var extn = path.extname(relativePath);
|
||||||
var baseName = baseNameNoExtn + "-" + region + extn;
|
var baseNameNoExtn = path.basename(relativePath, extn);
|
||||||
}
|
var baseName = region ? baseNameNoExtn + "-" + region + extn : baseNameNoExtn + extn;
|
||||||
|
|
||||||
var comma = ', '
|
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;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,11 +40,16 @@ module.exports = function exampleInlineTagDef(getLinkInfo, createDocMessage, log
|
||||||
};
|
};
|
||||||
|
|
||||||
function quote(str) {
|
function quote(str) {
|
||||||
|
if (str == null || str.length === 0) return str;
|
||||||
|
str = str.replace("'","'\'");
|
||||||
return "'" + str + "'";
|
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) {
|
function parseArgs(str) {
|
||||||
|
// regex from npm string-argv
|
||||||
//[^\s'"] Match if not a space ' or "
|
//[^\s'"] Match if not a space ' or "
|
||||||
|
|
||||||
//+|['] or Match '
|
//+|['] or Match '
|
||||||
|
@ -58,17 +61,32 @@ function parseArgs(str) {
|
||||||
//["] Close match if "
|
//["] Close match if "
|
||||||
var rx = /[^\s'"]+|[']([^']*?)[']|["]([^"]*?)["]/gi;
|
var rx = /[^\s'"]+|[']([^']*?)[']|["]([^"]*?)["]/gi;
|
||||||
var value = str;
|
var value = str;
|
||||||
var args = [];
|
var unnammedArgs = [];
|
||||||
var match;
|
var args = { _: unnammedArgs };
|
||||||
|
var match, key;
|
||||||
do {
|
do {
|
||||||
//Each call to exec returns the next regex match as an array
|
//Each call to exec returns the next regex match as an array
|
||||||
match = rx.exec(value);
|
match = rx.exec(value);
|
||||||
if (match !== null) {
|
if (match !== null) {
|
||||||
//Index 1 in the array is the captured group if it exists
|
//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
|
//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);
|
} while (match !== null);
|
||||||
|
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue