Doc
class
new Doc() extends Compiler
The main class that creates beautiful documentations.
class Doc extends Compiler {
// Initialize the compiler
// and pass the parser.
constructor(options) {
super(new Parser(options));
// Set the locals stack
this.locals = [];
}
generate
method
Doc.prototype.generate()
Generates the documentations.
generate() {
// Prepare promise
let d = when.defer();
// Compute all symboles
let allSymbols = this.files.reduce(function(m, a) {
m = m.concat(a.symbols || []);
return m;
}, []);
// Set package
let pkg = this.options.package;
// Set readme
var readme = pkg && pkg.readme,
readMeFile = Path.resolve(process.cwd(), this.options.readme ||
(pkg && pkg.readmeFileName) || 'README.md');
if (!readme && File.existsSync(readMeFile)) {
readme = File.readFileSync(readMeFile).toString();
} else {
console.warn('Mr. Doc [warn]: No README.md file found at ' + readMeFile);
}
if (!readme) {
console.warn('Mr. Doc [warn]: Empty README.md ' + readMeFile);
readme = '';
}
let md = new Markdown({
html: true
});
md = md.render.bind(md);
// Get readme data
this.files.unshift({
name: 'Main',
targetName: 'index.html',
readme: md(readme),
dox: [],
symbols: []
});
// Set title
let title = this.options.name ? this.options.name : pkg ? pkg.name : 'No title';
// Set description
let description = pkg && pkg.description ? pkg.description : '';
// Set URLs
let url = {
github: pkg && pkg.homepage ?
pkg.homepage.indexOf('github') > -1 ?
pkg.homepage : false : false,
npm: pkg && pkg.name ? 'https://npmjs.com/package/' + pkg.name : false,
homepage: pkg && pkg.homepage ? pkg.homepage.indexOf('github') === -1 ? pkg.homepage : false : false
};
// Set each files relName in relation
// to where this file is in the directory tree
this.files.forEach(file => {
file.targets = this.getTargets(file);
});
this.files.forEach((file) => {
// Set locals
this.locals.push(_.assign({}, file, {
project: {
title,
description,
url
},
allSymbols: allSymbols,
files: this.files,
current: {
name: file.name
},
file: {
targets: file.targets
}
}));
});
// Install theme
(new Theme(this.options)).install().then((result) => {
var {
theme
} = result;
if (theme) {
console.info('Mr. Doc [info]: Installed theme: ' + theme);
}
// Make sure the sub dirs that are not blacklisted exist.
const {
output,
blacklist
} = this.options,
sourcePath = Path.normalize(Path.resolve(process.cwd(), this.options.source));
File.walk(sourcePath)
.on('readable', function() {
let item;
while ((item = this.read())) {
if (item.stats.isDirectory()) {
const path = Path.normalize(item
.path
.replace(sourcePath, output));
if (blacklist.some(folder => path.indexOf(folder) < 0))
File.ensureDirSync(path);
}
}
}).on('end', () => {
_.forEach(this.files, (file, index) => {
// Set template
this.setTemplate(result.template);
// Compile the template
let compiled = this.compile(this.locals[index]);
// Write files
mkdirp(Path.normalize(this.options.output + '/'), error => {
if (error) return;
else File.writeFileSync(Path.join(this.options.output, file.targetName), compiled);
});
});
d.resolve();
});
}, console.error);
return d.promise;
}