fix: better php

This commit is contained in:
NaitLee 2025-09-30 02:43:14 +08:00
parent c8b4ac408e
commit 4c28ae5063
5 changed files with 47 additions and 27 deletions

View file

@ -61,11 +61,13 @@ omnisrv-9p -site src -proto unix -addr /tmp/omnisrv
新頁面放在 `src/page`
PHP 模板在 `tpl/*.php`omnisrv 模板`tmpl.html`。
PHP 模板在 `tpl/*.php`omnisrv 模板`src/tmpl.html`。
一級標題在 `/page` 頁面自動列出。
與 dummy-ssg 不同Markdown 文檔頭部使用簡易鍵值對,而非 JSON:
與 dummy-ssg 標準不同,資源文件放在 `src/assets` 並且直接鏈接 (`<link href="/assets/..." />`),而非放入 `tpl`
同時,倉庫中的 Markdown 文檔頭部使用簡易鍵值對,而非 JSON:
```
---

View file

@ -19,6 +19,7 @@
{{- .Html -}}
{{- end}}
</main>
{{if .Footer}}<footer>{{.Footer}}</footer>{{end}}
</body>
</html>

View file

@ -1,20 +1,8 @@
<!DOCTYPE html>
<!-- Powered by dummy-ssg -->
<?php
// let's use simple key-value headers instead of json
$raw = file_get_contents("src/" . $page["path"]);
preg_match('/^---\n\s*(.*?)\s*\n---\n/s', $raw, $matches);
if (!empty($matches[1])) {
foreach (explode("\n", $matches[1]) as $line) {
$line = trim($line);
if ($line === '') continue;
$kv = explode(' ', $line, 2);
if (empty($kv[1]))
$page[$kv[0]] = true;
else
$page[$kv[0]] = $kv[1];
}
}
[$main, $footer] = split($page['content']);
$page = kvhead($page);
?>
<html lang="<?php if (empty($page["lang"])) echo "zh-TW"; else echo $page["lang"]; ?>">
<head>
@ -30,9 +18,12 @@
<?php
if ($page["path"] === "index.md") include "bio.php";
else if ($page["path"] === "page/index.md") include "page-index.php";
else if (!empty($page["content"])) echo $page["content"];
else if (!empty($main)) echo $main;
else echo "此頁為空";
?>
</main>
<?php if (!empty($footer)) { ?>
<footer><?= $footer ?></footer>
<?php } ?>
</body>
</html>

33
tpl/init.php Normal file
View file

@ -0,0 +1,33 @@
<?php
// from https://codeberg.org/fsfans-cn/www/src/branch/new/template/init.php
function split($parsed) {
$hrtag = '<hr />';
$hrpos = strrpos($parsed, $hrtag);
if ($hrpos !== false) {
return [
substr($parsed, 0, $hrpos),
substr($parsed, $hrpos + strlen($hrtag))
];
}
return [$parsed, ''];
}
function kvhead($page) {
$raw = file_get_contents("src/" . $page["path"]);
preg_match('/^---\n\s*(.*?)\s*\n---\n/s', $raw, $matches);
if (!empty($matches[1])) {
foreach (explode("\n", $matches[1]) as $line) {
$line = trim($line);
if ($line === '') continue;
$kv = explode(' ', $line, 2);
if (empty($kv[1]))
$page[$kv[0]] = true;
else
$page[$kv[0]] = $kv[1];
}
}
return $page;
}
?>

View file

@ -32,15 +32,8 @@ array_multisort(
<?php echo $page["content"] ?>
<table class="pages">
<tbody>
<?php
foreach ($child_pages as $page)
echo "<tr><td>",
date("Y-m-d", $page["date"]),
"</td><td><a href=\"",
$page["href"],
"\">",
$page["title"],
"</a></td></tr>\n";
?>
<?php foreach ($child_pages as $page) { ?>
<tr><td><?= date("Y-m-d", $page["date"]) ?></td><td><a href="<?= $page["href"] ?>"><?= $page["title"] ?></a></td></tr>
<?php } ?>
</tbody>
</table>