Skip to content

Commit 1eccd08

Browse files
committed
pattern data rules to handle ingesting pattern data
1 parent f59aae6 commit 1eccd08

File tree

7 files changed

+667
-0
lines changed

7 files changed

+667
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/*!
4+
* Pattern Data Rule Class
5+
*
6+
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
7+
* Licensed under the MIT license
8+
*
9+
*/
10+
11+
namespace PatternLab\PatternData;
12+
13+
class Rule {
14+
15+
protected $depthProp;
16+
protected $extProp;
17+
protected $isDirProp;
18+
protected $isFileProp;
19+
protected $searchProp;
20+
protected $ignoreProp;
21+
22+
public function __construct($options) {
23+
24+
// nothing here yet
25+
26+
}
27+
28+
/**
29+
* Test the Pattern Data Rules to see if a Rule matches the supplied values
30+
* @param {Integer} the depth of the item
31+
* @param {String} the extension of the item
32+
* @param {Boolean} if the item is a directory
33+
* @param {Boolean} if the item is a file
34+
* @param {String} the name of the item
35+
*
36+
* @return {Boolean} whether the test was succesful or not
37+
*/
38+
public function test($depth, $ext, $isDir, $isFile, $name) {
39+
40+
if (($this->depthProp != 3) && ($depth != $this->depthProp)) {
41+
return false;
42+
}
43+
44+
if (($this->compareProp($ext,$this->extProp)) && ($isDir == $this->isDirProp) && ($isFile == $this->isFileProp)) {
45+
$result = true;
46+
if ($this->searchProp != "") {
47+
$result = $this->compareProp($name,$this->searchProp);
48+
}
49+
if ($this->ignoreProp != "") {
50+
$result = ($this->compareProp($name,$this->ignoreProp)) ? false : true;
51+
}
52+
return $result;
53+
}
54+
55+
return false;
56+
57+
}
58+
59+
/**
60+
* Compare the search and ignore props against the name.
61+
* Can use && or || in the comparison
62+
* @param {String} the name of the item
63+
* @param {String} the value of the property to compare
64+
*
65+
* @return {Boolean} whether the compare was successful or not
66+
*/
67+
protected function compareProp($name,$propCompare) {
68+
69+
if (($name == "") && ($propCompare == "")) {
70+
$result = true;
71+
} else if ((($name == "") && ($propCompare != "")) || (($name != "") && ($propCompare == ""))) {
72+
$result = false;
73+
} else if (strpos($propCompare,"&&") !== false) {
74+
$result = true;
75+
$props = explode("&&",$propCompare);
76+
foreach ($props as $prop) {
77+
$pos = (strpos($name,$prop) !== false) ? true : false;
78+
$result = ($result && $pos);
79+
}
80+
} else if (strpos($propCompare,"||") !== false) {
81+
$result = false;
82+
$props = explode("||",$propCompare);
83+
foreach ($props as $prop) {
84+
$pos = (strpos($name,$prop) !== false) ? true : false;
85+
$result = ($result || $pos);
86+
}
87+
} else {
88+
$result = (strpos($name,$propCompare) !== false) ? true : false;
89+
}
90+
91+
return $result;
92+
93+
}
94+
95+
/**
96+
* Get the name for a given pattern sans any possible digits used for reordering
97+
* @param {String} the pattern based on the filesystem name
98+
* @param {Boolean} whether or not to strip slashes from the pattern name
99+
*
100+
* @return {String} a lower-cased version of the pattern name
101+
*/
102+
protected function getPatternName($pattern, $clean = true) {
103+
$patternBits = explode("-",$pattern,2);
104+
$patternName = (((int)$patternBits[0] != 0) || ($patternBits[0] == '00')) ? $patternBits[1] : $pattern;
105+
return ($clean) ? (str_replace("-"," ",$patternName)) : $patternName;
106+
}
107+
108+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/*!
4+
* Pattern Data Documentation Rule Class
5+
*
6+
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
7+
* Licensed under the MIT license
8+
*
9+
* If a documentation file (.md) it is parsed and the info added to PatternData::$store
10+
*
11+
*/
12+
13+
namespace PatternLab\PatternData\Rules;
14+
15+
use \PatternLab\Config;
16+
use \PatternLab\PatternData;
17+
use \PatternLab\Parsers\Documentation;
18+
19+
class DocumentationRule extends \PatternLab\PatternData\Rule {
20+
21+
public function __construct($options) {
22+
23+
parent::__construct($options);
24+
25+
$this->depthProp = 3; // 3 means that depth won't be checked
26+
$this->extProp = "md";
27+
$this->isDirProp = false;
28+
$this->isFileProp = true;
29+
$this->searchProp = "";
30+
$this->ignoreProp = "";
31+
32+
}
33+
34+
public function run($depth, $ext, $path, $pathName, $name) {
35+
36+
// load default vars
37+
$patternType = PatternData::$patternType;
38+
$patternTypeDash = PatternData::$patternTypeDash;
39+
$dirSep = PatternData::$dirSep;
40+
41+
// set-up the names
42+
$docFull = $name; // 00-colors.md
43+
$doc = str_replace(".".$this->extProp,"",$docFull); // 00-colors
44+
$docDash = $this->getPatternName(str_replace("_","",$doc),false); // colors
45+
$docPartial = $patternTypeDash."-".$docDash;
46+
47+
// make sure the pattern isn't hidden
48+
$hidden = ($docFull[0] == "_");
49+
50+
// if the pattern isn't hidden do stuff
51+
if (!$hidden) {
52+
53+
// parse data
54+
$text = file_get_contents(__DIR__."/../..".Config::$options["patternSourceDir"]."/".$pathName);
55+
list($yaml,$markdown) = Documentation::parse($text);
56+
57+
// grab the title and unset it from the yaml so it doesn't get duped in the meta
58+
if (isset($yaml["title"])) {
59+
$title = $yaml["title"];
60+
unset($yaml["title"]);
61+
}
62+
63+
// figure out if this is a pattern subtype
64+
$patternSubtypeDoc = false;
65+
if ($depth == 1) {
66+
// go through all of the directories to see if this one matches our doc
67+
foreach (glob(__DIR__."/../..".Config::$options["patternSourceDir"].$patternType."/*",GLOB_ONLYDIR) as $dir) {
68+
$dir = str_replace(__DIR__."/../..".Config::$options["patternSourceDir"].$patternType."/","",$dir);
69+
if ($dir == $doc) {
70+
$patternSubtypeDoc = true;
71+
break;
72+
}
73+
}
74+
75+
}
76+
77+
$category = ($patternSubtypeDoc) ? "patternSubtype" : "pattern";
78+
$patternStoreKey = ($patternSubtypeDoc) ? $docPartial."-plsubtype" : $docPartial;
79+
80+
$patternStoreData = array("category" => $category,
81+
"nameClean" => $title,
82+
"desc" => $markdown,
83+
"descExists" => true,
84+
"meta" => $yaml,
85+
"full" => $doc);
86+
87+
// if the pattern data store already exists make sure this data overwrites it
88+
PatternData::$store[$patternStoreKey] = isset(PatternData::$store[$patternStoreKey]) ? array_replace_recursive(PatternData::$store[$patternStoreKey],$patternStoreData) : $patternStoreData;
89+
90+
}
91+
92+
}
93+
94+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*!
4+
* Pattern Data Info List Items Rule Class
5+
*
6+
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
7+
* Licensed under the MIT license
8+
*
9+
* If it's a *.listitems.json or *.listitems.yaml file it adds the data to PatternData::$store
10+
*
11+
*/
12+
13+
namespace PatternLab\PatternData\Rules;
14+
15+
use \PatternLab\Config;
16+
use \PatternLab\Data;
17+
use \PatternLab\PatternData;
18+
use \PatternLab\JSON;
19+
use \Symfony\Component\Yaml\Yaml;
20+
21+
class PatternInfoListItemsRule extends \PatternLab\PatternData\Rule {
22+
23+
public function __construct($options) {
24+
25+
parent::__construct($options);
26+
27+
$this->depthProp = 3; // 3 means that depth won't be checked
28+
$this->extProp = "json||yaml";
29+
$this->isDirProp = false;
30+
$this->isFileProp = true;
31+
$this->searchProp = ".listitems.";
32+
$this->ignoreProp = "";
33+
34+
}
35+
36+
public function run($depth, $ext, $path, $pathName, $name) {
37+
38+
// load default vars
39+
$patternTypeDash = PatternData::$patternTypeDash;
40+
41+
// set-up the names
42+
$patternFull = $name; // foo.listitems.json
43+
$pattern = str_replace(".listitems.".$ext,"",$patternFull); // foo
44+
$patternDash = $this->getPatternName($pattern,false); // foo
45+
$patternPartial = $patternTypeDash."-".$patternDash; // atoms-foo
46+
47+
// should this pattern get rendered?
48+
$hidden = ($patternFull[0] == "_");
49+
50+
if (!$hidden) {
51+
52+
$patternStoreData = array("category" => "pattern");
53+
54+
$data = Data::getListItems($pathName,$ext);
55+
$patternStoreData["listItems"] = $data;
56+
57+
}
58+
59+
// create a key for the data store
60+
$patternStoreKey = $patternPartial;
61+
62+
// if the pattern data store already exists make sure it is merged and overwrites this data
63+
PatternData::$store[$patternStoreKey] = isset(PatternData::$store[$patternStoreKey]) ? array_replace_recursive(PatternData::$store[$patternStoreKey],$patternStoreData) : $patternStoreData;
64+
65+
}
66+
67+
}
68+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*!
4+
* Pattern Data Info Rule Class
5+
*
6+
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
7+
* Licensed under the MIT license
8+
*
9+
* If it's a *.json or *.yaml file without a ~ it adds the data to PatternData::$store
10+
*
11+
*/
12+
13+
namespace PatternLab\PatternData\Rules;
14+
15+
use \PatternLab\Config;
16+
use \PatternLab\PatternData;
17+
use \PatternLab\JSON;
18+
use \Symfony\Component\Yaml\Yaml;
19+
20+
class PatternInfoRule extends \PatternLab\PatternData\Rule {
21+
22+
public function __construct($options) {
23+
24+
parent::__construct($options);
25+
26+
$this->depthProp = 3; // 3 means that depth won't be checked
27+
$this->extProp = "json||yaml";
28+
$this->isDirProp = false;
29+
$this->isFileProp = true;
30+
$this->searchProp = "";
31+
$this->ignoreProp = "~";
32+
33+
}
34+
35+
public function run($depth, $ext, $path, $pathName, $name) {
36+
37+
// load default vars
38+
$patternTypeDash = PatternData::$patternTypeDash;
39+
40+
// set-up the names
41+
$patternFull = $name; // foo.json
42+
$pattern = str_replace(".".$ext,"",$patternFull); // foo
43+
$patternDash = $this->getPatternName($pattern,false); // foo
44+
$patternPartial = $patternTypeDash."-".$patternDash; // atoms-foo
45+
46+
// should this pattern get rendered?
47+
$hidden = ($patternFull[0] == "_");
48+
49+
if (!$hidden) {
50+
51+
$patternStoreData = array("category" => "pattern");
52+
53+
$file = file_get_contents(__DIR__."/../..".Config::$options["patternSourceDir"]."/".$pathName);
54+
55+
if ($ext == "json") {
56+
$data = json_decode($file,true);
57+
if ($jsonErrorMessage = JSON::hasError()) {
58+
JSON::lastErrorMsg($name,$jsonErrorMessage,$data);
59+
}
60+
} else {
61+
$data = Yaml::parse($file);
62+
}
63+
64+
$patternStoreData["data"] = $data;
65+
66+
// create a key for the data store
67+
$patternStoreKey = $patternPartial;
68+
69+
// if the pattern data store already exists make sure it is merged and overwrites this data
70+
PatternData::$store[$patternStoreKey] = isset(PatternData::$store[$patternStoreKey]) ? array_replace_recursive(PatternData::$store[$patternStoreKey],$patternStoreData) : $patternStoreData;
71+
72+
}
73+
74+
}
75+
76+
}
77+

0 commit comments

Comments
 (0)