buttons
Version:
How many ways are there to build a button with Assemble? Many.
65 lines (57 loc) • 1.86 kB
JavaScript
/**
* {{prettify}} by Jon Schlinkert
* http://github.com/jonschlinkert
* Copyright (c) 2013 Jon Schlinkert
* MIT License
*/
var _ = require('lodash');
module.exports.register = function(Handlebars, options) {
var prettify = require('js-beautify').html;
var assembleOptions = options;
/**
* Prettify HTML output
* @example:
* {{#prettify indent="2"}}
* {{> body }}
* {{/prettify}}
*/
Handlebars.registerHelper('prettify', function (options) {
var hash = _.extend({}, assembleOptions.prettify, options.hash);
var content = prettifyHTML(options.fn(this), hash);
// Reduce multiple newlines to a single newline
if(assembleOptions.prettify.condense === true) {
content = content.replace(/(\n|\r){2,}/g, '\n');
}
// Add a single newline above code comments.
if(assembleOptions.prettify.newlines === true) {
content = content.replace(/(\s*<!--)/g, '\n$1');
}
return content;
});
/**
* Default options passed to js-beautify.
* @param {hash arguments} [Options received as hash arguments will override defaults.]
* @param {task options} [Options defined in the task/target override hash arguments.]
*/
var defaults = {
indent_size: 2,
indent_inner_html: true,
unformatted: ['code', 'pre']
};
defaults = _.extend(assembleOptions.prettify, defaults);
defaults.indent_size = defaults.indent;
/**
* Format HTML with js-beautify, pass in options.
* @param {String} source [The un-prettified HTML.]
* @param {Object} options [Object of options passed to js-beautify.]
* @returns {String} [Stunning HTML.]
*/
var prettifyHTML = function(source, options) {
try {
return prettify(source, options);
} catch (e) {
console.error(e);
console.warn('HTML beautification failed.');
}
};
};