22
33declare (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
4341function 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
10298function 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+ */
110108function 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
134127function 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
172162function 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
199187function 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
229216function 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
255240function 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+ */
295274function 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 */
414391function 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
450425function 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
478451checkPandocVersion ();
479452
480- // Check for CLI argument
481453if (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 );
0 commit comments