1
1
<?php
2
2
3
3
/*!
4
- * Pattern Lab Builder Class - v0.3.5
4
+ * Pattern Lab Builder Class - v0.3.6
5
5
*
6
6
* Copyright (c) 2013 Dave Olsen, http://dmolsen.com
7
7
* Licensed under the MIT license
@@ -28,6 +28,9 @@ class Buildr {
28
28
protected $ patternTypesRegex ; // the simple regex for the pattern types. used in getPath()
29
29
protected $ navItems ; // the items for the nav. includes view all links
30
30
protected $ viewAllPaths ; // the paths to the view all pages
31
+ protected $ enableCSS ; // decide if we'll enable CSS parsing
32
+ protected $ patternCSS ; // an array to hold the CSS generated for patterns
33
+ protected $ cssRuleSaver ; // where css rule saver will be initialized
31
34
32
35
/**
33
36
* When initializing the Builder class or the sub-classes make sure the base properties are configured
@@ -62,9 +65,16 @@ public function __construct() {
62
65
// generate patternTypes as well as patternPaths
63
66
$ this ->gatherPatternPaths ();
64
67
68
+ // generate patternLineages
69
+ $ this ->gatherLineages ();
70
+
65
71
// get nav items
66
72
$ this ->gatherNavItems ();
67
73
74
+ // provide the default for enable CSS. performance hog so it should be run infrequently
75
+ $ this ->enableCSS = false ;
76
+ $ this ->patternCSS = array ();
77
+
68
78
}
69
79
70
80
/**
@@ -188,6 +198,15 @@ private function generatePatternFile($f) {
188
198
$ pp = $ this ->getPatternPartial ($ f );
189
199
$ fr = str_replace ("{{ patternPartial }} " ,$ pp ,$ fr );
190
200
$ fr = str_replace ("{{ lineage }} " ,json_encode ($ this ->patternLineages [$ pp ]),$ fr );
201
+ $ fr = str_replace ("{{ patternHTML }} " ,$ rf ,$ fr );
202
+
203
+ // set-up the mark-up for CSS Rule Saver so it can figure out which rules to save
204
+ if ($ this ->enableCSS ) {
205
+ $ this ->cssRuleSaver ->loadHTML ($ rf ,false );
206
+ $ patternCSS = $ this ->cssRuleSaver ->saveRules ();
207
+ $ this ->patternCSS [$ pp ] = $ patternCSS ;
208
+ $ fr = str_replace ("{{ patternCSS }} " ,$ patternCSS ,$ fr );
209
+ }
191
210
192
211
return $ hr ."\n" .$ rf ."\n" .$ fr ;
193
212
@@ -343,7 +362,38 @@ protected function gatherData() {
343
362
344
363
}
345
364
346
- }
365
+ }
366
+
367
+ /**
368
+ * Finds the Lineages for the patterns
369
+ *
370
+ * @return {Array} an array of patterns with their lineages
371
+ */
372
+ protected function gatherLineages () {
373
+ $ this ->patternLineages = array ();
374
+
375
+ foreach ($ this ->patternPaths as $ patternType => $ patterns ) {
376
+
377
+ foreach ($ patterns as $ pattern => $ filename ) {
378
+
379
+ $ patternLineage = array ();
380
+ $ foundLineages = $ this ->getLineage ($ filename );
381
+
382
+ if (count ($ foundLineages ) > 0 ) {
383
+ foreach ($ foundLineages as $ lineage ) {
384
+ $ patternBits = explode ("- " ,$ lineage ,2 ); // BUG: this is making an assumption
385
+ $ path = str_replace ("/ " ,"- " ,$ this ->patternPaths [$ patternBits [0 ]][$ patternBits [1 ]]);
386
+ $ patternLineage [] = array ("lineagePattern " => $ lineage , "lineagePath " => "../../patterns/ " .$ path ."/ " .$ path .".html " );
387
+ }
388
+ }
389
+
390
+ $ this ->patternLineages [$ patternType ."- " .$ pattern ] = $ patternLineage ;
391
+
392
+ }
393
+
394
+ }
395
+
396
+ }
347
397
348
398
/**
349
399
* Finds Media Queries in CSS files in the source/css/ dir
@@ -356,7 +406,7 @@ protected function gatherMQs() {
356
406
357
407
foreach (glob (__DIR__ ."/../../source/css/*.css " ) as $ filename ) {
358
408
$ data = file_get_contents ($ filename );
359
- preg_match_all ("/(min|max)-width:( |) (([0-9]{1,5})(\.[0-9]{1,20}|)(px|em))/ " ,$ data ,$ matches );
409
+ preg_match_all ("/(min|max)-width:([ ]+)? (([0-9]{1,5})(\.[0-9]{1,20}|)(px|em))/ " ,$ data ,$ matches );
360
410
foreach ($ matches [3 ] as $ match ) {
361
411
if (!in_array ($ match ,$ mqs )) {
362
412
$ mqs [] = $ match ;
@@ -498,13 +548,13 @@ protected function gatherNavItems() {
498
548
*
499
549
* @return {Array} populates $this->patternPaths
500
550
* @return {Array} populates $this->patternTypes
551
+ * @return {Array} populates $this->patternLineages
501
552
*/
502
553
protected function gatherPatternPaths () {
503
554
504
555
// set-up vars
505
556
$ this ->patternPaths = array ();
506
557
$ this ->patternTypes = array ();
507
- $ this ->patternLineages = array ();
508
558
509
559
// get the pattern types
510
560
foreach (glob (__DIR__ .$ this ->sp ."/* " ,GLOB_ONLYDIR ) as $ patternType ) {
@@ -526,7 +576,6 @@ protected function gatherPatternPaths() {
526
576
$ pattern = $ this ->getPatternName ($ matches [1 ], false );
527
577
if (($ pattern [0 ] != "_ " ) && (!isset ($ patternTypePaths [$ pattern ]))) {
528
578
$ patternTypePaths [$ pattern ] = $ this ->getPath ($ filename );
529
- $ this ->patternLineages [$ patternTypeClean ."- " .$ pattern ] = $ this ->getLineage ($ filename );
530
579
}
531
580
}
532
581
@@ -536,7 +585,6 @@ protected function gatherPatternPaths() {
536
585
$ pattern = $ this ->getPatternName ($ matches [1 ], false );
537
586
if (($ pattern [0 ] != "_ " ) && (!isset ($ patternTypePaths [$ pattern ]))) {
538
587
$ patternTypePaths [$ pattern ] = $ this ->getPath ($ filename );
539
- $ this ->patternLineages [$ patternTypeClean ."- " .$ pattern ] = $ this ->getLineage ($ filename );
540
588
}
541
589
}
542
590
@@ -569,12 +617,26 @@ protected function gatherPartials() {
569
617
if (file_exists (__DIR__ ."/ " .$ this ->sp .$ path .".mustache " )) {
570
618
571
619
// create the pattern name & link, render the partial, and stick it all into the pattern array
572
- $ patternParts = explode ("/ " ,$ path );
573
- $ patternName = $ this ->getPatternName ($ patternParts [2 ]);
574
- $ patternLink = str_replace ("/ " ,"- " ,$ path )."/ " .str_replace ("/ " ,"- " ,$ path ).".html " ;
575
- $ patternPartial = $ this ->renderPattern ($ path .".mustache " );
576
- $ p ["partials " ][] = array ("patternName " => ucwords ($ patternName ), "patternLink " => $ patternLink , "patternPartialPath " => $ patternType ."- " .$ pattern , "patternPartial " => $ patternPartial );
620
+ $ patternParts = explode ("/ " ,$ path );
621
+ $ patternName = $ this ->getPatternName ($ patternParts [2 ]);
622
+ $ patternLink = str_replace ("/ " ,"- " ,$ path )."/ " .str_replace ("/ " ,"- " ,$ path ).".html " ;
623
+ $ patternCode = $ this ->renderPattern ($ path .".mustache " );
624
+ $ patternPartial = $ this ->getPatternPartial ($ path );
625
+ $ patternLineageExists = (count ($ this ->patternLineages [$ patternPartial ]) > 0 ) ? true : false ;
626
+ $ patternLineages = $ this ->patternLineages [$ patternPartial ];
627
+ $ patternCSSExists = $ this ->enableCSS ;
628
+ $ patternCSS = ($ this ->enableCSS ) ? $ this ->patternCSS [$ patternPartial ] : "" ;
577
629
630
+ $ p ["partials " ][] = array ("patternName " => ucwords ($ patternName ),
631
+ "patternLink " => $ patternLink ,
632
+ "patternPartialPath " => $ patternType ."- " .$ pattern ,
633
+ "patternPartial " => $ patternCode ,
634
+ "patternCSS " => $ patternCSS ,
635
+ "patternLineageExists " => $ patternLineageExists ,
636
+ "patternLineages " => $ patternLineages ,
637
+ "patternCSSExists " => $ patternCSSExists ,
638
+ "patternCSS " => $ patternCSS
639
+ );
578
640
}
579
641
580
642
}
@@ -614,10 +676,25 @@ protected function gatherPartialsByMatch($patternType, $patternSubType) {
614
676
if ($ patternParts [2 ][0 ] != "_ " ) {
615
677
616
678
// create the pattern name & link, render the partial, and stick it all into the pattern array
617
- $ patternName = $ this ->getPatternName ($ patternParts [2 ]);
618
- $ patternLink = str_replace ("/ " ,"- " ,$ path )."/ " .str_replace ("/ " ,"- " ,$ path ).".html " ;
619
- $ patternPartial = $ this ->renderPattern ($ path .".mustache " );
620
- $ p ["partials " ][] = array ("patternName " => ucwords ($ patternName ), "patternLink " => $ patternLink , "patternPartialPath " => str_replace (" " ,"- " ,$ patternTypeClean )."- " .str_replace (" " ,"- " ,$ patternName ), "patternPartial " => $ patternPartial );
679
+ $ patternName = $ this ->getPatternName ($ patternParts [2 ]);
680
+ $ patternLink = str_replace ("/ " ,"- " ,$ path )."/ " .str_replace ("/ " ,"- " ,$ path ).".html " ;
681
+ $ patternCode = $ this ->renderPattern ($ path .".mustache " );
682
+ $ patternPartial = $ this ->getPatternPartial ($ path );
683
+ $ patternLineages = $ this ->patternLineages [$ patternPartial ];
684
+ $ patternLineageExists = (count ($ patternLineages ) > 0 ) ? true : false ;
685
+ $ patternCSSExists = $ this ->enableCSS ;
686
+ $ patternCSS = ($ this ->enableCSS ) ? $ this ->patternCSS [$ patternPartial ] : "" ;
687
+
688
+ $ p ["partials " ][] = array ("patternName " => ucwords ($ patternName ),
689
+ "patternLink " => $ patternLink ,
690
+ "patternPartialPath " => str_replace (" " ,"- " ,$ patternTypeClean )."- " .str_replace (" " ,"- " ,$ patternName ),
691
+ "patternPartial " => $ patternCode ,
692
+ "patternCSS " => $ patternCSS ,
693
+ "patternLineageExists " => $ patternLineageExists ,
694
+ "patternLineages " => $ patternLineages ,
695
+ "patternCSSExists " => $ patternCSSExists ,
696
+ "patternCSS " => $ patternCSS
697
+ );
621
698
622
699
}
623
700
@@ -634,10 +711,11 @@ protected function gatherPartialsByMatch($patternType, $patternSubType) {
634
711
* @return {Array} a list of patterns
635
712
*/
636
713
protected function getLineage ($ filename ) {
637
- $ data = file_get_contents ($ filename );
714
+ $ data = file_get_contents (__DIR__ . $ this -> sp . $ filename. " .mustache " );
638
715
if (preg_match_all ('/{{>([ ]+)?([A-Za-z0-9-]+)([ ]+)?}}/ ' ,$ data ,$ matches )) {
639
716
return $ matches [2 ];
640
717
}
718
+ return array ();
641
719
}
642
720
643
721
/**
@@ -842,6 +920,19 @@ protected function ignoreDir($fileName) {
842
920
return false ;
843
921
}
844
922
923
+ /**
924
+ * Loads the CSS from source/css/ into CSS Rule Saver to be used for code view
925
+ */
926
+ protected function initializeCSSRuleSaver () {
927
+
928
+ $ this ->cssRuleSaver = new cssRuleSaver ;
929
+
930
+ foreach (glob (__DIR__ ."/../../source/css/*.css " ) as $ filename ) {
931
+ $ this ->cssRuleSaver ->loadCSS ($ filename );
932
+ }
933
+
934
+ }
935
+
845
936
/**
846
937
* Print out the data var. For debugging purposes
847
938
*
0 commit comments