|
| 1 | +/* |
| 2 | + * twig 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. Twig does not support |
| 15 | + * the mustache-specific syntax extensions, style modifiers and pattern |
| 16 | + * parameters, because their use cases are addressed by the core Twig feature |
| 17 | + * set. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +"use strict"; |
| 22 | + |
| 23 | +var Twig = require('twig'); |
| 24 | +var twig = Twig.twig; |
| 25 | + |
| 26 | +var engine_twig = { |
| 27 | + engine: Twig, |
| 28 | + engineName: 'twig', |
| 29 | + engineFileExtension: '.twig', |
| 30 | + |
| 31 | + //Important! Needed for Twig compilation. Can't resolve paths otherwise. |
| 32 | + expandPartials: true, |
| 33 | + |
| 34 | + // regexes, stored here so they're only compiled once |
| 35 | + findPartialsRE: /{%\s*(?:extends|include|embed)\s+('[^']+'|"[^"]+").*?%}/g, |
| 36 | + findPartialKeyRE: /"((?:\\.|[^"\\])*)"/, |
| 37 | + 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, // TODO |
| 38 | + |
| 39 | + // render it |
| 40 | + renderPattern: function renderPattern(pattern, data) { |
| 41 | + var result = twig({ |
| 42 | + data: pattern.extendedTemplate |
| 43 | + }).render(data); |
| 44 | + |
| 45 | + return result; |
| 46 | + }, |
| 47 | + |
| 48 | + // find and return any {% include 'template-name' %} within pattern |
| 49 | + findPartials: function findPartials(pattern) { |
| 50 | + var matches = pattern.template.match(this.findPartialsRE); |
| 51 | + return matches; |
| 52 | + }, |
| 53 | + findPartialsWithStyleModifiers: function () { |
| 54 | + // TODO: make the call to this from oPattern objects conditional on their |
| 55 | + // being implemented here. |
| 56 | + return []; |
| 57 | + }, |
| 58 | + |
| 59 | + // returns any patterns that match {{> value(foo:"bar") }} or {{> |
| 60 | + // value:mod(foo:"bar") }} within the pattern |
| 61 | + findPartialsWithPatternParameters: function () { |
| 62 | + // TODO: make the call to this from oPattern objects conditional on their |
| 63 | + // being implemented here. |
| 64 | + return []; |
| 65 | + }, |
| 66 | + findListItems: function (pattern) { |
| 67 | + var matches = pattern.template.match(this.findListItemsRE); |
| 68 | + return matches; |
| 69 | + }, |
| 70 | + |
| 71 | + // given a pattern, and a partial string, tease out the "pattern key" and |
| 72 | + // return it. |
| 73 | + findPartial: function (partialString) { |
| 74 | + //var partialKey = partialString.replace(this.findPartialsRE, '$1'); |
| 75 | + var partial = partialString.match(this.findPartialKeyRE)[0]; |
| 76 | + partial = partial.replace(/"/g, ''); |
| 77 | + |
| 78 | + return partial; |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +module.exports = engine_twig; |
0 commit comments