BlueGit

timfa 66wiki

simple search and chapter list

timfa Tim · pushed by timfa

Authored 2025-12-20 · 11 months ago

Hash
c4b8fa75f92b9edcdd5c6ed8681f3b41451567c6
Parent
e7fc3b65
5 files changed +353 −12
web/chapterScroll.js added +39 −0
@@ -0,0 +1,39 @@
1 (function ()
2 {
3 const btn = document.getElementById('chapterMenuBtn');
4 const dropdown = document.getElementById('chapterDropdown');
5
6 function closeDropdown ()
7 {
8 dropdown.classList.remove('open');
9 btn.setAttribute('aria-expanded', 'false');
10 }
11
12 btn.addEventListener('click', () =>
13 {
14 const isOpen = dropdown.classList.toggle('open');
15 btn.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
16 });
17
18 // Click outside closes it
19 document.addEventListener('click', (e) =>
20 {
21 if (!e.target.closest('.chapterPicker')) closeDropdown();
22 });
23
24 // Smooth scroll when selecting a chapter
25 dropdown.addEventListener('click', (e) =>
26 {
27 const target = e.target.closest('button[data-target]');
28 if (!target) return;
29
30 const id = target.getAttribute('data-target');
31 const el = document.getElementById(id);
32 if (el)
33 {
34 el.scrollIntoView({ behavior: 'smooth', block: 'start' });
35 closeDropdown();
36 history.replaceState(null, '', '#' + id); // optional: update URL fragment
37 }
38 });
39 })();
@@ -0,0 +1,39 @@
1(function ()
2{
3 const btn = document.getElementById('chapterMenuBtn');
4 const dropdown = document.getElementById('chapterDropdown');
5
6 function closeDropdown ()
7 {
8 dropdown.classList.remove('open');
9 btn.setAttribute('aria-expanded', 'false');
10 }
11
12 btn.addEventListener('click', () =>
13 {
14 const isOpen = dropdown.classList.toggle('open');
15 btn.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
16 });
17
18 // Click outside closes it
19 document.addEventListener('click', (e) =>
20 {
21 if (!e.target.closest('.chapterPicker')) closeDropdown();
22 });
23
24 // Smooth scroll when selecting a chapter
25 dropdown.addEventListener('click', (e) =>
26 {
27 const target = e.target.closest('button[data-target]');
28 if (!target) return;
29
30 const id = target.getAttribute('data-target');
31 const el = document.getElementById(id);
32 if (el)
33 {
34 el.scrollIntoView({ behavior: 'smooth', block: 'start' });
35 closeDropdown();
36 history.replaceState(null, '', '#' + id); // optional: update URL fragment
37 }
38 });
39})();
web/index.php +83 −11
@@ -2,21 +2,30 @@
2 2 require_once("folderNode.php");
3 3 require_once __DIR__ . '/templateRenderer.php';
4 4 require_once __DIR__ . '/templateCssLinker.php';
5 require_once __DIR__ . '/search.php';
5 6
6 $renderer = new TemplateRenderer(__DIR__ . DIRECTORY_SEPARATOR . '/templates');
7 $renderer = new TemplateRenderer(__DIR__ . DIRECTORY_SEPARATOR . 'templates');
7 8
8 if (isset($_GET['path'])) {
9 $html = $renderer->renderFromJsonFile($_GET['path']);
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);
10 15 } else {
11 $data = loadFolderTree(__DIR__ . DIRECTORY_SEPARATOR . 'data/chapters');
16 if (isset($_GET['path'])) {
17 $html = $renderer->renderFromJsonFile($_GET['path']);
18 } else {
19 $data = loadFolderTree(__DIR__ . DIRECTORY_SEPARATOR . 'data/chapters');
12 20
13 $chaptersOrdered = $data->jsonRecursive();
14 usort($chaptersOrdered, fn($a, $b) => ($a->number ?? 0) <=> ($b->number ?? 0));
21 $chaptersOrdered = $data->jsonRecursive();
22 usort($chaptersOrdered, fn($a, $b) => ($a->number ?? 0) <=> ($b->number ?? 0));
15 23
16 try {
17 $html = $renderer->renderAll($chaptersOrdered);
18 } catch (Throwable $e) {
19 $html = '<pre class="error">' . htmlspecialchars($e->getMessage()) . '</pre>';
24 try {
25 $html = $renderer->renderAll($chaptersOrdered);
26 } catch (Throwable $e) {
27 $html = '<pre class="error">' . htmlspecialchars($e->getMessage()) . '</pre>';
28 }
20 29 }
21 30 }
22 31 ?>
@@ -32,8 +41,71 @@ if (isset($_GET['path'])) {
32 41
33 42 <body>
34 43
35 <?= $html ?>
44 <div class="topBar">
45 <div class="topBarInner">
46
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
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
84 style="border:1px solid #ddd; border-radius:12px; padding:1rem; margin:0.75rem 0; background:#fff;">
85 <div style="display:flex; gap:0.75rem; flex-wrap:wrap; align-items:baseline;">
86 <strong><a
87 href="<?= "?path=" . DIRECTORY_SEPARATOR . "data" . htmlspecialchars($r['file'], ENT_QUOTES) ?>"><?= htmlspecialchars($r['name'] ?: '(no name)', ENT_QUOTES) ?></a></strong>
88 <small>type: <?= htmlspecialchars($r['type'], ENT_QUOTES) ?></small>
89 </div>
90
91 <ul>
92 <?php foreach (array_slice($r['hits'], 0, 5) as $hit): ?>
93 <li>
94 <code><?= htmlspecialchars($hit['field'], ENT_QUOTES) ?></code>:
95 <?= htmlspecialchars($hit['snippet'], ENT_QUOTES) ?>
96 </li>
97 <?php endforeach; ?>
98 </ul>
99 </article>
100 <?php endforeach; ?>
101 </div>
102 <?php endif; ?>
103 </div>
104 <?php else: ?>
105 <?= $html ?>
106 <?php endif; ?>
36 107
108 <script src="chapterScroll.js"></script>
37 109 </body>
38 110
39 111 </html>
@@ -2,21 +2,30 @@
2require_once("folderNode.php");
2require_once("folderNode.php");
3require_once __DIR__ . '/templateRenderer.php';
3require_once __DIR__ . '/templateRenderer.php';
4require_once __DIR__ . '/templateCssLinker.php';
4require_once __DIR__ . '/templateCssLinker.php';
5require_once __DIR__ . '/search.php';
5
6
6$renderer = new TemplateRenderer(__DIR__ . DIRECTORY_SEPARATOR . '/templates');
7$renderer = new TemplateRenderer(__DIR__ . DIRECTORY_SEPARATOR . 'templates');
7
8
8if (isset($_GET['path'])) {
9$q = isset($_GET['q']) ? (string) $_GET['q'] : '';
9 $html = $renderer->renderFromJsonFile($_GET['path']);
10$q = trim($q);
11
12$searchResults = [];
13if ($q !== '') {
14 $searchResults = searchAllJson(__DIR__ . DIRECTORY_SEPARATOR . 'data', $q);
10} else {
15} else {
11 $data = loadFolderTree(__DIR__ . DIRECTORY_SEPARATOR . 'data/chapters');
16 if (isset($_GET['path'])) {
17 $html = $renderer->renderFromJsonFile($_GET['path']);
18 } else {
19 $data = loadFolderTree(__DIR__ . DIRECTORY_SEPARATOR . 'data/chapters');
12
20
13 $chaptersOrdered = $data->jsonRecursive();
21 $chaptersOrdered = $data->jsonRecursive();
14 usort($chaptersOrdered, fn($a, $b) => ($a->number ?? 0) <=> ($b->number ?? 0));
22 usort($chaptersOrdered, fn($a, $b) => ($a->number ?? 0) <=> ($b->number ?? 0));
15
23
16 try {
24 try {
17 $html = $renderer->renderAll($chaptersOrdered);
25 $html = $renderer->renderAll($chaptersOrdered);
18 } catch (Throwable $e) {
26 } catch (Throwable $e) {
19 $html = '<pre class="error">' . htmlspecialchars($e->getMessage()) . '</pre>';
27 $html = '<pre class="error">' . htmlspecialchars($e->getMessage()) . '</pre>';
28 }
20 }
29 }
21}
30}
22?>
31?>
@@ -32,8 +41,71 @@ if (isset($_GET['path'])) {
32
41
33<body>
42<body>
34
43
35 <?= $html ?>
44 <div class="topBar">
45 <div class="topBarInner">
46
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
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
84 style="border:1px solid #ddd; border-radius:12px; padding:1rem; margin:0.75rem 0; background:#fff;">
85 <div style="display:flex; gap:0.75rem; flex-wrap:wrap; align-items:baseline;">
86 <strong><a
87 href="<?= "?path=" . DIRECTORY_SEPARATOR . "data" . htmlspecialchars($r['file'], ENT_QUOTES) ?>"><?= htmlspecialchars($r['name'] ?: '(no name)', ENT_QUOTES) ?></a></strong>
88 <small>type: <?= htmlspecialchars($r['type'], ENT_QUOTES) ?></small>
89 </div>
90
91 <ul>
92 <?php foreach (array_slice($r['hits'], 0, 5) as $hit): ?>
93 <li>
94 <code><?= htmlspecialchars($hit['field'], ENT_QUOTES) ?></code>:
95 <?= htmlspecialchars($hit['snippet'], ENT_QUOTES) ?>
96 </li>
97 <?php endforeach; ?>
98 </ul>
99 </article>
100 <?php endforeach; ?>
101 </div>
102 <?php endif; ?>
103 </div>
104 <?php else: ?>
105 <?= $html ?>
106 <?php endif; ?>
36
107
108 <script src="chapterScroll.js"></script>
37</body>
109</body>
38
110
39</html>
111</html>
web/main.css +97 −0
@@ -130,4 +130,101 @@ article {
130 130 pre {
131 131 background: #26262a;
132 132 }
133 }
134
135 .topBar {
136 position: sticky;
137 top: 0;
138 z-index: 999;
139 background: #ffffff;
140 border-bottom: 1px solid #ddd;
141 }
142
143 .topBarInner {
144 max-width: 1200px;
145 margin: 0 auto;
146 padding: 0.6rem 1rem;
147 display: flex;
148 align-items: center;
149 gap: 0.75rem;
150 }
151
152 .menuButton {
153 border: 1px solid #bbb;
154 background: #fff;
155 border-radius: 10px;
156 padding: 0.45rem 0.7rem;
157 cursor: pointer;
158 }
159
160 .menuButton:hover {
161 background: #f2f2f2;
162 }
163
164 .chapterPicker {
165 position: relative;
166 }
167
168 .chapterDropdown {
169 display: none;
170 position: absolute;
171 top: calc(100% + 0.4rem);
172 left: 0;
173 width: min(360px, 85vw);
174 max-height: 55vh;
175 overflow: auto;
176 border: 1px solid #ddd;
177 border-radius: 12px;
178 background: #fff;
179 box-shadow: 0 10px 25px rgba(0, 0, 0, 0.08);
180 padding: 0.35rem;
181 }
182
183 .chapterDropdown.open {
184 display: block;
185 }
186
187 .chapterDropdown button {
188 width: 100%;
189 text-align: left;
190 border: 0;
191 background: transparent;
192 padding: 0.55rem 0.6rem;
193 border-radius: 10px;
194 cursor: pointer;
195 }
196
197 .chapterDropdown button:hover {
198 background: #f3f3f3;
199 }
200
201 .searchForm {
202 margin-left: auto;
203 display: flex;
204 gap: 0.5rem;
205 align-items: center;
206 }
207
208 .searchInput {
209 width: min(420px, 45vw);
210 border: 1px solid #bbb;
211 border-radius: 999px;
212 padding: 0.45rem 0.75rem;
213 }
214
215 .searchSubmit {
216 border: 1px solid #bbb;
217 background: #fff;
218 border-radius: 999px;
219 padding: 0.45rem 0.75rem;
220 cursor: pointer;
221 }
222
223 .searchSubmit:hover {
224 background: #f2f2f2;
225 }
226
227 /* Make sure content doesn't hide behind sticky bar when using anchor links */
228 :target {
229 scroll-margin-top: 80px;
133 230 }
@@ -130,4 +130,101 @@ article {
130 pre {
130 pre {
131 background: #26262a;
131 background: #26262a;
132 }
132 }
133}
134
135.topBar {
136 position: sticky;
137 top: 0;
138 z-index: 999;
139 background: #ffffff;
140 border-bottom: 1px solid #ddd;
141}
142
143.topBarInner {
144 max-width: 1200px;
145 margin: 0 auto;
146 padding: 0.6rem 1rem;
147 display: flex;
148 align-items: center;
149 gap: 0.75rem;
150}
151
152.menuButton {
153 border: 1px solid #bbb;
154 background: #fff;
155 border-radius: 10px;
156 padding: 0.45rem 0.7rem;
157 cursor: pointer;
158}
159
160.menuButton:hover {
161 background: #f2f2f2;
162}
163
164.chapterPicker {
165 position: relative;
166}
167
168.chapterDropdown {
169 display: none;
170 position: absolute;
171 top: calc(100% + 0.4rem);
172 left: 0;
173 width: min(360px, 85vw);
174 max-height: 55vh;
175 overflow: auto;
176 border: 1px solid #ddd;
177 border-radius: 12px;
178 background: #fff;
179 box-shadow: 0 10px 25px rgba(0, 0, 0, 0.08);
180 padding: 0.35rem;
181}
182
183.chapterDropdown.open {
184 display: block;
185}
186
187.chapterDropdown button {
188 width: 100%;
189 text-align: left;
190 border: 0;
191 background: transparent;
192 padding: 0.55rem 0.6rem;
193 border-radius: 10px;
194 cursor: pointer;
195}
196
197.chapterDropdown button:hover {
198 background: #f3f3f3;
199}
200
201.searchForm {
202 margin-left: auto;
203 display: flex;
204 gap: 0.5rem;
205 align-items: center;
206}
207
208.searchInput {
209 width: min(420px, 45vw);
210 border: 1px solid #bbb;
211 border-radius: 999px;
212 padding: 0.45rem 0.75rem;
213}
214
215.searchSubmit {
216 border: 1px solid #bbb;
217 background: #fff;
218 border-radius: 999px;
219 padding: 0.45rem 0.75rem;
220 cursor: pointer;
221}
222
223.searchSubmit:hover {
224 background: #f2f2f2;
225}
226
227/* Make sure content doesn't hide behind sticky bar when using anchor links */
228:target {
229 scroll-margin-top: 80px;
133}
230}
web/search.php added +133 −0
@@ -0,0 +1,133 @@
1 <?php
2 function flattenValues(mixed $value, string $prefix = ''): array
3 {
4 $out = [];
5
6 if (is_array($value)) {
7 foreach ($value as $k => $v) {
8 $key = $prefix === '' ? (string) $k : $prefix . '.' . $k;
9 $out = array_merge($out, flattenValues($v, $key));
10 }
11 return $out;
12 }
13
14 if (is_object($value)) {
15 foreach (get_object_vars($value) as $k => $v) {
16 $key = $prefix === '' ? (string) $k : $prefix . '.' . $k;
17 $out = array_merge($out, flattenValues($v, $key));
18 }
19 return $out;
20 }
21
22 // Scalars: string/int/float/bool/null
23 $str = '';
24 if (is_string($value))
25 $str = $value;
26 elseif (is_int($value) || is_float($value))
27 $str = (string) $value;
28 elseif (is_bool($value))
29 $str = $value ? 'true' : 'false';
30 else
31 $str = '';
32
33 $out[] = ['path' => $prefix, 'value' => $str];
34 return $out;
35 }
36
37 function searchAllJson(string $dataRoot, string $query): array
38 {
39 $query = trim($query);
40 if ($query === '')
41 return [];
42
43 $q = mb_strtolower($query);
44 $results = [];
45
46 $it = new RecursiveIteratorIterator(
47 new RecursiveDirectoryIterator($dataRoot, FilesystemIterator::SKIP_DOTS)
48 );
49
50 foreach ($it as $file) {
51 /** @var SplFileInfo $file */
52 if (strtolower($file->getExtension()) !== 'json')
53 continue;
54
55 $path = $file->getPathname();
56 $rel = str_replace('\\', '/', substr($path, strlen($dataRoot)));
57 if ($rel === '')
58 $rel = '/' . $file->getFilename();
59
60 try {
61 $json = file_get_contents($path);
62 $obj = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
63 } catch (Throwable $e) {
64 // Skip invalid JSON (or you can report it)
65 continue;
66 }
67
68 $flat = flattenValues($obj);
69 $hits = [];
70
71 foreach ($flat as $entry) {
72 $val = $entry['value'] ?? '';
73 if ($val === '')
74 continue;
75
76 if (mb_stripos($val, $query) !== false) {
77 $hits[] = [
78 'field' => $entry['path'],
79 'snippet' => makeSnippet($val, $query, 120),
80 ];
81 }
82 }
83
84 if ($hits) {
85 $results[] = [
86 'file' => $rel,
87 'type' => $obj['type'] ?? '',
88 'name' => $obj['name'] ?? ($obj['title'] ?? ''),
89 'hits' => $hits,
90 ];
91 }
92 }
93
94 return $results;
95 }
96
97 function makeSnippet(string $text, string $query, int $maxLen = 120): string
98 {
99 if ($query === '') {
100 return mb_substr($text, 0, $maxLen)
101 . (mb_strlen($text) > $maxLen ? '…' : '');
102 }
103
104 $pos = mb_stripos($text, $query);
105 if ($pos === false) {
106 return mb_substr($text, 0, $maxLen)
107 . (mb_strlen($text) > $maxLen ? '…' : '');
108 }
109
110 // Determine snippet window
111 $start = max(0, $pos - intdiv($maxLen, 3));
112 $snippet = mb_substr($text, $start, $maxLen);
113
114 // Add ellipses if trimmed
115 if ($start > 0) {
116 $snippet = '…' . $snippet;
117 }
118 if ($start + $maxLen < mb_strlen($text)) {
119 $snippet .= '…';
120 }
121
122 // Highlight ALL occurrences inside snippet (case-insensitive)
123 $escapedQuery = preg_quote($query, '/');
124
125 $snippet = preg_replace(
126 "/($escapedQuery)/iu",
127 '<hit>$1</hit>',
128 $snippet
129 );
130
131 return $snippet;
132 }
133
@@ -0,0 +1,133 @@
1<?php
2function flattenValues(mixed $value, string $prefix = ''): array
3{
4 $out = [];
5
6 if (is_array($value)) {
7 foreach ($value as $k => $v) {
8 $key = $prefix === '' ? (string) $k : $prefix . '.' . $k;
9 $out = array_merge($out, flattenValues($v, $key));
10 }
11 return $out;
12 }
13
14 if (is_object($value)) {
15 foreach (get_object_vars($value) as $k => $v) {
16 $key = $prefix === '' ? (string) $k : $prefix . '.' . $k;
17 $out = array_merge($out, flattenValues($v, $key));
18 }
19 return $out;
20 }
21
22 // Scalars: string/int/float/bool/null
23 $str = '';
24 if (is_string($value))
25 $str = $value;
26 elseif (is_int($value) || is_float($value))
27 $str = (string) $value;
28 elseif (is_bool($value))
29 $str = $value ? 'true' : 'false';
30 else
31 $str = '';
32
33 $out[] = ['path' => $prefix, 'value' => $str];
34 return $out;
35}
36
37function searchAllJson(string $dataRoot, string $query): array
38{
39 $query = trim($query);
40 if ($query === '')
41 return [];
42
43 $q = mb_strtolower($query);
44 $results = [];
45
46 $it = new RecursiveIteratorIterator(
47 new RecursiveDirectoryIterator($dataRoot, FilesystemIterator::SKIP_DOTS)
48 );
49
50 foreach ($it as $file) {
51 /** @var SplFileInfo $file */
52 if (strtolower($file->getExtension()) !== 'json')
53 continue;
54
55 $path = $file->getPathname();
56 $rel = str_replace('\\', '/', substr($path, strlen($dataRoot)));
57 if ($rel === '')
58 $rel = '/' . $file->getFilename();
59
60 try {
61 $json = file_get_contents($path);
62 $obj = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
63 } catch (Throwable $e) {
64 // Skip invalid JSON (or you can report it)
65 continue;
66 }
67
68 $flat = flattenValues($obj);
69 $hits = [];
70
71 foreach ($flat as $entry) {
72 $val = $entry['value'] ?? '';
73 if ($val === '')
74 continue;
75
76 if (mb_stripos($val, $query) !== false) {
77 $hits[] = [
78 'field' => $entry['path'],
79 'snippet' => makeSnippet($val, $query, 120),
80 ];
81 }
82 }
83
84 if ($hits) {
85 $results[] = [
86 'file' => $rel,
87 'type' => $obj['type'] ?? '',
88 'name' => $obj['name'] ?? ($obj['title'] ?? ''),
89 'hits' => $hits,
90 ];
91 }
92 }
93
94 return $results;
95}
96
97function makeSnippet(string $text, string $query, int $maxLen = 120): string
98{
99 if ($query === '') {
100 return mb_substr($text, 0, $maxLen)
101 . (mb_strlen($text) > $maxLen ? '…' : '');
102 }
103
104 $pos = mb_stripos($text, $query);
105 if ($pos === false) {
106 return mb_substr($text, 0, $maxLen)
107 . (mb_strlen($text) > $maxLen ? '…' : '');
108 }
109
110 // Determine snippet window
111 $start = max(0, $pos - intdiv($maxLen, 3));
112 $snippet = mb_substr($text, $start, $maxLen);
113
114 // Add ellipses if trimmed
115 if ($start > 0) {
116 $snippet = '…' . $snippet;
117 }
118 if ($start + $maxLen < mb_strlen($text)) {
119 $snippet .= '…';
120 }
121
122 // Highlight ALL occurrences inside snippet (case-insensitive)
123 $escapedQuery = preg_quote($query, '/');
124
125 $snippet = preg_replace(
126 "/($escapedQuery)/iu",
127 '<hit>$1</hit>',
128 $snippet
129 );
130
131 return $snippet;
132}
133
web/templates/chapter.template +1 −1
@@ -1,4 +1,4 @@
1 <article class="chapter">
1 <article class="chapter" id="chapter-{{number}}">
2 2 <header class="chapterHeader">
3 3 <div class="chapterNumber">Chapter {{number}}</div>
4 4 <h1 class="chapterTitle"><a href="<<url>>">{{title}}</a></h1>
@@ -1,4 +1,4 @@
1<article class="chapter">
1<article class="chapter" id="chapter-{{number}}">
2 <header class="chapterHeader">
2 <header class="chapterHeader">
3 <div class="chapterNumber">Chapter {{number}}</div>
3 <div class="chapterNumber">Chapter {{number}}</div>
4 <h1 class="chapterTitle"><a href="<<url>>">{{title}}</a></h1>
4 <h1 class="chapterTitle"><a href="<<url>>">{{title}}</a></h1>