1
1
<?php
2
2
3
3
/*!
4
- * Pattern Lab Builder Class - v0.3.3
4
+ * Pattern Lab Builder Class - v0.3.5
5
5
*
6
6
* Copyright (c) 2013 Dave Olsen, http://dmolsen.com
7
7
* Licensed under the MIT license
10
10
*
11
11
*/
12
12
13
- class Builder {
13
+ class Buildr {
14
14
15
15
// i was lazy when i started this project & kept (mainly) to two letter vars. sorry.
16
16
protected $ mpl ; // mustache pattern loader instance
@@ -24,6 +24,7 @@ class Builder {
24
24
protected $ navSyncPort ; // for populating the websockets template partial
25
25
protected $ patternTypes ; // a list of pattern types that match the directory structure
26
26
protected $ patternPaths ; // the paths to patterns for use with mustache partials
27
+ protected $ patternLineages ; // the list of patterns that make up a particular pattern
27
28
protected $ patternTypesRegex ; // the simple regex for the pattern types. used in getPath()
28
29
protected $ navItems ; // the items for the nav. includes view all links
29
30
protected $ viewAllPaths ; // the paths to the view all pages
@@ -51,7 +52,7 @@ public function __construct() {
51
52
// if the variables are array-like make sure the properties are validated/trimmed/lowercased before saving
52
53
if (($ key == "ie " ) || ($ key == "id " )) {
53
54
$ values = explode (", " ,$ value );
54
- array_walk ($ values ,'Builder ::trim ' );
55
+ array_walk ($ values ,'Buildr ::trim ' );
55
56
$ this ->$ key = $ values ;
56
57
} else {
57
58
$ this ->$ key = $ value ;
@@ -122,12 +123,13 @@ protected function generateMainPages() {
122
123
$ this ->navItems ['navsyncport ' ] = $ this ->navSyncPort ;
123
124
$ this ->navItems ['patternpaths ' ] = json_encode ($ this ->patternPaths );
124
125
$ this ->navItems ['viewallpaths ' ] = json_encode ($ this ->viewAllPaths );
126
+ $ this ->navItems ['mqs ' ] = $ this ->gatherMQs ();
125
127
126
128
// grab the partials into a data object for the style guide
127
129
$ sd = $ this ->gatherPartials ();
128
130
129
131
// sort partials by patternLink
130
- usort ($ sd ['partials ' ], "Builder ::sortPartials " );
132
+ usort ($ sd ['partials ' ], "Buildr ::sortPartials " );
131
133
132
134
// render the "view all" pages
133
135
$ this ->generateViewAllPages ();
@@ -177,11 +179,18 @@ protected function generatePatterns() {
177
179
* @return {String} the final rendered pattern including the standard header and footer for a pattern
178
180
*/
179
181
private function generatePatternFile ($ f ) {
182
+
180
183
$ hr = file_get_contents (__DIR__ .$ this ->sp ."../_patternlab-files/pattern-header-footer/header.html " );
181
184
$ rf = $ this ->renderPattern ($ f );
182
185
$ fr = file_get_contents (__DIR__ .$ this ->sp ."../_patternlab-files/pattern-header-footer/footer.html " );
183
- $ fr = str_replace ("{{ patternPartial }} " ,$ this ->getPatternPartial ($ f ),$ fr );
186
+
187
+ // the footer isn't rendered as mustache but we have some variables there any way. find & replace.
188
+ $ pp = $ this ->getPatternPartial ($ f );
189
+ $ fr = str_replace ("{{ patternPartial }} " ,$ pp ,$ fr );
190
+ $ fr = str_replace ("{{ lineage }} " ,json_encode ($ this ->patternLineages [$ pp ]),$ fr );
191
+
184
192
return $ hr ."\n" .$ rf ."\n" .$ fr ;
193
+
185
194
}
186
195
187
196
/**
@@ -246,7 +255,7 @@ protected function generateViewAllPages() {
246
255
}
247
256
248
257
/**
249
- * Gather data from source/_data/data .json, source/_data/listitems .json, and pattern-specific json files
258
+ * Gather data from source/_data/_data .json, source/_data/_listitems .json, and pattern-specific json files
250
259
*
251
260
* Reserved attributes:
252
261
* - $this->d->listItems : listItems from listitems.json, duplicated into separate arrays for $this->d->listItems->one, $this->d->listItems->two, $this->d->listItems->three... etc.
@@ -258,14 +267,14 @@ protected function generateViewAllPages() {
258
267
protected function gatherData () {
259
268
260
269
// gather the data from the main source data.json
261
- if (file_exists (__DIR__ ."/../../source/_data/data .json " )) {
262
- $ this ->d = (object ) array_merge (array (), (array ) json_decode (file_get_contents (__DIR__ ."/../../source/_data/data .json " )));
270
+ if (file_exists (__DIR__ ."/../../source/_data/_data .json " )) {
271
+ $ this ->d = (object ) array_merge (array (), (array ) json_decode (file_get_contents (__DIR__ ."/../../source/_data/_data .json " )));
263
272
}
264
273
265
274
// add list item data, makes 'listItems' a reserved word
266
- if (file_exists (__DIR__ ."/../../source/_data/listitems .json " )) {
275
+ if (file_exists (__DIR__ ."/../../source/_data/_listitems .json " )) {
267
276
268
- $ listItems = (array ) json_decode (file_get_contents (__DIR__ ."/../../source/_data/listitems .json " ));
277
+ $ listItems = (array ) json_decode (file_get_contents (__DIR__ ."/../../source/_data/_listitems .json " ));
269
278
$ numbers = array ("one " ,"two " ,"three " ,"four " ,"five " ,"six " ,"seven " ,"eight " ,"nine " ,"ten " ,"eleven " ,"twelve " );
270
279
271
280
$ i = 0 ;
@@ -336,6 +345,30 @@ protected function gatherData() {
336
345
337
346
}
338
347
348
+ /**
349
+ * Finds Media Queries in CSS files in the source/css/ dir
350
+ *
351
+ * @return {Array} an array of the appropriate MQs
352
+ */
353
+ protected function gatherMQs () {
354
+
355
+ $ mqs = array ();
356
+
357
+ foreach (glob (__DIR__ ."/../../source/css/*.css " ) as $ filename ) {
358
+ $ data = file_get_contents ($ filename );
359
+ preg_match_all ("/(min|max)-width:( |)(([0-9]{1,5})(\.[0-9]{1,20}|)(px|em))/ " ,$ data ,$ matches );
360
+ foreach ($ matches [3 ] as $ match ) {
361
+ if (!in_array ($ match ,$ mqs )) {
362
+ $ mqs [] = $ match ;
363
+ }
364
+ }
365
+ }
366
+
367
+ sort ($ mqs );
368
+ return $ mqs ;
369
+
370
+ }
371
+
339
372
/**
340
373
* Gathers the partials for the nav drop down in Pattern Lab
341
374
*
@@ -469,8 +502,9 @@ protected function gatherNavItems() {
469
502
protected function gatherPatternPaths () {
470
503
471
504
// set-up vars
472
- $ this ->patternPaths = array ();
473
- $ this ->patternTypes = array ();
505
+ $ this ->patternPaths = array ();
506
+ $ this ->patternTypes = array ();
507
+ $ this ->patternLineages = array ();
474
508
475
509
// get the pattern types
476
510
foreach (glob (__DIR__ .$ this ->sp ."/* " ,GLOB_ONLYDIR ) as $ patternType ) {
@@ -482,14 +516,17 @@ protected function gatherPatternPaths() {
482
516
483
517
// find the patterns for the types
484
518
foreach ($ this ->patternTypes as $ patternType ) {
519
+
485
520
$ patternTypePaths = array ();
521
+ $ patternTypeClean = $ this ->getPatternName ($ patternType , false );
486
522
487
523
// find pattern paths for pattern subtypes
488
524
foreach (glob (__DIR__ .$ this ->sp .$ patternType ."/*/*.mustache " ) as $ filename ) {
489
525
preg_match ('/\/([A-z0-9-_]{1,})\.mustache$/ ' ,$ filename ,$ matches );
490
526
$ pattern = $ this ->getPatternName ($ matches [1 ], false );
491
527
if (($ pattern [0 ] != "_ " ) && (!isset ($ patternTypePaths [$ pattern ]))) {
492
528
$ patternTypePaths [$ pattern ] = $ this ->getPath ($ filename );
529
+ $ this ->patternLineages [$ patternTypeClean ."- " .$ pattern ] = $ this ->getLineage ($ filename );
493
530
}
494
531
}
495
532
@@ -499,10 +536,10 @@ protected function gatherPatternPaths() {
499
536
$ pattern = $ this ->getPatternName ($ matches [1 ], false );
500
537
if (($ pattern [0 ] != "_ " ) && (!isset ($ patternTypePaths [$ pattern ]))) {
501
538
$ patternTypePaths [$ pattern ] = $ this ->getPath ($ filename );
539
+ $ this ->patternLineages [$ patternTypeClean ."- " .$ pattern ] = $ this ->getLineage ($ filename );
502
540
}
503
541
}
504
542
505
- $ patternTypeClean = $ this ->getPatternName ($ patternType , false );
506
543
$ this ->patternPaths [$ patternTypeClean ] = $ patternTypePaths ;
507
544
508
545
}
@@ -590,6 +627,19 @@ protected function gatherPartialsByMatch($patternType, $patternSubType) {
590
627
591
628
}
592
629
630
+ /**
631
+ * Get the lineage for a given pattern by parsing it and matching mustache partials
632
+ * @param {String} the filename for the pattern to be parsed
633
+ *
634
+ * @return {Array} a list of patterns
635
+ */
636
+ protected function getLineage ($ filename ) {
637
+ $ data = file_get_contents ($ filename );
638
+ if (preg_match_all ('/{{>([ ]+)?([A-Za-z0-9-]+)([ ]+)?}}/ ' ,$ data ,$ matches )) {
639
+ return $ matches [2 ];
640
+ }
641
+ }
642
+
593
643
/**
594
644
* Get the directory path for a given pattern or json file by parsing the file path
595
645
* @param {String} the filepath for a directory that contained the match
@@ -693,9 +743,11 @@ protected function moveFile($s,$p) {
693
743
* Moves static files that aren't directly related to Pattern Lab
694
744
* @param {String} file name to be moved
695
745
* @param {String} copy for the message to be printed out
746
+ * @param {String} part of the file name to be found for replacement
747
+ * @param {String} the replacement
696
748
*/
697
- protected function moveStaticFile ($ fileName ,$ copy = "" ) {
698
- $ this ->moveFile ($ fileName ,$ fileName );
749
+ protected function moveStaticFile ($ fileName ,$ copy = "" , $ find = "" , $ replace = "" ) {
750
+ $ this ->moveFile ($ fileName ,str_replace ( $ find , $ replace , $ fileName) );
699
751
$ this ->updateChangeTime ();
700
752
if ($ copy != "" ) {
701
753
print $ fileName ." " .$ copy ."... \n" ;
0 commit comments