Skip to content

Commit 7089e15

Browse files
committed
build docs in dev
1 parent 05d9b21 commit 7089e15

File tree

8 files changed

+2924
-0
lines changed

8 files changed

+2924
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/tests export-ignore
55
/tools export-ignore
66
docker-compose.yml export-ignore
7+
/_docs_build export-ignore

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
/tools/*/composer.lock
88
/tools/*/vendor
99
/vendor/
10+
/_docs_build/vendor
11+
/_docs_build/output

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,27 @@ B) The generated code itself may change between minor releases. This
2525

2626
[1]: https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html
2727
[2]: https://symfony.com/doc/current/contributing/code/bc.html
28+
29+
---
30+
31+
Build Documentation Locally
32+
---------------------------
33+
34+
This is not needed for contributing, but it's useful if you would like to debug some
35+
issue in the docs or if you want to read MakerBundles Documentation offline.
36+
37+
```bash
38+
$ cd _docs_build/
39+
40+
$ composer install
41+
42+
$ php build.php
43+
```
44+
45+
After generating docs, serve them with the internal PHP server:
46+
47+
```bash
48+
$ php -S localhost:8000 -t output/
49+
```
50+
51+
Browse `http://localhost:8000` to read the docs.

_docs_build/build.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
/*
5+
* This file is part of the Symfony MakerBundle package.
6+
*
7+
* (c) Fabien Potencier <[email protected]>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
require __DIR__.'/vendor/autoload.php';
14+
15+
use Symfony\Component\Console\Application;
16+
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Input\InputOption;
18+
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Component\Console\Style\SymfonyStyle;
20+
use SymfonyDocsBuilder\BuildConfig;
21+
use SymfonyDocsBuilder\DocBuilder;
22+
23+
(new Application('Symfony Docs Builder', '1.0'))
24+
->register('build-docs')
25+
->addOption('generate-fjson-files', null, InputOption::VALUE_NONE, 'Use this option to generate docs both in HTML and JSON formats')
26+
->addOption('disable-cache', null, InputOption::VALUE_NONE, 'Use this option to force a full regeneration of all doc contents')
27+
->setCode(function (InputInterface $input, OutputInterface $output) {
28+
$io = new SymfonyStyle($input, $output);
29+
$io->text('Building all Symfony Docs...');
30+
31+
$outputDir = __DIR__.'/output';
32+
$buildConfig = (new BuildConfig())
33+
->setSymfonyVersion('7.1')
34+
->setContentDir(__DIR__.'/../src/Resources/doc')
35+
->setOutputDir($outputDir)
36+
->setImagesDir(__DIR__.'/output/_images')
37+
->setImagesPublicPrefix('_images')
38+
->setTheme('rtd')
39+
;
40+
41+
$buildConfig->setExcludedPaths(['.github/', '_build/']);
42+
43+
if (!$generateJsonFiles = $input->getOption('generate-fjson-files')) {
44+
$buildConfig->disableJsonFileGeneration();
45+
}
46+
47+
if ($isCacheDisabled = $input->getOption('disable-cache')) {
48+
$buildConfig->disableBuildCache();
49+
}
50+
51+
$io->comment(sprintf('cache: %s / output file type(s): %s', $isCacheDisabled ? 'disabled' : 'enabled', $generateJsonFiles ? 'HTML and JSON' : 'HTML'));
52+
if (!$isCacheDisabled) {
53+
$io->comment('Tip: add the --disable-cache option to this command to force the re-build of all docs.');
54+
}
55+
56+
$result = (new DocBuilder())->build($buildConfig);
57+
58+
if ($result->isSuccessful()) {
59+
// fix assets URLs to make them absolute (otherwise, they don't work in subdirectories)
60+
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($outputDir));
61+
62+
foreach (new RegexIterator($iterator, '/^.+\.html$/i', RegexIterator::GET_MATCH) as $match) {
63+
$htmlFilePath = array_shift($match);
64+
$htmlContents = file_get_contents($htmlFilePath);
65+
66+
$htmlRelativeFilePath = str_replace($outputDir.'/', '', $htmlFilePath);
67+
$subdirLevel = substr_count($htmlRelativeFilePath, '/');
68+
$baseHref = str_repeat('../', $subdirLevel);
69+
70+
$htmlContents = str_replace('<head>', '<head><base href="'.$baseHref.'">', $htmlContents);
71+
$htmlContents = str_replace('<img src="/_images/', '<img src="_images/', $htmlContents);
72+
file_put_contents($htmlFilePath, $htmlContents);
73+
}
74+
75+
foreach (new RegexIterator($iterator, '/^.+\.css/i', RegexIterator::GET_MATCH) as $match) {
76+
$htmlFilePath = array_shift($match);
77+
$htmlContents = file_get_contents($htmlFilePath);
78+
file_put_contents($htmlFilePath, str_replace('fonts/', '../fonts/', $htmlContents));
79+
}
80+
81+
$io->success(sprintf('The Symfony Docs were successfully built at %s', realpath($outputDir)));
82+
} else {
83+
$io->error(sprintf("There were some errors while building the docs:\n\n%s\n", $result->getErrorTrace()));
84+
$io->newLine();
85+
$io->comment('Tip: you can add the -v, -vv or -vvv flags to this command to get debug information.');
86+
87+
return 1;
88+
}
89+
90+
return 0;
91+
})
92+
->getApplication()
93+
->setDefaultCommand('build-docs', true)
94+
->run();

_docs_build/composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"minimum-stability": "dev",
3+
"prefer-stable": true,
4+
"config": {
5+
"platform": {
6+
"php": "8.1.0"
7+
},
8+
"preferred-install": {
9+
"*": "dist"
10+
},
11+
"sort-packages": true,
12+
"allow-plugins": {
13+
"symfony/flex": true
14+
}
15+
},
16+
"require": {
17+
"php": ">=8.1",
18+
"symfony-tools/docs-builder": "^0.23",
19+
"symfony/console": "^6.2",
20+
"symfony/process": "^6.2"
21+
}
22+
}

0 commit comments

Comments
 (0)