|
| 1 | +/* |
| 2 | + * mustache pattern engine for patternlab-node - v2.X.X - 2016 |
| 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 + extensions. Partial calls and lineage hunting are supported. Style |
| 15 | + * modifiers and pattern parameters are used to extend the core feature set of |
| 16 | + * Mustache templates. |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +"use strict"; |
| 21 | + |
| 22 | +var Mustache = require('mustache'); |
| 23 | +var utilMustache = require('./util_mustache'); |
| 24 | + |
| 25 | +var engine_mustache = { |
| 26 | + engine: Mustache, |
| 27 | + engineName: 'mustache', |
| 28 | + engineFileExtension: '.mustache', |
| 29 | + |
| 30 | + // partial expansion is only necessary for Mustache templates that have |
| 31 | + // style modifiers or pattern parameters (I think) |
| 32 | + expandPartials: true, |
| 33 | + |
| 34 | + // regexes, stored here so they're only compiled once |
| 35 | + findPartialsRE: utilMustache.partialsRE, |
| 36 | + findPartialsWithStyleModifiersRE: utilMustache.partialsWithStyleModifiersRE, |
| 37 | + findPartialsWithPatternParametersRE: utilMustache.partialsWithPatternParametersRE, |
| 38 | + findListItemsRE: utilMustache.listItemsRE, |
| 39 | + findPartialRE: utilMustache.partialRE, |
| 40 | + |
| 41 | + // render it |
| 42 | + renderPattern: function renderPattern(pattern, data, partials) { |
| 43 | + try { |
| 44 | + if (partials) { |
| 45 | + return Mustache.render(pattern.extendedTemplate, data, partials); |
| 46 | + } |
| 47 | + return Mustache.render(pattern.extendedTemplate, data); |
| 48 | + } catch (e) { |
| 49 | + debugger; |
| 50 | + console.log("e = ", e); |
| 51 | + } |
| 52 | + }, |
| 53 | + |
| 54 | + /** |
| 55 | + * Find regex matches within both pattern strings and pattern objects. |
| 56 | + * |
| 57 | + * @param {string|object} pattern Either a string or a pattern object. |
| 58 | + * @param {object} regex A JavaScript RegExp object. |
| 59 | + * @returns {array|null} An array if a match is found, null if not. |
| 60 | + */ |
| 61 | + patternMatcher: function patternMatcher(pattern, regex) { |
| 62 | + var matches; |
| 63 | + if (typeof pattern === 'string') { |
| 64 | + matches = pattern.match(regex); |
| 65 | + } else if (typeof pattern === 'object' && typeof pattern.template === 'string') { |
| 66 | + matches = pattern.template.match(regex); |
| 67 | + } |
| 68 | + return matches; |
| 69 | + }, |
| 70 | + |
| 71 | + // find and return any {{> template-name }} within pattern |
| 72 | + findPartials: function findPartials(pattern) { |
| 73 | + var matches = this.patternMatcher(pattern, this.findPartialsRE); |
| 74 | + return matches; |
| 75 | + }, |
| 76 | + findPartialsWithStyleModifiers: function (pattern) { |
| 77 | + var matches = this.patternMatcher(pattern, this.findPartialsWithStyleModifiersRE); |
| 78 | + return matches; |
| 79 | + }, |
| 80 | + |
| 81 | + // returns any patterns that match {{> value(foo:"bar") }} or {{> |
| 82 | + // value:mod(foo:"bar") }} within the pattern |
| 83 | + findPartialsWithPatternParameters: function (pattern) { |
| 84 | + var matches = this.patternMatcher(pattern, this.findPartialsWithPatternParametersRE); |
| 85 | + return matches; |
| 86 | + }, |
| 87 | + findListItems: function (pattern) { |
| 88 | + var matches = this.patternMatcher(pattern, this.findListItemsRE); |
| 89 | + return matches; |
| 90 | + }, |
| 91 | + |
| 92 | + // given a pattern, and a partial string, tease out the "pattern key" and |
| 93 | + // return it. |
| 94 | + findPartial_new: function (partialString) { |
| 95 | + var partial = partialString.replace(this.findPartialRE, '$1'); |
| 96 | + return partial; |
| 97 | + }, |
| 98 | + |
| 99 | + // GTP: the old implementation works better. We might not need |
| 100 | + // this.findPartialRE anymore if it works in all cases! |
| 101 | + findPartial: function (partialString) { |
| 102 | + //strip out the template cruft |
| 103 | + var foundPatternPartial = partialString.replace("{{> ", "").replace(" }}", "").replace("{{>", "").replace("}}", ""); |
| 104 | + |
| 105 | + // remove any potential pattern parameters. this and the above are rather brutish but I didn't want to do a regex at the time |
| 106 | + if (foundPatternPartial.indexOf('(') > 0) { |
| 107 | + foundPatternPartial = foundPatternPartial.substring(0, foundPatternPartial.indexOf('(')); |
| 108 | + } |
| 109 | + |
| 110 | + //remove any potential stylemodifiers. |
| 111 | + foundPatternPartial = foundPatternPartial.split(':')[0]; |
| 112 | + |
| 113 | + return foundPatternPartial; |
| 114 | + } |
| 115 | +}; |
| 116 | + |
| 117 | +module.exports = engine_mustache; |
0 commit comments