Skip to content

Commit bc31c35

Browse files
committed
[FEATURE] Add option input-file to render a specific file
This can be used to parse READMEs or other standalone reST files. It is also a preparation for rendering specific mark down files in future like a README.md
1 parent 7d7113b commit bc31c35

File tree

7 files changed

+92
-13
lines changed

7 files changed

+92
-13
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
</xsd:choice>
1717

1818
<xsd:attribute name="input" type="xsd:string"/>
19+
<xsd:attribute name="input-file" type="xsd:string"/>
1920
<xsd:attribute name="output" type="xsd:string"/>
2021
<xsd:attribute name="input-format" type="xsd:string"/>
2122
<xsd:attribute name="log-path" type="xsd:string"/>

packages/guides-cli/src/Command/Run.php

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use phpDocumentor\Guides\Compiler\CompilerContext;
1616
use phpDocumentor\Guides\Handlers\CompileDocumentsCommand;
1717
use phpDocumentor\Guides\Handlers\ParseDirectoryCommand;
18+
use phpDocumentor\Guides\Handlers\ParseFileCommand;
1819
use phpDocumentor\Guides\Handlers\RenderCommand;
1920
use phpDocumentor\Guides\Intersphinx\InventoryRepository;
2021
use phpDocumentor\Guides\Nodes\ProjectNode;
@@ -36,6 +37,7 @@
3637
use function implode;
3738
use function is_countable;
3839
use function is_dir;
40+
use function pathinfo;
3941
use function realpath;
4042
use function sprintf;
4143
use function str_starts_with;
@@ -63,6 +65,13 @@ public function __construct(
6365
'Directory to read for files',
6466
);
6567

68+
$this->addOption(
69+
'input-file',
70+
null,
71+
InputOption::VALUE_REQUIRED,
72+
'If set only this file is parsed.',
73+
);
74+
6675
$this->addOption(
6776
'input-format',
6877
null,
@@ -114,6 +123,19 @@ private function getSettingsOverridenWithInput(InputInterface $input): ProjectSe
114123
$settings->setOutput((string) $input->getArgument('output'));
115124
}
116125

126+
if ($input->getOption('input-file')) {
127+
$inputFile = (string) $input->getOption('input-file');
128+
$pathInfo = pathinfo($inputFile);
129+
$settings->setInputFile($pathInfo['filename']);
130+
if (!empty($pathInfo['extension'])) {
131+
$settings->setInputFormat($pathInfo['extension']);
132+
}
133+
}
134+
135+
if ($input->getOption('input-format')) {
136+
$settings->setInputFormat((string) $input->getOption('input-format'));
137+
}
138+
117139
if ($input->getOption('log-path')) {
118140
$settings->setLogPath((string) $input->getOption('log-path'));
119141
}
@@ -122,10 +144,6 @@ private function getSettingsOverridenWithInput(InputInterface $input): ProjectSe
122144
$settings->setFailOnError(true);
123145
}
124146

125-
if ($input->getOption('input-format')) {
126-
$settings->setInputFormat((string) $input->getOption('input-format'));
127-
}
128-
129147
if (count($input->getOption('output-format')) > 0) {
130148
$settings->setOutputFormats($input->getOption('output-format'));
131149
}
@@ -168,16 +186,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int
168186
$this->logger->pushProcessor($spyProcessor);
169187
}
170188

171-
$documents = $this->commandBus->handle(
172-
new ParseDirectoryCommand(
173-
$sourceFileSystem,
174-
'',
175-
$settings->getInputFormat(),
176-
$projectNode,
177-
),
178-
);
179-
189+
$documents = [];
180190

191+
if ($settings->getInputFile() === '') {
192+
$documents = $this->commandBus->handle(
193+
new ParseDirectoryCommand(
194+
$sourceFileSystem,
195+
'',
196+
$settings->getInputFormat(),
197+
$projectNode,
198+
),
199+
);
200+
} else {
201+
$documents[] = $this->commandBus->handle(
202+
new ParseFileCommand(
203+
$sourceFileSystem,
204+
'',
205+
$settings->getInputFile(),
206+
$settings->getInputFormat(),
207+
1,
208+
$projectNode,
209+
true,
210+
),
211+
);
212+
}
181213

182214
$this->themeManager->useTheme($settings->getTheme());
183215

packages/guides/src/DependencyInjection/GuidesExtension.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use function assert;
2323
use function dirname;
2424
use function is_array;
25+
use function pathinfo;
2526

2627
class GuidesExtension extends Extension implements CompilerPassInterface, ConfigurationInterface
2728
{
@@ -53,6 +54,7 @@ public function getConfigTreeBuilder(): TreeBuilder
5354
->end()
5455
->scalarNode('theme')->end()
5556
->scalarNode('input')->end()
57+
->scalarNode('input_file')->end()
5658
->scalarNode('output')->end()
5759
->scalarNode('input_format')->end()
5860
->arrayNode('output_format')
@@ -122,6 +124,15 @@ public function load(array $configs, ContainerBuilder $container): void
122124
$projectSettings->setInput((string) $config['input']);
123125
}
124126

127+
if (isset($config['input_file']) && $config['input_file'] !== '') {
128+
$inputFile = (string) $config['input_file'];
129+
$pathInfo = pathinfo($inputFile);
130+
$projectSettings->setInputFile($pathInfo['filename']);
131+
if (!empty($pathInfo['extension'])) {
132+
$projectSettings->setInputFormat($pathInfo['extension']);
133+
}
134+
}
135+
125136
if (isset($config['output'])) {
126137
$projectSettings->setOutput((string) $config['output']);
127138
}

packages/guides/src/Settings/ProjectSettings.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class ProjectSettings
1212
private string $version = '';
1313
private string $theme = 'default';
1414
private string $input = 'docs';
15+
private string $inputFile = '';
1516
private string $output = 'output';
1617
private string $inputFormat = 'rst';
1718
/** @var string[] */
@@ -133,4 +134,14 @@ public function setOutputFormats(array $outputFormats): void
133134
{
134135
$this->outputFormats = $outputFormats;
135136
}
137+
138+
public function getInputFile(): string
139+
{
140+
return $this->inputFile;
141+
}
142+
143+
public function setInputFile(string $inputFile): void
144+
{
145+
$this->inputFile = $inputFile;
146+
}
136147
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Document Title</title>
5+
</head>
6+
<body>
7+
<div class="section" id="document-title">
8+
<h1>Document Title</h1>
9+
<p>Lorem Ipsum Dolor.</p>
10+
</div>
11+
</body>
12+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
==============
2+
Document Title
3+
==============
4+
5+
Lorem Ipsum Dolor.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<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-file="README.rst"
6+
>
7+
</guides>

0 commit comments

Comments
 (0)