Refactor themes, move markdown theme to module

This commit is contained in:
Tom MacWright 2015-07-10 15:03:48 -04:00
parent b3b3e10852
commit 13ee09e3cc
5 changed files with 56 additions and 77 deletions

View File

@ -1,15 +1,15 @@
'use strict';
var fs = require('fs');
var through2 = require('through2'),
File = require('vinyl'),
vfs = require('vinyl-fs'),
path = require('path'),
Handlebars = require('handlebars'),
extend = require('extend'),
slugg = require('slugg'),
splicer = require('stream-splicer'),
hierarchy = require('../hierarchy'),
getTemplate = require('./lib/get_template'),
resolveTheme = require('./lib/resolve_theme'),
helpers = require('./lib/html_helpers'),
highlight = require('../highlight');
@ -24,19 +24,6 @@ function slug(p) {
return p ? slugg(p) : '';
}
/**
* Get a Handlebars template file out of a theme and compile it into
* a template function
*
* @param {string} themeModule base directory of themey
* @param {string} name template name
* @returns {Function} template function
*/
function getTemplate(themeModule, name) {
return Handlebars
.compile(fs.readFileSync(path.join(themeModule, name), 'utf8'));
}
/**
* Create a transform stream that formats documentation as HTML.
* Receives parsed & pivoted stream of documentation data, and emits
@ -53,18 +40,11 @@ module.exports = function (opts) {
theme: 'documentation-theme-default'
}, opts);
try {
var themeModule = path.dirname(require.resolve(options.theme));
} catch(e) {
throw new Error('Theme ' + options.theme + ' not found');
}
var themeModule = resolveTheme(options.theme);
try {
var pageTemplate = getTemplate(themeModule, 'index.hbs');
Handlebars.registerPartial('section', getTemplate(themeModule, 'section.hbs'));
} catch(e) {
throw new Error('Template file (index.hbs, section.hbs) missing');
}
var pageTemplate = getTemplate(Handlebars, themeModule, 'index.hbs');
Handlebars.registerPartial('section',
getTemplate(Handlebars, themeModule, 'section.hbs'));
var htmlStream = through2.obj(function (comments, enc, callback) {

View File

@ -0,0 +1,22 @@
'use strict';
var fs = require('fs'),
path = require('path');
/**
* Get a Handlebars template file out of a theme and compile it into
* a template function
*
* @param {Object} Handlebars handlebars instance
* @param {string} themeModule base directory of themey
* @param {string} name template name
* @returns {Function} template function
*/
module.exports = function getTemplate(Handlebars, themeModule, name) {
try {
return Handlebars
.compile(fs.readFileSync(path.join(themeModule, name), 'utf8'));
} catch(e) {
throw new Error('Template file ' + name + ' missing');
}
};

View File

@ -0,0 +1,20 @@
'use strict';
var path = require('path');
/**
* Given the name of a theme as a module, return the directory it
* resides in, or throw an error if it is not found
* @param {string} theme the module name
* @throws {Error} if theme is not found
* @returns {string} directory
*/
function resolveTheme(theme) {
try {
return path.dirname(require.resolve(theme));
} catch(e) {
throw new Error('Theme ' + theme + ' not found');
}
}
module.exports = resolveTheme;

View File

@ -1,9 +1,10 @@
'use strict';
var fs = require('fs');
var through2 = require('through2'),
path = require('path'),
extend = require('extend'),
getTemplate = require('./lib/get_template'),
helpers = require('./lib/markdown_helpers'),
resolveTheme = require('./lib/resolve_theme'),
Handlebars = require('handlebars');
/**
@ -20,11 +21,11 @@ var through2 = require('through2'),
*/
module.exports = function (opts) {
var templateStr = (opts && opts.template) ?
fs.readFileSync(opts.template, 'utf8') :
fs.readFileSync(path.join(__dirname, '/share/markdown.hbs'), 'utf8');
var template = Handlebars.compile(templateStr);
var options = extend({}, {
theme: 'documentation-theme-default'
}, opts);
var themeModule = resolveTheme(options.theme);
var template = getTemplate(Handlebars, themeModule, 'markdown.hbs');
helpers(Handlebars);

View File

@ -1,44 +0,0 @@
## `{{name}}`
{{#description}}
{{inlines .}}
{{/description}}
{{#if params}}
### Parameters
{{#params}}
* `{{name}}` **{{format_type type}}** {{format_description description}}{{#default}} (optional, default `{{.}}`){{/default}}
{{/params}}
{{/if}}
{{#if properties}}
| name | type | description |
| ---- | ---- | ----------- |
{{#properties}}
| `{{name}}` | {{format_type type}} | {{format_description description}} |
{{/properties}}
{{/if}}
{{#if examples}}
### Examples
{{#each examples ~}}
```js
{{{.}}}
```
{{/each}}
{{/if}}
{{#returns}}
Returns {{#type.name}}`{{.}}`{{/type.name}} {{inlines description}}
{{/returns}}
{{#if throws}}
| type | description |
| ---- | ----------- |
{{#throws}}
| {{format_type type}} | {{format_description description}} |
{{/throws}}
{{/if}}