|
| 1 | +/* |
| 2 | + * handlebars pattern engine for patternlab-node - v0.15.1 - 2015 |
| 3 | + * |
| 4 | + * Geoffrey Pursell, Brian Muenzenmeyer, and the web community. |
| 5 | + * Licensed under the MIT license. |
| 6 | + * |
| 7 | + * Many thanks to Brad Frost and Dave Olsen for inspiration, encouragement, and advice. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +/* |
| 12 | + * ENGINE SUPPORT LEVEL: |
| 13 | + * |
| 14 | + * Full. Partial calls and lineage hunting are supported. Handlebars does not |
| 15 | + * support the mustache-specific syntax extensions, style modifiers and pattern |
| 16 | + * parameters, because their use cases are addressed by the core Handlebars |
| 17 | + * feature set. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +"use strict"; |
| 22 | + |
| 23 | +var Handlebars = require('handlebars'); |
| 24 | + |
| 25 | +var engine_handlebars = { |
| 26 | + engine: Handlebars, |
| 27 | + engineName: 'handlebars', |
| 28 | + engineFileExtension: '.hbs', |
| 29 | + |
| 30 | + // partial expansion is only necessary for Mustache templates that have |
| 31 | + // style modifiers or pattern parameters (I think) |
| 32 | + expandPartials: false, |
| 33 | + |
| 34 | + // regexes, stored here so they're only compiled once |
| 35 | + findPartialsRE: /{{#?>\s*([\w-\/.]+)(?:.|\s+)*?}}/g, |
| 36 | + 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, |
| 37 | + |
| 38 | + // render it |
| 39 | + renderPattern: function renderPattern(template, data, partials) { |
| 40 | + if (partials) { |
| 41 | + Handlebars.registerPartial(partials); |
| 42 | + } |
| 43 | + var compiled = Handlebars.compile(template); |
| 44 | + return compiled(data); |
| 45 | + }, |
| 46 | + |
| 47 | + registerPartial: function (oPattern) { |
| 48 | + Handlebars.registerPartial(oPattern.key, oPattern.template); |
| 49 | + }, |
| 50 | + |
| 51 | + // find and return any {{> template-name }} within pattern |
| 52 | + findPartials: function findPartials(pattern) { |
| 53 | + var matches = pattern.template.match(this.findPartialsRE); |
| 54 | + return matches; |
| 55 | + }, |
| 56 | + findPartialsWithStyleModifiers: function () { |
| 57 | + // TODO: make the call to this from oPattern objects conditional on their |
| 58 | + // being implemented here. |
| 59 | + return []; |
| 60 | + }, |
| 61 | + |
| 62 | + // returns any patterns that match {{> value(foo:"bar") }} or {{> |
| 63 | + // value:mod(foo:"bar") }} within the pattern |
| 64 | + findPartialsWithPatternParameters: function () { |
| 65 | + // TODO: make the call to this from oPattern objects conditional on their |
| 66 | + // being implemented here. |
| 67 | + return []; |
| 68 | + }, |
| 69 | + findListItems: function (pattern) { |
| 70 | + var matches = pattern.template.match(this.findListItemsRE); |
| 71 | + return matches; |
| 72 | + }, |
| 73 | + |
| 74 | + // given a pattern, and a partial string, tease out the "pattern key" and |
| 75 | + // return it. |
| 76 | + findPartialKey: function (partialString) { |
| 77 | + var partialKey = partialString.replace(this.findPartialsRE, '$1'); |
| 78 | + return partialKey; |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +module.exports = engine_handlebars; |
0 commit comments