crojsdoc
Version:
A documentation generator for JavaScript and CoffeeScript
352 lines (326 loc) • 11.2 kB
JavaScript
// Generated by CoffeeScript 1.8.0
(function() {
var Renderer, dirname, fs, jade, render, resolve, _ref;
fs = require('fs.extra');
jade = require('jade');
_ref = require('path'), resolve = _ref.resolve, dirname = _ref.dirname;
Renderer = (function() {
function Renderer(result, options) {
var theme;
this.result = result;
this.options = options;
theme = 'default';
this.resources_dir = resolve(__dirname, '../themes', theme, 'resources');
this.templates_dir = resolve(__dirname, '../themes', theme, 'templates');
}
Renderer.prototype.makeMissingLink = function(type, place) {
var txt;
if (place == null) {
place = '';
}
txt = this.result.ids[type] ? "'" + type + "' link is ambiguous" : "'" + type + "' link does not exist";
console.log(txt + (" " + place));
return "<span class='missing-link'>" + type + "</span>";
};
Renderer.prototype.makeTypeLink = function(rel_path, type, place) {
var getlink, res;
if (place == null) {
place = '';
}
if (!type) {
return type;
}
getlink = (function(_this) {
return function(type) {
var filename, html_id, link;
if (_this.options.types[type]) {
link = _this.options.types[type];
} else if (_this.result.ids[type] && _this.result.ids[type] !== 'DUPLICATED ENTRY') {
filename = _this.result.ids[type].filename + '.html';
html_id = _this.result.ids[type].html_id || '';
link = "" + rel_path + filename + "#" + html_id;
} else {
return _this.makeMissingLink(type, place);
}
return "<a href='" + link + "'>" + type + "</a>";
};
})(this);
if (res = type.match(/\[(.*)\]\((.*)\)/)) {
this.options.types[res[1]] = res[2];
return "<a href='" + res[2] + "'>" + res[1] + "</a>";
}
if (res = type.match(/(.*?)<(.*)>/)) {
return "" + (this.makeTypeLink(rel_path, res[1])) + "<" + (this.makeTypeLink(rel_path, res[2])) + ">";
} else {
return getlink(type);
}
};
Renderer.prototype.makeSeeLink = function(rel_path, str) {
var filename, html_id;
if (this.result.ids[str]) {
filename = this.result.ids[str].filename + '.html';
html_id = this.result.ids[str].html_id || '';
str = "<a href='" + rel_path + filename + "#" + html_id + "'>" + str + "</a>";
}
return str;
};
Renderer.prototype.convertLink = function(rel_path, str) {
if (!str) {
return '';
}
str = str.replace(/\[\[#([^\[\]]+)\]\]/g, (function(_this) {
return function(_, $1) {
var filename, html_id;
if (_this.result.ids[$1] && _this.result.ids[$1] !== 'DUPLICATED ENTRY') {
filename = _this.result.ids[$1].filename + '.html';
html_id = _this.result.ids[$1].html_id || '';
return "<a href='" + rel_path + filename + "#" + html_id + "'>" + $1 + "</a>";
} else {
return _this.makeMissingLink($1);
}
};
})(this));
return str;
};
Renderer.prototype.copyResources = function(source, target, callback) {
var file, files, _i, _len;
try {
files = fs.readdirSync(target);
} catch (_error) {
files = [];
}
for (_i = 0, _len = files.length; _i < _len; _i++) {
file = files[_i];
if (file[0] !== '.') {
fs.rmrfSync(resolve(target, file));
}
}
try {
fs.mkdirSync(target);
} catch (_error) {}
return fs.copyRecursive(source, target, function() {
return callback();
});
};
Renderer.prototype.renderOne = function(jade_options, template, output) {
jade_options.result = this.result;
if (!jade_options.makeTypeLink) {
jade_options.makeTypeLink = this.makeTypeLink.bind(this);
}
jade_options.makeSeeLink = this.makeSeeLink.bind(this);
jade_options.convertLink = this.convertLink.bind(this);
jade_options.github = this.options.github;
jade_options.cache = true;
jade_options.self = true;
return jade.renderFile("" + this.templates_dir + "/" + template + ".jade", jade_options, (function(_this) {
return function(error, result) {
var output_file;
if (error) {
return console.error(error.stack);
}
output_file = "" + _this.options.output_dir + "/" + output + ".html";
return fs.writeFile(output_file, result, function(error) {
if (error) {
return console.error('failed to create ' + output_file);
}
if (!_this.options.quite) {
return console.log(output_file + ' is created');
}
});
};
})(this));
};
Renderer.prototype.renderReadme = function() {
var jade_options;
jade_options = {
rel_path: './',
name: 'README',
content: this.result.readme,
type: 'home'
};
return this.renderOne(jade_options, 'extra', 'index');
};
Renderer.prototype.renderGuides = function() {
if (this.result.guides.length === 0) {
return;
}
try {
fs.mkdirSync("" + this.options.output_dir + "/guides");
} catch (_error) {}
return this.result.guides.forEach((function(_this) {
return function(guide) {
var jade_options;
jade_options = {
rel_path: '../',
name: guide.name,
content: guide.content,
type: 'guides'
};
return _this.renderOne(jade_options, 'extra', guide.filename);
};
})(this));
};
Renderer.prototype.renderPages = function() {
var jade_options;
if (this.result.pages.length > 0) {
jade_options = {
rel_path: './',
name: 'Pages',
type: 'pages'
};
return this.renderOne(jade_options, 'pages', 'pages');
}
};
Renderer.prototype.renderRESTApis = function() {
var jade_options;
if (this.result.restapis.length > 0) {
jade_options = {
rel_path: './',
name: 'REST APIs',
type: 'restapis'
};
return this.renderOne(jade_options, 'restapis', 'restapis');
}
};
Renderer.prototype.renderClasses = function() {
var jade_options;
if (this.result.classes.length === 0) {
return;
}
try {
fs.mkdirSync("" + this.options.output_dir + "/classes");
} catch (_error) {}
jade_options = {
rel_path: '../',
type: 'classes'
};
this.renderOne(jade_options, 'class-toc', 'classes/index');
return this.result.classes.forEach((function(_this) {
return function(klass) {
jade_options = {
rel_path: '../',
name: klass.ctx.name,
klass: klass,
properties: klass.properties,
type: 'classes',
makeTypeLink: function(path, type) {
return _this.makeTypeLink(path, type, "(in " + klass.defined_in + ")");
}
};
return _this.renderOne(jade_options, 'class', klass.filename);
};
})(this));
};
Renderer.prototype.renderModules = function() {
var jade_options;
if (this.result.modules.length === 0) {
return;
}
try {
fs.mkdirSync("" + this.options.output_dir + "/modules");
} catch (_error) {}
jade_options = {
rel_path: '../',
type: 'modules'
};
this.renderOne(jade_options, 'module-toc', 'modules/index');
return this.result.modules.forEach((function(_this) {
return function(module) {
jade_options = {
rel_path: '../',
name: module.ctx.name,
module_data: module,
properties: module.properties,
type: 'modules'
};
return _this.renderOne(jade_options, 'module', module.filename);
};
})(this));
};
Renderer.prototype.renderFeatures = function() {
if (this.result.features.length === 0) {
return;
}
try {
fs.mkdirSync("" + this.options.output_dir + "/features");
} catch (_error) {}
return this.result.features.forEach((function(_this) {
return function(feature) {
var jade_options;
jade_options = {
rel_path: '../',
name: feature.name,
feature: feature,
type: 'features'
};
return _this.renderOne(jade_options, 'feature', feature.filename);
};
})(this));
};
Renderer.prototype.renderFiles = function() {
if (this.result.files.length === 0) {
return;
}
try {
fs.mkdirSync("" + this.options.output_dir + "/files");
} catch (_error) {}
return this.result.files.forEach((function(_this) {
return function(file) {
var jade_options;
jade_options = {
rel_path: '../',
name: file.name,
file: file,
type: 'files'
};
return _this.renderOne(jade_options, 'file', file.filename);
};
})(this));
};
Renderer.prototype.groupByNamespaces = function(items) {
var current_group, current_namespace, grouped_items;
if (items.length === 0) {
return [];
}
current_group = [];
grouped_items = [current_group];
current_namespace = items[0].namespace;
items.forEach(function(item) {
if (current_namespace !== item.namespace) {
current_group = [];
grouped_items.push(current_group);
current_namespace = item.namespace;
}
return current_group.push(item);
});
return grouped_items;
};
Renderer.prototype.run = function() {
this.result.ns_pages = this.groupByNamespaces(this.result.pages);
this.result.ns_restapis = this.groupByNamespaces(this.result.restapis);
this.result.ns_classes = this.groupByNamespaces(this.result.classes);
this.result.ns_modules = this.groupByNamespaces(this.result.modules);
this.result.ns_features = this.groupByNamespaces(this.result.features);
this.result.ns_files = this.groupByNamespaces(this.result.files);
return this.copyResources(this.resources_dir, this.options.output_dir, (function(_this) {
return function() {
_this.renderReadme();
_this.renderGuides();
_this.renderPages();
_this.renderRESTApis();
_this.renderClasses();
_this.renderModules();
_this.renderFeatures();
return _this.renderFiles();
};
})(this));
};
return Renderer;
})();
render = function(result, options) {
var renderer;
renderer = new Renderer(result, options);
return renderer.run();
};
module.exports = render;
}).call(this);