Skip to content

Commit b7652ed

Browse files
committed
chore: add script to add "Edit this page" button in User Guide
1 parent 93f6ff9 commit b7652ed

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

user_guide_src/add-edit-this-page

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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) {
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/i',
29+
RecursiveRegexIterator::MATCH
30+
);
31+
32+
foreach ($files as $file_path => $file_info) {
33+
echo 'processing... ' . $file_path . PHP_EOL;
34+
35+
$uriPath = str_replace('.html', '.rst', str_replace($dir, '', $file_path));
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($file_path);
40+
41+
$pattern = '!">Edit this page</a>!u';
42+
if (preg_match($pattern, $content) === 1) {
43+
echo 'skip' . PHP_EOL;
44+
45+
continue;
46+
}
47+
48+
$pattern = '/<div role="navigation" aria-label="breadcrumbs navigation">/u';
49+
$content = preg_replace(
50+
$pattern,
51+
$gitHubLink . PHP_EOL . '<div role="navigation" aria-label="breadcrumbs navigation">',
52+
$content
53+
);
54+
55+
file_put_contents($file_path, $content, LOCK_EX);
56+
}

0 commit comments

Comments
 (0)