From 892fe9ce7d96dff42b3cc82f640a85a48bbe444a Mon Sep 17 00:00:00 2001 From: Manuel Mujica Date: Mon, 6 Feb 2017 20:26:05 -0700 Subject: [PATCH] Add support to beautify html through js-beautify --- package.json | 1 + write/beautify_html.js | 41 +++++++++++++++++++++++++++++++++++++++++ write/doc_object.js | 23 ++++++++++++++--------- 3 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 write/beautify_html.js diff --git a/package.json b/package.json index 8aa06aa..406721b 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "escape-html": "^1.0.3", "fs-extra": "0.30.0", "handlebars": "1.X", + "js-beautify": "^1.6.8", "lodash": "~4.13.1", "md5": "2.1.0", "steal-tools": "0.16.X", diff --git a/write/beautify_html.js b/write/beautify_html.js new file mode 100644 index 0000000..6968657 --- /dev/null +++ b/write/beautify_html.js @@ -0,0 +1,41 @@ +var _ = require("lodash"); + +/** + * Returns js-beautify options object + * + * @param {{}} siteConfig The site configuration object + * @returns {{}} The js-beautify options object + * + * Options can be provided through the `beautifyHtml` object, if no + * options were provided some defaults are set. + */ +function getOptions(siteConfig) { + var opts = _.isObject(siteConfig.beautifyHtml) ? + siteConfig.beautifyHtml : {}; + + return _.defaults(opts, { + "indent_size": 2, + "max_preserve_newlines": 1, + "indent_scripts": "keep" + }); +} + +/** + * Beautifies html through js-beautify + * + * @param {string} rendered The rendered html string + * @param {{}} The site configuration object + */ +module.exports = function(rendered, siteConfig) { + var beautify = siteConfig.beautifyHtml === true || + _.isObject(siteConfig.beautifyHtml); + + if (beautify) { + var beautifier = require("js-beautify").html; + + return beautifier(rendered, getOptions(siteConfig)); + } + else { + return rendered; + } +}; diff --git a/write/doc_object.js b/write/doc_object.js index 786e979..a030da1 100644 --- a/write/doc_object.js +++ b/write/doc_object.js @@ -4,7 +4,9 @@ var _ = require("lodash"), fs = require("fs"), writeFile = Q.denodeify(fs.writeFile), path = require("path"); - mkdirs = Q.denodeify(require("fs-extra").mkdirs); + mkdirs = Q.denodeify(require("fs-extra").mkdirs), + beautifyHtml = require("./beautify_html"); + /** * @function documentjs.generators.html.write.docObject * @parent documentjs.generators.html.write.methods @@ -28,9 +30,9 @@ var _ = require("lodash"), * @return {Promise} A promise that resolves when the file has been written out. */ module.exports = function(docObject, renderer, siteConfig, setCurrentDocObjectForHelpers){ - - var out = path.join(siteConfig.dest, filename(docObject, siteConfig) ); + var out = path.join(siteConfig.dest, filename(docObject, siteConfig)); var rendered; + if(siteConfig.debug) { console.log('OUT: ' + path.relative(process.cwd(),out) ); } @@ -38,15 +40,18 @@ module.exports = function(docObject, renderer, siteConfig, setCurrentDocObjectFo // render the content setCurrentDocObjectForHelpers(docObject); - if(docObject.renderer) { + if (docObject.renderer) { rendered = docObject.renderer(docObject, renderer); } else { rendered = renderer(docObject); } - return writeFile(out, rendered).catch(function(){ - return mkdirs(path.dirname(out)).then(function(){ - return writeFile(out, rendered); - }); - }); + var output = beautifyHtml(rendered, siteConfig); + + return writeFile(out, output) + .catch(function() { + return mkdirs(path.dirname(out)).then(function() { + return writeFile(out, output); + }); + }); };