updated version of example inline tag def

This commit is contained in:
Jay Traband 2015-09-18 00:42:25 -07:00
parent e3d525cf2a
commit 4dcf2210d7
1 changed files with 56 additions and 15 deletions

View File

@ -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
* @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
* @param {Object} url The url to match
* @param {Object} path The relative path to example
* @param {Function} docs error message
* @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',
handler: function(doc, tagName, tagDescription) {
// Parse out the uri and title
return tagDescription.replace(INLINE_EXAMPLE, function(match, uri, region, attr1, attr2) {
//var linkInfo = getLinkInfo(uri, title, doc);
//
//if ( !linkInfo.valid ) {
// log.warn(createDocMessage(linkInfo.error, doc));
//}
return "+makeExample('styleguide', 'js/index.html', 'index.html')";
});
var tagArgs = parseArgs(tagDescription);
var args = yargs.parse(tagArgs);
var unnamedArgs = args._;
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 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;
}