aglio-theme-olio
Version:
Default theme for the Aglio API Blueprint renderer
401 lines (379 loc) • 12.3 kB
JavaScript
// Generated by CoffeeScript 1.9.2
(function() {
var ROOT, benchmark, cache, crypto, decorate, fs, getCached, getCss, getTemplate, highlight, hljs, jade, less, markdownIt, md, moment, path, slug;
crypto = require('crypto');
fs = require('fs');
hljs = require('highlight.js');
jade = require('jade');
less = require('less');
markdownIt = require('markdown-it');
moment = require('moment');
path = require('path');
ROOT = path.dirname(__dirname);
cache = {};
benchmark = {
start: function(message) {
if (process.env.BENCHMARK) {
return console.time(message);
}
},
end: function(message) {
if (process.env.BENCHMARK) {
return console.timeEnd(message);
}
}
};
slug = function(value) {
if (value == null) {
value = '';
}
return value.toLowerCase().replace(/[ \t\n\\:\/]/g, '-');
};
highlight = function(code, lang, subset) {
var response;
benchmark.start("highlight " + lang);
response = (function() {
switch (lang) {
case 'no-highlight':
return code;
case void 0:
case null:
case '':
return hljs.highlightAuto(code, subset).value;
default:
return hljs.highlight(lang, code).value;
}
})();
benchmark.end("highlight " + lang);
return response;
};
md = markdownIt({
html: true,
linkify: true,
typographer: true,
highlight: highlight
}).use(require('markdown-it-anchor'), {
slugify: function(value) {
return "header-" + (slug(value));
},
permalink: true,
permalinkClass: 'permalink'
});
getCached = function(key, compiledPath, sources, load, done) {
var compiledStats, err, i, len, source, sourceStats;
if (cache[key]) {
return done(null, cache[key]);
}
try {
if (fs.existsSync(compiledPath)) {
compiledStats = fs.statSync(compiledPath);
for (i = 0, len = sources.length; i < len; i++) {
source = sources[i];
sourceStats = fs.statSync(source);
if (sourceStats.mtime > compiledStats.mtime) {
return done(null);
}
}
return load(compiledPath, function(err, item) {
if (err) {
return done(err);
}
cache[key] = item;
return done(null, cache[key]);
});
} else {
return done(null);
}
} catch (_error) {
err = _error;
return done(err);
}
};
getCss = function(variables, style, done) {
var compiledPath, customColorPath, defaultColorPath, key, load, sources, stylePath;
key = "css-" + variables + "-" + style;
if (cache[key]) {
return done(null, cache[key]);
}
compiledPath = path.join(ROOT, 'cache', (slug(variables)) + "-" + (slug(style)) + ".css");
defaultColorPath = path.join(ROOT, 'styles', 'variables-default.less');
sources = [defaultColorPath];
customColorPath = null;
if (variables !== 'default') {
customColorPath = path.join(ROOT, 'styles', "variables-" + variables + ".less");
if (!fs.existsSync(customColorPath)) {
customColorPath = variables;
if (!fs.existsSync(customColorPath)) {
return done(new Error(customColorPath + " does not exist!"));
}
}
sources.push(customColorPath);
}
stylePath = path.join(ROOT, 'styles', "layout-" + style + ".less");
if (!fs.existsSync(stylePath)) {
stylePath = style;
if (!fs.existsSync(stylePath)) {
return done(new Error(stylePath + " does not exist!"));
}
}
sources.push(stylePath);
load = function(filename, loadDone) {
return fs.readFile(filename, 'utf-8', loadDone);
};
return getCached(key, compiledPath, sources, load, function(err, css) {
var tmp;
if (err) {
return done(err);
}
if (css) {
return done(null, css);
}
tmp = "@import \"" + defaultColorPath + "\";\n";
if (customColorPath) {
tmp += "@import \"" + customColorPath + "\";\n";
}
tmp += "@import \"" + stylePath + "\";\n";
benchmark.start('less-compile');
return less.render(tmp, {
compress: true
}, function(err, result) {
var writeErr;
if (err) {
return done(err);
}
try {
css = result.css;
fs.writeFileSync(compiledPath, css, 'utf-8');
} catch (_error) {
writeErr = _error;
return done(writeErr);
}
benchmark.end('less-compile');
cache[key] = css;
return done(null, cache[key]);
});
});
};
getTemplate = function(name, done) {
var compiledPath, key, load;
key = "template-" + name;
if (cache[key]) {
return done(null, cache[key]);
}
compiledPath = path.join(ROOT, 'cache', (slug(name)) + ".js");
load = function(filename, loadDone) {
return loadDone(null, require(filename));
};
return getCached(key, compiledPath, [name], load, function(err, template) {
var compileErr, compileOptions, compiled;
if (err) {
return done(err);
}
if (template) {
return done(null, template);
}
benchmark.start('jade-compile');
compileOptions = {
filename: name,
name: 'compiledFunc',
self: true,
compileDebug: false
};
try {
compiled = "var jade = require('jade/runtime');\n" + (jade.compileFileClient(name, compileOptions)) + "\nmodule.exports = compiledFunc;";
} catch (_error) {
compileErr = _error;
return done(compileErr);
}
fs.writeFileSync(compiledPath, compiled, 'utf-8');
benchmark.end('jade-compile');
cache[key] = require(compiledPath);
return done(null, cache[key]);
});
};
decorate = function(api) {
var action, err, example, i, item, len, name, ref, resource, resourceGroup, results;
ref = api.resourceGroups || [];
results = [];
for (i = 0, len = ref.length; i < len; i++) {
resourceGroup = ref[i];
resourceGroup.elementId = slug(resourceGroup.name);
resourceGroup.elementLink = "#" + resourceGroup.elementId;
results.push((function() {
var j, len1, ref1, results1;
ref1 = resourceGroup.resources || [];
results1 = [];
for (j = 0, len1 = ref1.length; j < len1; j++) {
resource = ref1[j];
resource.elementId = slug(resourceGroup.name + "-" + resource.name);
resource.elementLink = "#" + resource.elementId;
results1.push((function() {
var k, len2, ref2, results2;
ref2 = resource.actions || [];
results2 = [];
for (k = 0, len2 = ref2.length; k < len2; k++) {
action = ref2[k];
action.elementId = slug(resourceGroup.name + "-" + resource.name + "-" + action.method);
action.elementLink = "#" + action.elementId;
action.methodLower = action.method.toLowerCase();
if (!action.parameters || !action.parameters.length) {
action.parameters = resource.parameters;
}
results2.push((function() {
var l, len3, ref3, results3;
ref3 = action.examples || [];
results3 = [];
for (l = 0, len3 = ref3.length; l < len3; l++) {
example = ref3[l];
results3.push((function() {
var len4, m, ref4, results4;
ref4 = ['requests', 'responses'];
results4 = [];
for (m = 0, len4 = ref4.length; m < len4; m++) {
name = ref4[m];
results4.push((function() {
var len5, n, ref5, results5;
ref5 = example[name] || [];
results5 = [];
for (n = 0, len5 = ref5.length; n < len5; n++) {
item = ref5[n];
item.hasContent = item.description || Object.keys(item.headers).length || item.body || item.schema;
try {
if (item.body) {
item.body = JSON.stringify(JSON.parse(item.body), null, 2);
}
if (item.schema) {
results5.push(item.schema = JSON.stringify(JSON.parse(item.schema), null, 2));
} else {
results5.push(void 0);
}
} catch (_error) {
err = _error;
results5.push(false);
}
}
return results5;
})());
}
return results4;
})());
}
return results3;
})());
}
return results2;
})());
}
return results1;
})());
}
return results;
};
exports.getConfig = function() {
return {
formats: ['1A'],
options: [
{
name: 'variables',
description: 'Color scheme name or path to custom variables',
"default": 'default'
}, {
name: 'condense-nav',
description: 'Condense navigation links',
boolean: true,
"default": true
}, {
name: 'full-width',
description: 'Use full window width',
boolean: true,
"default": false
}, {
name: 'template',
description: 'Template name or path to custom template',
"default": 'default'
}, {
name: 'style',
description: 'Layout style name or path to custom stylesheet'
}
]
};
};
exports.render = function(input, options, done) {
if (done == null) {
done = options;
options = {};
}
if (options.condenseNav) {
options.themeCondenseNav = options.condenseNav;
}
if (options.fullWidth) {
options.themeFullWidth = options.fullWidth;
}
if (options.themeVariables == null) {
options.themeVariables = 'default';
}
if (options.themeStyle == null) {
options.themeStyle = 'default';
}
if (options.themeTemplate == null) {
options.themeTemplate = 'default';
}
if (options.themeCondenseNav == null) {
options.themeCondenseNav = true;
}
if (options.themeFullWidth == null) {
options.themeFullWidth = false;
}
if (options.themeTemplate === 'default') {
options.themeTemplate = path.join(ROOT, 'templates', 'index.jade');
}
benchmark.start('decorate');
decorate(input);
benchmark.end('decorate');
benchmark.start('css-total');
return getCss(options.themeVariables, options.themeStyle, function(err, css) {
var key, locals, ref, value;
if (err) {
return done(err);
}
benchmark.end('css-total');
locals = {
api: input,
condenseNav: options.themeCondenseNav,
css: css,
fullWidth: options.themeFullWidth,
date: moment,
hash: function(value) {
return crypto.createHash('md5').update(value.toString()).digest('hex');
},
highlight: highlight,
markdown: function(content) {
return md.render(content);
},
slug: slug
};
ref = options.locals || {};
for (key in ref) {
value = ref[key];
locals[key] = value;
}
benchmark.start('get-template');
return getTemplate(options.themeTemplate, function(getTemplateErr, renderer) {
var html;
if (getTemplateErr) {
return done(getTemplateErr);
}
benchmark.end('get-template');
benchmark.start('call-template');
try {
html = renderer(locals);
} catch (_error) {
err = _error;
return done(err);
}
benchmark.end('call-template');
return done(null, html);
});
});
};
}).call(this);