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

71 lines
2.1 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');
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 fragInfos = doc.fragPaths.map(function(fragPath) {
var relativeFragPath = path.join(options.fragmentsDir, fragPath) + '.md';
var fullPath = path.join(options.basePath, relativeFragPath);
var examplePath = getExampleName(fragPath);
var relativeExamplePath = path.join(options.examplesDir, examplePath);
var fragInfo = { fragPath: relativeFragPath, examplePath: relativeExamplePath, exists: fs.existsSync(fullPath) };
2015-08-11 03:27:09 -04:00
if (fragInfo.exists) {
var jadePaths = fragToJadeMap[fragInfo];
if (!jadePaths) {
jadePaths = [];
fragToJadeMap[fragPath] = jadePaths;
}
jadePaths.push(jadePath);
}
return fragInfo;
});
jadeToFragMap[jadePath] = fragInfos;
2015-08-10 14:17:02 -04:00
});
var newDocs = [{
2015-08-11 03:27:09 -04:00
docType: 'xref-jade.html',
basePath: this.options.basePath,
jadeToFragMap: jadeToFragMap,
outputPath: 'xref-jade-to-frag.html'
2015-08-10 14:17:02 -04:00
}, {
2015-08-11 03:27:09 -04:00
docType: 'xref-frag.html',
basePath: this.options.basePath,
fragToJadeMap: fragToJadeMap,
outputPath: 'xref-frag-to-jade.html'
}, {
docType: 'xref-doc.json',
json: JSON.stringify({
basePath: this.options.basePath,
jadeToFragMap: jadeToFragMap,
}, null, 2),
outputPath: 'xref-jade.json'
2015-08-10 14:17:02 -04:00
}]
return newDocs;
}
};
};
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;
}
}