-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathDocumentationRule.php
More file actions
129 lines (103 loc) · 4.22 KB
/
DocumentationRule.php
File metadata and controls
129 lines (103 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/*!
* Pattern Data Documentation Rule Class
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* If a documentation file (.md) it is parsed and the info added to PatternData::$store
*
*/
namespace PatternLab\PatternData\Rules;
use \PatternLab\Config;
use \PatternLab\PatternData;
use \PatternLab\Parsers\Documentation;
use \PatternLab\Timer;
class DocumentationRule extends \PatternLab\PatternData\Rule {
public function __construct($options) {
parent::__construct($options);
$this->depthProp = 3; // 3 means that depth won't be checked
$this->extProp = "md";
$this->isDirProp = false;
$this->isFileProp = true;
$this->searchProp = "";
$this->ignoreProp = "";
}
public function run($depth, $ext, $path, $pathName, $name) {
// load default vars
$patternType = PatternData::getPatternType();
$patternTypeDash = PatternData::getPatternTypeDash();
$dirSep = PatternData::getDirSep();
// set-up the names, $name == 00-colors.md
$doc = str_replace(".".$this->extProp,"",$name); // 00-colors
$docDash = $this->getPatternName(str_replace("_","",$doc),false); // colors
$docPartial = $patternTypeDash."-".$docDash;
// default vars
$patternSourceDir = Config::getOption("patternSourceDir");
// parse data
$text = file_get_contents($patternSourceDir.DIRECTORY_SEPARATOR.$pathName);
list($yaml,$markdown) = Documentation::parse($text);
// grab the title and unset it from the yaml so it doesn't get duped in the meta
if (isset($yaml["title"])) {
$title = $yaml["title"];
unset($yaml["title"]);
}
// figure out if this is a top level pattern type or pattern subtype
$patternSubtypeDoc = false;
$patternTypeDoc = false;
if ($depth == 0) {
foreach (glob($patternSourceDir.DIRECTORY_SEPARATOR.$patternType,GLOB_ONLYDIR) as $dir) {
$dir = str_replace($patternSourceDir.DIRECTORY_SEPARATOR,"",$dir);
if ($dir == $doc) {
$patternTypeDoc = true;
break;
}
}
} else if ($depth == 1){
// go through all of the directories to see if this one matches our doc
foreach (glob($patternSourceDir.DIRECTORY_SEPARATOR.$patternType.DIRECTORY_SEPARATOR."*",GLOB_ONLYDIR) as $dir) {
$dir = str_replace($patternSourceDir.DIRECTORY_SEPARATOR.$patternType.DIRECTORY_SEPARATOR,"",$dir);
if ($dir == $doc) {
$patternSubtypeDoc = true;
break;
}
}
}
$category = "pattern"; // By default, make the pattern type a "pattern"
$patternStoreKey = $docPartial;
// Update if patternType or subtype
if ($patternTypeDoc) {
$category = "patternType";
$patternStoreKey = $patternTypeDash."-pltype";
// organisms-pltype
} else if ($patternSubtypeDoc){
$category = "patternSubtype";
$patternStoreKey = $docPartial."-plsubtype";
}
$patternStoreData = array("category" => $category,
"desc" => trim($markdown),
"descExists" => true,
"meta" => $yaml,
"full" => $doc);
// can set `title: My Cool Pattern` instead of lifting from file name
if (isset($title)) {
$patternStoreData["nameClean"] = $title;
}
$availableKeys = [
'state', // can use `state: inprogress` instead of `button@inprogress.mustache`
'hidden', // setting to `true`, removes from menu and viewall, which is same as adding `_` prefix
'noviewall', // setting to `true`, removes from view alls but keeps in menu, which is same as adding `-` prefix
'order', // @todo implement order
'tags', // not implemented, awaiting spec approval and integration with styleguide kit. adding to be in sync with Node version.
'links', // not implemented, awaiting spec approval and integration with styleguide kit. adding to be in sync with Node version.
];
foreach ($availableKeys as $key) {
if (isset($yaml[$key])) {
$patternStoreData[$key] = $yaml[$key];
}
}
// if the pattern data store already exists make sure this data overwrites it
$patternStoreData = (PatternData::checkOption($patternStoreKey)) ? array_replace_recursive(PatternData::getOption($patternStoreKey),$patternStoreData) : $patternStoreData;
PatternData::setOption($patternStoreKey, $patternStoreData);
}
}