forked from PhilJ/gulp-kss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlebarHelpers.js
185 lines (156 loc) · 5.33 KB
/
handlebarHelpers.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var kss = require('kss'),
fs = require('fs');
module.exports = function (handlebars, styleguide, markupDirectory) {
/**
* Equivalent to the {#if} block helper with multiple arguments.
*/
handlebars.registerHelper('ifAny', function() {
var argLength = arguments.length - 2,
content = arguments[argLength + 1],
success = true;
for (var i = 0; i < argLength; i += 1) {
if (!arguments[i]) {
success = false;
break;
}
}
return success ? content.fn(this) : content.inverse(this);
});
/**
* Returns a single section, found by its reference number
* @param {String|Number} reference The reference number to search for.
*/
handlebars.registerHelper('section', function(reference) {
var section = styleguide.section(reference);
if (!section) return false;
return arguments[arguments.length-1](section.data);
});
/**
* Loop over a section query. If a number is supplied, will convert into
* a query for all children and descendants of that reference.
* @param {Mixed} query The section query
*/
handlebars.registerHelper('eachSection', function(query) {
var sections,
i, l, buffer = "";
query = (typeof query === 'string') ? query : query.toString();
if (!query.match(/x|\*/g)) {
query = new RegExp('^' + query + '$|^' + query + "\\..*");
}
sections = styleguide.section(query);
if (!sections) return '';
l = sections.length;
for (i = 0; i < l; i += 1) {
buffer += arguments[arguments.length-1].fn(sections[i].data);
}
return buffer;
});
/**
* Loop over each section root, i.e. each section only one level deep.
*/
handlebars.registerHelper('eachRoot', function() {
var sections,
i, l, buffer = "";
sections = styleguide.section('x');
if (!sections) return '';
l = sections.length;
for (i = 0; i < l; i += 1) {
buffer += arguments[arguments.length-1].fn(sections[i].data);
}
return buffer;
});
/**
* Equivalent to "if the current section is X levels deep". e.g:
*
* {{#refDepth 1}}
* ROOT ELEMENTS ONLY
* {{else}}
* ANYTHING ELSE
* {{/refDepth}}
*/
handlebars.registerHelper('whenDepth', function(depth, context) {
if (!(context && this.refDepth)) {
return '';
}
if (depth == this.refDepth) {
return context.fn(this);
}
if (context.inverse) {
return context.inverse(this);
}
});
/**
* Similar to the {#eachSection} helper, however will loop over each modifier
* @param {Object} section Supply a section object to loop over it's modifiers. Defaults to the current section.
*/
handlebars.registerHelper('eachModifier', function(section) {
var modifiers, i, l, buffer = '';
// Default to current modifiers, but allow supplying a custom section
if (section.data) modifiers = section.data.modifiers;
modifiers = modifiers || this.modifiers || false;
if (!modifiers) return {};
l = modifiers.length;
for (i = 0; i < l; i++) {
buffer += arguments[arguments.length-1].fn(modifiers[i].data || '');
}
return buffer;
});
/**
* Outputs a modifier's markup, if possible.
* @param {Object} modifier Specify a particular modifier object. Defaults to the current modifier.
*/
handlebars.registerHelper('modifierMarkup', function(modifier) {
modifier = arguments.length < 2 ? this : modifier || this || false;
if (!modifier) {
return false;
}
modifier.markup = getMarkup(modifier.markup);
// Maybe it's actually a section?
if (modifier.modifiers) {
return new handlebars.SafeString(
modifier.markup
);
}
// Otherwise return the modifier markup
return new handlebars.SafeString(
new kss.KssModifier(modifier).markup()
);
});
/**
* Use Markup from a given template file. To detect any template files use
* the following convention in your markup section:
*
* @example
* // Markup:
* // includeTemplate path/to/your/file.html
*
* @param {String} markup the markup to check against the pattern
*/
function getMarkup(markup) {
var data = '',
file,
pattern = /includeTemplate\s(.*\.html)/;
if (markupDirectory && pattern.exec(markup)) {
file = process.cwd() + '/' + markupDirectory + '/' + RegExp.$1;
try {
data = fs.readFileSync(file, 'utf8');
} catch(err) {
console.log(err.message);
}
return data;
} else {
return markup;
}
}
handlebars.registerHelper('templatedMarkup', function(markup) {
return getMarkup(markup);
});
/**
* Quickly avoid escaping strings
* @param {String} arg The unescaped HTML
*/
handlebars.registerHelper('html', function(arg) {
return new handlebars.SafeString(arg || '');
});
return handlebars;
};