| 1 | <?php |
| 2 | require_once("folderNode.php"); |
| 3 | require_once __DIR__ . '/templateRenderer.php'; |
| 4 | require_once __DIR__ . '/templateCssLinker.php'; |
| 5 | require_once __DIR__ . '/search.php'; |
| 6 | |
| 7 | $renderer = new TemplateRenderer(__DIR__ . DIRECTORY_SEPARATOR . 'templates'); |
| 8 | |
| 9 | $q = isset($_GET['q']) ? (string) $_GET['q'] : ''; |
| 10 | $q = trim($q); |
| 11 | |
| 12 | $searchResults = []; |
| 13 | if ($q !== '') { |
| 14 | $searchResults = searchAllJson(__DIR__ . DIRECTORY_SEPARATOR . 'data', $q); |
| 15 | } else { |
| 16 | if (isset($_GET['path'])) { |
| 17 | $html = $renderer->renderFromJsonFile($_GET['path']); |
| 18 | } else { |
| 19 | $data = loadFolderTree(__DIR__ . DIRECTORY_SEPARATOR . 'data/chapters'); |
| 20 | |
| 21 | $chaptersOrdered = $data->jsonRecursive(); |
| 22 | usort($chaptersOrdered, fn($a, $b) => ($a->number ?? 0) <=> ($b->number ?? 0)); |
| 23 | |
| 24 | try { |
| 25 | $html = $renderer->renderAll($chaptersOrdered); |
| 26 | } catch (Throwable $e) { |
| 27 | $html = '<pre class="error">' . htmlspecialchars($e->getMessage()) . '</pre>'; |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | ?> |
| 32 | <!DOCTYPE html> |
| 33 | <html lang="en"> |
| 34 | |
| 35 | <head> |
| 36 | <meta charset="UTF-8"> |
| 37 | <title>66 Wiki</title> |
| 38 | <link rel="stylesheet" href="main.css"> |
| 39 | <?php echoTemplateCssLinks(__DIR__ . DIRECTORY_SEPARATOR . 'templates', 'templates'); ?> |
| 40 | </head> |
| 41 | |
| 42 | <body> |
| 43 | |
| 44 | <div class="topBar"> |
| 45 | <div class="topBarInner"> |
| 46 | <?php if ($q === '' && !isset($_GET['path'])): ?> |
| 47 | <div class="chapterPicker"> |
| 48 | <button class="menuButton" id="chapterMenuBtn" type="button" aria-expanded="false"> |
| 49 | Chapters ▾ |
| 50 | </button> |
| 51 | |
| 52 | <div class="chapterDropdown" id="chapterDropdown" role="menu" aria-label="Chapter list"> |
| 53 | <?php foreach ($chaptersOrdered as $ch): |
| 54 | $num = $ch->number ?? ''; |
| 55 | $title = $ch->title ?? ''; |
| 56 | $id = 'chapter-' . $num; |
| 57 | ?> |
| 58 | <button type="button" data-target="<?= htmlspecialchars($id, ENT_QUOTES) ?>"> |
| 59 | <?= htmlspecialchars("Chapter $num — $title", ENT_QUOTES) ?> |
| 60 | </button> |
| 61 | <?php endforeach; ?> |
| 62 | </div> |
| 63 | </div> |
| 64 | <?php endif; ?> |
| 65 | <form class="searchForm" method="get" action=""> |
| 66 | <input class="searchInput" type="search" name="q" value="<?= htmlspecialchars($q, ENT_QUOTES) ?>" |
| 67 | placeholder="Search rules, items, proficiencies…"> |
| 68 | <button class="searchSubmit" type="submit">Search</button> |
| 69 | </form> |
| 70 | |
| 71 | </div> |
| 72 | </div> |
| 73 | |
| 74 | <?php if ($q !== ''): ?> |
| 75 | <div class="page"> |
| 76 | <h2>Search results for “<?= htmlspecialchars($q, ENT_QUOTES) ?>”</h2> |
| 77 | |
| 78 | <?php if (!$searchResults): ?> |
| 79 | <p>No matches found.</p> |
| 80 | <?php else: ?> |
| 81 | <div> |
| 82 | <?php foreach ($searchResults as $r): ?> |
| 83 | <article class="searchResultArticle"> |
| 84 | <div class="searchResult"> |
| 85 | <strong><a |
| 86 | href="<?= "?path=" . DIRECTORY_SEPARATOR . "data" . htmlspecialchars($r['file'], ENT_QUOTES) ?>"><?= htmlspecialchars($r['name'] ?: '(no name)', ENT_QUOTES) ?></a></strong> |
| 87 | <small>type: <?= htmlspecialchars($r['type'], ENT_QUOTES) ?></small> |
| 88 | </div> |
| 89 | |
| 90 | <ul> |
| 91 | <?php foreach (array_slice($r['hits'], 0, 5) as $hit): ?> |
| 92 | <li> |
| 93 | <code><?= $hit['field'] ?></code>: |
| 94 | <?= $hit['snippet'] ?> |
| 95 | </li> |
| 96 | <?php endforeach; ?> |
| 97 | </ul> |
| 98 | </article> |
| 99 | <?php endforeach; ?> |
| 100 | </div> |
| 101 | <?php endif; ?> |
| 102 | </div> |
| 103 | <?php else: ?> |
| 104 | <?= $html ?> |
| 105 | <?php endif; ?> |
| 106 | |
| 107 | <script src="chapterScroll.js"></script> |
| 108 | </body> |
| 109 | |
| 110 | </html> |