-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_docs_indices.php
More file actions
516 lines (437 loc) · 17.4 KB
/
generate_docs_indices.php
File metadata and controls
516 lines (437 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<?php
declare(strict_types=1);
/**
* generate_docs_indices.php
*
* Generates/refreshes:
* 1) Root README.md (WIP + auto index grouped by top-level).
* 2) Per-directory README.md auto-index blocks (files + subfolders),
* updating only the section between <!-- AUTO-INDEX:BEGIN --> ... <!-- AUTO-INDEX:END -->.
*
* Behavior:
* - Scans recursively for *.md files.
* - Skips root README.md as a "content" page.
* - Excludes /.git/, /site/, /assets/ from indices (still allows README in parent dirs).
* - Creates README.md where missing, with a minimal header + auto-index block.
*
* PHP 8.2+, tabs, K&R braces. No external deps.
*/
const EXCLUDE_DIRS = ['/.git/', '/site/', '/assets/'];
const SUPPORT_EMAIL = 'support@citomni.com';
const AUTO_BEGIN = '<!-- AUTO-INDEX:BEGIN -->';
const AUTO_END = '<!-- AUTO-INDEX:END -->';
const SECTION_INFO_BEGIN = '<!-- SECTION-INFO:BEGIN -->';
const SECTION_INFO_END = '<!-- SECTION-INFO:END -->';
const README_DESCRIPTIONS = [
'concepts' => "Concept documents explain the architectural ideas, design principles, and structural decisions that shape CitOmni.\n\nThey describe how the framework is intended to work and why particular patterns, boundaries, or runtime rules exist.",
'contribute' => "Contribution documents describe the conventions, expectations, and practical workflows used when improving CitOmni.\n\nThey are intended for contributors who need clear rules for authoring, testing, reviewing, and maintaining framework code and documentation.",
'cookbook' => "Cookbook documents provide practical, example-driven solutions for common tasks in CitOmni.\n\nThey focus on applied patterns and ready-to-adapt recipes rather than full reference material or deep architectural discussion.",
'get-started' => "Get-started documents help new users become productive with CitOmni as quickly as possible.\n\nThey introduce the framework, its structure, and the minimum practical steps needed to begin building or running an application.",
'how-to' => "How-to documents explain how to perform specific tasks in CitOmni.\n\nThey are practical implementation guides focused on concrete outcomes such as building, configuring, integrating, or operating framework features.",
'legal' => "Legal documents cover legal, licensing, ownership, and related framework governance matters for CitOmni.\n\nThey define the formal legal context around the project rather than implementation details or usage guidance.",
'packages' => "Package documents describe CitOmni packages, their roles, boundaries, and intended responsibilities within the wider framework ecosystem.\n\nThey help readers understand how the framework is divided into installable units and how those units fit together.",
'policies' => "Policy documents define mandatory or strongly recommended rules for how CitOmni should be used, implemented, or operated in specific areas.\n\nThey are normative documents intended to reduce ambiguity and keep framework behavior consistent.",
'programs' => "Program documents describe bundled utilities, scripts, commands, or other executable framework-adjacent tools used within the CitOmni ecosystem.\n\nThey focus on what those programs do, when they should be used, and any relevant operational context.",
'reference' => "Reference documents define the formal behavior, contracts, configuration model, and runtime semantics of specific CitOmni components.\n\nThey are intended to be authoritative technical documentation for developers who need exact and stable information.",
'release-notes' => "Release-notes documents record notable changes between versions of CitOmni.\n\nThey provide a historical view of additions, changes, fixes, and other developments relevant to framework users and maintainers.",
'reports' => "Report documents capture findings, evaluations, measurements, or structured summaries related to CitOmni.\n\nThey typically document observed results or concluded analysis rather than prescribing framework rules.",
'research' => "Research documents are exploratory working papers used to investigate, compare, question, and discuss possible directions for CitOmni's continued development.\n\nThey are not automatically normative, canonical, or final framework documentation.",
'troubleshooting' => "Troubleshooting documents help diagnose, explain, and resolve problems encountered while installing, configuring, developing with, or operating CitOmni.\n\nThey focus on practical problem-solving, common failure modes, and corrective actions.",
];
function normPath(string $path): string {
$path = str_replace('\\', '/', $path);
$path = preg_replace('#/+#', '/', $path) ?? $path;
if (\strlen($path) > 3) {
$path = rtrim($path, '/');
}
return $path;
}
function relFromRoot(string $absPath, string $root): string {
$abs = normPath($absPath);
$root = rtrim(normPath($root), '/');
return str_starts_with($abs, $root . '/') ? substr($abs, \strlen($root) + 1) : $abs;
}
function isExcludedPath(string $absPath): bool {
$abs = normPath($absPath);
foreach (EXCLUDE_DIRS as $needle) {
if (str_contains($abs, $needle)) {
return true;
}
}
return false;
}
function listDirs(string $dir): array {
$out = [];
foreach (scandir($dir) ?: [] as $name) {
if ($name === '.' || $name === '..') {
continue;
}
$path = $dir . '/' . $name;
if (is_dir($path)) {
$out[] = $path;
}
}
natcasesort($out);
return array_values($out);
}
function listMarkdownFiles(string $dir): array {
$out = [];
foreach (scandir($dir) ?: [] as $name) {
if ($name === '.' || $name === '..') {
continue;
}
$path = $dir . '/' . $name;
if (is_file($path) && strcasecmp(pathinfo($path, PATHINFO_EXTENSION), 'md') === 0) {
$out[] = $path;
}
}
natcasesort($out);
return array_values($out);
}
function humanTitle(string $pathRelative): string {
// Use filename without extension; replace dashes/underscores
$base = basename($pathRelative);
$base = preg_replace('/\.md$/i', '', $base) ?? $base;
$base = str_replace(['-', '_'], ' ', $base);
$base = preg_replace('/\s+/', ' ', $base) ?? $base;
return ucfirst(trim($base));
}
function topLevelOf(string $relativePath): string {
$relativePath = ltrim($relativePath, '/');
$pos = strpos($relativePath, '/');
return $pos === false ? '(root)' : substr($relativePath, 0, $pos);
}
function replaceManagedBlock(string $existing, string $beginMarker, string $endMarker, string $newBlock): string {
$begin = preg_quote($beginMarker, '/');
$end = preg_quote($endMarker, '/');
$pattern = "/{$begin}.*?{$end}/s";
if (preg_match($pattern, $existing)) {
return preg_replace($pattern, $newBlock, $existing) ?? $existing;
}
return rtrim($existing) . "\n\n" . $newBlock . "\n";
}
function removeManagedBlock(string $existing, string $beginMarker, string $endMarker): string {
$begin = preg_quote($beginMarker, '/');
$end = preg_quote($endMarker, '/');
$pattern = "/\n*\s*{$begin}.*?{$end}\n*/s";
$updated = preg_replace($pattern, "\n\n", $existing);
if (!is_string($updated)) {
return $existing;
}
$updated = preg_replace("/\n{3,}/", "\n\n", $updated) ?? $updated;
return rtrim($updated) . "\n";
}
function replaceAutoIndexBlock(string $existing, string $newBlock): string {
return replaceManagedBlock($existing, AUTO_BEGIN, AUTO_END, $newBlock);
}
function getReadmeDescription(string $absDir, string $root): string {
$relDir = trim(relFromRoot($absDir, $root), '/');
return README_DESCRIPTIONS[$relDir] ?? '';
}
function buildSectionInfoBlock(string $absDir, string $root): string {
$description = trim(getReadmeDescription($absDir, $root));
if ($description === '') {
return '';
}
$lines = [];
$lines[] = SECTION_INFO_BEGIN;
$lines[] = '';
$lines[] = '## About this section';
$lines[] = '';
$lines[] = $description;
$lines[] = '';
$lines[] = SECTION_INFO_END;
return implode("\n", $lines);
}
function insertBlockBeforeAutoIndex(string $existing, string $block): string {
$autoBeginQuoted = preg_quote(AUTO_BEGIN, '/');
$pattern = "/\n*{$autoBeginQuoted}/";
if (preg_match($pattern, $existing, $matches, PREG_OFFSET_CAPTURE) === 1) {
$offset = $matches[0][1];
$before = rtrim(substr($existing, 0, $offset));
$after = ltrim(substr($existing, $offset), "\n");
return $before . "\n\n" . $block . "\n\n" . $after;
}
return rtrim($existing) . "\n\n" . $block . "\n";
}
function directoryHasContentToIndex(string $absDir, string $root): bool {
// There is indexable content if there is any *.md other than README.md or any subdir (non-excluded) with md files.
$md = listMarkdownFiles($absDir);
foreach ($md as $file) {
$rel = relFromRoot($file, $root);
if (strcasecmp(basename($file), 'README.md') !== 0 && !isExcludedPath($file)) {
return true;
}
}
foreach (listDirs($absDir) as $sub) {
if (isExcludedPath($sub)) {
continue;
}
// recursive peek
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sub, FilesystemIterator::SKIP_DOTS));
foreach ($rii as $f) {
/** @var SplFileInfo $f */
if ($f->isFile() && strcasecmp($f->getExtension(), 'md') === 0) {
if (strcasecmp($f->getFilename(), 'README.md') !== 0 && !isExcludedPath($f->getPathname())) {
return true;
}
}
}
}
return false;
}
/**
* Build the auto-index block for a given directory (files + subfolders).
*/
function buildAutoIndexBlock(string $absDir, string $root): string {
$relDir = relFromRoot($absDir, $root);
$relPrefix = $relDir === '' ? '' : $relDir . '/';
$lines = [];
$lines[] = AUTO_BEGIN;
$lines[] = '';
$lines[] = '## Index';
$lines[] = '';
// Files in this dir (exclude README.md)
foreach (listMarkdownFiles($absDir) as $file) {
if (strcasecmp(basename($file), 'README.md') === 0) {
continue;
}
if (isExcludedPath($file)) {
continue;
}
$rel = relFromRoot($file, $root);
$lines[] = '- [' . humanTitle($rel) . '](./' . basename($rel) . ')';
}
// Subfolders with at least one indexable md
$subDirs = listDirs($absDir);
foreach ($subDirs as $sub) {
if (isExcludedPath($sub)) {
continue;
}
if (!directoryHasContentToIndex($sub, $root)) {
continue;
}
$subRel = relFromRoot($sub, $root);
$lines[] = '- **' . basename($subRel) . '/** → [' . 'README' . '](./' . basename($subRel) . '/README.md)';
}
$lines[] = '';
$lines[] = AUTO_END;
return implode("\n", $lines);
}
function ensureDirectoryReadme(string $absDir, string $root): void {
$readme = rtrim($absDir, '/') . '/README.md';
$sectionInfo = buildSectionInfoBlock($absDir, $root);
$auto = buildAutoIndexBlock($absDir, $root);
if (!file_exists($readme)) {
$title = basename($absDir);
$parts = [];
$parts[] = '# ' . $title;
$parts[] = '';
$parts[] = '_Section landing page._';
$parts[] = '';
$parts[] = '- [Back to Docs Home](../README.md)';
$parts[] = '';
if ($sectionInfo !== '') {
$parts[] = $sectionInfo;
$parts[] = '';
}
$parts[] = $auto;
$parts[] = '';
file_put_contents($readme, implode("\n", $parts));
return;
}
$existing = file_get_contents($readme) ?: '';
$updated = $existing;
// First update/remove the managed section-info block.
if ($sectionInfo !== '') {
if (str_contains($updated, SECTION_INFO_BEGIN)) {
$updated = replaceManagedBlock($updated, SECTION_INFO_BEGIN, SECTION_INFO_END, $sectionInfo);
} else {
$updated = insertBlockBeforeAutoIndex($updated, $sectionInfo);
}
} else {
$updated = removeManagedBlock($updated, SECTION_INFO_BEGIN, SECTION_INFO_END);
}
// Then update the auto-index block.
$updated = replaceAutoIndexBlock($updated, $auto);
if ($updated !== $existing) {
file_put_contents($readme, $updated);
}
}
function generateRootReadme(string $root): void {
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS));
$groups = []; // top-level => list of rel paths
$count = 0;
foreach ($rii as $f) {
/** @var SplFileInfo $f */
if (!$f->isFile() || strcasecmp($f->getExtension(), 'md') !== 0) {
continue;
}
$abs = normPath($f->getPathname());
$rel = relFromRoot($abs, $root);
if (strcasecmp($rel, 'README.md') === 0 || strcasecmp($rel, 'CONVENTIONS.md') === 0 || strcasecmp($rel, 'TRADEMARKS.md') === 0) {
continue;
}
if (isExcludedPath($abs)) {
continue;
}
$top = topLevelOf($rel);
$groups[$top] ??= [];
$groups[$top][] = $rel;
$count++;
}
/**
* Move the section's own README.md to the first position (if present).
* Example: "concepts/README.md" goes first under group "concepts".
*/
function promoteSectionReadme(string $group, array $list): array {
// Root group never shows root README.md (already handled elsewhere).
if ($group === '(root)') {
return array_values($list);
}
$want = $group . '/README.md';
$head = [];
$tail = [];
foreach ($list as $rel) {
if (strcasecmp($rel, $want) === 0) {
$head[] = $rel;
} else {
$tail[] = $rel;
}
}
return array_values(array_merge($head, $tail));
}
// Sort
uksort($groups, static fn(string $a, string $b): int => strnatcasecmp($a, $b));
foreach ($groups as $g => &$list) {
natcasesort($list);
$list = promoteSectionReadme($g, array_values($list));
}
unset($list);
$now = date('Y-m-d H:i:s');
$lines = [];
$lines[] = '# CitOmni Documentation (Work in Progress)';
$lines[] = '';
$lines[] = 'This repository hosts the public documentation for the CitOmni Framework.';
$lines[] = '';
$lines[] = '> **Heads up:** The documentation is a work in progress. If you\'re missing anything specific, please reach out at **' . SUPPORT_EMAIL . '**.';
$lines[] = '';
$lines[] = '## Available docs (auto-generated)';
$lines[] = '';
if ($count === 0) {
$lines[] = '*(No individual pages detected yet. Check back soon.)*';
$lines[] = '';
} else {
foreach ($groups as $group => $list) {
$lines[] = '### ' . $group;
foreach ($list as $rel) {
$lines[] = '- [' . $rel . '](./' . $rel . ')';
}
$lines[] = '';
}
}
// Expected structure (same as earlier)
$lines[] = '## Expected structure (reference)';
$lines[] = '';
$lines[] = 'Below is the planned layout. Some sections may be empty while we migrate content.';
$lines[] = '';
$lines[] = '```';
$lines[] = '/README.md';
$lines[] = '/get-started/';
$lines[] = ' quickstart-http.md';
$lines[] = ' quickstart-cli.md';
$lines[] = ' skeletons.md';
$lines[] = '/concepts/';
$lines[] = ' runtime-modes.md';
$lines[] = ' config-merge.md # deterministic "last-wins"';
$lines[] = ' services-and-providers.md';
$lines[] = ' caching-and-warmup.md';
$lines[] = ' error-handling.md';
$lines[] = '/how-to/';
$lines[] = ' build-a-provider.md';
$lines[] = ' routes-and-controllers.md';
$lines[] = ' csrf-cookies-sessions.md';
$lines[] = ' perf-budgets-and-metrics.md # p95 TTFB, RSS, req/watt-notes';
$lines[] = ' deployment-one.com.md';
$lines[] = '/troubleshooting/';
$lines[] = ' providers-autoload.md';
$lines[] = ' routes-404.md';
$lines[] = ' csrf-token-mismatch.md';
$lines[] = ' caching-warmup.md';
$lines[] = ' composer-autoload.md';
$lines[] = ' http-500-during-boot.md';
$lines[] = '/policies/';
$lines[] = ' security.md';
$lines[] = ' public-uploads.md';
$lines[] = ' cache-and-retention.md';
$lines[] = ' backups.md';
$lines[] = ' data-protection.md';
$lines[] = ' error-pages.md';
$lines[] = ' maintenance.md';
$lines[] = '/reference/';
$lines[] = ' config-keys.md # complete key list with examples';
$lines[] = ' cli-commands.md';
$lines[] = ' http-objects.md # Request/Response/Cookie';
$lines[] = ' router-reference.md';
$lines[] = '/packages/';
$lines[] = ' kernel.md';
$lines[] = ' http.md';
$lines[] = ' cli.md';
$lines[] = ' auth.md';
$lines[] = ' testing.md';
$lines[] = '/cookbook/';
$lines[] = ' cms-starter.md';
$lines[] = ' commerce-notes.md';
$lines[] = ' admin-starter.md';
$lines[] = '/contribute/';
$lines[] = ' conventions.md # coding/documentation conventions';
$lines[] = ' docs-style-guide.md # tone, examples, code blocks';
$lines[] = ' issue-templates.md';
$lines[] = '/legal/';
$lines[] = ' licenses.md # MIT/GPL explanations, SPDX';
$lines[] = ' trademark.md # TM usage, "official", "certified"';
$lines[] = ' notice-template.md';
$lines[] = '/programs/';
$lines[] = ' certification.md # criteria, green KPIs';
$lines[] = ' partners.md # partner program guidelines';
$lines[] = '/release-notes/';
$lines[] = ' index.md # links to package CHANGELOGs';
$lines[] = '/assets/';
$lines[] = ' img/ css/ js/';
$lines[] = '```';
$lines[] = '';
$lines[] = '---';
$lines[] = '_This README was generated automatically._ ';
$lines[] = '_Last updated: ' . $now . '_';
$lines[] = '';
file_put_contents($root . '/README.md', implode("\n", $lines));
}
function main(): void {
$root = normPath(getcwd() ?: '.');
// 1) Generate/refresh root README
generateRootReadme($root);
// 2) Generate/refresh per-directory README auto blocks (skip root; it's handled above)
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS));
$seenDirs = [];
foreach ($rii as $f) {
/** @var SplFileInfo $f */
$dir = normPath($f->getPath());
// Skip root directory; root README is generated by generateRootReadme()
if ($dir === $root) {
continue;
}
// Process each directory once
if (isset($seenDirs[$dir])) {
continue;
}
$seenDirs[$dir] = true;
// Exclude certain directories entirely
if (isExcludedPath($dir)) {
continue;
}
// Ensure README & auto-index block for this directory
ensureDirectoryReadme($dir, $root);
}
echo "Done. Root README and per-directory indices updated.\n";
}
main();