19
19
20
20
"use strict" ;
21
21
22
+ const fs = require ( 'fs' ) ;
23
+ const path = require ( 'path' ) ;
22
24
const React = require ( 'react' ) ;
23
25
const ReactDOMServer = require ( 'react-dom/server' ) ;
24
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
+ ) ;
25
37
26
38
var engine_react = {
27
39
engine : React ,
28
40
engineName : 'react' ,
29
41
engineFileExtension : '.jsx' ,
30
42
31
- // partial expansion is only necessary for React templates that have
32
- // style modifiers or pattern parameters (I think)
43
+ // hell no
33
44
expandPartials : false ,
34
45
35
46
// regexes, stored here so they're only compiled once
@@ -40,7 +51,7 @@ var engine_react = {
40
51
findPartialRE : null ,
41
52
42
53
// render it
43
- renderPattern : function renderPattern ( pattern , data , partials ) {
54
+ renderPattern ( pattern , data , partials ) {
44
55
try {
45
56
/* eslint-disable no-eval */
46
57
const componentString = pattern . template || pattern . extendedTemplate ;
@@ -50,40 +61,19 @@ var engine_react = {
50
61
} ) ;
51
62
const runtimeComponent = Babel . transform ( componentString , {
52
63
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
+ } ] ]
54
69
} ) ;
55
70
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
-
67
71
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
+ } ) ;
87
77
}
88
78
catch ( e ) {
89
79
console . log ( "Error rendering React pattern." , e ) ;
@@ -98,7 +88,7 @@ ReactDOM.render(React.createElement(component, JSON.parse(patternJSON)), documen
98
88
* @param {object } regex A JavaScript RegExp object.
99
89
* @returns {array|null } An array if a match is found, null if not.
100
90
*/
101
- patternMatcher : function patternMatcher ( pattern , regex ) {
91
+ patternMatcher ( pattern , regex ) {
102
92
var matches ;
103
93
if ( typeof pattern === 'string' ) {
104
94
matches = pattern . match ( regex ) ;
@@ -109,32 +99,42 @@ ReactDOM.render(React.createElement(component, JSON.parse(patternJSON)), documen
109
99
} ,
110
100
111
101
// find and return any {{> template-name }} within pattern
112
- findPartials : function findPartials ( pattern ) {
102
+ findPartials ( pattern ) {
113
103
return [ ] ;
114
104
} ,
115
- findPartialsWithStyleModifiers : function ( pattern ) {
105
+ findPartialsWithStyleModifiers ( pattern ) {
116
106
return [ ] ;
117
107
} ,
118
108
119
109
// returns any patterns that match {{> value(foo:"bar") }} or {{>
120
110
// value:mod(foo:"bar") }} within the pattern
121
- findPartialsWithPatternParameters : function ( pattern ) {
111
+ findPartialsWithPatternParameters ( pattern ) {
122
112
return [ ] ;
123
113
} ,
124
- findListItems : function ( pattern ) {
114
+ findListItems ( pattern ) {
125
115
return [ ] ;
126
116
} ,
127
117
128
118
// given a pattern, and a partial string, tease out the "pattern key" and
129
119
// return it.
130
- findPartial_new : function ( partialString ) {
120
+ findPartial ( partialString ) {
131
121
return [ ] ;
132
122
} ,
133
123
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
+ } ,
137
133
134
+ markupOnlyCodeFormatter ( unformattedString ) {
135
+ const $ = cheerio . load ( unformattedString ) ;
136
+ console . log ( 'markupOnlyCodeFormatter()' ) ;
137
+ return beautify . html ( $ ( '#reactContainer' ) . html ( ) , { indent_size : 2 } ) ;
138
138
}
139
139
} ;
140
140
0 commit comments