Skip to content

Commit 1762e82

Browse files
authored
Merge pull request #1116 from phpDocumentor/task/menu
[FEATURE] Make index file configurable
2 parents 9bc8130 + 34b41fe commit 1762e82

File tree

13 files changed

+100
-12
lines changed

13 files changed

+100
-12
lines changed

packages/guides-cli/resources/schema/guides.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
<xsd:attribute name="input" type="xsd:string"/>
2323
<xsd:attribute name="input-file" type="xsd:string"/>
24+
<xsd:attribute name="index-name" type="xsd:string"/>
2425
<xsd:attribute name="output" type="xsd:string"/>
2526
<xsd:attribute name="input-format" type="xsd:string"/>
2627
<xsd:attribute name="log-path" type="xsd:string"/>

packages/guides-restructured-text/src/RestructuredText/Directives/MenuDirective.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
2121
use phpDocumentor\Guides\RestructuredText\Parser\DirectiveOption;
2222
use phpDocumentor\Guides\RestructuredText\Toc\ToctreeBuilder;
23+
use phpDocumentor\Guides\Settings\ProjectSettings;
24+
use phpDocumentor\Guides\Settings\SettingsManager;
2325

2426
use function count;
2527

@@ -28,12 +30,18 @@
2830
* is for display only. It does not change the position of document in the document tree and can therefore be included
2931
* all pages as navigation.
3032
*
31-
* By default it displays a menu of the pages on level 1 up to level 2.
33+
* By default, it displays a menu of the pages on level 1 up to level 2.
3234
*/
3335
final class MenuDirective extends BaseDirective
3436
{
35-
public function __construct(private readonly ToctreeBuilder $toctreeBuilder)
36-
{
37+
private SettingsManager $settingsManager;
38+
39+
public function __construct(
40+
private readonly ToctreeBuilder $toctreeBuilder,
41+
SettingsManager|null $settingsManager = null,
42+
) {
43+
// if for backward compatibility reasons no settings manager was passed, use the defaults
44+
$this->settingsManager = $settingsManager ?? new SettingsManager(new ProjectSettings());
3745
}
3846

3947
public function getName(): string
@@ -49,7 +57,8 @@ public function process(
4957
$parserContext = $blockContext->getDocumentParserContext()->getParser()->getParserContext();
5058
$options = $directive->getOptions();
5159
$options['glob'] = new DirectiveOption('glob', true);
52-
$options['globExclude'] ??= new DirectiveOption('globExclude', 'index,Index');
60+
$indexName = $this->settingsManager->getProjectSettings()->getIndexName();
61+
$options['globExclude'] ??= new DirectiveOption('globExclude', $indexName);
5362

5463
$toctreeFiles = $this->toctreeBuilder->buildToctreeEntries(
5564
$parserContext,

packages/guides-restructured-text/src/RestructuredText/Directives/ToctreeDirective.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use phpDocumentor\Guides\RestructuredText\Parser\DirectiveOption;
2222
use phpDocumentor\Guides\RestructuredText\Parser\Productions\Rule;
2323
use phpDocumentor\Guides\RestructuredText\Toc\ToctreeBuilder;
24+
use phpDocumentor\Guides\Settings\ProjectSettings;
25+
use phpDocumentor\Guides\Settings\SettingsManager;
2426

2527
/**
2628
* Sphinx based Toctree directive.
@@ -33,11 +35,16 @@
3335
*/
3436
final class ToctreeDirective extends BaseDirective
3537
{
38+
private SettingsManager $settingsManager;
39+
3640
/** @param Rule<InlineCompoundNode> $startingRule */
3741
public function __construct(
3842
private readonly ToctreeBuilder $toctreeBuilder,
3943
private readonly Rule $startingRule,
44+
SettingsManager|null $settingsManager = null,
4045
) {
46+
// if for backward compatibility reasons no settings manager was passed, use the defaults
47+
$this->settingsManager = $settingsManager ?? new SettingsManager(new ProjectSettings());
4148
}
4249

4350
public function getName(): string
@@ -52,7 +59,8 @@ public function process(
5259
): Node|null {
5360
$parserContext = $blockContext->getDocumentParserContext()->getParser()->getParserContext();
5461
$options = $directive->getOptions();
55-
$options['globExclude'] ??= new DirectiveOption('globExclude', 'index,Index');
62+
$indexName = $this->settingsManager->getProjectSettings()->getIndexName();
63+
$options['globExclude'] ??= new DirectiveOption('globExclude', $indexName);
5664

5765
$toctreeFiles = $this->toctreeBuilder->buildToctreeEntries(
5866
$parserContext,

packages/guides/src/DependencyInjection/GuidesExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ static function ($value) {
114114
->scalarNode('theme')->end()
115115
->scalarNode('input')->end()
116116
->scalarNode('input_file')->end()
117+
->scalarNode('index_name')->end()
117118
->scalarNode('output')->end()
118119
->scalarNode('input_format')->end()
119120
->arrayNode('output_format')
@@ -237,6 +238,10 @@ public function load(array $configs, ContainerBuilder $container): void
237238
}
238239
}
239240

241+
if (isset($config['index_name']) && $config['index_name'] !== '') {
242+
$projectSettings->setIndexName((string) $config['index_name']);
243+
}
244+
240245
if (isset($config['output'])) {
241246
$projectSettings->setOutput((string) $config['output']);
242247
}

packages/guides/src/Handlers/ParseDirectoryHandler.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,28 @@
2121
use phpDocumentor\Guides\Event\PreParseProcess;
2222
use phpDocumentor\Guides\FileCollector;
2323
use phpDocumentor\Guides\Nodes\DocumentNode;
24+
use phpDocumentor\Guides\Settings\ProjectSettings;
25+
use phpDocumentor\Guides\Settings\SettingsManager;
2426
use Psr\EventDispatcher\EventDispatcherInterface;
2527

28+
use function array_map;
2629
use function assert;
30+
use function explode;
31+
use function implode;
2732
use function sprintf;
2833

2934
final class ParseDirectoryHandler
3035
{
31-
private const INDEX_FILE_NAMES = ['index', 'Index'];
36+
private SettingsManager $settingsManager;
3237

3338
public function __construct(
3439
private readonly FileCollector $fileCollector,
3540
private readonly CommandBus $commandBus,
3641
private readonly EventDispatcherInterface $eventDispatcher,
42+
SettingsManager|null $settingsManager = null,
3743
) {
44+
// if for backward compatibility reasons no settings manager was passed, use the defaults
45+
$this->settingsManager = $settingsManager ?? new SettingsManager(new ProjectSettings());
3846
}
3947

4048
/** @return DocumentNode[] */
@@ -98,17 +106,20 @@ private function getDirectoryIndexFile(
98106
$hashedContentFromFilesystem[$itemFromFilesystem['basename']] = true;
99107
}
100108

101-
foreach (self::INDEX_FILE_NAMES as $indexName) {
102-
$indexFilename = sprintf('%s.%s', $indexName, $extension);
103-
if (isset($hashedContentFromFilesystem[$indexFilename])) {
109+
$indexFileNames = array_map('trim', explode(',', $this->settingsManager->getProjectSettings()->getIndexName()));
110+
111+
$indexNamesNotFound = [];
112+
foreach ($indexFileNames as $indexName) {
113+
$fullIndexFilename = sprintf('%s.%s', $indexName, $extension);
114+
if (isset($hashedContentFromFilesystem[$fullIndexFilename])) {
104115
return $indexName;
105116
}
106-
}
107117

108-
$indexFilename = sprintf('%s.%s', self::INDEX_FILE_NAMES[0], $extension);
118+
$indexNamesNotFound[] = $fullIndexFilename;
119+
}
109120

110121
throw new InvalidArgumentException(
111-
sprintf('Could not find index file "%s" in "%s"', $indexFilename, $directory),
122+
sprintf('Could not find an index file "%s", expected file names: %s', $directory, implode(', ', $indexNamesNotFound)),
112123
);
113124
}
114125
}

packages/guides/src/Settings/ProjectSettings.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ final class ProjectSettings
2424
private string $theme = 'default';
2525
private string $input = 'docs';
2626
private string $inputFile = '';
27+
private string $indexName = 'index,Index';
2728
private string $output = 'output';
2829
private string $inputFormat = 'rst';
2930
/** @var string[] */
@@ -230,4 +231,16 @@ public function setIgnoredDomains(array $ignoredDomains): void
230231
{
231232
$this->ignoredDomains = $ignoredDomains;
232233
}
234+
235+
public function getIndexName(): string
236+
{
237+
return $this->indexName;
238+
}
239+
240+
public function setIndexName(string $indexName): ProjectSettings
241+
{
242+
$this->indexName = $indexName;
243+
244+
return $this;
245+
}
233246
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- content start -->
2+
<div class="section" id="another-page">
3+
<h1>Another Page</h1>
4+
5+
<p>Lorem Ipsum Dolor.</p>
6+
7+
</div>
8+
<!-- content end -->
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- content start -->
2+
<div class="section" id="sample-markdown-document">
3+
<h1>Sample Markdown Document</h1>
4+
5+
<p>Lorem Ipsum</p>
6+
7+
</div>
8+
<!-- content end -->
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- content start -->
2+
<div class="section" id="yet-another-page">
3+
<h1>Yet Another Page</h1>
4+
5+
<p>Lorem Ipsum Dolor.</p>
6+
7+
</div>
8+
<!-- content end -->
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Another Page
2+
3+
Lorem Ipsum Dolor.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<guides xmlns="https://www.phpdoc.org/guides"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"
5+
input-format="md"
6+
index-name="readme"
7+
>
8+
</guides>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Sample Markdown Document
2+
3+
Lorem Ipsum
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Yet Another Page
2+
3+
Lorem Ipsum Dolor.

0 commit comments

Comments
 (0)