Skip to content

Commit 49317d6

Browse files
committed
feature: add ability for Pattern Lab to automatically compile inline templates / patterns embedded in markdown files. Living Design System docs FTW
1 parent 1fa44ec commit 49317d6

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/PatternLab/PatternData/Rules/DocumentationRule.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use \PatternLab\PatternData;
1717
use \PatternLab\Parsers\Documentation;
1818
use \PatternLab\Timer;
19+
use \PatternLab\Data;
20+
use \PatternLab\PatternData\Exporters\PatternPathSrcExporter;
21+
use \PatternLab\PatternEngine;
1922

2023
class DocumentationRule extends \PatternLab\PatternData\Rule {
2124

@@ -49,8 +52,7 @@ public function run($depth, $ext, $path, $pathName, $name) {
4952

5053
// parse data
5154
$text = file_get_contents($patternSourceDir.DIRECTORY_SEPARATOR.$pathName);
52-
list($yaml,$markdown) = Documentation::parse($text);
53-
55+
5456
// grab the title and unset it from the yaml so it doesn't get duped in the meta
5557
if (isset($yaml["title"])) {
5658
$title = $yaml["title"];
@@ -73,7 +75,34 @@ public function run($depth, $ext, $path, $pathName, $name) {
7375

7476
$category = ($patternSubtypeDoc) ? "patternSubtype" : "pattern";
7577
$patternStoreKey = ($patternSubtypeDoc) ? $docPartial."-plsubtype" : $docPartial;
76-
78+
79+
/**
80+
* Setup the Pattern Loader so we can pre-render template markup used
81+
* in our markdown files, prior to any markup getting parsed.
82+
* Taken from Builder.php
83+
*/
84+
$ppdExporter = new PatternPathSrcExporter();
85+
$patternPathSrc = $ppdExporter->run();
86+
$options = array();
87+
$options["patternPaths"] = $patternPathSrc;
88+
$patternEngineBasePath = PatternEngine::getInstance()->getBasePath();
89+
$patternLoaderClass = $patternEngineBasePath . "\Loaders\PatternLoader";
90+
$patternLoader = new $patternLoaderClass($options);
91+
92+
93+
// Setup the default pattern data.
94+
$data = Data::getPatternSpecificData($patternStoreKey);
95+
96+
// Render the markdown content as a pattern, piping in the pattern-specific data from above.
97+
$text = $patternLoader->render(array(
98+
"pattern" => $text,
99+
"data" => $data
100+
));
101+
102+
// Finally parse the resulting content as normal markup; continue as usual.
103+
list($yaml,$markdown) = Documentation::parse($text);
104+
105+
77106
$patternStoreData = array("category" => $category,
78107
"desc" => trim($markdown),
79108
"descExists" => true,

0 commit comments

Comments
 (0)