Skip to content

Commit 0e9799c

Browse files
Add a way of asking for the parsed grammar in Transformer
1 parent fe80b5c commit 0e9799c

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Phiki\Adapters\CommonMark\Transformers;
4+
5+
use Phiki\Contracts\RequiresGrammarInterface;
6+
use Phiki\Transformers\AbstractTransformer;
7+
use Phiki\Transformers\Concerns\RequiresGrammar;
8+
9+
class AnnotationsTransformer extends AbstractTransformer implements RequiresGrammarInterface
10+
{
11+
use RequiresGrammar;
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Phiki\Contracts;
4+
5+
use Phiki\Grammar\ParsedGrammar;
6+
7+
interface RequiresGrammarInterface
8+
{
9+
/**
10+
* Set the parsed grammar for the transformer.
11+
*/
12+
public function withGrammar(ParsedGrammar $grammar): void;
13+
}

src/Output/Html/PendingHtmlOutput.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Phiki\Output\Html;
44

55
use Closure;
6+
use Phiki\Contracts\RequiresGrammarInterface;
67
use Phiki\Contracts\TransformerInterface;
78
use Phiki\Grammar\ParsedGrammar;
89
use Phiki\Phast\ClassList;
@@ -173,6 +174,10 @@ public function __toString(): string
173174

174175
foreach ($this->transformers as $transformer) {
175176
$transformer->withMeta($this->meta);
177+
178+
if ($transformer instanceof RequiresGrammarInterface) {
179+
$transformer->withGrammar($this->grammar);
180+
}
176181
}
177182

178183
[$code] = $this->callTransformerMethod('preprocess', $this->code);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Phiki\Transformers\Concerns;
4+
5+
use Phiki\Grammar\ParsedGrammar;
6+
7+
trait RequiresGrammar
8+
{
9+
protected ParsedGrammar $grammar;
10+
11+
public function withGrammar(ParsedGrammar $grammar): void
12+
{
13+
$this->grammar = $grammar;
14+
}
15+
}

0 commit comments

Comments
 (0)