updated version of example inline tag def
This commit is contained in:
parent
e3d525cf2a
commit
4dcf2210d7
@ -1,11 +1,14 @@
|
|||||||
var INLINE_EXAMPLE = /(\S+)\s*(\S*)\s*(title=(?:".*?"|'.*?'))?\s*(stylePattern=(?:".*?"|'.*?'))?/
|
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 some/uri region -title='some title' -stylePattern='{some style pattern}' }), replacing them with HTML anchors
|
||||||
* @kind function
|
* @kind function
|
||||||
* @param {Object} url The url to match
|
* @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 html link information
|
||||||
*
|
*
|
||||||
@ -17,17 +20,55 @@ module.exports = function exampleInlineTagDef(getLinkInfo, createDocMessage, log
|
|||||||
description: 'Process inline example tags (of the form {@example some/uri Some Title}), replacing them with HTML anchors',
|
description: 'Process inline example tags (of the form {@example some/uri Some Title}), replacing them with HTML anchors',
|
||||||
handler: function(doc, tagName, tagDescription) {
|
handler: function(doc, tagName, tagDescription) {
|
||||||
|
|
||||||
// Parse out the uri and title
|
var tagArgs = parseArgs(tagDescription);
|
||||||
return tagDescription.replace(INLINE_EXAMPLE, function(match, uri, region, attr1, attr2) {
|
var args = yargs.parse(tagArgs);
|
||||||
|
var unnamedArgs = args._;
|
||||||
//var linkInfo = getLinkInfo(uri, title, doc);
|
var relativePath = unnamedArgs[0];
|
||||||
//
|
var region = unnamedArgs.length > 1 && unnamedArgs[1];
|
||||||
//if ( !linkInfo.valid ) {
|
var title = args.title;
|
||||||
// log.warn(createDocMessage(linkInfo.error, doc));
|
var stylePattern = args.stylePattern;
|
||||||
//}
|
if (region) {
|
||||||
|
var dir = path.join("_api", path.dirname(relativePath));
|
||||||
return "+makeExample('styleguide', 'js/index.html', 'index.html')";
|
var extn = path.extname(relativePath);
|
||||||
});
|
var baseNameNoExtn = path.basename(relativePath, extn);
|
||||||
|
var baseName = baseNameNoExtn + "-" + region + extn;
|
||||||
|
}
|
||||||
|
var comma = ', '
|
||||||
|
var res = [ "+makeExample(", quote(dir), comma, quote(baseName), comma, title || 'null', ")" ].join('');
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function quote(str) {
|
||||||
|
return "'" + str + "'";
|
||||||
|
}
|
||||||
|
|
||||||
|
// copied with some mods/fixes from npm string-argv
|
||||||
|
function parseArgs(str) {
|
||||||
|
//[^\s'"] Match if not a space ' or "
|
||||||
|
|
||||||
|
//+|['] or Match '
|
||||||
|
//([^']*) Match anything that is not '
|
||||||
|
//['] Close match if '
|
||||||
|
|
||||||
|
//+|["] or Match "
|
||||||
|
//([^"]*) Match anything that is not "
|
||||||
|
//["] Close match if "
|
||||||
|
var rx = /[^\s'"]+|[']([^']*?)[']|["]([^"]*?)["]/gi;
|
||||||
|
var value = str;
|
||||||
|
var args = [];
|
||||||
|
var match;
|
||||||
|
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]));
|
||||||
|
}
|
||||||
|
} while (match !== null);
|
||||||
|
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user