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

Commit 7717937

Browse files
committed
Add a watch target that reconverts changed RST pages
1 parent 514cc6b commit 7717937

2 files changed

Lines changed: 138 additions & 1 deletion

File tree

‎Makefile‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ MKDOCS := $(VENV)/bin/mkdocs
66
DOCS_PROJECT_PATH ?= $(shell sed -n 's/^DOCS_PROJECT_PATH=//p' .env 2>/dev/null)
77
export DOCS_PROJECT_PATH
88

9-
.PHONY: help install convert verify build serve
9+
.PHONY: help install convert verify build serve watch
1010

1111
help:
1212
@echo 'make install Install MkDocs into ./$(VENV)'
@@ -15,6 +15,7 @@ help:
1515
@echo 'make verify Compare ./docs with the RST source'
1616
@echo 'make build Build the site and its llms files into ./site, failing on warnings'
1717
@echo 'make serve Preview the site at http://127.0.0.1:8000'
18+
@echo 'make watch Preview the site, reconverting the RST source as it changes'
1819

1920
install: $(MKDOCS)
2021

@@ -36,3 +37,6 @@ build: $(MKDOCS)
3637

3738
serve: $(MKDOCS)
3839
$(MKDOCS) serve
40+
41+
watch: $(MKDOCS)
42+
php admin/watch.php & watcher=$$!; trap 'kill $$watcher' EXIT INT TERM; $(MKDOCS) serve

‎admin/watch.php‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @return array<string, int>
7+
*/
8+
function snapshot(string $sourceDir): array
9+
{
10+
$files = [];
11+
$iterator = new RecursiveIteratorIterator(
12+
new RecursiveDirectoryIterator($sourceDir, FilesystemIterator::SKIP_DOTS),
13+
);
14+
15+
/** @var SplFileInfo $file */
16+
foreach ($iterator as $file) {
17+
$path = substr($file->getPathname(), strlen($sourceDir));
18+
19+
if (
20+
str_starts_with($path, '_static/')
21+
|| str_starts_with($path, 'images/')
22+
|| str_ends_with($path, '.rst_')
23+
) {
24+
continue;
25+
}
26+
27+
$files[$path] = $file->getMTime();
28+
}
29+
30+
return $files;
31+
}
32+
33+
/**
34+
* The page that a changed source file belongs to: itself for an RST file,
35+
* the page named by the folder for a sample.
36+
*/
37+
function owning_page(string $path, string $sourceDir): ?string
38+
{
39+
if (str_ends_with($path, '.rst')) {
40+
return substr($path, 0, -4);
41+
}
42+
43+
for ($directory = dirname($path); $directory !== '.'; $directory = dirname($directory)) {
44+
if (is_file($sourceDir . $directory . '.rst')) {
45+
return $directory;
46+
}
47+
}
48+
49+
return null;
50+
}
51+
52+
function convert(string $page = ''): bool
53+
{
54+
$start = microtime(true);
55+
56+
exec(
57+
sprintf(
58+
'php %s %s 2>&1',
59+
escapeshellarg(__DIR__ . '/convert.php'),
60+
$page === '' ? '' : escapeshellarg($page),
61+
),
62+
$output,
63+
$exitCode,
64+
);
65+
66+
printf(
67+
"[%s] %s %s (%.1fs)\n",
68+
date('H:i:s'),
69+
$exitCode === 0 ? 'Converted' : 'Failed',
70+
$page === '' ? 'the whole guide' : $page,
71+
microtime(true) - $start,
72+
);
73+
74+
foreach ($output as $line) {
75+
if ($exitCode !== 0 || preg_match('/warning|not found/i', $line)) {
76+
echo ' ', $line, "\n";
77+
}
78+
}
79+
80+
return $exitCode === 0;
81+
}
82+
83+
function main(): void
84+
{
85+
$sourceDir = rtrim((string) getenv('DOCS_PROJECT_PATH'), '/') . '/';
86+
$wholeGuideThreshold = 15;
87+
88+
if (!is_dir($sourceDir)) {
89+
fwrite(STDERR, "Set DOCS_PROJECT_PATH to the user guide source.\n");
90+
exit(1);
91+
}
92+
93+
$known = snapshot($sourceDir);
94+
95+
printf("Watching %s\n", $sourceDir);
96+
97+
while (true) {
98+
usleep(500000);
99+
100+
if (snapshot($sourceDir) === $known) {
101+
continue;
102+
}
103+
104+
// An editor or `git stash pop` may still be writing files.
105+
usleep(300000);
106+
107+
$current = snapshot($sourceDir);
108+
$changed = array_keys(array_diff_assoc($current, $known) + array_diff_assoc($known, $current));
109+
$addedOrRemoved = array_keys(array_diff_key($current, $known) + array_diff_key($known, $current));
110+
$known = $current;
111+
112+
$pages = array_unique(array_filter(array_map(
113+
fn (string $path): ?string => owning_page($path, $sourceDir),
114+
$changed,
115+
)));
116+
$pageListChanged = array_filter(
117+
$addedOrRemoved,
118+
fn (string $path): bool => str_ends_with($path, '.rst'),
119+
) !== [];
120+
121+
if ($pageListChanged || count($pages) >= $wholeGuideThreshold) {
122+
convert();
123+
124+
continue;
125+
}
126+
127+
foreach ($pages as $page) {
128+
convert($page);
129+
}
130+
}
131+
}
132+
133+
main();

0 commit comments

Comments
 (0)