remove useless comments
@@ -92,7 +92,6 @@ function loadFolderTree(string $rootPath): FolderNode
92
92
93
93
/**
94
94
* Parse JSON with sane error handling. Returns array/object depending on JSON.
95
* If you always want associative arrays, pass true to json_decode.
96
95
*/
97
96
function parseJsonFile(string $path): mixed
98
97
{
@@ -101,12 +100,8 @@ function parseJsonFile(string $path): mixed
101
100
throw new RuntimeException("Failed to read JSON file: {$path}");
102
101
}
103
102
104
// Change second arg to true if you want associative arrays always.
105
103
$data = json_decode($raw, false, 512, JSON_THROW_ON_ERROR);
106
104
$data->wikiPath = substr($path, strlen(__DIR__));
107
105
108
// Optional: attach filename/path metadata
109
// if (is_object($data)) { $data->_source = $path; }
110
111
106
return $data;
112
107
}
@@ -92,7 +92,6 @@ function loadFolderTree(string $rootPath): FolderNode92
92
93
/**93
/**94
* Parse JSON with sane error handling. Returns array/object depending on JSON.94
* Parse JSON with sane error handling. Returns array/object depending on JSON.95
* If you always want associative arrays, pass true to json_decode.96
*/95
*/97
function parseJsonFile(string $path): mixed96
function parseJsonFile(string $path): mixed98
{97
{@@ -101,12 +100,8 @@ function parseJsonFile(string $path): mixed101
throw new RuntimeException("Failed to read JSON file: {$path}");100
throw new RuntimeException("Failed to read JSON file: {$path}");102
}101
}103
102
104
// Change second arg to true if you want associative arrays always.105
$data = json_decode($raw, false, 512, JSON_THROW_ON_ERROR);103
$data = json_decode($raw, false, 512, JSON_THROW_ON_ERROR);106
$data->wikiPath = substr($path, strlen(__DIR__));104
$data->wikiPath = substr($path, strlen(__DIR__));107
105
108
// Optional: attach filename/path metadata109
// if (is_object($data)) { $data->_source = $path; }110
111
return $data;106
return $data;112
}107
}@@ -19,7 +19,6 @@ function flattenValues(mixed $value, string $prefix = ''): array
19
19
return $out;
20
20
}
21
21
22
// Scalars: string/int/float/bool/null
23
22
$str = '';
24
23
if (is_string($value))
25
24
$str = $value;
@@ -61,7 +60,7 @@ function searchAllJson(string $dataRoot, string $query): array
61
60
$json = file_get_contents($path);
62
61
$obj = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
63
62
} catch (Throwable $e) {
64
// Skip invalid JSON (or you can report it)
63
// Skip invalid JSON (should never happen with gitlab pipeline)
65
64
continue;
66
65
}
67
66
@@ -107,11 +106,9 @@ function makeSnippet(string $text, string $query, int $maxLen = 120): string
107
106
. (mb_strlen($text) > $maxLen ? '…' : '');
108
107
}
109
108
110
// Determine snippet window
111
109
$start = max(0, $pos - intdiv($maxLen, 3));
112
110
$snippet = mb_substr($text, $start, $maxLen);
113
111
114
// Add ellipses if trimmed
115
112
if ($start > 0) {
116
113
$snippet = '…' . $snippet;
117
114
}
@@ -119,7 +116,7 @@ function makeSnippet(string $text, string $query, int $maxLen = 120): string
119
116
$snippet .= '…';
120
117
}
121
118
122
// Highlight ALL occurrences inside snippet (case-insensitive)
119
// Highlight ALL occurrences inside snippet
123
120
$escapedQuery = preg_quote($query, '/');
124
121
125
122
$snippet = preg_replace(
@@ -19,7 +19,6 @@ function flattenValues(mixed $value, string $prefix = ''): array19
return $out;19
return $out;20
}20
}21
21
22
// Scalars: string/int/float/bool/null23
$str = '';22
$str = '';24
if (is_string($value))23
if (is_string($value))25
$str = $value;24
$str = $value;@@ -61,7 +60,7 @@ function searchAllJson(string $dataRoot, string $query): array61
$json = file_get_contents($path);60
$json = file_get_contents($path);62
$obj = json_decode($json, true, 512, JSON_THROW_ON_ERROR);61
$obj = json_decode($json, true, 512, JSON_THROW_ON_ERROR);63
} catch (Throwable $e) {62
} catch (Throwable $e) {64
// Skip invalid JSON (or you can report it)63
// Skip invalid JSON (should never happen with gitlab pipeline)65
continue;64
continue;66
}65
}67
66
@@ -107,11 +106,9 @@ function makeSnippet(string $text, string $query, int $maxLen = 120): string107
. (mb_strlen($text) > $maxLen ? '…' : '');106
. (mb_strlen($text) > $maxLen ? '…' : '');108
}107
}109
108
110
// Determine snippet window111
$start = max(0, $pos - intdiv($maxLen, 3));109
$start = max(0, $pos - intdiv($maxLen, 3));112
$snippet = mb_substr($text, $start, $maxLen);110
$snippet = mb_substr($text, $start, $maxLen);113
111
114
// Add ellipses if trimmed115
if ($start > 0) {112
if ($start > 0) {116
$snippet = '…' . $snippet;113
$snippet = '…' . $snippet;117
}114
}@@ -119,7 +116,7 @@ function makeSnippet(string $text, string $query, int $maxLen = 120): string119
$snippet .= '…';116
$snippet .= '…';120
}117
}121
118
122
// Highlight ALL occurrences inside snippet (case-insensitive)119
// Highlight ALL occurrences inside snippet123
$escapedQuery = preg_quote($query, '/');120
$escapedQuery = preg_quote($query, '/');124
121
125
$snippet = preg_replace(122
$snippet = preg_replace(@@ -20,13 +20,11 @@ function echoTemplateCssLinks(string $templatesDirFs, string $templatesDirUrl =
20
20
return;
21
21
}
22
22
23
// Deterministic order
24
23
sort($files, SORT_NATURAL | SORT_FLAG_CASE);
25
24
26
25
foreach ($files as $filePath) {
27
26
$fileName = basename($filePath);
28
27
29
// Basic hardening: only allow .css files with sane filenames
30
28
if (!preg_match('/^[a-zA-Z0-9_.-]+\.css$/', $fileName)) {
31
29
continue;
32
30
}
@@ -20,13 +20,11 @@ function echoTemplateCssLinks(string $templatesDirFs, string $templatesDirUrl =20
return;20
return;21
}21
}22
22
23
// Deterministic order24
sort($files, SORT_NATURAL | SORT_FLAG_CASE);23
sort($files, SORT_NATURAL | SORT_FLAG_CASE);25
24
26
foreach ($files as $filePath) {25
foreach ($files as $filePath) {27
$fileName = basename($filePath);26
$fileName = basename($filePath);28
27
29
// Basic hardening: only allow .css files with sane filenames30
if (!preg_match('/^[a-zA-Z0-9_.-]+\.css$/', $fileName)) {28
if (!preg_match('/^[a-zA-Z0-9_.-]+\.css$/', $fileName)) {31
continue;29
continue;32
}30
}@@ -72,6 +72,7 @@ final class TemplateRenderer
72
72
* 1) resolve child-template tags [[path:tplName]]
73
73
* 2) resolve raw {{{path}}}
74
74
* 3) resolve escaped {{path}}
75
* 4) entire lists of a complete folder
75
76
*/
76
77
private function renderTemplateString(string $template, array $data): string
77
78
{
@@ -135,12 +136,12 @@ final class TemplateRenderer
135
136
$template
136
137
);
137
138
139
// 4) [[[entire folders]]]
138
140
$template = preg_replace_callback(
139
141
'/\[\[\[\s*([a-zA-Z0-9_.-]+)\s*\]\]\]/',
140
142
function ($m) use ($data) {
141
143
$folderName = $m[1];
142
144
143
// Basic hardening: only allow simple folder names
144
145
if (!preg_match('/^[a-zA-Z0-9_.-]+$/', $folderName)) {
145
146
return '';
146
147
}
@@ -148,7 +149,6 @@ final class TemplateRenderer
148
149
$folderPath = $this->dataDir . DIRECTORY_SEPARATOR . $folderName;
149
150
150
151
if (!is_dir($folderPath)) {
151
// If you prefer to see errors during authoring, you could return a warning div instead.
152
152
return '';
153
153
}
154
154
@@ -164,7 +164,6 @@ final class TemplateRenderer
164
164
try {
165
165
$out .= $this->renderFromJsonFile($jsonFile);
166
166
} catch (\Throwable $e) {
167
// Fail-soft: embed an error block so one bad file doesn't blank the whole page
168
167
$out .= '<pre class="renderError">' .
169
168
htmlspecialchars("Render error in {$jsonFile}: " . $e->getMessage(), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') .
170
169
'</pre>';
@@ -224,7 +223,6 @@ final class TemplateRenderer
224
223
225
224
private function isList(array $arr): bool
226
225
{
227
// PHP 8.1+: array_is_list($arr)
228
226
$i = 0;
229
227
foreach ($arr as $k => $_) {
230
228
if ($k !== $i++)
@@ -72,6 +72,7 @@ final class TemplateRenderer72
* 1) resolve child-template tags [[path:tplName]]72
* 1) resolve child-template tags [[path:tplName]]73
* 2) resolve raw {{{path}}}73
* 2) resolve raw {{{path}}}74
* 3) resolve escaped {{path}}74
* 3) resolve escaped {{path}}75
* 4) entire lists of a complete folder75
*/76
*/76
private function renderTemplateString(string $template, array $data): string77
private function renderTemplateString(string $template, array $data): string77
{ 78
{ @@ -135,12 +136,12 @@ final class TemplateRenderer135
$template136
$template136
);137
);137
138
139
// 4) [[[entire folders]]]138
$template = preg_replace_callback(140
$template = preg_replace_callback(139
'/\[\[\[\s*([a-zA-Z0-9_.-]+)\s*\]\]\]/',141
'/\[\[\[\s*([a-zA-Z0-9_.-]+)\s*\]\]\]/',140
function ($m) use ($data) {142
function ($m) use ($data) {141
$folderName = $m[1];143
$folderName = $m[1];142
144
143
// Basic hardening: only allow simple folder names144
if (!preg_match('/^[a-zA-Z0-9_.-]+$/', $folderName)) {145
if (!preg_match('/^[a-zA-Z0-9_.-]+$/', $folderName)) {145
return '';146
return '';146
}147
}@@ -148,7 +149,6 @@ final class TemplateRenderer148
$folderPath = $this->dataDir . DIRECTORY_SEPARATOR . $folderName;149
$folderPath = $this->dataDir . DIRECTORY_SEPARATOR . $folderName;149
150
150
if (!is_dir($folderPath)) {151
if (!is_dir($folderPath)) {151
// If you prefer to see errors during authoring, you could return a warning div instead.152
return '';152
return '';153
}153
}154
154
@@ -164,7 +164,6 @@ final class TemplateRenderer164
try {164
try {165
$out .= $this->renderFromJsonFile($jsonFile);165
$out .= $this->renderFromJsonFile($jsonFile);166
} catch (\Throwable $e) {166
} catch (\Throwable $e) {167
// Fail-soft: embed an error block so one bad file doesn't blank the whole page168
$out .= '<pre class="renderError">' .167
$out .= '<pre class="renderError">' .169
htmlspecialchars("Render error in {$jsonFile}: " . $e->getMessage(), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') .168
htmlspecialchars("Render error in {$jsonFile}: " . $e->getMessage(), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') .170
'</pre>';169
'</pre>';@@ -224,7 +223,6 @@ final class TemplateRenderer224
223
225
private function isList(array $arr): bool224
private function isList(array $arr): bool226
{225
{227
// PHP 8.1+: array_is_list($arr)228
$i = 0;226
$i = 0;229
foreach ($arr as $k => $_) {227
foreach ($arr as $k => $_) {230
if ($k !== $i++)228
if ($k !== $i++)