-
Notifications
You must be signed in to change notification settings - Fork 64
/
RSTCopier.php
167 lines (127 loc) · 5.66 KB
/
RSTCopier.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
declare(strict_types=1);
namespace Doctrine\Website\Docs\RST;
use Doctrine\Website\Model\Project;
use Doctrine\Website\Model\ProjectVersion;
use Symfony\Component\Filesystem\Filesystem;
use function file_exists;
use function preg_replace;
use function sprintf;
use function str_replace;
/** @final */
class RSTCopier
{
final public const RST_TEMPLATE = <<<'TEMPLATE'
SIDEBAR BEGIN
{{ sidebar }}
CONTENT BEGIN
{{ content }}
TEMPLATE;
final public const DEFAULT_SIDEBAR = <<<'SIDEBAR'
.. toctree::
:depth: 3
:glob:
/*
SIDEBAR;
public function __construct(
private readonly RSTFileRepository $rstFileRepository,
private readonly Filesystem $filesystem,
private readonly string $docsDir,
) {
}
public function copyRst(Project $project, ProjectVersion $version): void
{
foreach ($version->getDocsLanguages() as $language) {
$outputPath = $project->getProjectVersionDocsPath($this->docsDir, $version, $language->getCode());
// clear existing files before copying the rst over
$this->filesystem->remove($this->rstFileRepository->findFiles($outputPath));
$sidebar = $this->getSidebarRST($language->getPath());
$files = $this->rstFileRepository->getSourceFiles($language->getPath());
foreach ($files as $file) {
$this->copyFile(
$project,
$version,
$language,
$file,
$outputPath,
$sidebar,
);
}
}
}
private function copyFile(
Project $project,
ProjectVersion $version,
RSTLanguage $language,
string $file,
string $outputPath,
string $sidebar,
): void {
$filePath = str_replace($language->getPath(), '', $file);
$fileContents = $this->rstFileRepository->getFileContents($file);
$fixedRst = $this->fixRSTSyntax($project, $fileContents);
$path = str_replace('/' . $language->getCode(), '', $language->getPath());
$sourceFile = str_replace($path, '', $file);
$rstTemplate = $this->prepareRSTTemplate($fixedRst, $sidebar, $sourceFile);
$writePath = $outputPath . $filePath;
$this->filesystem->dumpFile($writePath, $rstTemplate);
}
private function prepareRSTTemplate(string $content, string $sidebar, string $sourceFile): string
{
// replace the sidebar in the RST template
$rstTemplate = str_replace('{{ sidebar }}', $sidebar, self::RST_TEMPLATE);
// put the content in the RST template
$content = str_replace('{{ content }}', $content, $rstTemplate);
// append the source file name to the content so we can parse it back out
// for use in the build process
return $content . "\n\n" . sprintf('{{ DOCS_SOURCE_PATH : %s }}', $sourceFile);
}
private function fixRSTSyntax(Project $project, string $content): string
{
// fix incorrect casing of note
$content = str_replace('.. Note::', '.. note::', $content);
// fix :maxdepth: to :depth:
$content = str_replace(':maxdepth:', ':depth:', $content);
// get rid of .. include:: toc.rst
$content = str_replace('.. include:: toc.rst', '', $content);
// replace \n::\n with \n.. code-block::\n
// this corrects code blocks that don't render properly.
// we should update the docs code but this makes old docs code render properly.
$content = preg_replace("/\n::\n/", "\n.. code-block::\n", $content);
$content = preg_replace("/\n:: \n/", "\n.. code-block::\n", $content);
$content = preg_replace("/\n.. code-block :: (.*)\n/", "\n.. code-block:: $1\n", $content);
// replace .. code:: with .. code-block::
$content = str_replace('.. code::', '.. code-block::', $content);
// fix list syntax
$content = str_replace("\n- \n", "\n- ", $content);
// stuff from doctrine1 docs
if ($project->getSlug() === 'doctrine1') {
$content = preg_replace("/:code:(.*)\n/", '$1', $content);
$content = preg_replace('/:php:(.*):`(.*)`/', '$2', $content);
$content = preg_replace('/:file:`(.*)`/', '$1', $content);
$content = preg_replace('/:code:`(.*)`/', '$1', $content);
$content = preg_replace('/:literal:`(.*)`/', '$1', $content);
$content = preg_replace('/:token:`(.*)`/', '$1', $content);
$content = str_replace('.. productionlist::', '', $content);
$content = preg_replace('/.. rubric:: Notes/', '', $content);
$content = preg_replace("/.. sidebar:: (.*)\n/", '$1', $content);
$content = str_replace('.. sidebar::', '', $content);
}
// we don't support :term:`*` syntax
$content = preg_replace('/:term:`(.*)`/', '$1', $content);
return $content;
}
private function getSidebarRST(string $docsDir): string
{
// check if we have an explicit sidebar file to use
// otherwise just use the default autogenerated sidebar
$sidebarPath = $docsDir . '/sidebar.rst';
if (file_exists($sidebarPath)) {
$sidebar = $this->rstFileRepository->getFileContents($sidebarPath);
// sidebar.rst paths were wrong. Remove this once sidebar.rst is updated everywhere
$sidebar = preg_replace('/^([ \t]*)(reference|tutorials|cookbook)/m', '$1/$2', $sidebar);
return $sidebar;
}
return self::DEFAULT_SIDEBAR;
}
}