Skip to content

Commit

Permalink
Merge branch 'main' into task/output-file
Browse files Browse the repository at this point in the history
  • Loading branch information
linawolf authored Nov 27, 2023
2 parents 674aff6 + a4f80ed commit 659ca55
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
/** @see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#embedded-uris-and-aliases */
abstract class AbstractReferenceTextRole implements TextRole
{
/** @see https://regex101.com/r/htMn5p/1 */
public const INTERLINK_REGEX = '/^([a-zA-Z0-9-_]+):(.*$)/';
private readonly InlineLexer $lexer;

public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getAliases(): array
/** @return DocReferenceNode */
protected function createNode(string $referenceTarget, string|null $referenceName, string $role): AbstractLinkInlineNode
{
$pattern = '/^([a-zA-Z0-9]+):(.*$)/';
$pattern = AbstractReferenceTextRole::INTERLINK_REGEX;
if (preg_match($pattern, $referenceTarget, $matches)) {
$interlinkDomain = $matches[1];
$path = $matches[2];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function createNode(string $referenceTarget, string|null $referenceNam
{
$linkType = $this->genericLinkProvider->getLinkType($role);
$pattern = '/^([a-zA-Z0-9]+):(.*$)/';
if (preg_match($pattern, $referenceTarget, $matches)) {
if (preg_match(AbstractReferenceTextRole::INTERLINK_REGEX, $referenceTarget, $matches)) {
$interlinkDomain = $matches[1];
$id = $this->anchorReducer->reduceAnchor($matches[2]);
} else {
Expand Down
23 changes: 12 additions & 11 deletions packages/guides/src/Interlink/InventoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace phpDocumentor\Guides\Interlink;

use phpDocumentor\Guides\ReferenceResolvers\AnchorReducer;
use RuntimeException;

use function array_key_exists;
use function strtolower;

class InventoryRepository
{
Expand All @@ -16,36 +16,37 @@ class InventoryRepository

/** @param array<int, array<string, string>> $inventoryConfigs */
public function __construct(
private readonly AnchorReducer $anchorReducer,
private readonly InventoryLoader $inventoryLoader,
array $inventoryConfigs,
) {
foreach ($inventoryConfigs as $inventory) {
$this->inventories[$inventory['id']] = new Inventory($inventory['url']);
$this->inventories[$this->anchorReducer->reduceAnchor($inventory['id'])] = new Inventory($inventory['url']);
}
}

public function hasInventory(string $key): bool
{
$lowerCaseKey = strtolower($key);
$reducedKey = $this->anchorReducer->reduceAnchor($key);

return array_key_exists($lowerCaseKey, $this->inventories);
return array_key_exists($reducedKey, $this->inventories);
}

public function getInventory(string $key): Inventory
{
$lowerCaseKey = strtolower($key);
if (!$this->hasInventory($lowerCaseKey)) {
throw new RuntimeException('Inventory with key ' . $lowerCaseKey . ' not found. ', 1_671_398_986);
$reducedKey = $this->anchorReducer->reduceAnchor($key);
if (!$this->hasInventory($reducedKey)) {
throw new RuntimeException('Inventory with key ' . $reducedKey . ' not found. ', 1_671_398_986);
}

$this->inventoryLoader->loadInventory($this->inventories[$lowerCaseKey]);
$this->inventoryLoader->loadInventory($this->inventories[$reducedKey]);

return $this->inventories[$lowerCaseKey];
return $this->inventories[$reducedKey];
}

public function addInventory(string $key, Inventory $inventory): void
{
$lowerCaseKey = strtolower($key);
$this->inventories[$lowerCaseKey] = $inventory;
$reducedKey = $this->anchorReducer->reduceAnchor($key);
$this->inventories[$reducedKey] = $inventory;
}
}
10 changes: 9 additions & 1 deletion packages/guides/tests/unit/Interlink/InventoryLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace phpDocumentor\Guides\Interlink;

use phpDocumentor\Guides\ReferenceResolvers\SluggerAnchorReducer;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
Expand All @@ -30,13 +31,14 @@ protected function setUp(): void
self::createStub(NullLogger::class),
$this->jsonLoader,
);
$this->inventoryRepository = new InventoryRepository($this->inventoryLoader, []);
$this->inventoryRepository = new InventoryRepository(new SluggerAnchorReducer(), $this->inventoryLoader, []);
$jsonString = file_get_contents(__DIR__ . '/input/objects.inv.json');
assertIsString($jsonString);
$this->json = (array) json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR);
$inventory = new Inventory('https://example.com/');
$this->inventoryLoader->loadInventoryFromJson($inventory, $this->json);
$this->inventoryRepository->addInventory('somekey', $inventory);
$this->inventoryRepository->addInventory('some-key', $inventory);
}

public function testInventoryLoaderLoadsInventory(): void
Expand All @@ -59,4 +61,10 @@ public function testInventoryLoaderGetInventoryIsCaseInsensitive(): void
$inventory = $this->inventoryRepository->getInventory('SomeKey');
self::assertGreaterThan(1, count($inventory->getGroups()));
}

public function testInventoryLoaderGetInventoryIsSlugged(): void
{
$inventory = $this->inventoryRepository->getInventory('Some_Key');
self::assertGreaterThan(1, count($inventory->getGroups()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- content start -->
<div class="section" id="document-title">
<h1>Document Title</h1>

<p>See <a href="https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/10.1/Feature-89010-IntroduceSiteConfigForDistributionPackages.html">Feature: #89010 - Introduce Site Configuration for Distribution Packages</a>.</p>
<p>See <a href="https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/10.1/Feature-89010-IntroduceSiteConfigForDistributionPackages.html">Feature: #89010 - Introduce Site Configuration for Distribution Packages</a>.</p>
</div>

<!-- content end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
==============
Document Title
==============

See :doc:`ext_core:Changelog/10.1/Feature-89010-IntroduceSiteConfigForDistributionPackages`.


See :doc:`ext-core:Changelog/10.1/Feature-89010-IntroduceSiteConfigForDistributionPackages`.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<guides xmlns="https://www.phpdoc.org/guides"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
>
<project title="My Project" version="main (development)" />
<inventory id="ext_core" url="https://docs.typo3.org/c/typo3/cms-core/main/en-us/"/>
</guides>

0 comments on commit 659ca55

Please sign in to comment.