Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to beautify html through js-beautify #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
41 changes: 41 additions & 0 deletions write/beautify_html.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
23 changes: 14 additions & 9 deletions write/doc_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,25 +30,28 @@ 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) );
}

// 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);
});
});
};