Skip to content

Commit

Permalink
Extract span parser from spannode
Browse files Browse the repository at this point in the history
Nodes should be value objects without any dependencies. The spannode
still had a dependency.
  • Loading branch information
jaapio committed Apr 6, 2022
1 parent 5327beb commit ee7f168
Show file tree
Hide file tree
Showing 24 changed files with 154 additions and 102 deletions.
Expand Up @@ -21,7 +21,7 @@ final class Paragraph extends AbstractBlock
*/
public function parse(MarkupLanguageParser $parser, NodeWalker $walker): Nodes\Node
{
$context = new ParagraphNode(SpanNode::create($parser, ''));
$context = new ParagraphNode(new SpanNode('', []));

while ($event = $walker->next()) {
$node = $event->getNode();
Expand Down
Expand Up @@ -14,9 +14,9 @@
namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\SpanNode;
use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser;
use phpDocumentor\Guides\RestructuredText\Nodes\AdmonitionNode;
use phpDocumentor\Guides\RestructuredText\Span\SpanParser;

abstract class AbstractAdmonitionDirective extends SubDirective
{
Expand All @@ -25,11 +25,13 @@ abstract class AbstractAdmonitionDirective extends SubDirective

/** @var string */
private $text;
private SpanParser $spanParser;

public function __construct(string $name, string $text)
public function __construct(string $name, string $text, SpanParser $spanParser)
{
$this->name = $name;
$this->text = $text;
$this->spanParser = $spanParser;
}

final public function processSub(
Expand All @@ -42,7 +44,7 @@ final public function processSub(
return (new AdmonitionNode(
$this->name,
$this->text,
$document ?? SpanNode::create($parser, $data)
$document ?? $this->spanParser->parse($data, $parser->getEnvironment())
))->withOptions($options);
}

Expand Down
Expand Up @@ -13,10 +13,12 @@

namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\RestructuredText\Span\SpanParser;

class AdmonitionDirective extends AbstractAdmonitionDirective
{
public function __construct()
public function __construct(SpanParser $spanParser)
{
parent::__construct('admonition', 'Admonition');
parent::__construct('admonition', 'Admonition', $spanParser);
}
}
Expand Up @@ -13,10 +13,12 @@

namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\RestructuredText\Span\SpanParser;

class BestPracticeDirective extends AbstractAdmonitionDirective
{
public function __construct()
public function __construct(SpanParser $spanParser)
{
parent::__construct('best-practice', 'Best Practice');
parent::__construct('best-practice', 'Best Practice', $spanParser);
}
}
Expand Up @@ -13,10 +13,12 @@

namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\RestructuredText\Span\SpanParser;

class CautionDirective extends AbstractAdmonitionDirective
{
public function __construct()
public function __construct(SpanParser $spanParser)
{
parent::__construct('caution', 'Caution');
parent::__construct('caution', 'Caution', $spanParser);
}
}
Expand Up @@ -13,10 +13,12 @@

namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\RestructuredText\Span\SpanParser;

class HintDirective extends AbstractAdmonitionDirective
{
public function __construct()
public function __construct(SpanParser $spanParser)
{
parent::__construct('hint', 'Hint');
parent::__construct('hint', 'Hint', $spanParser);
}
}
Expand Up @@ -13,10 +13,12 @@

namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\RestructuredText\Span\SpanParser;

class ImportantDirective extends AbstractAdmonitionDirective
{
public function __construct()
public function __construct(SpanParser $spanParser)
{
parent::__construct('important', 'Important');
parent::__construct('important', 'Important', $spanParser);
}
}
Expand Up @@ -13,10 +13,12 @@

namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\RestructuredText\Span\SpanParser;

class NoteDirective extends AbstractAdmonitionDirective
{
public function __construct()
public function __construct(SpanParser $spanParser)
{
parent::__construct('note', 'Note');
parent::__construct('note', 'Note', $spanParser);
}
}
Expand Up @@ -5,8 +5,8 @@
namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\SpanNode;
use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser;
use phpDocumentor\Guides\RestructuredText\Span\SpanParser;

/**
* The Replace directive will set the variables for the spans
Expand All @@ -15,6 +15,13 @@
*/
class Replace extends Directive
{
private SpanParser $spanParser;

public function __construct(SpanParser $spanParser)
{
$this->spanParser = $spanParser;
}

public function getName(): string
{
return 'replace';
Expand All @@ -29,6 +36,6 @@ public function processNode(
string $data,
array $options
): Node {
return SpanNode::create($parser, $data);
return $this->spanParser->parse($data, $parser->getEnvironment());
}
}
Expand Up @@ -13,10 +13,12 @@

namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\RestructuredText\Span\SpanParser;

class SeeAlsoDirective extends AbstractAdmonitionDirective
{
public function __construct()
public function __construct(SpanParser $spanParser)
{
parent::__construct('seealso', 'See also');
parent::__construct('seealso', 'See also', $spanParser);
}
}
Expand Up @@ -13,10 +13,12 @@

namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\RestructuredText\Span\SpanParser;

class TipDirective extends AbstractAdmonitionDirective
{
public function __construct()
public function __construct(SpanParser $spanParser)
{
parent::__construct('tip', 'Tip');
parent::__construct('tip', 'Tip', $spanParser);
}
}
Expand Up @@ -13,10 +13,12 @@

namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\RestructuredText\Span\SpanParser;

class WarningDirective extends AbstractAdmonitionDirective
{
public function __construct()
public function __construct(SpanParser $spanParser)
{
parent::__construct('warning', 'Warning');
parent::__construct('warning', 'Warning', $spanParser);
}
}
Expand Up @@ -8,8 +8,8 @@
use phpDocumentor\Guides\Nodes\DefinitionLists\DefinitionListTerm;
use phpDocumentor\Guides\Nodes\Links\Link;
use phpDocumentor\Guides\Nodes\Lists\ListItem;
use phpDocumentor\Guides\Nodes\SpanNode;
use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser;
use phpDocumentor\Guides\RestructuredText\Span\SpanParser;

use function array_map;
use function count;
Expand All @@ -23,10 +23,12 @@ class LineDataParser
{
/** @var MarkupLanguageParser */
private $parser;
private SpanParser $spanParser;

public function __construct(MarkupLanguageParser $parser)
public function __construct(MarkupLanguageParser $parser, SpanParser $spanParser)
{
$this->parser = $parser;
$this->spanParser = $spanParser;
}

public function parseLink(string $line): ?Link
Expand Down Expand Up @@ -167,21 +169,24 @@ public function parseDefinitionList(array $lines): DefinitionList

$classifiers = array_map(
function (string $classifier) {
return SpanNode::create($this->parser, $classifier);
return $this->spanParser->parse($classifier, $this->parser->getEnvironment());
},
array_map('trim', $parts)
);

$definitionListTerm = [
'term' => SpanNode::create($this->parser, $term),
'term' => $this->spanParser->parse($term, $this->parser->getEnvironment()),
'classifiers' => $classifiers,
'definitions' => [],
];

// last line
} elseif ($definitionListTerm !== null && trim($line) === '' && count($lines) - 1 === $key) {
if ($currentDefinition !== null) {
$definitionListTerm['definitions'][] = SpanNode::create($this->parser, $currentDefinition);
$definitionListTerm['definitions'][] = $this->spanParser->parse(
$currentDefinition,
$this->parser->getEnvironment()
);

$currentDefinition = null;
}
Expand All @@ -194,7 +199,10 @@ function (string $classifier) {

// empty line, start of a new definition for the current term
} elseif ($currentDefinition !== null && $definitionListTerm !== null && trim($line) === '') {
$definitionListTerm['definitions'][] = SpanNode::create($this->parser, $currentDefinition);
$definitionListTerm['definitions'][] = $this->spanParser->parse(
$currentDefinition,
$this->parser->getEnvironment()
);

$currentDefinition = null;
}
Expand Down
Expand Up @@ -14,6 +14,7 @@
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParser;
use phpDocumentor\Guides\RestructuredText\Parser\LineDataParser;
use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator;
use phpDocumentor\Guides\RestructuredText\Span\SpanParser;

use function array_search;

Expand All @@ -35,14 +36,15 @@ public function __construct(
) {
$this->documentParser = $documentParser;

$lineDataParser = new LineDataParser($parser);
$spanParser = new SpanParser();
$lineDataParser = new LineDataParser($parser, $spanParser);

$literalBlockRule = new LiteralBlockRule();

// TODO: Somehow move this into the top of the instantiation chain so that you can configure which rules
// to use when consuming this library
$this->productions = [
new TitleRule($parser, $documentParser),
new TitleRule($parser, $documentParser, $spanParser),
new TransitionRule(), // Transition rule must follow Title rule
new LinkRule($lineDataParser, $parser),
$literalBlockRule,
Expand All @@ -54,7 +56,7 @@ public function __construct(
new TableRule($parser),

// For now: ParagraphRule must be last as it is the rule that applies if none other applies.
new ParagraphRule($parser, $documentParser),
new ParagraphRule($parser, $documentParser, $spanParser),
];
}

Expand Down
Expand Up @@ -15,11 +15,11 @@

use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\ParagraphNode;
use phpDocumentor\Guides\Nodes\SpanNode;
use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser;
use phpDocumentor\Guides\RestructuredText\Parser\Buffer;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParser;
use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator;
use phpDocumentor\Guides\RestructuredText\Span\SpanParser;

use function array_pop;
use function implode;
Expand All @@ -36,11 +36,13 @@ final class ParagraphRule implements Rule

/** @var DocumentParser */
private $documentParser;
private SpanParser $spanParser;

public function __construct(MarkupLanguageParser $parser, DocumentParser $documentParser)
public function __construct(MarkupLanguageParser $parser, DocumentParser $documentParser, SpanParser $spanParser)
{
$this->parser = $parser;
$this->documentParser = $documentParser;
$this->spanParser = $spanParser;
}

public function applies(DocumentParser $documentParser): bool
Expand Down Expand Up @@ -91,7 +93,7 @@ public function apply(LinesIterator $documentIterator, ?Node $on = null): ?Node
return null;
}

return new ParagraphNode(SpanNode::create($this->parser, $lines));
return new ParagraphNode($this->spanParser->parse($lines, $this->parser->getEnvironment()));
}

private function isWhiteline(string $line): bool
Expand Down
Expand Up @@ -21,6 +21,7 @@
use phpDocumentor\Guides\RestructuredText\Parser\LineDataParser;
use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator;
use phpDocumentor\Guides\RestructuredText\Parser\TableParser;
use phpDocumentor\Guides\RestructuredText\Span\SpanParser;

use function trim;

Expand All @@ -41,7 +42,7 @@ final class TableRule implements Rule
public function __construct(MarkupLanguageParser $parser)
{
$this->parser = $parser;
$this->lineChecker = new LineChecker(new LineDataParser($parser));
$this->lineChecker = new LineChecker(new LineDataParser($parser, new SpanParser()));
$this->tableParser = new TableParser();
}

Expand Down

0 comments on commit ee7f168

Please sign in to comment.