From 13ee09e3cc3ca6eeceeb8636fee2909bc2257b47 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Fri, 10 Jul 2015 15:03:48 -0400 Subject: [PATCH] Refactor themes, move markdown theme to module --- streams/output/html.js | 32 ++++----------------- streams/output/lib/get_template.js | 22 +++++++++++++++ streams/output/lib/resolve_theme.js | 20 +++++++++++++ streams/output/markdown.js | 15 +++++----- streams/output/share/markdown.hbs | 44 ----------------------------- 5 files changed, 56 insertions(+), 77 deletions(-) create mode 100644 streams/output/lib/get_template.js create mode 100644 streams/output/lib/resolve_theme.js delete mode 100644 streams/output/share/markdown.hbs diff --git a/streams/output/html.js b/streams/output/html.js index ef2860a..d6ab54d 100644 --- a/streams/output/html.js +++ b/streams/output/html.js @@ -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) { diff --git a/streams/output/lib/get_template.js b/streams/output/lib/get_template.js new file mode 100644 index 0000000..4f3b1f0 --- /dev/null +++ b/streams/output/lib/get_template.js @@ -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'); + } +}; diff --git a/streams/output/lib/resolve_theme.js b/streams/output/lib/resolve_theme.js new file mode 100644 index 0000000..35652d8 --- /dev/null +++ b/streams/output/lib/resolve_theme.js @@ -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; diff --git a/streams/output/markdown.js b/streams/output/markdown.js index dbd87ab..6b8658f 100644 --- a/streams/output/markdown.js +++ b/streams/output/markdown.js @@ -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); diff --git a/streams/output/share/markdown.hbs b/streams/output/share/markdown.hbs deleted file mode 100644 index c090a99..0000000 --- a/streams/output/share/markdown.hbs +++ /dev/null @@ -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}}