1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace ApiGen\Renderer\Latte; |
4: | |
5: | use Generator; |
6: | use Latte; |
7: | use Latte\Compiler\Node; |
8: | use Latte\Compiler\Nodes\AreaNode; |
9: | use Latte\Compiler\Nodes\TextNode; |
10: | use Latte\Compiler\NodeTraverser; |
11: | use Latte\Compiler\PrintContext; |
12: | use Latte\Compiler\Tag; |
13: | use Latte\Compiler\TemplateParser; |
14: | use Nette\Utils\Strings; |
15: | |
16: | use function assert; |
17: | |
18: | |
19: | class LattePreNode extends Latte\Compiler\Nodes\StatementNode |
20: | { |
21: | public function __construct( |
22: | public AreaNode $content, |
23: | ) { |
24: | } |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | public static function create(Tag $tag, TemplateParser $parser): Generator |
31: | { |
32: | [$content] = yield; |
33: | $transformed = (new NodeTraverser)->traverse($content, self::removeWhitespace(...)); |
34: | assert($transformed instanceof AreaNode); |
35: | |
36: | return new self($transformed); |
37: | } |
38: | |
39: | |
40: | public function print(PrintContext $context): string |
41: | { |
42: | return $this->content->print($context); |
43: | } |
44: | |
45: | |
46: | public function &getIterator(): Generator |
47: | { |
48: | yield $this->content; |
49: | } |
50: | |
51: | |
52: | protected static function removeWhitespace(Node $node): Node |
53: | { |
54: | return $node instanceof TextNode ? new TextNode(Strings::replace($node->content, '#[\n\t]++#')) : $node; |
55: | } |
56: | } |
57: | |