-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
63 lines (49 loc) · 1.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* {{prettify}} by Jon Schlinkert
* http://github.com/helpers/prettify
*
* Copyright (c) 2013 Jon Schlinkert
* MIT License
*/
// node_modules
var _ = require('lodash');
var prettifyHTML = require('js-beautify').html;
// Local utils.
var defaults = require('./lib/defaults');
var Utils = require('./lib/utils');
module.exports.register = function (Handlebars, options, params) {
'use strict';
var opts = options || {};
opts.prettify = opts.prettify || {};
Handlebars.registerHelper('prettify', function(options) {
options = options || {};
var hash = options.hash || {};
// Pass an object to the hash. E.g: `{{#prettify opts=foo}}
hash.opts = hash.opts || {};
// Define the mode in which to run prettify: 'html', 'css' or 'js'
var mode = hash.mode || 'html';
if(mode === 'css' || mode === 'js') {
opts.prettify = opts.prettify[mode] || {};
}
// Extend options
options = _.extend(options, defaults[mode], opts.prettify, hash, hash.opts);
var content = options.fn(this);
// Alias for indent_size
options.indent_size = options.indent;
// Run js-beautify before cleaning up newlines below.
content = require('js-beautify')[mode](content, options);
// Clean up newlines, spacing
if(options.condense === true || options.condense === 'true') {
content = Utils.condense(content);
}
if(options.padcomments === true || options.padcomments === 'true') {
content = Utils.padcomments(content);
}
if (mode === 'html') {
if(options.fixspaces === true || options.fixspaces === 'true') {
content = Utils.fixspaces(content);
}
}
return new Handlebars.SafeString(content);
});
};