@@ -61,87 +61,64 @@ var patternlab_engine = function(grunt){
6161 var currentPattern ;
6262 var flatPatternPath ;
6363
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' ) ) {
6666 return ;
6767 }
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 ) {
6883
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+ }
11085
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 ) ;
12387
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' ;
12597
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 ) ;
129100
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 ;
132103
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 ) ;
135108
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 ) ;
137113
138- currentPattern . patternLink = flatPatternPath ;
114+ patternlab . partials [ partialname ] = currentPattern . template ;
139115
140- //done
141- } else {
142- grunt . log . error ( 'json encountered!? there should only be one' ) ;
143- }
116+ //done
144117 }
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 ) ;
145122
146123 } ) ;
147124
@@ -155,7 +132,7 @@ var patternlab_engine = function(grunt){
155132
156133 //build the styleguide
157134 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 } ) ;
159136 grunt . file . write ( './public/styleguide/html/styleguide.html' , styleguideHtml ) ;
160137
161138 //build the patternlab website
@@ -285,29 +262,33 @@ var patternlab_engine = function(grunt){
285262 //the patternlab site requires a lot of partials to be rendered.
286263 //patternNav
287264 var patternNavTemplate = grunt . file . read ( './source/_patternlab-files/partials/patternNav.mustache' ) ;
288- var patternNavPartialHtml = mustache . render ( patternNavTemplate , patternlab ) ;
265+ var patternNavPartialHtml = renderPattern ( patternNavTemplate , patternlab ) ;
289266
290267 //ishControls
291268 var ishControlsTemplate = grunt . file . read ( './source/_patternlab-files/partials/ishControls.mustache' ) ;
269+ < << << << HEAD
292270 var ishControlsPartialHtml = mustache . render ( ishControlsTemplate , patternlab . config ) ;
271+ = === ===
272+ var ishControlsPartialHtml = renderPattern ( ishControlsTemplate , patternlab . config ) ;
273+ > >>> >>> a38e78c13ca1f9b51ca570c2c1ebb077f880214c
293274
294275 //patternPaths
295276 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 ) } ) ;
297278
298279 //viewAllPaths
299280 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 ) } ) ;
301282
302283 //websockets
303284 var websocketsTemplate = grunt . file . read ( './source/_patternlab-files/partials/websockets.mustache' ) ;
304285 patternlab . contentsyncport = patternlab . config . contentSyncPort ;
305286 patternlab . navsyncport = patternlab . config . navSyncPort ;
306287
307- var websocketsPartialHtml = mustache . render ( websocketsTemplate , patternlab ) ;
288+ var websocketsPartialHtml = renderPattern ( websocketsTemplate , patternlab ) ;
308289
309290 //render the patternlab template, with all partials
310- var patternlabSiteHtml = mustache . render ( patternlabSiteTemplate , { } , {
291+ var patternlabSiteHtml = renderPattern ( patternlabSiteTemplate , { } , {
311292 'ishControls' : ishControlsPartialHtml ,
312293 'patternNav' : patternNavPartialHtml ,
313294 'patternPaths' : patternPathsPartialHtml ,
@@ -318,6 +299,14 @@ var patternlab_engine = function(grunt){
318299
319300 }
320301
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+
321310 return {
322311 version : function ( ) {
323312 return getVersion ( ) ;
0 commit comments