Skip to content

Commit 02100c4

Browse files
committed
Handle nested contents in code blocks
1 parent 60a43fe commit 02100c4

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/Phug/AbstractNodeCompiler.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Phug\Formatter\Element\CodeElement;
77
use Phug\Formatter\Element\VariableElement;
88
use Phug\Formatter\ElementInterface;
9+
use Phug\Parser\Node\CommentNode;
910
use Phug\Parser\Node\TextNode;
1011
use Phug\Parser\NodeInterface as ParserNodeInterface;
1112

@@ -30,26 +31,35 @@ public function __construct(CompilerInterface $compiler)
3031

3132
protected function getTextChildren($node)
3233
{
34+
$children = array_filter($node->getChildren(), function (NodeInterface $node) {
35+
return !($node instanceof CommentNode);
36+
});
37+
3338
return implode("\n", array_map(function (TextNode $text) {
3439
return $text->getValue();
35-
}, $node->getChildren()));
40+
}, $children));
3641
}
3742

3843
public function getCompiler()
3944
{
4045
return $this->compiler;
4146
}
4247

43-
public function getCompiledChildren(NodeInterface $node, ElementInterface $element = null)
48+
public function getCompiledNodeList($nodeList, ElementInterface $element = null)
4449
{
4550
return array_values(array_filter(array_map(
4651
function (NodeInterface $childNode) use ($element) {
4752
return $this->compileParserNode($childNode, $element);
4853
},
49-
array_filter($node->getChildren())
54+
array_filter($nodeList)
5055
)));
5156
}
5257

58+
public function getCompiledChildren(NodeInterface $node, ElementInterface $element = null)
59+
{
60+
return $this->getCompiledNodeList($node->getChildren(), $element);
61+
}
62+
5363
public function compileNodeChildren(NodeInterface $node, ElementInterface $element = null)
5464
{
5565
$children = array_filter($node->getChildren());

src/Phug/Compiler/CodeCompiler.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Phug\Formatter\Element\CodeElement;
88
use Phug\Formatter\ElementInterface;
99
use Phug\Parser\Node\CodeNode;
10+
use Phug\Parser\Node\CommentNode;
11+
use Phug\Parser\Node\TextNode;
1012
use Phug\Parser\NodeInterface;
1113

1214
class CodeCompiler extends AbstractNodeCompiler
@@ -19,6 +21,25 @@ public function compileNode(NodeInterface $node, ElementInterface $parent = null
1921
);
2022
}
2123

22-
return new CodeElement($this->getTextChildren($node));
24+
$children = array_filter($node->getChildren(), function (NodeInterface $node) {
25+
return !($node instanceof CommentNode);
26+
});
27+
28+
$texts = array_filter($children, function (NodeInterface $node) {
29+
return $node instanceof TextNode;
30+
});
31+
32+
if (count($texts) === count($children)) {
33+
return new CodeElement($this->getTextChildren($node));
34+
}
35+
36+
$code = new CodeElement();
37+
if ($children[0] instanceof TextNode) {
38+
$code->setValue($children[0]->getValue());
39+
$children = array_slice($children, 1);
40+
}
41+
$code->setChildren($this->getCompiledNodeList($children, $parent));
42+
43+
return $code;
2344
}
2445
}

src/Phug/NodeCompilerInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99

1010
interface NodeCompilerInterface
1111
{
12+
/**
13+
* @param array $node
14+
* @param ElementInterface $parent
15+
*
16+
* @return array
17+
*/
18+
public function getCompiledNodeList($nodeList, ElementInterface $parent = null);
19+
1220
/**
1321
* @param NodeInterface $node
1422
* @param ElementInterface $parent

0 commit comments

Comments
 (0)