Skip to content
This repository was archived by the owner on Nov 23, 2026. It is now read-only.

Commit cd87e1a

Browse files
committed
Clean up converter comments
1 parent a4ea9f4 commit cd87e1a

4 files changed

Lines changed: 29 additions & 140 deletions

File tree

‎admin/convert.php‎

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
declare(strict_types=1);
44

5-
// Load environment configuration
65
/**
76
* @return array<string, string>
87
*/
@@ -39,7 +38,6 @@ function loadEnvConfig(): array {
3938
return $config;
4039
}
4140

42-
// Get source directory based on configuration
4341
function getSourceDirectory(): string {
4442
$config = loadEnvConfig();
4543

@@ -53,7 +51,6 @@ function getSourceDirectory(): string {
5351
return $projectPath;
5452
}
5553

56-
// Default to local docs-rst directory
5754
return __DIR__ . '/../docs-rst/';
5855
}
5956

@@ -98,26 +95,24 @@ function checkPandocVersion(): void {
9895
'codeBlocks',
9996
]);
10097

101-
// Load all filters
10298
function loadFilters(): void {
10399
$filters = array_unique(array_merge(BEFORE_FILTERS, AFTER_FILTERS));
104100
foreach ($filters as $filter) {
105101
include_once(__DIR__ . '/filters/' . $filter . '.php');
106102
}
107103
}
108104

109-
// Apply before filters
105+
/**
106+
* Filtered copy of `$path`, saved next to it as `$path_` until applyAfterFilters() deletes it.
107+
*/
110108
function applyBeforeFilters(string $path): string {
111109
$file = [
112110
'path' => $path . '_',
113111
'folder' => dirname($path),
114112
];
115113

116-
// Make a copy, so we can apply before filters
117-
// without consequences to source file
118114
copy($path, $file['path']);
119115

120-
// Iterate over after filter
121116
foreach (BEFORE_FILTERS as $action) {
122117
$function = 'before_filter_' . $action;
123118

@@ -126,11 +121,9 @@ function applyBeforeFilters(string $path): string {
126121
}
127122
}
128123

129-
// Return new file name (copy)
130124
return $file['path'];
131125
}
132126

