Skip to content

Commit 018fa54

Browse files
committed
[FEATURE] Make index file configurable
The index file will also be used in automatic menu building. As oposed to setting "input-file" not only the one file mentioned will be rendererd but all files from the directory. However the name of the input file can be changed. Some markdown projects like to change the name of the index file. For example powermail is using Readme.md as index file in every directory: https://github.com/in2code-de/powermail/tree/master/Documentation References #1108
1 parent 9bc8130 commit 018fa54

File tree

13 files changed

+85
-6
lines changed

13 files changed

+85
-6
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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
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\SettingsManager;
2324

2425
use function count;
2526

@@ -32,8 +33,10 @@
3233
*/
3334
final class MenuDirective extends BaseDirective
3435
{
35-
public function __construct(private readonly ToctreeBuilder $toctreeBuilder)
36-
{
36+
public function __construct(
37+
private readonly ToctreeBuilder $toctreeBuilder,
38+
private readonly SettingsManager $settingsManager,
39+
) {
3740
}
3841

3942
public function getName(): string
@@ -49,7 +52,8 @@ public function process(
4952
$parserContext = $blockContext->getDocumentParserContext()->getParser()->getParserContext();
5053
$options = $directive->getOptions();
5154
$options['glob'] = new DirectiveOption('glob', true);
52-
$options['globExclude'] ??= new DirectiveOption('globExclude', 'index,Index');
55+
$indexName = $this->settingsManager->getProjectSettings()->getIndexName() ?? 'index,Index';
56+
$options['globExclude'] ??= new DirectiveOption('globExclude', $indexName);
5357

5458
$toctreeFiles = $this->toctreeBuilder->buildToctreeEntries(
5559
$parserContext,

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
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\SettingsManager;
2425

2526
/**
2627
* Sphinx based Toctree directive.
@@ -37,6 +38,7 @@ final class ToctreeDirective extends BaseDirective
3738
public function __construct(
3839
private readonly ToctreeBuilder $toctreeBuilder,
3940
private readonly Rule $startingRule,
41+
private readonly SettingsManager $settingsManager,
4042
) {
4143
}
4244

@@ -52,7 +54,8 @@ public function process(
5254
): Node|null {
5355
$parserContext = $blockContext->getDocumentParserContext()->getParser()->getParserContext();
5456
$options = $directive->getOptions();
55-
$options['globExclude'] ??= new DirectiveOption('globExclude', 'index,Index');
57+
$indexName = $this->settingsManager->getProjectSettings()->getIndexName() ?? 'index,Index';
58+
$options['globExclude'] ??= new DirectiveOption('globExclude', $indexName);
5659

5760
$toctreeFiles = $this->toctreeBuilder->buildToctreeEntries(
5861
$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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use phpDocumentor\Guides\Event\PreParseProcess;
2222
use phpDocumentor\Guides\FileCollector;
2323
use phpDocumentor\Guides\Nodes\DocumentNode;
24+
use phpDocumentor\Guides\Settings\SettingsManager;
2425
use Psr\EventDispatcher\EventDispatcherInterface;
2526

2627
use function assert;
@@ -34,6 +35,7 @@ public function __construct(
3435
private readonly FileCollector $fileCollector,
3536
private readonly CommandBus $commandBus,
3637
private readonly EventDispatcherInterface $eventDispatcher,
38+
private readonly SettingsManager $settingsManager,
3739
) {
3840
}
3941

@@ -84,6 +86,7 @@ private function getDirectoryIndexFile(
8486
string $sourceFormat,
8587
): string {
8688
$extension = $sourceFormat;
89+
$indexName = $this->settingsManager->getProjectSettings()->getIndexName();
8790

8891
// On macOS filesystems, a file-check against "index.rst"
8992
// using $filesystem->has() would return TRUE, when in fact
@@ -98,14 +101,23 @@ private function getDirectoryIndexFile(
98101
$hashedContentFromFilesystem[$itemFromFilesystem['basename']] = true;
99102
}
100103

101-
foreach (self::INDEX_FILE_NAMES as $indexName) {
104+
if ($indexName !== null) {
102105
$indexFilename = sprintf('%s.%s', $indexName, $extension);
103106
if (isset($hashedContentFromFilesystem[$indexFilename])) {
104107
return $indexName;
105108
}
109+
} else {
110+
foreach (self::INDEX_FILE_NAMES as $indexName) {
111+
$indexFilename = sprintf('%s.%s', $indexName, $extension);
112+
if (isset($hashedContentFromFilesystem[$indexFilename])) {
113+
return $indexName;
114+
}
115+
}
116+
117+
$indexName = self::INDEX_FILE_NAMES[0];
106118
}
107119

108-
$indexFilename = sprintf('%s.%s', self::INDEX_FILE_NAMES[0], $extension);
120+
$indexFilename = sprintf('%s.%s', $indexName, $extension);
109121

110122
throw new InvalidArgumentException(
111123
sprintf('Could not find index file "%s" in "%s"', $indexFilename, $directory),

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|null $indexName = null;
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|null
236+
{
237+
return $this->indexName;
238+
}
239+
240+
public function setIndexName(string|null $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)