-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Introduce textroles for component (#28)
* [TASK] Move shared functionality of textroles to common abstract class * [Feature] Introduce :php:class: TextRole * [Feature] Introduce :php:enum: TextRole * [Feature] Introduce :php:trait: TextRole * [Feature] Introduce :php:exception: TextRole * Update src/TextRoles/PhpComponentTextRole.php Co-authored-by: Chris Müller <2566282+brotkrueml@users.noreply.github.com> --------- Co-authored-by: Chris Müller <2566282+brotkrueml@users.noreply.github.com>
- Loading branch information
1 parent
55fba46
commit e92c96e
Showing
18 changed files
with
486 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace T3Docs\GuidesPhpDomain\TextRoles; | ||
|
||
use Doctrine\Common\Lexer\Token; | ||
use phpDocumentor\Guides\Nodes\Inline\AbstractLinkInlineNode; | ||
use phpDocumentor\Guides\Nodes\Inline\ReferenceNode; | ||
use phpDocumentor\Guides\ReferenceResolvers\AnchorReducer; | ||
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext; | ||
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer; | ||
use phpDocumentor\Guides\RestructuredText\TextRoles\TextRole; | ||
use Psr\Log\LoggerInterface; | ||
|
||
final class ClassTextRole extends PhpComponentTextRole | ||
{ | ||
private const TYPE = 'class'; | ||
|
||
public function getName(): string | ||
{ | ||
return self::TYPE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace T3Docs\GuidesPhpDomain\TextRoles; | ||
|
||
use Doctrine\Common\Lexer\Token; | ||
use phpDocumentor\Guides\Nodes\Inline\AbstractLinkInlineNode; | ||
use phpDocumentor\Guides\Nodes\Inline\ReferenceNode; | ||
use phpDocumentor\Guides\ReferenceResolvers\AnchorReducer; | ||
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext; | ||
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer; | ||
use phpDocumentor\Guides\RestructuredText\TextRoles\TextRole; | ||
use Psr\Log\LoggerInterface; | ||
|
||
final class EnumTextRole extends PhpComponentTextRole | ||
{ | ||
private const TYPE = 'enum'; | ||
|
||
public function getName(): string | ||
{ | ||
return self::TYPE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace T3Docs\GuidesPhpDomain\TextRoles; | ||
|
||
use Doctrine\Common\Lexer\Token; | ||
use phpDocumentor\Guides\Nodes\Inline\AbstractLinkInlineNode; | ||
use phpDocumentor\Guides\Nodes\Inline\ReferenceNode; | ||
use phpDocumentor\Guides\ReferenceResolvers\AnchorReducer; | ||
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext; | ||
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer; | ||
use phpDocumentor\Guides\RestructuredText\TextRoles\TextRole; | ||
use Psr\Log\LoggerInterface; | ||
|
||
final class ExceptionTextRole extends PhpComponentTextRole | ||
{ | ||
private const TYPE = 'exception'; | ||
|
||
public function getAliases(): array | ||
{ | ||
return ['exc']; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return self::TYPE; | ||
} | ||
|
||
public function processNode( | ||
DocumentParserContext $documentParserContext, | ||
string $role, | ||
string $content, | ||
string $rawContent, | ||
): AbstractLinkInlineNode { | ||
if ($role !== 'php:exception') { | ||
$this->logger->warning(sprintf('Text role :%s: is deprecated. Use :php:exception: instead. ', $role), $documentParserContext->getLoggerInformation()); | ||
} | ||
return parent::processNode($documentParserContext, $role, $content, $rawContent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace T3Docs\GuidesPhpDomain\TextRoles; | ||
|
||
use Doctrine\Common\Lexer\Token; | ||
use phpDocumentor\Guides\Nodes\Inline\AbstractLinkInlineNode; | ||
use phpDocumentor\Guides\Nodes\Inline\ReferenceNode; | ||
use phpDocumentor\Guides\ReferenceResolvers\AnchorReducer; | ||
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext; | ||
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer; | ||
use phpDocumentor\Guides\RestructuredText\TextRoles\TextRole; | ||
use Psr\Log\LoggerInterface; | ||
|
||
abstract class PhpComponentTextRole implements TextRole | ||
{ | ||
/** | ||
* @see https://regex101.com/r/OyN05v/1 | ||
*/ | ||
private const INTERLINK_NAME_REGEX = '/^([a-zA-Z0-9]+):(.*$)/'; | ||
|
||
private readonly InlineLexer $lexer; | ||
|
||
public function __construct( | ||
protected readonly LoggerInterface $logger, | ||
private readonly AnchorReducer $anchorReducer, | ||
) { | ||
// Do not inject the $lexer. It contains a state. | ||
$this->lexer = new InlineLexer(); | ||
} | ||
|
||
/** | ||
* @return list<string> | ||
*/ | ||
public function getAliases(): array | ||
{ | ||
return []; | ||
} | ||
|
||
public function processNode( | ||
DocumentParserContext $documentParserContext, | ||
string $role, | ||
string $content, | ||
string $rawContent, | ||
): AbstractLinkInlineNode { | ||
$referenceTarget = null; | ||
$value = null; | ||
|
||
$part = ''; | ||
$this->lexer->setInput($rawContent); | ||
$this->lexer->moveNext(); | ||
$this->lexer->moveNext(); | ||
while ($this->lexer->token instanceof Token) { | ||
$token = $this->lexer->token; | ||
switch ($token->type) { | ||
case InlineLexer::EMBEDED_URL_START: | ||
$value = trim($part); | ||
$part = ''; | ||
|
||
break; | ||
case InlineLexer::EMBEDED_URL_END: | ||
if ($value === null) { | ||
// not inside the embedded URL | ||
$part .= $token->value; | ||
break; | ||
} | ||
|
||
if ($this->lexer->peek() !== null) { | ||
$this->logger->warning( | ||
sprintf( | ||
'Reference contains unexpected content after closing `>`: "%s"', | ||
$content, | ||
), | ||
$documentParserContext->getLoggerInformation(), | ||
); | ||
} | ||
|
||
$referenceTarget = $part; | ||
$part = ''; | ||
|
||
break 2; | ||
default: | ||
$part .= $token->value; | ||
} | ||
|
||
$this->lexer->moveNext(); | ||
} | ||
|
||
$value .= trim($part); | ||
|
||
if ($referenceTarget === null) { | ||
$referenceTarget = $value; | ||
$value = null; | ||
} | ||
|
||
return $this->createNode($referenceTarget, $value, $role); | ||
} | ||
|
||
/** @return ReferenceNode */ | ||
protected function createNode(string $referenceTarget, string|null $referenceName, string $role): AbstractLinkInlineNode | ||
{ | ||
if (preg_match(self::INTERLINK_NAME_REGEX, $referenceTarget, $matches)) { | ||
$interlinkDomain = $matches[1]; | ||
$id = $this->anchorReducer->reduceAnchor($matches[2]); | ||
} else { | ||
$interlinkDomain = ''; | ||
$id = $this->anchorReducer->reduceAnchor($referenceTarget); | ||
} | ||
|
||
return new ReferenceNode($id, $referenceName ?? '', $interlinkDomain, 'php:' . $this->getName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace T3Docs\GuidesPhpDomain\TextRoles; | ||
|
||
use Doctrine\Common\Lexer\Token; | ||
use phpDocumentor\Guides\Nodes\Inline\AbstractLinkInlineNode; | ||
use phpDocumentor\Guides\Nodes\Inline\ReferenceNode; | ||
use phpDocumentor\Guides\ReferenceResolvers\AnchorReducer; | ||
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext; | ||
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer; | ||
use phpDocumentor\Guides\RestructuredText\TextRoles\TextRole; | ||
use Psr\Log\LoggerInterface; | ||
|
||
final class TraitTextRole extends PhpComponentTextRole | ||
{ | ||
private const TYPE = 'trait'; | ||
|
||
public function getName(): string | ||
{ | ||
return self::TYPE; | ||
} | ||
} |
Oops, something went wrong.