angular-docs-cn/public/doc-shredder/shredMapProcessor.js

79 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-08-10 14:17:02 -04:00
/**
* dgProcessor shredMapProcessor
* @description
*
*/
2015-08-11 03:27:09 -04:00
var path = require('canonical-path');
var fs = require('fs');
var _ = require('lodash');
2015-08-11 03:27:09 -04:00
2015-08-10 14:17:02 -04:00
module.exports = function shredMapProcessor(log) {
return {
$runAfter: ['readFilesProcessor'],
$runBefore: ['rendering-docs'],
2015-08-11 03:27:09 -04:00
2015-08-10 14:17:02 -04:00
$process: function(docs) {
2015-08-11 03:27:09 -04:00
var options = this.options;
var jadeToFragMap = {};
var fragToJadeMap = {};
2015-08-10 14:17:02 -04:00
docs.forEach(function(doc) {
2015-08-11 03:27:09 -04:00
var jadePath = path.join(options.jadeDir, doc.fileInfo.relativePath);
var fragInfoSet = {};
doc.fragPaths.forEach(function(fragPath) {
var fullFragPath = path.join(options.fragmentsDir, fragPath) + '.md';
var examplePath = getExampleName(fragPath);
var fullExamplePath = path.join(options.examplesDir, examplePath);
var fragInfo = { fragPath: fullFragPath, examplePath: fullExamplePath, exists: fs.existsSync(fullFragPath) };
fragInfoSet[fragPath] = fragInfo;
2015-08-11 03:27:09 -04:00
if (fragInfo.exists) {
var jadePathsSet = fragToJadeMap[fragPath];
if (!jadePathsSet) {
jadePathsSet = {};
fragToJadeMap[fragPath] = jadePathsSet;
2015-08-11 03:27:09 -04:00
}
jadePathsSet[jadePath] = jadePath;
2015-08-11 03:27:09 -04:00
}
});
jadeToFragMap[jadePath] = _.values(fragInfoSet);
2015-08-10 14:17:02 -04:00
});
for (var key in fragToJadeMap) {
fragToJadeMap[key] = _.keys(fragToJadeMap[key]);
}
var shredMap = {
jadeToFragMap: jadeToFragMap
};
if (!options.writeFilesEnabled) {
return [ shredMap ];
} else {
var newDocs = [ {
docType: 'xref-doc.json',
json: JSON.stringify(shredMap, null, 2),
outputPath: 'xref-jade.json'
}, {
docType: 'xref-jade.html',
2015-08-11 03:27:09 -04:00
jadeToFragMap: jadeToFragMap,
outputPath: 'xref-jade-to-frag.html'
}, {
docType: 'xref-frag.html',
fragToJadeMap: fragToJadeMap,
outputPath: 'xref-frag-to-jade.html'
}];
return newDocs;
}
2015-08-10 14:17:02 -04:00
}
}
};
function getExampleName(fragPath) {
// pattern to isolate base fileName and extension from fragment name
var rx = /(.*)\-(.*)\.(.s)/;
var r = rx.exec(fragPath);
if (r) {
return r[1] + '.' + r[3];
} else {
return fragPath;
}
}