105 lines
2.8 KiB
JavaScript
105 lines
2.8 KiB
JavaScript
/**
|
|
* Code generation of the index file to pull together all the other documentation
|
|
*/
|
|
'use strict';
|
|
var Handlebars = require('handlebars');
|
|
var fs = require('fs');
|
|
var os = require('os');
|
|
|
|
//
|
|
// Define the exports
|
|
//
|
|
module.exports = {
|
|
GenerateIndex: docgen
|
|
};
|
|
|
|
/**
|
|
* Function to generate the index AsciiDoc document for all files
|
|
*
|
|
* @param {Object} options - The docgen options
|
|
*
|
|
* @returns {Promise} Promise that is resolved/rejected on success/failure
|
|
*/
|
|
function docgen(options) {
|
|
return new Promise(function(resolve, reject) {
|
|
//
|
|
// Register some Handlebars helpers
|
|
//
|
|
registerHandlebarsHelpers();
|
|
|
|
//
|
|
// Register Handlebars partials
|
|
//
|
|
var handlebarsOpts = {noEscape: true};
|
|
registerHandlebarsPartials(options.indexdocs.options, handlebarsOpts);
|
|
|
|
//
|
|
// Process the templates with all the config info
|
|
//
|
|
var templateData = {
|
|
options: options
|
|
};
|
|
|
|
try {
|
|
var indexOptions = options.indexdocs.options;
|
|
|
|
for (var name in indexOptions.pages) {
|
|
var dest = indexOptions.dest + name + '.adoc';
|
|
console.log('Building %s -> %s', indexOptions.pages[name], dest);
|
|
|
|
var templateText = fs.readFileSync(indexOptions.pages[name], 'utf-8');
|
|
var func = Handlebars.compile(templateText, handlebarsOpts);
|
|
var result = func(templateData);
|
|
fs.writeFileSync(dest, result);
|
|
}
|
|
|
|
//
|
|
// Resolve the promise to report success
|
|
//
|
|
resolve();
|
|
} catch (err) {
|
|
reject(err);
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Register handlebars partials
|
|
*
|
|
* @param {Object} options - configuration options
|
|
* @param {Object} handlebarsOpts - options for handlebars
|
|
*/
|
|
function registerHandlebarsPartials(options, handlebarsOpts) {
|
|
for (var name in options.templates) {
|
|
registerHandlebarsPartial(options.templates[name], name, handlebarsOpts);
|
|
}
|
|
}
|
|
|
|
function registerHandlebarsPartial(file, name, handlebarsOpts) {
|
|
var templateText = fs.readFileSync(file, 'utf-8');
|
|
var template = Handlebars.compile(templateText, handlebarsOpts);
|
|
|
|
Handlebars.registerPartial(name, template);
|
|
}
|
|
|
|
/**
|
|
* Registers handlebars helper functions
|
|
*/
|
|
function registerHandlebarsHelpers() {
|
|
Handlebars.registerHelper('filenameify', filenameify);
|
|
}
|
|
|
|
/**
|
|
* Handlebars helper function to replace all '/' with '-' to make a single
|
|
* filename rather than a nested path
|
|
*
|
|
* @param {Object} item - the item to look at.
|
|
*
|
|
* @return {Object} - a safestring of the type string
|
|
*/
|
|
function filenameify(item) {
|
|
var result = item.replace(/\//g, '-');
|
|
return new Handlebars.SafeString(result);
|
|
}
|