Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.

Commit 4050920

Browse files
committed
generateAnnotations support so markdown can be used
1 parent fb99498 commit 4050920

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

core/lib/PatternLab/Builder.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use \PatternLab\Config;
1616
use \PatternLab\Data;
17+
use \PatternLab\Parsers\Documentation;
1718
use \PatternLab\PatternData\Exporters\NavItemsExporter;
1819
use \PatternLab\PatternData\Exporters\PatternPartialsExporter;
1920
use \PatternLab\PatternData\Exporters\PatternPathDestsExporter;
@@ -73,6 +74,70 @@ protected function gatherMQs() {
7374

7475
}
7576

77+
protected function generateAnnotations() {
78+
79+
$annotations = array();
80+
$annotations["comments"] = array();
81+
82+
// iterate over all of the files in the annotations dir
83+
$directoryIterator = new \RecursiveDirectoryIterator(Config::$options["sourceDir"]."/_annotations");
84+
$objects = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST);
85+
86+
// make sure dots are skipped
87+
$objects->setFlags(\FilesystemIterator::SKIP_DOTS);
88+
89+
foreach ($objects as $name => $object) {
90+
91+
// if it's an .md file parse and generate the proper info
92+
if ($object->isFile() && ($object->getExtension() == "md")) {
93+
94+
$data = array();
95+
$data[0] = array();
96+
97+
$text = file_get_contents($object->getPathname());
98+
list($yaml,$markdown) = Documentation::parse($text);
99+
100+
if (isset($yaml["el"]) || isset($yaml["selector"])) {
101+
$data[0]["el"] = (isset($yaml["el"])) ? $yaml["el"] : $yaml["selector"];
102+
} else {
103+
$data[0]["el"] = "#someimpossibleselector";
104+
}
105+
$data[0]["title"] = isset($yaml["title"]) ? $yaml["title"] : "";
106+
$data[0]["comment"] = $markdown;
107+
108+
$annotations["comments"] = array_merge($annotations["comments"],$data);
109+
110+
}
111+
112+
}
113+
114+
// read in the old style annotations.js, modify the data and generate JSON array to merge
115+
if (file_exists(Config::$options["sourceDir"]."/_annotations/annotations.js")) {
116+
$text = file_get_contents(Config::$options["sourceDir"]."/_annotations/annotations.js");
117+
$text = str_replace("var comments = ","",$text);
118+
$text = rtrim($text,";");
119+
$data = json_decode($text,true);
120+
if ($jsonErrorMessage = JSON::hasError()) {
121+
JSON::lastErrorMsg("_annotations/annotations.js",$jsonErrorMessage,$data);
122+
}
123+
}
124+
125+
// merge in any data from the old file
126+
$annotations["comments"] = array_merge($annotations["comments"],$data["comments"]);
127+
128+
// encode the content so it can be written out
129+
$json = json_encode($annotations);
130+
131+
// make sure annotations/ exists
132+
if (!is_dir(Config::$options["publicDir"]."/annotations")) {
133+
mkdir(Config::$options["publicDir"]."/annotations");
134+
}
135+
136+
// write out the new annotations.js file
137+
file_put_contents(Config::$options["publicDir"]."/annotations/annotations.js","var comments = ".$json.";");
138+
139+
}
140+
76141
/**
77142
* Generates the index page and style guide
78143
*/

0 commit comments

Comments
 (0)