|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php declare(strict_types=1); |
| 3 | + |
| 4 | +if ($argc === 1) { |
| 5 | + echo 'Usage: ' . $argv[0] . ' <html-dir>' . PHP_EOL; |
| 6 | + echo ' e.g: ' . $argv[0] . ' user_guide_src/build/html/' . PHP_EOL; |
| 7 | + |
| 8 | + exit(1); |
| 9 | +} |
| 10 | + |
| 11 | +$dir = realpath($argv[1]); |
| 12 | + |
| 13 | +if ($dir === false || ! is_dir($dir)) { |
| 14 | + throw new RuntimeException('Cannot find directory: ' . $dir); |
| 15 | +} |
| 16 | + |
| 17 | +$iterator = new RecursiveIteratorIterator( |
| 18 | + new RecursiveDirectoryIterator( |
| 19 | + $dir, |
| 20 | + FilesystemIterator::CURRENT_AS_FILEINFO | |
| 21 | + FilesystemIterator::KEY_AS_PATHNAME | |
| 22 | + FilesystemIterator::SKIP_DOTS |
| 23 | + ) |
| 24 | +); |
| 25 | + |
| 26 | +$files = new RegexIterator( |
| 27 | + $iterator, |
| 28 | + '/\A.+\.html\z/', |
| 29 | + RecursiveRegexIterator::MATCH |
| 30 | +); |
| 31 | + |
| 32 | +foreach ($files as $filePath => $fileInfo) { |
| 33 | + echo 'processing... ' . PHP_EOL; |
| 34 | + |
| 35 | + $uriPath = str_replace('.html', '.rst', str_replace($dir, '', $filePath)); |
| 36 | + $gitHubLink = '<a class="btn btn-neutral float-right" href="https://github.com/codeigniter4/CodeIgniter4/edit/develop/user_guide_src/source' |
| 37 | + . $uriPath . '">Edit this page</a>'; |
| 38 | + |
| 39 | + $content = file_get_contents($filePath); |
| 40 | + |
| 41 | + $pattern = '!">Edit this page</a>!u'; |
| 42 | + if (preg_match($pattern, $content) === 1) { |
| 43 | + // Move the cursor up 1 line |
| 44 | + echo "\033[1A"; |
| 45 | + echo 'skip: ' . $filePath . PHP_EOL; |
| 46 | + |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + $pattern = '/<div role="navigation" aria-label="breadcrumbs navigation">/u'; |
| 51 | + $content = preg_replace( |
| 52 | + $pattern, |
| 53 | + $gitHubLink . PHP_EOL . '<div role="navigation" aria-label="breadcrumbs navigation">', |
| 54 | + $content |
| 55 | + ); |
| 56 | + |
| 57 | + file_put_contents($filePath, $content, LOCK_EX); |
| 58 | + |
| 59 | + // Move the cursor up 1 line |
| 60 | + echo "\033[1A"; |
| 61 | + echo 'done: ' . $filePath . PHP_EOL; |
| 62 | +} |
0 commit comments