|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +$srcFolder = __DIR__ . '/source'; |
| 13 | + |
| 14 | +// Exclude static folders |
| 15 | +$excludes = ['_static', 'images']; |
| 16 | + |
| 17 | +// Safety prefix |
| 18 | +$prefix = 'old_'; |
| 19 | + |
| 20 | +// Begin user info |
| 21 | +echo 'The following sections were renumerated:', PHP_EOL; |
| 22 | + |
| 23 | +// Loop source directory |
| 24 | +$srcIterator = new DirectoryIterator($srcFolder); |
| 25 | + |
| 26 | +foreach ($srcIterator as $chapterInfo) { |
| 27 | + if (! $chapterInfo->isDot() && $chapterInfo->isDir() && ! in_array($chapterInfo->getFilename(), $excludes, true)) { |
| 28 | + $chapterName = $chapterInfo->getFilename(); |
| 29 | + |
| 30 | + // Iterate the working directory |
| 31 | + $chapterIterator = new DirectoryIterator($chapterInfo->getPathname()); |
| 32 | + |
| 33 | + foreach ($chapterIterator as $sectionInfo) { |
| 34 | + if (! $sectionInfo->isDot() && $sectionInfo->isFile() && $sectionInfo->getExtension() === 'rst') { |
| 35 | + $sectionName = $sectionInfo->getBasename('.rst'); |
| 36 | + $sectionFolder = $sectionInfo->getPath() . '/' . $sectionName; |
| 37 | + |
| 38 | + // Read the section file |
| 39 | + $sectionContent = file_get_contents($sectionInfo->getPathname()); |
| 40 | + |
| 41 | + // Match all includes |
| 42 | + preg_match_all("~\\.\\. literalinclude:: {$sectionName}\\/(.+)\\.php(\n[ ]*:lines: 2\\-)?~", $sectionContent, $matches); |
| 43 | + $includeStrings = $matches[0]; |
| 44 | + $exampleNames = $matches[1]; |
| 45 | + |
| 46 | + // Exit early if no matches |
| 47 | + if (count($exampleNames) === 0) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + // Check if examples are consecutive |
| 52 | + $consecutive = true; |
| 53 | + |
| 54 | + foreach ($exampleNames as $exampleIndex => $exampleName) { |
| 55 | + if (str_pad($exampleIndex + 1, 3, '0', STR_PAD_LEFT) !== $exampleName) { |
| 56 | + $consecutive = false; |
| 57 | + break; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Exit if examples are already consecutive |
| 62 | + if ($consecutive) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + // Rename all example files to avoid conflicts |
| 67 | + $exampleIterator = new DirectoryIterator($sectionFolder); |
| 68 | + |
| 69 | + foreach ($exampleIterator as $exampleInfo) { |
| 70 | + if (! $exampleInfo->isDot() && $exampleInfo->isFile() && $exampleInfo->getExtension() === 'php') { |
| 71 | + rename($exampleInfo->getPathname(), $exampleInfo->getPath() . '/' . $prefix . $exampleInfo->getFilename()); |
| 72 | + } |
| 73 | + } |
| 74 | + $sectionContent = preg_replace("~\\.\\. literalinclude:: {$sectionName}\\/(.+)\\.php~", ".. literalinclude:: {$sectionName}/{$prefix}$1.php", $sectionContent); |
| 75 | + |
| 76 | + // Renumerate examples |
| 77 | + foreach ($exampleNames as $exampleIndex => $exampleName) { |
| 78 | + $newName = str_pad($exampleIndex + 1, 3, '0', STR_PAD_LEFT); |
| 79 | + |
| 80 | + // Rename example file |
| 81 | + rename($sectionFolder . '/' . $prefix . $exampleName . '.php', $sectionFolder . '/' . $newName . '.php'); |
| 82 | + |
| 83 | + // Fix include link |
| 84 | + $sectionContent = preg_replace(preg_quote(str_replace($exampleName, $prefix . $exampleName, $includeStrings[$exampleIndex]), '~'), str_replace($exampleName, $newName, $includeStrings[$exampleIndex]), $sectionContent, 1, $count); |
| 85 | + } |
| 86 | + |
| 87 | + // Write new content to rst |
| 88 | + file_put_contents($sectionInfo->getPathname(), $sectionContent); |
| 89 | + |
| 90 | + // User info |
| 91 | + echo $chapterName, '/', $sectionName, PHP_EOL; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// End user info |
| 98 | +echo 'Renumerating finished.', PHP_EOL; |
0 commit comments