diff --git a/web/docs/index.php b/web/docs/index.php index ddf7be0..ef87d67 100644 --- a/web/docs/index.php +++ b/web/docs/index.php @@ -1,44 +1,70 @@ (defaults to "index"). */ +/* PMM docs site — recursive collapsible file tree (left) + rendered md (right). + * Scans md/ recursively; folders →
, .md → file links. + * URL: docs/index.php?page= (defaults to "index"). */ define('PMM_SITE', 1); require dirname(__DIR__) . '/_common.php'; $mdDir = __DIR__ . '/md'; +$page = isset($_GET['page']) ? (string)$_GET['page'] : 'index'; -/* Discover every .md file; title = first "# " line, else the filename. */ -$pages = []; // slug -> title -if (is_dir($mdDir)) { - foreach (glob($mdDir . '/*.md') as $f) { - $slug = basename($f, '.md'); - $title = $slug; - $raw = (string)file_get_contents($f); - if (preg_match('/^#\s+(.+)$/m', $raw, $m)) $title = trim($m[1]); - $pages[$slug] = $title; +/* Recursively render the tree under `dir` (relative to mdDir). */ +function render_tree(string $dir, string $rel): string { + $html = ''; + $items = scandir($dir); + if (!$items) return ''; + $folders = []; $files = []; + foreach ($items as $it) { + if ($it === '.' || $it === '..') continue; + $full = $dir . '/' . $it; + if (is_dir($full)) $folders[] = $it; + elseif (substr($it, -3) === '.md') $files[] = $it; } + sort($folders); sort($files); + foreach ($folders as $f) { + $childRel = $rel === '' ? $f : $rel . '/' . $f; + $html .= '
📁 ' . + htmlspecialchars($f) . '
' . + render_tree($dir . '/' . $f, $childRel) . '
'; + } + foreach ($files as $fname) { + $slug = $rel === '' ? basename($fname, '.md') : $rel . '/' . basename($fname, '.md'); + $title = basename($fname, '.md'); + $raw = (string)@file_get_contents($dir . '/' . $fname); + if (preg_match('/^#\s+(.+)$/m', $raw, $m)) $title = trim($m[1]); + $active = ($slug === $GLOBALS['page']) ? ' active' : ''; + $html .= '📄 ' . htmlspecialchars($title) . ''; + } + return $html; } -$page = isset($_GET['page']) ? (string)$_GET['page'] : 'index'; -if (!isset($pages[$page])) $page = 'index'; -$title = $pages[$page]; - $body = ''; $f = $mdDir . '/' . $page . '.md'; if (is_file($f)) { - $body = (string)file_get_contents($f); - $body = str_replace("\r", "", $body); // md() regex hard-codes \n - $body = md($body); + $raw = (string)file_get_contents($f); + $raw = str_replace("\r", "", $raw); + $body = md($raw); } +// crumb title = file's first heading or slug +$title = $page; +if ($body !== '' && preg_match('/^#\s+(.+)$/m', str_replace('<', '', $body), $m)) $title = trim(strip_tags($m[1])); pmm_header('docs', '文档 · ' . $title); ?>