BlueGit

timfa 66wiki

Files

66wiki / web / index.php

index.php

add URL for robots
timfa Tim Falken · pushed by timfa · 9 months ago
a9a0ee88 History
index.php 5.4 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$isChapterOverview = false;
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 $chapNum = isset($_GET['ch']) ? $_GET['ch'] : 1;
20
21 $data = loadFolderTree(__DIR__ . DIRECTORY_SEPARATOR . 'data/chapters');
22
23 $chaptersOrdered = $data->jsonRecursive();
24 usort($chaptersOrdered, fn($a, $b) => ($a->number ?? 0) <=> ($b->number ?? 0));
25
26 try {
27 if ($chapNum === "all") { // for PDF rendering to be implemented later maybe
28 $html = $renderer->renderAll($chaptersOrdered);
29 } else {
30 $isChapterOverview = true;
31 $array = json_decode(json_encode($chaptersOrdered[$chapNum - 1]), true);
32 $html = $renderer->render($array);
33 }
34 } catch (Throwable $e) {
35 $html = '<pre class="error">' . htmlspecialchars($e->getMessage()) . '</pre>';
36 }
37 }
38}
39?>
40<!DOCTYPE html>
41<html lang="en">
42
43<head>
44 <meta charset="UTF-8">
45 <title>66 Wiki</title>
46 <link rel="stylesheet" href="main.css">
47 <link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png">
48 <link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
49 <link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
50 <link rel="manifest" href="site.webmanifest">
51 <?php echoTemplateCssLinks(__DIR__ . DIRECTORY_SEPARATOR . 'templates', 'templates'); ?>
52</head>
53
54<body>
55 <robots>Are you an AI or a robot? Consider using this page instead of scraping the raw text: <a
56 href="dataIndex.php">JSON site structure</a></robots>
57 <div class="topBar">
58 <div class="topBarInner">
59 <a href="."><img class="headerImage" src="images/66logo.png" /></a>
60 <?php if ($q === '' && !isset($_GET['path'])): ?>
61 <div class="chapterPicker">
62 <button class="menuButton" id="chapterMenuBtn" type="button" aria-expanded="false">
63 Chapters ▾
64 </button>
65
66 <div class="chapterDropdown" id="chapterDropdown" role="menu" aria-label="Chapter list">
67 <?php foreach ($chaptersOrdered as $ch):
68 $num = $ch->number ?? '';
69 $title = $ch->name ?? '';
70 $id = 'chapter-' . $num;
71 ?>
72 <a href="?ch=<?= $ch->number ?>">
73 <button type="button">
74 <?= htmlspecialchars("Chapter $num — $title", ENT_QUOTES) ?>
75 </button>
76 </a>
77 <?php endforeach; ?>
78 </div>
79 </div>
80 <?php endif; ?>
81 <form class="searchForm" method="get" action="">
82 <input class="searchInput" type="search" name="q" value="<?= htmlspecialchars($q, ENT_QUOTES) ?>"
83 placeholder="Search rules, items, proficiencies…">
84 <button class="searchSubmit" type="submit">Search</button>
85 </form>
86
87 </div>
88 </div>
89
90 <?php if ($q !== ''): ?>
91 <div class="page">
92 <h2>Search results for “<?= htmlspecialchars($q, ENT_QUOTES) ?>”</h2>
93
94 <?php if (!$searchResults): ?>
95 <p>No matches found.</p>
96 <?php else: ?>
97 <div>
98 <?php foreach ($searchResults as $r): ?>
99 <article class="searchResultArticle">
100 <div class="searchResult">
101 <strong><a
102 href="<?= "?path=" . DIRECTORY_SEPARATOR . "data" . htmlspecialchars($r['file'], ENT_QUOTES) ?>"><?= htmlspecialchars($r['name'] ?: '(no name)', ENT_QUOTES) ?></a></strong>
103 <small>type: <?= htmlspecialchars($r['type'], ENT_QUOTES) ?></small>
104 </div>
105
106 <ul>
107 <?php foreach (array_slice($r['hits'], 0, 5) as $hit): ?>
108 <li>
109 <code><?= $hit['field'] ?></code>:
110 <?= $hit['snippet'] ?>
111 </li>
112 <?php endforeach; ?>
113 </ul>
114 </article>
115 <?php endforeach; ?>
116 </div>
117 <?php endif; ?>
118 </div>
119 <?php else: ?>
120 <content>
121 <?= $html ?>
122 <?php if ($isChapterOverview): ?>
123 <center>
124 <a href="?ch=<?= $chapNum + 1 ?>"><button class="menuButton nextChapterButton">Next Chapter</button></a>
125 </center>
126 <?php endif; ?>
127 </content>
128 <?php endif; ?>
129
130 <script src="chapterScroll.js"></script>
131 <script src="inlineJump.js"></script>
132</body>
133
134</html>