@@ -61,87 +61,64 @@ var patternlab_engine = function(grunt){
61
61
var currentPattern ;
62
62
var flatPatternPath ;
63
63
64
- //ignore _underscored patterns
65
- if ( filename . charAt ( 0 ) === '_' ) {
64
+ //ignore _underscored patterns and json
65
+ if ( filename . charAt ( 0 ) === '_' || grunt . util . _ . str . include ( filename , 'json' ) ) {
66
66
return ;
67
67
}
68
+
69
+
70
+ //make a new Pattern Object
71
+ var flatPatternName = subdir . replace ( / \/ / g, '-' ) + '-' + patternName ;
72
+ flatPatternName = flatPatternName . replace ( / \/ / g, '-' ) ;
73
+ currentPattern = new of . oPattern ( flatPatternName , subdir , filename , { } ) ;
74
+ currentPattern . patternName = patternName . substring ( patternName . indexOf ( '-' ) + 1 ) ;
75
+ currentPattern . data = null ;
76
+
77
+ //look for a json file for this template
78
+ try {
79
+ var jsonFilename = abspath . substr ( 0 , abspath . lastIndexOf ( "." ) ) + ".json" ;
80
+ currentPattern . data = grunt . file . readJSON ( jsonFilename ) ;
81
+ }
82
+ catch ( e ) {
68
83
69
- //two reasons could return no pattern, 1) just a bare mustache, or 2) a json found before the mustache
70
- //returns -1 if patterns does not exist, otherwise returns the index
71
- //add the pattern array if first time, otherwise pull it up
72
- if ( patternIndex === - 1 ) {
73
- grunt . verbose . ok ( 'pattern not found, adding to array' ) ;
74
- var flatPatternName = subdir . replace ( / \/ / g, '-' ) + '-' + patternName ;
75
- flatPatternName = flatPatternName . replace ( / \/ / g, '-' ) ;
76
- currentPattern = new of . oPattern ( flatPatternName , subdir , filename , { } ) ;
77
- currentPattern . patternName = patternName . substring ( patternName . indexOf ( '-' ) + 1 ) ;
78
-
79
- if ( grunt . util . _ . str . include ( filename , 'json' ) ) {
80
- grunt . verbose . ok ( 'json file found first, so add it to the pattern and continuing' ) ;
81
- currentPattern . data = grunt . file . readJSON ( abspath ) ;
82
- //done
83
- } else {
84
- grunt . verbose . ok ( 'mustache file found, assume no data, so compile it right away' ) ;
85
- currentPattern . template = grunt . file . read ( abspath ) ;
86
-
87
- //render the pattern. pass partials object just in case.
88
- currentPattern . patternPartial = mustache . render ( currentPattern . template , patternlab . data , patternlab . partials ) ;
89
-
90
- //write the compiled template to the public patterns directory
91
- flatPatternPath = currentPattern . name + '/' + currentPattern . name + '.html' ;
92
-
93
- //add footer info before writing
94
- var currentPatternFooter = mustache . render ( patternlab . footer , currentPattern ) ;
95
-
96
- grunt . file . write ( './public/patterns/' + flatPatternPath , patternlab . header + currentPattern . patternPartial + currentPatternFooter ) ;
97
- currentPattern . patternLink = flatPatternPath ;
98
-
99
- //add as a partial in case this is referenced later. convert to syntax needed by existing patterns
100
- var sub = subdir . substring ( subdir . indexOf ( '-' ) + 1 ) ;
101
- var folderIndex = sub . indexOf ( '/' ) ; //THIS IS MOST LIKELY WINDOWS ONLY. path.sep not working yet
102
- var cleanSub = sub . substring ( 0 , folderIndex ) ;
103
-
104
- //add any templates found to an object of partials, so downstream templates may use them too
105
- //exclude the template patterns - we don't need them as partials because pages will just swap data
106
- if ( cleanSub !== '' ) {
107
- var partialname = cleanSub + '-' + patternName . substring ( patternName . indexOf ( '-' ) + 1 ) ;
108
-
109
- patternlab . partials [ partialname ] = currentPattern . template ;
84
+ }
110
85
111
- //done
112
- }
113
- }
114
- //add to patternlab arrays so we can look these up later. this could probably just be an object.
115
- patternlab . patternIndex . push ( currentPattern . name ) ;
116
- patternlab . patterns . push ( currentPattern ) ;
117
- } else {
118
- //if we get here, we can almost ensure we found the json first, so render the template and pass in the unique json
119
- currentPattern = patternlab . patterns [ patternIndex ] ;
120
- grunt . verbose . ok ( 'pattern found, loaded' ) ;
121
- //determine if this file is data or pattern
122
- if ( grunt . util . _ . str . include ( filename , 'mustache' ) ) {
86
+ currentPattern . template = grunt . file . read ( abspath ) ;
123
87
124
- currentPattern . template = grunt . file . read ( abspath ) ;
88
+ //render the pattern. pass partials object just in case.
89
+ if ( currentPattern . data ) { // Pass JSON as data
90
+ currentPattern . patternPartial = renderPattern ( currentPattern . template , currentPattern . data , patternlab . partials ) ;
91
+ } else { // Pass global patternlab data
92
+ currentPattern . patternPartial = renderPattern ( currentPattern . template , patternlab . data , patternlab . partials ) ;
93
+ }
94
+
95
+ //write the compiled template to the public patterns directory
96
+ flatPatternPath = currentPattern . name + '/' + currentPattern . name + '.html' ;
125
97
126
- //render the pattern. pass partials object just in case.
127
- currentPattern . patternPartial = mustache . render ( currentPattern . template , currentPattern . data , patternlab . partials ) ;
128
- grunt . verbose . ok ( 'template compiled with data!' ) ;
98
+ //add footer info before writing
99
+ var currentPatternFooter = renderPattern ( patternlab . footer , currentPattern ) ;
129
100
130
- //write the compiled template to the public patterns directory
131
- flatPatternPath = currentPattern . name + '/' + currentPattern . name + '.html' ;
101
+ grunt . file . write ( './public/patterns/' + flatPatternPath , patternlab . header + currentPattern . patternPartial + currentPatternFooter ) ;
102
+ currentPattern . patternLink = flatPatternPath ;
132
103
133
- //add footer info before writing
134
- var currentPatternFooter = mustache . render ( patternlab . footer , currentPattern ) ;
104
+ //add as a partial in case this is referenced later. convert to syntax needed by existing patterns
105
+ var sub = subdir . substring ( subdir . indexOf ( '-' ) + 1 ) ;
106
+ var folderIndex = sub . indexOf ( '/' ) ; //THIS IS MOST LIKELY WINDOWS ONLY. path.sep not working yet
107
+ var cleanSub = sub . substring ( 0 , folderIndex ) ;
135
108
136
- grunt . file . write ( './public/patterns/' + flatPatternPath , patternlab . header + currentPattern . patternPartial + currentPatternFooter ) ;
109
+ //add any templates found to an object of partials, so downstream templates may use them too
110
+ //exclude the template patterns - we don't need them as partials because pages will just swap data
111
+ if ( cleanSub !== '' ) {
112
+ var partialname = cleanSub + '-' + patternName . substring ( patternName . indexOf ( '-' ) + 1 ) ;
137
113
138
- currentPattern . patternLink = flatPatternPath ;
114
+ patternlab . partials [ partialname ] = currentPattern . template ;
139
115
140
- //done
141
- } else {
142
- grunt . log . error ( 'json encountered!? there should only be one' ) ;
143
- }
116
+ //done
144
117
}
118
+
119
+ //add to patternlab arrays so we can look these up later. this could probably just be an object.
120
+ patternlab . patternIndex . push ( currentPattern . name ) ;
121
+ patternlab . patterns . push ( currentPattern ) ;
145
122
146
123
} ) ;
147
124
@@ -155,7 +132,7 @@ var patternlab_engine = function(grunt){
155
132
156
133
//build the styleguide
157
134
var styleguideTemplate = grunt . file . read ( './source/_patternlab-files/styleguide.mustache' ) ;
158
- var styleguideHtml = mustache . render ( styleguideTemplate , { partials : patternlab . patterns } ) ;
135
+ var styleguideHtml = renderPattern ( styleguideTemplate , { partials : patternlab . patterns } ) ;
159
136
grunt . file . write ( './public/styleguide/html/styleguide.html' , styleguideHtml ) ;
160
137
161
138
//build the patternlab website
@@ -285,29 +262,33 @@ var patternlab_engine = function(grunt){
285
262
//the patternlab site requires a lot of partials to be rendered.
286
263
//patternNav
287
264
var patternNavTemplate = grunt . file . read ( './source/_patternlab-files/partials/patternNav.mustache' ) ;
288
- var patternNavPartialHtml = mustache . render ( patternNavTemplate , patternlab ) ;
265
+ var patternNavPartialHtml = renderPattern ( patternNavTemplate , patternlab ) ;
289
266
290
267
//ishControls
291
268
var ishControlsTemplate = grunt . file . read ( './source/_patternlab-files/partials/ishControls.mustache' ) ;
269
+ < << << << HEAD
292
270
var ishControlsPartialHtml = mustache . render ( ishControlsTemplate , patternlab . config ) ;
271
+ = === ===
272
+ var ishControlsPartialHtml = renderPattern ( ishControlsTemplate , patternlab . config ) ;
273
+ > >>> >>> a38e78c13ca1f9b51ca570c2c1ebb077f880214c
293
274
294
275
//patternPaths
295
276
var patternPathsTemplate = grunt . file . read ( './source/_patternlab-files/partials/patternPaths.mustache' ) ;
296
- var patternPathsPartialHtml = mustache . render ( patternPathsTemplate , { 'patternPaths' : JSON . stringify ( patternlab . patternPaths ) } ) ;
277
+ var patternPathsPartialHtml = renderPattern ( patternPathsTemplate , { 'patternPaths' : JSON . stringify ( patternlab . patternPaths ) } ) ;
297
278
298
279
//viewAllPaths
299
280
var viewAllPathsTemplate = grunt . file . read ( './source/_patternlab-files/partials/viewAllPaths.mustache' ) ;
300
- var viewAllPathersPartialHtml = mustache . render ( viewAllPathsTemplate , { 'viewallpaths' : JSON . stringify ( patternlab . viewAllPaths ) } ) ;
281
+ var viewAllPathersPartialHtml = renderPattern ( viewAllPathsTemplate , { 'viewallpaths' : JSON . stringify ( patternlab . viewAllPaths ) } ) ;
301
282
302
283
//websockets
303
284
var websocketsTemplate = grunt . file . read ( './source/_patternlab-files/partials/websockets.mustache' ) ;
304
285
patternlab . contentsyncport = patternlab . config . contentSyncPort ;
305
286
patternlab . navsyncport = patternlab . config . navSyncPort ;
306
287
307
- var websocketsPartialHtml = mustache . render ( websocketsTemplate , patternlab ) ;
288
+ var websocketsPartialHtml = renderPattern ( websocketsTemplate , patternlab ) ;
308
289
309
290
//render the patternlab template, with all partials
310
- var patternlabSiteHtml = mustache . render ( patternlabSiteTemplate , { } , {
291
+ var patternlabSiteHtml = renderPattern ( patternlabSiteTemplate , { } , {
311
292
'ishControls' : ishControlsPartialHtml ,
312
293
'patternNav' : patternNavPartialHtml ,
313
294
'patternPaths' : patternPathsPartialHtml ,
@@ -318,6 +299,14 @@ var patternlab_engine = function(grunt){
318
299
319
300
}
320
301
302
+ function renderPattern ( name , data , partials ) {
303
+ if ( partials ) {
304
+ return mustache . render ( name , data , partials ) ;
305
+ } else {
306
+ return mustache . render ( name , data ) ;
307
+ }
308
+ }
309
+
321
310
return {
322
311
version : function ( ) {
323
312
return getVersion ( ) ;
0 commit comments