Skip to content

Commit 22999c9

Browse files
committed
Rendering JSON files as .fjson and in same directory
This is to match Sphinx
1 parent 6abf1a2 commit 22999c9

File tree

9 files changed

+19
-41
lines changed

9 files changed

+19
-41
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
/vendor/
22
/tests/_output
3-
/tests/_outputJson
43
/var/

src/BuildContext.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ class BuildContext
1212

1313
private $runtimeInitialized = false;
1414
private $sourceDir;
15-
private $htmlOutputDir;
16-
private $jsonOutputDir;
15+
private $outputDir;
1716
private $parseSubPath;
1817
private $disableCache = false;
1918

@@ -31,11 +30,10 @@ public function __construct(
3130
$this->symfonyDocUrl = $symfonyDocUrl;
3231
}
3332

34-
public function initializeRuntimeConfig(string $sourceDir, string $htmlOutputDir, ?string $jsonOutputDir = null, ?string $parseSubPath = null, bool $disableCache = false)
33+
public function initializeRuntimeConfig(string $sourceDir, string $outputDir, ?string $parseSubPath = null, bool $disableCache = false)
3534
{
3635
$this->sourceDir = $sourceDir;
37-
$this->htmlOutputDir = $htmlOutputDir;
38-
$this->jsonOutputDir = $jsonOutputDir;
36+
$this->outputDir = $outputDir;
3937
$this->parseSubPath = $parseSubPath;
4038
$this->disableCache = $disableCache;
4139
$this->runtimeInitialized = true;
@@ -73,18 +71,11 @@ public function getSourceDir(): string
7371
return $this->sourceDir;
7472
}
7573

76-
public function getHtmlOutputDir(): string
74+
public function getOutputDir(): string
7775
{
7876
$this->checkThatRuntimeConfigIsInitialized();
7977

80-
return $this->htmlOutputDir;
81-
}
82-
83-
public function getJsonOutputDir(): string
84-
{
85-
$this->checkThatRuntimeConfigIsInitialized();
86-
87-
return $this->jsonOutputDir;
78+
return $this->outputDir;
8879
}
8980

9081
public function getParseSubPath(): ?string

src/CI/MissingFilesChecker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function getMissingFiles(): array
3333
foreach ($this->finder as $file) {
3434
$htmlFile = str_replace(
3535
[$this->buildContext->getSourceDir(), '.rst'],
36-
[$this->buildContext->getHtmlOutputDir(), '.html'],
36+
[$this->buildContext->getOutputDir(), '.html'],
3737
$file->getRealPath()
3838
);
3939

src/Command/CheckUrlsCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6161
$this->io->success('All urls in the docs are valid!');
6262
}
6363

64-
$this->filesystem->remove($this->buildContext->getHtmlOutputDir());
64+
$this->filesystem->remove($this->buildContext->getOutputDir());
6565
}
6666

6767
public function preBuildRender()

