BlueGit

timfa 66wiki

Files

66wiki / web / index.php

index.php

Add jump to links
timfa ruud · pushed by timfa · 11 months ago
e43828c5 History
index.php 4.6 KiB Download Raw
1<?php
2require_once("folderNode.php");
3require_once __DIR__ . '/templateRenderer.php';
4require_once __DIR__ . '/templateCssLinker.php';
5require_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 = [];
13if ($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 <link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
40 <link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
41 <link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
42 <link rel="manifest" href="site.webmanifest">
43 <?php echoTemplateCssLinks(__DIR__ . DIRECTORY_SEPARATOR . 'templates', 'templates'); ?>
44</head>
45
46<body>
47
48 <div class="topBar">
49 <div class="topBarInner">
50 <a href="."><img class="headerImage" src="images/66logo.png" /></a>
51 <?php if ($q === '' && !isset($_GET['path'])): ?>
52 <div class="chapterPicker">
53 <button class="menuButton" id="chapterMenuBtn" type="button" aria-expanded="false">
54 Chapters ▾
55 </button>
56
57 <div class="chapterDropdown" id="chapterDropdown" role="menu" aria-label="Chapter list">
58 <?php foreach ($chaptersOrdered as $ch):
59 $num = $ch->number ?? '';
60 $title = $ch->title ?? '';
61 $id = 'chapter-' . $num;
62 ?>
63 <button type="button" data-target="<?= htmlspecialchars($id, ENT_QUOTES) ?>">
64 <?= htmlspecialchars("Chapter $num — $title", ENT_QUOTES) ?>
65 </button>
66 <?php endforeach; ?>
67 </div>
68 </div>
69 <?php endif; ?>
70 <form class="searchForm" method="get" action="">
71 <input class="searchInput" type="search" name="q" value="<?= htmlspecialchars($q, ENT_QUOTES) ?>"
72 placeholder="Search rules, items, proficiencies…">
73 <button class="searchSubmit" type="submit">Search</button>
74 </form>
75
76 </div>
77 </div>
78
79 <?php if ($q !== ''): ?>
80 <div class="page">
81 <h2>Search results for “<?= htmlspecialchars($q, ENT_QUOTES) ?>”</h2>
82
83 <?php if (!$searchResults): ?>
84 <p>No matches found.</p>
85 <?php else: ?>
86 <div>
87 <?php foreach ($searchResults as $r): ?>
88 <article class="searchResultArticle">
89 <div class="searchResult">
90 <strong><a
91 href="<?= "?path=" . DIRECTORY_SEPARATOR . "data" . htmlspecialchars($r['file'], ENT_QUOTES) ?>"><?= htmlspecialchars($r['name'] ?: '(no name)', ENT_QUOTES) ?></a></strong>
92 <small>type: <?= htmlspecialchars($r['type'], ENT_QUOTES) ?></small>
93 </div>
94
95 <ul>
96 <?php foreach (array_slice($r['hits'], 0, 5) as $hit): ?>
97 <li>
98 <code><?= $hit['field'] ?></code>:
99 <?= $hit['snippet'] ?>
100 </li>
101 <?php endforeach; ?>
102 </ul>
103 </article>
104 <?php endforeach; ?>
105 </div>
106 <?php endif; ?>
107 </div>
108 <?php else: ?>
109 <content>
110 <?= $html ?>
111 </content>
112 <?php endif; ?>
113
114 <script src="chapterScroll.js"></script>
115 <script src="inlineJump.js"></script>
116</body>
117
118</html>