Skip to content

Commit

Permalink
Merge pull request #123 from weaverryan/add-code-node-options
Browse files Browse the repository at this point in the history
Finish add code node options
  • Loading branch information
greg0ire authored Dec 5, 2020
2 parents 596ace1 + ab10cf1 commit 10a5e33
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/Directives/CodeBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function process(

if ($node instanceof CodeNode) {
$node->setLanguage(trim($data));
$node->setOptions($options);
}

if ($variable !== '') {
Expand Down
19 changes: 19 additions & 0 deletions lib/Nodes/CodeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class CodeNode extends Node
/** @var string|null */
protected $language = null;

/** @var string[] */
private $options;

/**
* @param string[] $lines
*/
Expand Down Expand Up @@ -47,4 +50,20 @@ public function isRaw(): bool
{
return $this->raw;
}

/**
* @param string[] $options
*/
public function setOptions(array $options = []): void
{
$this->options = $options;
}

/**
* @return string[]
*/
public function getOptions(): array
{
return $this->options;
}
}
18 changes: 18 additions & 0 deletions tests/Parser/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ public function testCodeNode(): void
self::assertSame("A\nB\n C", trim($nodes[0]->getValueString()));
}

/**
* Testing that code node options are parsed
*/
public function testCodeNodeWithOptions(): void
{
$document = $this->parse('code-block-with-options.rst');

$nodes = $document->getNodes(static function (Node $node): bool {
return $node instanceof CodeNode;
});

self::assertSame(1, count($nodes));
$codeNode = $nodes[0];
assert($codeNode instanceof CodeNode);
self::assertSame("A\nB\nC", trim($codeNode->getValueString()));
self::assertSame(['name' => 'My Very Best Code'], $codeNode->getOptions());
}

/**
* Testing paragraph nodes
*/
Expand Down
7 changes: 7 additions & 0 deletions tests/Parser/files/code-block-with-options.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

.. code-block::
:name: My Very Best Code
A
B
C

0 comments on commit 10a5e33

Please sign in to comment.