src/Command/CommandInitializerTrait.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ private function doInitialize(InputInterface $input, OutputInterface $output, st
4949
$this->buildContext->initializeRuntimeConfig(
5050
$sourceDir,
5151
$this->initializeHtmlOutputDir($this->filesystem, $outputDir),
52-
$this->initializeJsonOutputDir($outputDir),
5352
$this->initializeParseSubPath($input, $sourceDir),
5453
$this->isCacheDisabled()
5554
);
@@ -104,16 +103,6 @@ private function initializeParseSubPath(InputInterface $input, string $sourceDir
104103
return $parseSubPath;
105104
}
106105

107-
private function initializeJsonOutputDir(string $outputDir): string
108-
{
109-
$jsonOutputDir = $this->getRealAbsolutePath($outputDir.'/_json', $this->filesystem);
110-
if ($this->isCacheDisabled() && $this->filesystem->exists($jsonOutputDir)) {
111-
$this->filesystem->remove($jsonOutputDir);
112-
}
113-
114-
return $jsonOutputDir;
115-
}
116-
117106
private function getRealAbsolutePath(string $path, Filesystem $filesystem): string
118107
{
119108
return sprintf(
@@ -157,7 +146,7 @@ private function startBuild()
157146

158147
$this->builder->build(
159148
$this->buildContext->getSourceDir(),
160-
$this->buildContext->getHtmlOutputDir()
149+
$this->buildContext->getOutputDir()
161150
);
162151
}
163152

@@ -174,8 +163,8 @@ function (string $rstFile) {
174163
array_keys(iterator_to_array($finder))
175164
);
176165

177-
$this->sanitizeOutputDir($rstFiles, $this->buildContext->getHtmlOutputDir(), 'html');
178-
$this->sanitizeOutputDir($rstFiles, $this->buildContext->getJsonOutputDir(), 'json');
166+
$this->sanitizeOutputDir($rstFiles, $this->buildContext->getOutputDir(), 'html');
167+
$this->sanitizeOutputDir($rstFiles, $this->buildContext->getOutputDir(), 'fjson');
179168
}
180169

181170
private function sanitizeOutputDir(array $existingRstFiles, string $outputDir, string $format)

src/Generator/HtmlForPdfGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct(Metas $metas, BuildContext $buildContext)
2424
public function generateHtmlForPdf() {
2525

2626
$finder = new Finder();
27-
$finder->in($this->buildContext->getHtmlOutputDir())
27+
$finder->in($this->buildContext->getOutputDir())
2828
->depth(0)
2929
->notName([$this->buildContext->getParseSubPath(), '_images']);
3030

@@ -33,21 +33,21 @@ public function generateHtmlForPdf() {
3333
$fs->remove($file->getRealPath());
3434
}
3535

36-
$basePath = sprintf('%s/%s', $this->buildContext->getHtmlOutputDir(), $this->buildContext->getParseSubPath());
36+
$basePath = sprintf('%s/%s', $this->buildContext->getOutputDir(), $this->buildContext->getParseSubPath());
3737
$indexFile = sprintf('%s/%s', $basePath, 'index.html');
3838
if (!$fs->exists($indexFile)) {
3939
throw new \InvalidArgumentException(sprintf('File "%s" does not exist', $indexFile));
4040
}
4141

4242
// extracting all files from index's TOC, in the right order
43-
$parserFilename = $this->getParserFilename($indexFile, $this->buildContext->getHtmlOutputDir());
43+
$parserFilename = $this->getParserFilename($indexFile, $this->buildContext->getOutputDir());
4444
$meta = $this->getMetaEntry($parserFilename);
4545
$files = current($meta->getTocs());
4646
array_unshift($files, sprintf('%s/index', $this->buildContext->getParseSubPath()));
4747

4848
// building one big html file with all contents
4949
$content = '';
50-
$htmlDir = $this->buildContext->getHtmlOutputDir();
50+
$htmlDir = $this->buildContext->getOutputDir();
5151
$relativeImagesPath = str_repeat('../', substr_count($this->buildContext->getParseSubPath(), '/'));
5252
foreach ($files as $file) {
5353
$meta = $this->getMetaEntry($file);

src/Generator/JsonGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ public function generateJson(ProgressBar $progressBar)
3333

3434
foreach ($this->metas->getAll() as $filename => $metaEntry) {
3535
$parserFilename = $filename;
36-
$jsonFilename = $this->buildContext->getJsonOutputDir().'/'.$filename.'.json';
36+
$jsonFilename = $this->buildContext->getOutputDir().'/'.$filename.'.fjson';
3737

38-
$crawler = new Crawler(file_get_contents($this->buildContext->getHtmlOutputDir().'/'.$filename.'.html'));
38+
$crawler = new Crawler(file_get_contents($this->buildContext->getOutputDir().'/'.$filename.'.html'));
3939

4040
$data = [
41-
'body' => $crawler->filter('body')->html(),
4241
'title' => $metaEntry->getTitle(),
4342
'current_page_name' => $parserFilename,
4443
'toc' => $this->generateToc($metaEntry, current($metaEntry->getTitles())[1]),
@@ -48,6 +47,7 @@ public function generateJson(ProgressBar $progressBar)
4847
$this->guessNext($parserFilename),
4948
$this->guessPrev($parserFilename),
5049
],
50+
'body' => $crawler->filter('body')->html(),
5151
];
5252

5353
$fs->dumpFile(

src/Listener/CopyImagesDirectoryListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function postBuildRender()
1818
{
1919
$fs = new Filesystem();
2020
if ($fs->exists($imageDir = sprintf('%s/_images', $this->buildContext->getSourceDir()))) {
21-
$fs->mirror($imageDir, sprintf('%s/_images', $this->buildContext->getHtmlOutputDir()));
21+
$fs->mirror($imageDir, sprintf('%s/_images', $this->buildContext->getOutputDir()));
2222
}
2323
}
2424
}

tests/IntegrationTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function testIntegration(string $folder)
7777

7878
foreach ($finder as $htmlFile) {
7979
$relativePath = $htmlFile->getRelativePathname();
80-
$actualFilename = __DIR__.'/_outputJson/'.str_replace('.html', '.json', $relativePath);
80+
$actualFilename = __DIR__.'/_output/'.str_replace('.html', '.fjson', $relativePath);
8181
$this->assertFileExists($actualFilename);
8282

8383
$jsonData = json_decode(file_get_contents($actualFilename), true);
@@ -286,7 +286,6 @@ private function createBuildContext(string $sourceDir): BuildContext
286286
$buildContext->initializeRuntimeConfig(
287287
$sourceDir,
288288
__DIR__.'/_output',
289-
__DIR__.'/_outputJson',
290289
null
291290
);
292291

0 commit comments

Comments
 (0)