|
| 1 | +/* |
| 2 | + * react pattern engine for patternlab-node - v0.1.0 - 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 | + * React templates. |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +"use strict"; |
| 21 | + |
| 22 | +const fs = require('fs'); |
| 23 | +const path = require('path'); |
| 24 | +const React = require('react'); |
| 25 | +const ReactDOMServer = require('react-dom/server'); |
| 26 | +const Babel = require('babel-core'); |
| 27 | +const Hogan = require('hogan.js'); |
| 28 | +const beautify = require('js-beautify'); |
| 29 | +const cheerio = require('cheerio'); |
| 30 | + |
| 31 | +const outputTemplate = Hogan.compile( |
| 32 | + fs.readFileSync( |
| 33 | + path.join(__dirname, './outputTemplate.mustache'), |
| 34 | + 'utf8' |
| 35 | + ) |
| 36 | +); |
| 37 | + |
| 38 | +var engine_react = { |
| 39 | + engine: React, |
| 40 | + engineName: 'react', |
| 41 | + engineFileExtension: '.jsx', |
| 42 | + |
| 43 | + // hell no |
| 44 | + expandPartials: false, |
| 45 | + |
| 46 | + // regexes, stored here so they're only compiled once |
| 47 | + findPartialsRE: null, |
| 48 | + findPartialsWithStyleModifiersRE: null, |
| 49 | + findPartialsWithPatternParametersRE: null, |
| 50 | + findListItemsRE: null, |
| 51 | + findPartialRE: null, |
| 52 | + |
| 53 | + // render it |
| 54 | + renderPattern(pattern, data, partials) { |
| 55 | + try { |
| 56 | + /* eslint-disable no-eval */ |
| 57 | + const componentString = pattern.template || pattern.extendedTemplate; |
| 58 | + const nodeComponent = Babel.transform(componentString, { |
| 59 | + presets: [ require('babel-preset-react') ], |
| 60 | + plugins: [ require('babel-plugin-transform-es2015-modules-commonjs') ] |
| 61 | + }); |
| 62 | + const runtimeComponent = Babel.transform(componentString, { |
| 63 | + presets: [ require('babel-preset-react') ], |
| 64 | + plugins: [[require('babel-plugin-transform-es2015-modules-umd'), { |
| 65 | + globals: { |
| 66 | + "react": "React" |
| 67 | + } |
| 68 | + }]] |
| 69 | + }); |
| 70 | + const Component = React.createFactory(eval(nodeComponent.code)); |
| 71 | + |
| 72 | + return outputTemplate.render({ |
| 73 | + patternPartial: pattern.patternPartial, |
| 74 | + json: JSON.stringify(data), |
| 75 | + htmlOutput: ReactDOMServer.renderToStaticMarkup(Component(data)), |
| 76 | + runtimeCode: runtimeComponent.code |
| 77 | + }); |
| 78 | + } |
| 79 | + catch (e) { |
| 80 | + console.log("Error rendering React pattern.", e); |
| 81 | + return ""; |
| 82 | + } |
| 83 | + }, |
| 84 | + |
| 85 | + /** |
| 86 | + * Find regex matches within both pattern strings and pattern objects. |
| 87 | + * |
| 88 | + * @param {string|object} pattern Either a string or a pattern object. |
| 89 | + * @param {object} regex A JavaScript RegExp object. |
| 90 | + * @returns {array|null} An array if a match is found, null if not. |
| 91 | + */ |
| 92 | + patternMatcher(pattern, regex) { |
| 93 | + var matches; |
| 94 | + if (typeof pattern === 'string') { |
| 95 | + matches = pattern.match(regex); |
| 96 | + } else if (typeof pattern === 'object' && typeof pattern.template === 'string') { |
| 97 | + matches = pattern.template.match(regex); |
| 98 | + } |
| 99 | + return matches; |
| 100 | + }, |
| 101 | + |
| 102 | + // find and return any {{> template-name }} within pattern |
| 103 | + findPartials(pattern) { |
| 104 | + return []; |
| 105 | + }, |
| 106 | + findPartialsWithStyleModifiers(pattern) { |
| 107 | + return []; |
| 108 | + }, |
| 109 | + |
| 110 | + // returns any patterns that match {{> value(foo:"bar") }} or {{> |
| 111 | + // value:mod(foo:"bar") }} within the pattern |
| 112 | + findPartialsWithPatternParameters(pattern) { |
| 113 | + return []; |
| 114 | + }, |
| 115 | + findListItems(pattern) { |
| 116 | + return []; |
| 117 | + }, |
| 118 | + |
| 119 | + // given a pattern, and a partial string, tease out the "pattern key" and |
| 120 | + // return it. |
| 121 | + findPartial(partialString) { |
| 122 | + return []; |
| 123 | + }, |
| 124 | + |
| 125 | + rawTemplateCodeFormatter(unformattedString) { |
| 126 | + return beautify(unformattedString, {e4x: true, indent_size: 2}); |
| 127 | + }, |
| 128 | + |
| 129 | + renderedCodeFormatter(unformattedString) { |
| 130 | + return unformattedString; |
| 131 | + }, |
| 132 | + |
| 133 | + markupOnlyCodeFormatter(unformattedString, pattern) { |
| 134 | + const $ = cheerio.load(unformattedString); |
| 135 | + return beautify.html($('.reactPatternContainer').html(), {indent_size: 2}); |
| 136 | + }, |
| 137 | + |
| 138 | + /** |
| 139 | + * Add custom output files to the pattern output |
| 140 | + * @param {object} patternlab - the global state object |
| 141 | + * @returns {(object|object[])} - an object or array of objects, |
| 142 | + * each with two properties: path, and content |
| 143 | + */ |
| 144 | + addOutputFiles(paths, patternlab) { |
| 145 | + return []; |
| 146 | + } |
| 147 | +}; |
| 148 | + |
| 149 | +module.exports = engine_react; |
0 commit comments