2015-09-11 03:19:23 -04:00
|
|
|
//- Mixins and associated functions
|
2015-08-08 16:55:53 -04:00
|
|
|
|
2015-09-11 03:19:23 -04:00
|
|
|
mixin makeExample(path, fileName, title, stylePatterns)
|
|
|
|
- var language = attributes.language || getExtn(fileName);
|
|
|
|
- var format = attributes.format || "linenums";
|
2015-09-12 03:28:01 -04:00
|
|
|
- var frag = getFrag(path, fileName);
|
2015-09-11 03:19:23 -04:00
|
|
|
if (title)
|
|
|
|
.example-title #{title}
|
|
|
|
code-example(language="#{language}" format="#{format}")
|
2015-09-12 03:28:01 -04:00
|
|
|
!= styleString(frag, stylePatterns)
|
2015-09-11 03:19:23 -04:00
|
|
|
|
|
|
|
mixin makeTabs(path, fileNames, tabNames, stylePatterns)
|
|
|
|
- fileNames = fileNames.split(",");
|
|
|
|
- tabNames = tabNames.split(",")
|
|
|
|
|
|
|
|
code-tabs
|
|
|
|
each fileName,index in fileNames
|
|
|
|
- var tabName = tabNames[index].trim();
|
|
|
|
- var fileName = fileNames[index].trim();
|
|
|
|
- var language = attributes.language || getExtn(fileName);
|
|
|
|
- var format = attributes.format || "linenums";
|
2015-09-12 03:28:01 -04:00
|
|
|
|
|
|
|
- var frag = getFrag(path, fileName);
|
2015-09-11 03:19:23 -04:00
|
|
|
- var sps = Array.isArray(stylePatterns) ? stylePatterns[index] : stylePatterns;
|
|
|
|
code-pane(language="#{language}" name="#{tabName}" format="#{format}")
|
2015-09-12 03:28:01 -04:00
|
|
|
!= styleString(frag, sps)
|
2015-08-08 16:55:53 -04:00
|
|
|
|
2015-09-12 03:28:01 -04:00
|
|
|
mixin makeJson(path, fileName, title, jsonConfig, stylePatterns)
|
2015-09-11 16:50:19 -04:00
|
|
|
- var language = attributes.language || getExtn(fileName);
|
|
|
|
- var format = attributes.format || "linenums";
|
2015-09-12 03:28:01 -04:00
|
|
|
- var frag = getFrag(path, fileName);
|
|
|
|
- var json = unescapeHtml(frag);
|
|
|
|
- var jsonExtract = extractJson(json, jsonConfig);
|
|
|
|
|
2015-09-11 16:50:19 -04:00
|
|
|
if (title)
|
|
|
|
.example-title #{title}
|
|
|
|
code-example(language="#{language}" format="#{format}")
|
2015-09-12 03:28:01 -04:00
|
|
|
!= styleString(jsonExtract, stylePatterns)
|
|
|
|
|
2015-09-11 16:50:19 -04:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------
|
2015-09-11 04:10:48 -04:00
|
|
|
|
2015-09-12 03:28:01 -04:00
|
|
|
- var getFrag = function(path, fileName) {
|
|
|
|
- var fullFileName = getPathToFrags() + path + "/" + fileName + '.md';
|
|
|
|
- var frag = partial(fullFileName);
|
2015-08-03 20:45:58 -04:00
|
|
|
- if (frag == null) {
|
2015-09-12 03:28:01 -04:00
|
|
|
- return "BAD FILENAME: " + fullFileName + " Current path: " + current.path + " PathToDocs: " + getPathToDocs();
|
2015-08-03 20:45:58 -04:00
|
|
|
- } else {
|
|
|
|
- // ``` gets translated to <pre><code>.....</code></pre> and we need
|
|
|
|
- // to remove this from the fragment prefix is 11 long and suffix is 13 long
|
2015-09-12 03:28:01 -04:00
|
|
|
- frag = frag.substring(11, frag.length-13);
|
2015-09-11 16:50:19 -04:00
|
|
|
- // Uncomment next line for debugging.
|
2015-09-12 03:28:01 -04:00
|
|
|
- // frag = "FileName: " + fullFileName + " Current path: " + current.path + " PathToDocs: " + getPathToDocs() + "\n" + frag;
|
|
|
|
- return frag;
|
2015-08-03 20:45:58 -04:00
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
2015-09-12 03:28:01 -04:00
|
|
|
- var extractJson = function(json, jsonConfig ) {
|
|
|
|
- if (jsonConfig) {
|
|
|
|
- return extractJsonFragment(json, jsonConfig.rootPath || null, jsonConfig.paths || [], jsonConfig.space || " ");
|
|
|
|
- } else {
|
|
|
|
- return json;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
|
|
- var unescapeHtml = function(s) {
|
|
|
|
- // can't break across multiple lines because of jade limitations.
|
|
|
|
- return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, "'");
|
|
|
|
- }
|
|
|
|
|
2015-09-11 16:50:19 -04:00
|
|
|
- var styleString = function(source, stylePatterns) {
|
|
|
|
- if (stylePatterns) {
|
|
|
|
- for (var styleName in stylePatterns) {
|
|
|
|
- var rxs = stylePatterns[styleName];
|
|
|
|
- rxs = Array.isArray(rxs) ? rxs : [rxs];
|
|
|
|
- rxs.forEach(function(rx) {
|
|
|
|
- source = applyStyle(source, styleName, rx );
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return source;
|
|
|
|
- }
|
|
|
|
|
|
|
|
//- styles a string according to a regular expression.
|
|
|
|
//- The match groups resulting from the regexp application are what is styled
|
|
|
|
//- (not the whole regexp).
|
|
|
|
- var applyStyle = function(str, styleName, rx) {
|
2015-08-08 16:55:53 -04:00
|
|
|
- var repls = {};
|
|
|
|
- var matches;
|
|
|
|
- do {
|
|
|
|
- matches = rx.exec(str);
|
|
|
|
- if (matches) {
|
|
|
|
- matches.slice(1).forEach(function(match) {
|
|
|
|
- var repl = '<span class="' + styleName + '">' + match + '</span>';
|
|
|
|
- repls[match] = { repl: repl, isGlobal: rx.global };
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- } while (matches != null && rx.global );
|
|
|
|
- for (var match in repls) {
|
|
|
|
- var repl = repls[match];
|
2015-09-15 15:26:37 -04:00
|
|
|
- // var escapedMatch = match.replace(/\\/g,'\\').replace(/\(/g,'\(').replace(/\)/g, '\)').replace(/\./,'\.');
|
|
|
|
- // var rx2 = new RegExp(escapedMatch, repl.isGlobal ? "g" : "");
|
|
|
|
- var rx2 = match;
|
2015-08-08 16:55:53 -04:00
|
|
|
- str = str.replace(rx2, repl.repl);
|
|
|
|
- };
|
|
|
|
- return str;
|
|
|
|
- }
|
|
|
|
|
2015-08-03 20:45:58 -04:00
|
|
|
- var getExtn = function(fileName) {
|
|
|
|
- var ix = fileName.lastIndexOf('.');
|
|
|
|
- return ix > 0 ? fileName.substr(ix+1) : "";
|
|
|
|
- }
|
|
|
|
|
2015-09-11 16:50:19 -04:00
|
|
|
- var getPathToDocs = function() {
|
2015-08-08 16:55:53 -04:00
|
|
|
- // simple way to only take as many '../' sections as we need to back up to the 'docs' dir
|
|
|
|
- // from the current document
|
|
|
|
- // we will almost certainly never go 10 or 11 deep but ...
|
2015-09-11 16:50:19 -04:00
|
|
|
- return current.pathToDocs || "../../../../../../../../../../../".substr(0, (current.path.length-2)*3);
|
|
|
|
- }
|
|
|
|
|
|
|
|
- var getPathToFrags = function() {
|
|
|
|
- return getPathToDocs() + "_fragments/";
|
|
|
|
- }
|
|
|
|
|
|
|
|
- var getPathToExamples = function() {
|
|
|
|
- return getPathToDocs() + "_examples/";
|
|
|
|
- }
|
|
|
|
|
|
|
|
//- Extract a subset of a json file in a specified order defined
|
|
|
|
//- by extractPaths while retaining original order for any
|
|
|
|
//- unspecified subobjects.
|
|
|
|
//-
|
|
|
|
//- based on the principle that JSON.parse(source) constructs objects
|
|
|
|
//- in order from the top down in 'source' and JSON.stringify(source) iterates 'source'
|
|
|
|
//- properties according to the order in which they were inserted. ( the spec actually
|
|
|
|
//- discourages this assumption but this method will only
|
|
|
|
//- run on node (v8) and the assumption seems safe there. )
|
2015-09-12 03:28:01 -04:00
|
|
|
- function extractJsonFragment(source, rootPath, extractPaths, space) {
|
2015-09-11 16:50:19 -04:00
|
|
|
- var objSource = JSON.parse(source);
|
|
|
|
- if (rootPath && rootPath.length > 0) {
|
|
|
|
- objSource = getSubObject(objSource, rootPath);
|
|
|
|
- }
|
|
|
|
- var objDest = {};
|
|
|
|
- extractPaths.trim().split(",").forEach(function(dotPath) {
|
|
|
|
- processPath(objSource, objDest, dotPath );
|
|
|
|
- });
|
2015-09-12 03:28:01 -04:00
|
|
|
- var result = JSON.stringify(objDest, null, space || " ");
|
2015-09-11 16:50:19 -04:00
|
|
|
- return result;
|
|
|
|
|
2015-09-12 03:28:01 -04:00
|
|
|
- function getSubObject(source, path) {
|
|
|
|
- var nextSource = source;
|
|
|
|
- var pathParts = path.trim().split(".");
|
|
|
|
- while (pathParts.length > 0) {
|
|
|
|
- var nextProp = pathParts.shift();
|
|
|
|
- nextSource = nextSource[nextProp];
|
|
|
|
- }
|
|
|
|
- return nextSource;
|
2015-09-11 16:50:19 -04:00
|
|
|
- }
|
|
|
|
|
2015-09-12 03:28:01 -04:00
|
|
|
- function processPath(source, dest, dotPath) {
|
|
|
|
- var nextSource = source;
|
|
|
|
- var nextDest = dest;
|
|
|
|
- var pathParts = dotPath.trim().split(".");
|
|
|
|
- while (pathParts.length > 0) {
|
|
|
|
- var nextProp = pathParts.shift();
|
|
|
|
- nextSource = nextSource[nextProp];
|
|
|
|
- if (pathParts.length > 0) {
|
|
|
|
- var val = nextDest[nextProp] || {};
|
|
|
|
- nextDest[nextProp] = val;
|
|
|
|
- nextDest = val;
|
|
|
|
- } else {
|
|
|
|
- nextDest[nextProp] = nextSource;
|
|
|
|
- }
|
2015-09-11 16:50:19 -04:00
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|