|
| 1 | +/* |
| 2 | + * underscore 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 | +/* |
| 13 | + * ENGINE SUPPORT LEVEL: |
| 14 | + * |
| 15 | + * Basic. We can't call partials from inside underscore templates yet, but we |
| 16 | + * can render templates with backing JSON. |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | + |
| 21 | +"use strict"; |
| 22 | + |
| 23 | +var _ = require('underscore'); |
| 24 | +var partialRegistry = {}; |
| 25 | + |
| 26 | +// extend underscore with partial-ing methods and other necessary tooling |
| 27 | +// HANDLESCORE! UNDERBARS! |
| 28 | +function addParentContext(data, currentContext) { |
| 29 | + return Object.assign({}, currentContext, data); |
| 30 | +} |
| 31 | + |
| 32 | +_.mixin({ |
| 33 | + renderAtomicPartial: function (partialKey, data, currentContext) { |
| 34 | + return _.renderPartial(partialRegistry[partialKey], data, currentContext); |
| 35 | + }, |
| 36 | + renderPartial: function (partial, dataIn, currentContext) { |
| 37 | + var data = dataIn || {}; |
| 38 | + var compiled; |
| 39 | + if (dataIn && currentContext && |
| 40 | + dataIn instanceof Object && currentContext instanceof Object) { |
| 41 | + data = addParentContext(data, currentContext); |
| 42 | + } |
| 43 | + |
| 44 | + compiled = _.template(partial); |
| 45 | + |
| 46 | + return compiled(data); |
| 47 | + }, |
| 48 | + assignContext: function (viewModel, data) { |
| 49 | + return viewModel(data); |
| 50 | + }, |
| 51 | + |
| 52 | + /* eslint-disable no-eval, no-unused-vars */ |
| 53 | + getPath: function (pathString, currentContext, debug) { |
| 54 | + try { |
| 55 | + var result = eval('currentContext.' + pathString); |
| 56 | + if (debug) { |
| 57 | + console.log("getPath result = ", result); |
| 58 | + } |
| 59 | + return result; |
| 60 | + } catch (e) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + } |
| 64 | +}); |
| 65 | + |
| 66 | +var engine_underscore = { |
| 67 | + engine: _, |
| 68 | + engineName: 'underscore', |
| 69 | + engineFileExtension: '.html', |
| 70 | + |
| 71 | + // partial expansion is only necessary for Mustache templates that have |
| 72 | + // style modifiers or pattern parameters (I think) |
| 73 | + expandPartials: false, |
| 74 | + |
| 75 | + // regexes, stored here so they're only compiled once |
| 76 | + findPartialsRE: /<%=[ \t]*_\.renderPartial[ \t]*\((?:"([^"].*?)"|'([^'].*?)')/g, // TODO, |
| 77 | + findPartialsWithStyleModifiersRE: /<%= _.renderPartial\((.*?)\).*?%>/g, // TODO |
| 78 | + findPartialsWithPatternParametersRE: /<%= _.renderPartial\((.*?)\).*?%>/g, // TODO |
| 79 | + 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, |
| 80 | + |
| 81 | + // render it |
| 82 | + renderPattern: function renderPattern(pattern, data, partials) { |
| 83 | + var renderedHTML; |
| 84 | + var compiled; |
| 85 | + |
| 86 | + try { |
| 87 | + compiled = _.template(pattern.extendedTemplate); |
| 88 | + } catch (e) { |
| 89 | + console.log(`Error compiling template ${pattern.patternName}:`, pattern.extendedTemplate); |
| 90 | + } |
| 91 | + |
| 92 | + // This try-catch is necessary because references to undefined variables |
| 93 | + // in underscore templates are eval()ed directly as javascript, and as |
| 94 | + // such will throw very real exceptions that will shatter the whole build |
| 95 | + // process if we don't handle them. |
| 96 | + try { |
| 97 | + renderedHTML = compiled(_.extend(data || {}, { |
| 98 | + _allData: data, |
| 99 | + _partials: partials |
| 100 | + })); |
| 101 | + } catch (e) { |
| 102 | + var errorMessage = `Error in underscore template ${pattern.patternName} (${pattern.relPath}): [${e.toString()}]`; |
| 103 | + console.log(errorMessage); |
| 104 | + renderedHTML = `<h1>Error in underscore template ${pattern.patternName} (${pattern.relPath})</h1><p>${e.toString()}</p>`; |
| 105 | + } |
| 106 | + |
| 107 | + return renderedHTML; |
| 108 | + }, |
| 109 | + |
| 110 | + registerPartial: function (oPattern) { |
| 111 | + partialRegistry[oPattern.key] = oPattern.template; |
| 112 | + }, |
| 113 | + |
| 114 | + // find and return any {{> template-name }} within pattern |
| 115 | + findPartials: function findPartials(pattern) { |
| 116 | + var matches = pattern.template.match(this.findPartialsRE); |
| 117 | + return matches; |
| 118 | + }, |
| 119 | + findPartialsWithStyleModifiers: function () { |
| 120 | + return []; |
| 121 | + }, |
| 122 | + |
| 123 | + // returns any patterns that match {{> value(foo:"bar") }} or {{> |
| 124 | + // value:mod(foo:"bar") }} within the pattern |
| 125 | + findPartialsWithPatternParameters: function () { |
| 126 | + return []; |
| 127 | + }, |
| 128 | + findListItems: function (pattern) { |
| 129 | + var matches = pattern.template.match(this.findListItemsRE); |
| 130 | + return matches; |
| 131 | + }, |
| 132 | + |
| 133 | + // given a pattern, and a partial string, tease out the "pattern key" and |
| 134 | + // return it. |
| 135 | + findPartial: function (partialString) { |
| 136 | + var partial = partialString.replace(this.findPartialsRE, '$1'); |
| 137 | + return partial; |
| 138 | + } |
| 139 | +}; |
| 140 | + |
| 141 | +module.exports = engine_underscore; |
0 commit comments