133-
// Apply after filters
134127
function applyAfterFilters(string $path, string $sourceCopy): void {
135128
if (file_exists($sourceCopy)) {
136129
unlink($sourceCopy);
@@ -140,7 +133,6 @@ function applyAfterFilters(string $path, string $sourceCopy): void {
140133
'path' => $path,
141134
'folder' => dirname($path),
142135
];
143-
// Iterate over after filter
144136
foreach (AFTER_FILTERS as $action) {
145137
$function = 'after_filter_' . $action;
146138

@@ -150,7 +142,6 @@ function applyAfterFilters(string $path, string $sourceCopy): void {
150142
}
151143
}
152144

153-
// Modify file
154145
/**
155146
* @param array{path: string, folder: string} $file
156147
* @param callable-string $function
@@ -168,10 +159,8 @@ function updateFile(array $file, string $function): void {
168159
}
169160

170161

171-
// Completely empties the target directory
172162
function clean(string $target): void {
173163
if (!is_dir($target)) {
174-
// Directory doesn't exist, create it
175164
mkdir($target, 0755, true);
176165
return;
177166
}
@@ -195,7 +184,6 @@ function clean(string $target): void {
195184
closedir($dir);
196185
}
197186

198-
// Copy a directory and all its contents recursively
199187
function copyDirectory(string $source, string $target): bool {
200188
if (!is_dir($source)) {
201189
return false;
@@ -225,7 +213,6 @@ function copyDirectory(string $source, string $target): bool {
225213
return true;
226214
}
227215

228-
// Copy essential assets that are needed for the docs
229216
function copyAssets(): void {
230217
$assetsToTransfer = [
231218
'_static' => '_static',
@@ -247,11 +234,9 @@ function copyAssets(): void {
247234
}
248235
}
249236

250-
// Create MkDocs assets structure from _static
251237
createMkDocsAssets();
252238
}
253239

254-
// Create the assets folder structure that MkDocs expects
255240
function createMkDocsAssets(): void {
256241
$staticDir = TARGET_DIR . '_static/';
257242
$assetsDir = TARGET_DIR . 'assets/';
@@ -265,13 +250,6 @@ function createMkDocsAssets(): void {
265250
mkdir($assetsDir, 0755, true);
266251
}
267252

268-
// CSS files are now handled by createMkDocsThemeCSS() from admin/assets
269-
// No need to copy from _static/css anymore
270-
271-
// CSS and JS files are now handled by createMkDocsThemeCSS() from admin/assets
272-
// No need to copy from _static/css or _static/js anymore
273-
274-
// Copy logo and favicon
275253
$iconMapping = [
276254
'ci-logo-text.svg' => 'flame.svg',
277255
'favicon.ico' => 'favicon.ico'
@@ -287,11 +265,12 @@ function createMkDocsAssets(): void {
287265
}
288266
}
289267

290-
// Create MkDocs Material theme-compatible CSS
291268
createMkDocsThemeCSS();
292269
}
293270

294-
// Copy CSS files from admin/assets folder
271+
/**
272+
* Copies the theme CSS and JavaScript from `admin/assets`.
273+
*/
295274
function createMkDocsThemeCSS(): void {
296275
$assetsDir = TARGET_DIR . 'assets/';
297276
$assetsCssDir = $assetsDir . 'css/';
@@ -306,7 +285,6 @@ function createMkDocsThemeCSS(): void {
306285
mkdir($assetsJsDir, 0755, true);
307286
}
308287

309-
// Copy the pre-built CSS files from admin/assets
310288
$cssFiles = [
311289
'codeigniter.css' => 'codeigniter.css',
312290
'codeigniter_dark_mode.css' => 'codeigniter_dark_mode.css'
@@ -324,7 +302,6 @@ function createMkDocsThemeCSS(): void {
324302
}
325303
}
326304

327-
// Copy the pre-built JavaScript files from admin/assets
328305
$jsFiles = [
329306
'hljs.js' => 'hljs.js'
330307
];
@@ -342,8 +319,9 @@ function createMkDocsThemeCSS(): void {
342319
}
343320
}
344321

345-
// Core conversion function that handles the actual pandoc conversion
346322
/**
323+
* Runs the before-filters, pandoc and the after-filters as three passes, so every after-filter sees every page's pandoc output.
324+
*
347325
* @param list<array{source: string, target: string}> $jobs
348326
*
349327
* @return int Number of files that pandoc failed on
@@ -405,10 +383,9 @@ function runPandoc(array $jobs, array $copies): array {
405383
return $exitCodes;
406384
}
407385

408-
// Recursively scan the source directory,
409-
// using pandoc to convert any .rst files to .md
410-
// and copying any other files and directories as-is
411386
/**
387+
* Mirrors `$source` into `$target`, copying every file except the RST pages, which it appends to `$jobs`.
388+
*
412389
* @param list<array{source: string, target: string}> $jobs Accumulated RST conversion jobs
413390
*/
414391
function convert(string $source, string $target, array &$jobs): void {
@@ -418,15 +395,14 @@ function convert(string $source, string $target, array &$jobs): void {
418395
mkdir($target);
419396
}
420397

421-
// Assets that are handled separately by copyAssets()
398+
// Copied by copyAssets() instead.
422399
$skipAssets = ['_static', 'images', '.nojekyll', 'conf.py', 'assets'];
423400

424401
while (($file = readdir($dir)) !== false) {
425402
if ($file == '.' || $file == '..') {
426403
continue;
427404
}
428405

429-
// Skip assets that are copied separately
430406
if (in_array($file, $skipAssets)) {
431407
continue;
432408
}
@@ -446,11 +422,9 @@ function convert(string $source, string $target, array &$jobs): void {
446422
closedir($dir);
447423
}
448424

449-
// Convert a single RST file
450425
function convertSingleFile(string $filename): bool {
451426
loadFilters();
452427

453-
// Remove .rst extension if provided
454428
$filename = preg_replace('/\.rst$/', '', $filename);
455429

456430
$sourceFile = realpath(SOURCE_DIR . $filename . '.rst');
@@ -461,7 +435,6 @@ function convertSingleFile(string $filename): bool {
461435
return false;
462436
}
463437

464-
// Ensure target directory exists
465438
$targetDir = dirname($targetFile);
466439
if (!is_dir($targetDir)) {
467440
mkdir($targetDir, 0755, true);
@@ -477,15 +450,12 @@ function convertSingleFile(string $filename): bool {
477450

478451
checkPandocVersion();
479452

480-
// Check for CLI argument
481453
if (isset($argv[1])) {
482454
$filename = $argv[1];
483455
exit(convertSingleFile($filename) ? 0 : 1);
484456
} else {
485-
// Do full conversion
486457
echo "Starting full conversion...\n";
487458

488-
// Ensure target directory exists
489459
if (!is_dir(TARGET_DIR)) {
490460
echo "Creating target directory...\n";
491461
mkdir(TARGET_DIR, 0755, true);

‎admin/filters/docLink.php‎

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,15 @@
33
declare(strict_types=1);
44

55
function before_filter_docLink(string $data, string $folder): ?string {
6-
// We only need a parent folder
76
$folder = explode('/', trim($folder, '/'));
87
$folder = array_pop($folder);
98

10-
// Handle :doc: directives FIRST before general backtick patterns
11-
// :doc:`View Decorators <../outgoing/view_decorators>`
12-
// :doc:`Feature Tests </testing/feature>`
13-
// :doc:`configuration <configuration>`
9+
// Runs before the generic `Text <target>` pattern below, which also matches :doc:`Text <path>`.
1410
$data = preg_replace_callback('/:doc:`(.*?) <(.*?)>`/', function (array $matches) use ($folder): string {
1511
$text = $matches[1];
1612
$url = $matches[2];
1713

1814
if (identifyRelativePath($url) === '../') {
19-
// Convert directly to markdown link for relative paths
2015
return "[{$text}]({$url}.md)";
2116
}
2217

@@ -38,37 +33,29 @@ function before_filter_docLink(string $data, string $folder): ?string {
3833
return "[{$text}]({$url}.md)";
3934
}, $data);
4035

41-
// Handle RST cross-references like `CodeIgniter URLs <urls-remove-index-php-apache>`
42-
// Convert them to proper markdown links, and convert external URLs to HTML with target="_blank"
4336
$data = preg_replace_callback('/`([^`]+?) <([^>]+?)>`_?/', function (array $matches): string {
4437
$text = $matches[1];
4538
$reference = $matches[2];
4639

47-
// Handle external URLs - use placeholder that won't get escaped
40+
// A placeholder, since pandoc escapes raw HTML written into the RST.
4841
if (preg_match('/^https?:\/\//', $reference)) {
4942
return "{{EXTERNAL_LINK_START}}{$reference}{{EXTERNAL_LINK_MIDDLE}}{$text}{{EXTERNAL_LINK_END}}";
5043
}
5144

52-
// Try to find actual files that match the reference
5345
$targetFile = findMatchingFile($reference);
5446

5547
if ($targetFile) {
5648
return "[{$text}]({$targetFile})";
5749
}
5850

59-
// For unmapped references, try to convert to a reasonable link
60-
// Convert underscore/dash patterns to section anchors
6151
if (strpos($reference, '-') !== false || strpos($reference, '_') !== false) {
6252
$sectionLink = '#' . str_replace('_', '-', $reference);
6353
return "[{$text}]({$sectionLink})";
6454
}
6555

66-
// If we can't map it, at least make it a proper link format
6756
return "[{$text}](#{$reference})";
6857
}, $data);
6958

70-
// :doc:`../outgoing/view_decorators`
71-
// :doc:`./backward_compatibility_notes`
7259
return preg_replace_callback('/:doc:`(.*?)`/', function (array $matches) use ($folder): string {
7360
$url = $matches[1];
7461
$text = explode('/', $url);
@@ -97,7 +84,6 @@ function before_filter_docLink(string $data, string $folder): ?string {
9784
}, $data);
9885
}
9986

100-
// helper function
10187
function identifyRelativePath(string $input): string|false
10288
{
10389
$pattern = '/^(\.\.\/|\.\/|\/)/';
@@ -109,9 +95,11 @@ function identifyRelativePath(string $input): string|false
10995
return false;
11096
}
11197

112-
// Helper function to find matching files based on reference patterns
98+
/**
99+
* Link to the page holding the `$reference` label, or null when no page is known.
100+
*/
113101
function findMatchingFile(string $reference): ?string {
114-
// Common reference patterns and their likely file locations
102+
// Labels whose page cannot be guessed from the label itself.
115103
$patterns = [
116104
'urls-remove-index-php' => 'general/urls.md#removing-indexphp',
117105
'urls-remove-index-php-apache' => 'general/urls.md#removing-indexphp-with-apache',
@@ -122,14 +110,11 @@ function findMatchingFile(string $reference): ?string {
122110
'dotenv-file' => 'general/configuration.md#environment-variables'
123111
];
124112

125-
// Check if we have a direct mapping
126113
if (isset($patterns[$reference])) {
127114
$filePath = $patterns[$reference];
128-
// Convert to relative path from installation folder
129115
return '../' . $filePath;
130116
}
131117

132-
// Try to find files by scanning the source directory
133118
$possiblePaths = [
134119
"general/{$reference}.md",
135120
"concepts/{$reference}.md",
@@ -147,43 +132,32 @@ function findMatchingFile(string $reference): ?string {
147132
}
148133

149134
function after_filter_docLink(string $data, string $folder): ?string {
150-
// Clean up any remaining RST-style references and fix escaped markdown links
151-
152-
// Remove escaping backslashes that break markdown links
135+
// pandoc escapes brackets and parentheses that it reads as plain text.
153136
$data = str_replace('\\]', ']', $data);
154137
$data = str_replace('\\(', '(', $data);
155138
$data = str_replace('\\)', ')', $data);
156139

157-
// Fix escaped markdown links (pandoc often escapes square brackets)
158140
$data = str_replace('\\[', '[', $data);
159141

160-
// Handle the literal backslash-escaped format as it appears in the output
161-
// Pattern: :ref:\[text\](url) -> [text](url)
162142
$data = str_replace(':ref:\\[', '[', $data);
163143
$data = str_replace(':doc:\\[', '[', $data);
164144
$data = str_replace('\\]\\(', '](', $data);
165145

166-
// Handle any remaining :ref: and :doc: patterns
167146
$data = preg_replace('/:ref:\[([^\]]+)\]\(([^)]+)\)/', '[$1]($2)', $data);
168147
$data = preg_replace('/:doc:\[([^\]]+)\]\(([^)]+)\)/', '[$1]($2)', $data);
169148

170-
// Handle :ref: directives that may have been created without links
171149
$data = preg_replace('/:ref:`([^`]+)`_?/', '[$1]', $data);
172150

173-
// Handle :doc: directives that may have been created without links
174151
$data = preg_replace('/:doc:`([^`]+)`_?/', '[$1]', $data);
175152

176-
// Clean up any remaining RST-style backtick references with trailing underscores
177153
$data = preg_replace('/`([^`]+)`_/', '[$1]', $data);
178154

179-
// Convert external markdown links to placeholder format
180155
$data = preg_replace_callback('/\[([^\]]+)\]\((https?:\/\/[^)]+)\)/', function (array $matches): string {
181156
$text = $matches[1];
182157
$url = $matches[2];
183158
return "{{EXTERNAL_LINK_START}}{$url}{{EXTERNAL_LINK_MIDDLE}}{$text}{{EXTERNAL_LINK_END}}";
184159
}, $data);
185160

186-
// Convert all external link placeholders to final HTML with target="_blank"
187161
$data = preg_replace('/{{EXTERNAL_LINK_START}}([^{]+){{EXTERNAL_LINK_MIDDLE}}([^{]+){{EXTERNAL_LINK_END}}/', '<a href="$1" target="_blank">$2</a>', $data);
188162

189163
return $data;

0 commit comments

Comments
 (0)