mirror of
https://github.com/pattern-lab/patternlab-php.git
synced 2025-04-26 17:03:05 +02:00
generateAnnotations support so markdown can be used
This commit is contained in:
parent
fb99498d33
commit
40509206a6
@ -14,6 +14,7 @@ namespace PatternLab;
|
||||
|
||||
use \PatternLab\Config;
|
||||
use \PatternLab\Data;
|
||||
use \PatternLab\Parsers\Documentation;
|
||||
use \PatternLab\PatternData\Exporters\NavItemsExporter;
|
||||
use \PatternLab\PatternData\Exporters\PatternPartialsExporter;
|
||||
use \PatternLab\PatternData\Exporters\PatternPathDestsExporter;
|
||||
@ -73,6 +74,70 @@ class Builder {
|
||||
|
||||
}
|
||||
|
||||
protected function generateAnnotations() {
|
||||
|
||||
$annotations = array();
|
||||
$annotations["comments"] = array();
|
||||
|
||||
// iterate over all of the files in the annotations dir
|
||||
$directoryIterator = new \RecursiveDirectoryIterator(Config::$options["sourceDir"]."/_annotations");
|
||||
$objects = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST);
|
||||
|
||||
// make sure dots are skipped
|
||||
$objects->setFlags(\FilesystemIterator::SKIP_DOTS);
|
||||
|
||||
foreach ($objects as $name => $object) {
|
||||
|
||||
// if it's an .md file parse and generate the proper info
|
||||
if ($object->isFile() && ($object->getExtension() == "md")) {
|
||||
|
||||
$data = array();
|
||||
$data[0] = array();
|
||||
|
||||
$text = file_get_contents($object->getPathname());
|
||||
list($yaml,$markdown) = Documentation::parse($text);
|
||||
|
||||
if (isset($yaml["el"]) || isset($yaml["selector"])) {
|
||||
$data[0]["el"] = (isset($yaml["el"])) ? $yaml["el"] : $yaml["selector"];
|
||||
} else {
|
||||
$data[0]["el"] = "#someimpossibleselector";
|
||||
}
|
||||
$data[0]["title"] = isset($yaml["title"]) ? $yaml["title"] : "";
|
||||
$data[0]["comment"] = $markdown;
|
||||
|
||||
$annotations["comments"] = array_merge($annotations["comments"],$data);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// read in the old style annotations.js, modify the data and generate JSON array to merge
|
||||
if (file_exists(Config::$options["sourceDir"]."/_annotations/annotations.js")) {
|
||||
$text = file_get_contents(Config::$options["sourceDir"]."/_annotations/annotations.js");
|
||||
$text = str_replace("var comments = ","",$text);
|
||||
$text = rtrim($text,";");
|
||||
$data = json_decode($text,true);
|
||||
if ($jsonErrorMessage = JSON::hasError()) {
|
||||
JSON::lastErrorMsg("_annotations/annotations.js",$jsonErrorMessage,$data);
|
||||
}
|
||||
}
|
||||
|
||||
// merge in any data from the old file
|
||||
$annotations["comments"] = array_merge($annotations["comments"],$data["comments"]);
|
||||
|
||||
// encode the content so it can be written out
|
||||
$json = json_encode($annotations);
|
||||
|
||||
// make sure annotations/ exists
|
||||
if (!is_dir(Config::$options["publicDir"]."/annotations")) {
|
||||
mkdir(Config::$options["publicDir"]."/annotations");
|
||||
}
|
||||
|
||||
// write out the new annotations.js file
|
||||
file_put_contents(Config::$options["publicDir"]."/annotations/annotations.js","var comments = ".$json.";");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the index page and style guide
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user