1919
2020"use strict" ;
2121
22+ const fs = require ( 'fs' ) ;
23+ const path = require ( 'path' ) ;
2224const React = require ( 'react' ) ;
2325const ReactDOMServer = require ( 'react-dom/server' ) ;
2426const 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+ ) ;
2537
2638var engine_react = {
2739 engine : React ,
2840 engineName : 'react' ,
2941 engineFileExtension : '.jsx' ,
3042
31- // partial expansion is only necessary for React templates that have
32- // style modifiers or pattern parameters (I think)
43+ // hell no
3344 expandPartials : false ,
3445
3546 // regexes, stored here so they're only compiled once
@@ -40,7 +51,7 @@ var engine_react = {
4051 findPartialRE : null ,
4152
4253 // render it
43- renderPattern : function renderPattern ( pattern , data , partials ) {
54+ renderPattern ( pattern , data , partials ) {
4455 try {
4556 /* eslint-disable no-eval */
4657 const componentString = pattern . template || pattern . extendedTemplate ;
@@ -50,40 +61,19 @@ var engine_react = {
5061 } ) ;
5162 const runtimeComponent = Babel . transform ( componentString , {
5263 presets : [ require ( 'babel-preset-react' ) ] ,
53- plugins : [ require ( 'babel-plugin-transform-es2015-modules-umd' ) ]
64+ plugins : [ [ require ( 'babel-plugin-transform-es2015-modules-umd' ) , {
65+ globals : {
66+ "react" : "React"
67+ }
68+ } ] ]
5469 } ) ;
5570 const Component = React . createFactory ( eval ( nodeComponent . code ) ) ;
56- const output = ReactDOMServer . renderToStaticMarkup ( Component ( data ) ) ;
57-
58- return `<div id="reactContainer">
59-
60- <!-- pattern HTML -->
61- ${ output }
62-
63- </div>
64-
65-
66-
6771
68- <!-- pattern JSON (React props) -->
69- <script id="patternJSON" type="application/json">
70- ${ JSON . stringify ( data ) }
71- </script>
72-
73- <!-- dependencies -->
74- <script>
75- var react = React;
76- </script>
77-
78- <!-- runtime React output -->
79- <script>
80- ${ runtimeComponent . code } ;
81-
82- <!-- runtime rendering -->
83- var component = unknown.default;
84- var patternJSON = document.getElementById('patternJSON').textContent;
85- ReactDOM.render(React.createElement(component, JSON.parse(patternJSON)), document.getElementById('reactContainer'));
86- </script>` ;
72+ return outputTemplate . render ( {
73+ json : JSON . stringify ( data ) ,
74+ htmlOutput : ReactDOMServer . renderToStaticMarkup ( Component ( data ) ) ,
75+ runtimeCode : runtimeComponent . code
76+ } ) ;
8777 }
8878 catch ( e ) {
8979 console . log ( "Error rendering React pattern." , e ) ;
@@ -98,7 +88,7 @@ ReactDOM.render(React.createElement(component, JSON.parse(patternJSON)), documen
9888 * @param {object } regex A JavaScript RegExp object.
9989 * @returns {array|null } An array if a match is found, null if not.
10090 */
101- patternMatcher : function patternMatcher ( pattern , regex ) {
91+ patternMatcher ( pattern , regex ) {
10292 var matches ;
10393 if ( typeof pattern === 'string' ) {
10494 matches = pattern . match ( regex ) ;
@@ -109,32 +99,42 @@ ReactDOM.render(React.createElement(component, JSON.parse(patternJSON)), documen
10999 } ,
110100
111101 // find and return any {{> template-name }} within pattern
112- findPartials : function findPartials ( pattern ) {
102+ findPartials ( pattern ) {
113103 return [ ] ;
114104 } ,
115- findPartialsWithStyleModifiers : function ( pattern ) {
105+ findPartialsWithStyleModifiers ( pattern ) {
116106 return [ ] ;
117107 } ,
118108
119109 // returns any patterns that match {{> value(foo:"bar") }} or {{>
120110 // value:mod(foo:"bar") }} within the pattern
121- findPartialsWithPatternParameters : function ( pattern ) {
111+ findPartialsWithPatternParameters ( pattern ) {
122112 return [ ] ;
123113 } ,
124- findListItems : function ( pattern ) {
114+ findListItems ( pattern ) {
125115 return [ ] ;
126116 } ,
127117
128118 // given a pattern, and a partial string, tease out the "pattern key" and
129119 // return it.
130- findPartial_new : function ( partialString ) {
120+ findPartial ( partialString ) {
131121 return [ ] ;
132122 } ,
133123
134- // GTP: the old implementation works better. We might not need
135- // this.findPartialRE anymore if it works in all cases!
136- findPartial : function ( partialString ) {
124+ rawTemplateCodeFormatter ( unformattedString ) {
125+ console . log ( 'rawTemplateCodeFormatter()' ) ;
126+ return beautify ( unformattedString , { e4x : true , indent_size : 2 } ) ;
127+ } ,
128+
129+ renderedCodeFormatter ( unformattedString ) {
130+ console . log ( 'renderedCodeFormatter()' ) ;
131+ return unformattedString ;
132+ } ,
137133
134+ markupOnlyCodeFormatter ( unformattedString ) {
135+ const $ = cheerio . load ( unformattedString ) ;
136+ console . log ( 'markupOnlyCodeFormatter()' ) ;
137+ return beautify . html ( $ ( '#reactContainer' ) . html ( ) , { indent_size : 2 } ) ;
138138 }
139139} ;
140140
0 commit comments