Skip to content

Commit

Permalink
[FEATURE] Use :ref: Textrole to link to confvals and viewhelpers
Browse files Browse the repository at this point in the history
  • Loading branch information
linawolf committed Nov 18, 2024
1 parent e280832 commit 16e4839
Show file tree
Hide file tree
Showing 20 changed files with 379 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use phpDocumentor\Guides\RestructuredText\Parser\Productions\DocumentRule;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use T3Docs\Typo3DocsTheme\Api\Typo3ApiService;
use T3Docs\Typo3DocsTheme\Compiler\NodeTransformers\CollectPrefixLinkTargetsTransformer;
use T3Docs\Typo3DocsTheme\Compiler\NodeTransformers\ConfvalMenuNodeTransformer;
use T3Docs\Typo3DocsTheme\Compiler\NodeTransformers\RemoveInterlinkSelfReferencesFromCrossReferenceNodeTransformer;
use T3Docs\Typo3DocsTheme\Directives\ConfvalMenuDirective;
Expand Down Expand Up @@ -82,6 +83,8 @@
->bind('$startingRule', service(DirectiveContentRule::class))
->instanceof(BaseDirective::class)
->tag('phpdoc.guides.directive')
->set(CollectPrefixLinkTargetsTransformer::class)
->tag('phpdoc.guides.compiler.nodeTransformers')
->set(ConfvalMenuNodeTransformer::class)
->tag('phpdoc.guides.compiler.nodeTransformers')
->set(RemoveInterlinkSelfReferencesFromCrossReferenceNodeTransformer::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

namespace T3Docs\Typo3DocsTheme\Compiler\NodeTransformers;

use phpDocumentor\Guides\Compiler\CompilerContextInterface;
use phpDocumentor\Guides\Compiler\NodeTransformer;
use phpDocumentor\Guides\Exception\DuplicateLinkAnchorException;
use phpDocumentor\Guides\Meta\InternalTarget;
use phpDocumentor\Guides\Nodes\AnchorNode;
use phpDocumentor\Guides\Nodes\DocumentNode;
use phpDocumentor\Guides\Nodes\LinkTargetNode;
use phpDocumentor\Guides\Nodes\MultipleLinkTargetsNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\OptionalLinkTargetsNode;
use phpDocumentor\Guides\Nodes\PrefixedLinkTargetNode;
use phpDocumentor\Guides\Nodes\SectionNode;
use phpDocumentor\Guides\ReferenceResolvers\AnchorNormalizer;
use Psr\Log\LoggerInterface;
use SplStack;
use Webmozart\Assert\Assert;

use function sprintf;

/** @implements NodeTransformer<DocumentNode|AnchorNode|SectionNode> */
final class CollectPrefixLinkTargetsTransformer implements NodeTransformer
{
/** @var SplStack<DocumentNode> */
private readonly SplStack $documentStack;

public function __construct(
private readonly AnchorNormalizer $anchorReducer,
private LoggerInterface|null $logger = null,
) {
/*
* TODO: remove stack here, as we should not have sub documents in this way, sub documents are
* now produced by the {@see \phpDocumentor\Guides\RestructuredText\MarkupLanguageParser::getSubParser}
* as this works right now in isolation includes do not work as they should.
*/
$this->documentStack = new SplStack();
}

public function enterNode(Node $node, CompilerContextInterface $compilerContext): Node
{
if ($node instanceof DocumentNode) {
$this->documentStack->push($node);
return $node;
}
if ($node instanceof SectionNode) {
return $node;
}

if ($node instanceof LinkTargetNode) {
if ($node instanceof OptionalLinkTargetsNode && $node->isNoindex()) {
return $node;
}
if ($node->getLinkText() === SectionNode::STD_LABEL) {
return $node;
}

$currentDocument = $this->documentStack->top();
Assert::notNull($currentDocument);
$anchor = $this->anchorReducer->reduceAnchor($node->getId());
$prefix = '';
if ($node instanceof PrefixedLinkTargetNode) {
$prefix = $node->getPrefix();
}

$this->addLinkTargetToProject(
$compilerContext,
new InternalTarget(
$currentDocument->getFilePath(),
$prefix . $anchor,
$node->getLinkText(),
SectionNode::STD_LABEL,
),
);
if ($node instanceof MultipleLinkTargetsNode) {
foreach ($node->getAdditionalIds() as $id) {
$anchor = $this->anchorReducer->reduceAnchor($id);
$this->addLinkTargetToProject(
$compilerContext,
new InternalTarget(
$currentDocument->getFilePath(),
$prefix . $anchor,
$node->getLinkText(),
SectionNode::STD_LABEL,
),
);
}
}
}

return $node;
}

public function leaveNode(Node $node, CompilerContextInterface $compilerContext): Node|null
{
if ($node instanceof DocumentNode) {
$this->documentStack->pop();
}

return $node;
}

public function supports(Node $node): bool
{
return $node instanceof DocumentNode || $node instanceof LinkTargetNode;
}

public function getPriority(): int
{
// After CollectLinkTargetsTransformer
return 4000;
}

private function addLinkTargetToProject(CompilerContextInterface $compilerContext, InternalTarget $internalTarget): void
{
if ($compilerContext->getProjectNode()->hasInternalTarget($internalTarget->getAnchor(), $internalTarget->getLinkType())) {
$otherLink = $compilerContext->getProjectNode()->getInternalTarget($internalTarget->getAnchor(), $internalTarget->getLinkType());
$this->logger?->warning(
sprintf(
'Duplicate anchor "%s" for link type "%s" in document "%s". The anchor is already used at "%s"',
$internalTarget->getAnchor(),
$internalTarget->getLinkType(),
$compilerContext->getDocumentNode()->getFilePath(),
$otherLink?->getDocumentPath(),
),
$compilerContext->getLoggerInformation(),
);

return;
}

try {
$compilerContext->getProjectNode()->addLinkTarget(
$internalTarget->getAnchor(),
$internalTarget,
);
} catch (DuplicateLinkAnchorException $exception) {
$this->logger?->warning($exception->getMessage(), $compilerContext->getLoggerInformation());
}
}
}
16 changes: 10 additions & 6 deletions packages/typo3-docs-theme/src/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use phpDocumentor\Guides\Nodes\Metadata\NoSearchNode;
use phpDocumentor\Guides\Nodes\Metadata\OrphanNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\PrefixedLinkTargetNode;
use phpDocumentor\Guides\Nodes\SectionNode;
use phpDocumentor\Guides\ReferenceResolvers\DocumentNameResolverInterface;
use phpDocumentor\Guides\RenderContext;
Expand Down Expand Up @@ -201,27 +202,30 @@ public function isNoSearch(array $context): bool
public function getRstCodeForLink(array $context, LinkTargetNode $linkTargetNode): string
{
$interlink = $this->themeSettings->getSettings('interlink_shortcode') !== '' ? $this->themeSettings->getSettings('interlink_shortcode') : 'somemanual';
if ($linkTargetNode->getLinkType() === ConfvalNode::LINK_TYPE) {
if ($linkTargetNode instanceof PrefixedLinkTargetNode && $linkTargetNode->getLinkType() === ConfvalNode::LINK_TYPE) {
return sprintf(
':confval:`%s <%s:%s>`',
':ref:`%s <%s:%s%s>`',
$linkTargetNode->getLinkText(),
$interlink,
$linkTargetNode->getPrefix(),
$linkTargetNode->getId()
);
}
if ($linkTargetNode->getLinkType() === ViewHelperNode::LINK_TYPE) {
if ($linkTargetNode instanceof PrefixedLinkTargetNode && $linkTargetNode->getLinkType() === ViewHelperNode::LINK_TYPE) {
return sprintf(
':typo3:viewhelper:`%s <%s:%s>`',
':ref:`%s <%s:%s%s>`',
$linkTargetNode->getLinkText(),
$interlink,
$linkTargetNode->getPrefix(),
$linkTargetNode->getId()
);
}
if ($linkTargetNode->getLinkType() === ViewHelperArgumentNode::LINK_TYPE) {
if ($linkTargetNode instanceof PrefixedLinkTargetNode && $linkTargetNode->getLinkType() === ViewHelperArgumentNode::LINK_TYPE) {
return sprintf(
':typo3:viewhelper-argument:`%s <%s:%s>`',
':ref:`%s <%s:%s%s>`',
$linkTargetNode->getLinkText(),
$interlink,
$linkTargetNode->getPrefix(),
$linkTargetNode->getId()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h3 class="sr-only">demo</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-demo"
data-rstCode=":confval:`demo &lt;somemanual:demo&gt;`"
data-rstCode=":ref:`demo &lt;somemanual:confval-demo&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand All @@ -41,7 +41,7 @@ <h3 class="sr-only">demo</h3>
</dl>
</section>

<p>See also <a href="#confval-demo">demo</a>.</p>
<p>See also <a href="#confval-demo">demo</a> or <a href="#confval-demo">demo</a>.</p>

</section>
<!-- content end -->
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ Confval

Some Text

See also :confval:`demo`.
See also :confval:`demo` or :ref:`demo <confval-demo>`.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h3 class="sr-only">demo</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-another-demo"
data-rstCode=":confval:`demo &lt;somemanual:another-demo&gt;`"
data-rstCode=":ref:`demo &lt;somemanual:confval-another-demo&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h3 class="sr-only">demo</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-demo"
data-rstCode=":confval:`demo &lt;somemanual:demo&gt;`"
data-rstCode=":ref:`demo &lt;somemanual:confval-demo&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h3 class="sr-only">demo</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-demo"
data-rstCode=":confval:`demo &lt;somemanual:demo&gt;`"
data-rstCode=":ref:`demo &lt;somemanual:confval-demo&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h3 class="sr-only">demo</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-demo"
data-rstCode=":confval:`demo &lt;somemanual:demo&gt;`"
data-rstCode=":ref:`demo &lt;somemanual:confval-demo&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ <h3 class="sr-only">array of cObjects</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-case-array"
data-rstCode=":confval:`array of cObjects &lt;somemanual:case-array&gt;`"
data-rstCode=":ref:`array of cObjects &lt;somemanual:confval-case-array&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand Down Expand Up @@ -100,7 +100,7 @@ <h3 class="sr-only">cache</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-case-cache"
data-rstCode=":confval:`cache &lt;somemanual:case-cache&gt;`"
data-rstCode=":ref:`cache &lt;somemanual:confval-case-cache&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand Down Expand Up @@ -137,7 +137,7 @@ <h3 class="sr-only">default</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-case-default"
data-rstCode=":confval:`default &lt;somemanual:case-default&gt;`"
data-rstCode=":ref:`default &lt;somemanual:confval-case-default&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand Down Expand Up @@ -178,7 +178,7 @@ <h3 class="sr-only">if</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-case-if"
data-rstCode=":confval:`if &lt;somemanual:case-if&gt;`"
data-rstCode=":ref:`if &lt;somemanual:confval-case-if&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ <h3 class="sr-only">array of cObjects</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-case-array"
data-rstCode=":confval:`array of cObjects &lt;somemanual:case-array&gt;`"
data-rstCode=":ref:`array of cObjects &lt;somemanual:confval-case-array&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand Down Expand Up @@ -103,7 +103,7 @@ <h3 class="sr-only">cache</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-case-cache"
data-rstCode=":confval:`cache &lt;somemanual:case-cache&gt;`"
data-rstCode=":ref:`cache &lt;somemanual:confval-case-cache&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand Down Expand Up @@ -139,7 +139,7 @@ <h3 class="sr-only">default</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-case-default"
data-rstCode=":confval:`default &lt;somemanual:case-default&gt;`"
data-rstCode=":ref:`default &lt;somemanual:confval-case-default&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand Down Expand Up @@ -178,7 +178,7 @@ <h3 class="sr-only">if</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-case-if"
data-rstCode=":confval:`if &lt;somemanual:case-if&gt;`"
data-rstCode=":ref:`if &lt;somemanual:confval-case-if&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand Down Expand Up @@ -254,7 +254,7 @@ <h3 class="sr-only">1,2,3,4...</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-coa-array"
data-rstCode=":confval:`1,2,3,4... &lt;somemanual:coa-array&gt;`"
data-rstCode=":ref:`1,2,3,4... &lt;somemanual:confval-coa-array&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand Down Expand Up @@ -291,7 +291,7 @@ <h3 class="sr-only">cache</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-coa-cache"
data-rstCode=":confval:`cache &lt;somemanual:coa-cache&gt;`"
data-rstCode=":ref:`cache &lt;somemanual:confval-coa-cache&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand Down Expand Up @@ -327,7 +327,7 @@ <h3 class="sr-only">if</h3>
data-bs-toggle="modal"
data-bs-target="#linkReferenceModal"
data-id="confval-coa-if"
data-rstCode=":confval:`if &lt;somemanual:coa-if&gt;`"
data-rstCode=":ref:`if &lt;somemanual:confval-coa-if&gt;`"
title="Reference this configuration value"><i
class="fa-solid fa-paragraph"></i></a>
</div>
Expand Down
Loading

0 comments on commit 16e4839

Please sign in to comment.