-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1121 from phpDocumentor/task/menu-automatic
[FEATURE] Add option to create an automatic menu
- Loading branch information
Showing
52 changed files
with
1,652 additions
and
8 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
72 changes: 72 additions & 0 deletions
72
...ides/src/Compiler/NodeTransformers/MenuNodeTransformers/TocNodeReplacementTransformer.php
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,72 @@ | ||
<?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 phpDocumentor\Guides\Compiler\NodeTransformers\MenuNodeTransformers; | ||
|
||
use phpDocumentor\Guides\Compiler\CompilerContextInterface; | ||
use phpDocumentor\Guides\Compiler\NodeTransformer; | ||
use phpDocumentor\Guides\Nodes\Menu\MenuNode; | ||
use phpDocumentor\Guides\Nodes\Menu\NavMenuNode; | ||
use phpDocumentor\Guides\Nodes\Menu\TocNode; | ||
use phpDocumentor\Guides\Nodes\Node; | ||
use phpDocumentor\Guides\Settings\SettingsManager; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** @implements NodeTransformer<MenuNode> */ | ||
final class TocNodeReplacementTransformer implements NodeTransformer | ||
{ | ||
public function __construct( | ||
private readonly LoggerInterface $logger, | ||
private readonly SettingsManager $settingsManager, | ||
) { | ||
} | ||
|
||
public function enterNode(Node $node, CompilerContextInterface $compilerContext): Node | ||
{ | ||
return $node; | ||
} | ||
|
||
public function leaveNode(Node $node, CompilerContextInterface $compilerContext): Node|null | ||
{ | ||
if (!$node instanceof TocNode) { | ||
return $node; | ||
} | ||
|
||
if (!$this->settingsManager->getProjectSettings()->isAutomaticMenu()) { | ||
return $node; | ||
} | ||
|
||
if ($node->hasOption('hidden')) { | ||
$this->logger->warning('The `.. toctree::` directive with option `:hidden:` is not supported in automatic-menu mode. ', $compilerContext->getLoggerInformation()); | ||
|
||
return null; | ||
} | ||
|
||
$this->logger->warning('The `.. toctree::` directive is not supported in automatic-menu mode. Use `.. menu::` instead. ', $compilerContext->getLoggerInformation()); | ||
$menuNode = new NavMenuNode($node->getMenuEntries()); | ||
$menuNode = $menuNode->withOptions($node->getOptions()); | ||
$menuNode = $menuNode->withCaption($node->getCaption()); | ||
|
||
return $menuNode; | ||
} | ||
|
||
public function supports(Node $node): bool | ||
{ | ||
return $node instanceof TocNode; | ||
} | ||
|
||
public function getPriority(): int | ||
{ | ||
return 20_000; | ||
} | ||
} |
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,63 @@ | ||
<?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 phpDocumentor\Guides\Compiler\Passes; | ||
|
||
use phpDocumentor\Guides\Compiler\CompilerContextInterface; | ||
use phpDocumentor\Guides\Compiler\CompilerPass; | ||
use phpDocumentor\Guides\Nodes\DocumentNode; | ||
use phpDocumentor\Guides\Settings\SettingsManager; | ||
|
||
final class AutomaticMenuPass implements CompilerPass | ||
{ | ||
public function __construct( | ||
private readonly SettingsManager $settingsManager, | ||
) { | ||
} | ||
|
||
public function getPriority(): int | ||
{ | ||
return 20; // must be run very late | ||
} | ||
|
||
/** | ||
* @param DocumentNode[] $documents | ||
* | ||
* @return DocumentNode[] | ||
*/ | ||
public function run(array $documents, CompilerContextInterface $compilerContext): array | ||
{ | ||
if (!$this->settingsManager->getProjectSettings()->isAutomaticMenu()) { | ||
return $documents; | ||
} | ||
|
||
$projectNode = $compilerContext->getProjectNode(); | ||
$rootDocumentEntry = $projectNode->getRootDocumentEntry(); | ||
foreach ($documents as $documentNode) { | ||
if ($documentNode->isOrphan()) { | ||
// Do not add orphans to the automatic menu | ||
continue; | ||
} | ||
|
||
if ($documentNode->isRoot()) { | ||
continue; | ||
} | ||
|
||
$documentEntry = $projectNode->getDocumentEntry($documentNode->getFilePath()); | ||
$documentEntry->setParent($rootDocumentEntry); | ||
$rootDocumentEntry->addChild($documentEntry); | ||
} | ||
|
||
return $documents; | ||
} | ||
} |
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
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
Oops, something went wrong.