This repository was archived by the owner on Dec 10, 2019. It is now read-only.
forked from geoffp/patternengine-node-handlebars
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathengine_handlebars.js
99 lines (86 loc) · 3.17 KB
/
engine_handlebars.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
/*
* handlebars pattern engine for patternlab-node - v0.15.1 - 2015
*
* Geoffrey Pursell, Brian Muenzenmeyer, and the web community.
* Licensed under the MIT license.
*
* Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice.
*
*/
/*
* ENGINE SUPPORT LEVEL:
*
* Full. Partial calls and lineage hunting are supported. Handlebars does not
* support the mustache-specific syntax extensions, style modifiers and pattern
* parameters, because their use cases are addressed by the core Handlebars
* feature set.
*
*/
"use strict";
var Handlebars = require('handlebars');
var engine_handlebars = {
engine: Handlebars,
engineName: 'handlebars',
engineFileExtension: '.hbs',
// partial expansion is only necessary for Mustache templates that have
// style modifiers or pattern parameters (I think)
expandPartials: false,
// regexes, stored here so they're only compiled once
findPartialsRE: /{{#?>\s*([\w-\/.]+)(?:.|\s+)*?}}/g,
findListItemsRE: /({{#( )?)(list(I|i)tems.)(one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty)( )?}}/g,
// render it
renderPattern: function renderPattern(pattern, data, partials) {
if (partials) {
Handlebars.registerPartial(partials);
}
var compiled = Handlebars.compile(pattern.extendedTemplate);
return compiled(data);
},
registerPartial: function (pattern) {
Handlebars.registerPartial(pattern.patternPartial, pattern.template);
},
/**
* Find regex matches within both pattern strings and pattern objects.
*
* @param {string|object} pattern Either a string or a pattern object.
* @param {object} regex A JavaScript RegExp object.
* @returns {array|null} An array if a match is found, null if not.
*/
patternMatcher: function patternMatcher(pattern, regex) {
var matches;
if (typeof pattern === 'string') {
matches = pattern.match(regex);
} else if (typeof pattern === 'object' && typeof pattern.extendedTemplate === 'string') {
matches = pattern.extendedTemplate.match(regex);
}
return matches;
},
// find and return any {{> template-name }} within pattern
findPartials: function findPartials(pattern) {
var matches = this.patternMatcher(pattern, this.findPartialsRE);
return matches;
},
findPartialsWithStyleModifiers: function () {
// TODO: make the call to this from oPattern objects conditional on their
// being implemented here.
return [];
},
// returns any patterns that match {{> value(foo:"bar") }} or {{>
// value:mod(foo:"bar") }} within the pattern
findPartialsWithPatternParameters: function () {
// TODO: make the call to this from oPattern objects conditional on their
// being implemented here.
return [];
},
findListItems: function (pattern) {
var matches = this.patternMatcher(pattern, this.findListItemsRE);
return matches;
},
// given a pattern, and a partial string, tease out the "pattern key" and
// return it.
findPartial: function (partialString) {
var partial = partialString.replace(this.findPartialsRE, '$1');
return partial;
}
};
module.exports = engine_handlebars;