add configuration page, semantic uri fix, improve children macro

This commit is contained in:
Anya Tokugawa committed 2026-05-31 20:42:49 +09:00
1 parent 4da7d4e42d
commit 627466c935
9 files changed
+581 -263

No files matched your search

+6
View File
@@ -55,6 +55,12 @@ func main() {
st := store.New(database)
err = st.EnsureConfigTable()
if err != nil {
log.Fatal(err)
}
srv := server.New(st)
err = srv.Run(":3000")
+64
View File
@@ -0,0 +1,64 @@
package server
import (
"net/http"
)
func (s *Server) handleConfig(
w http.ResponseWriter,
r *http.Request,
) {
if r.Method == http.MethodPost {
homePath := r.FormValue("home_path")
err := s.store.SetConfig(
"home_path",
homePath,
)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
http.Redirect(
w,
r,
"/-/sys/config",
http.StatusFound,
)
return
}
homePath, err :=
s.store.GetConfig(
"home_path",
)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
recent, err :=
s.store.ListRecentObjects(
50,
)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
s.render(
w,
"config.gohtml",
map[string]any{
"HomePath": homePath,
"Objects": recent,
},
)
}
+54 -11
View File
@@ -1,6 +1,7 @@
package server
import (
"html/template"
"net/http"
)
@@ -9,27 +10,69 @@ func (s *Server) handleHome(
r *http.Request,
) {
objects, err :=
s.store.ListRecentObjects(
30,
homePath, err :=
s.store.GetConfig(
"home_path",
)
if err != nil {
http.Error(
w,
err.Error(),
500,
)
http.Error(w, err.Error(), 500)
return
}
if homePath != "" {
objectUUID, err :=
s.store.ResolvePath(
homePath,
)
if err == nil {
latest, err :=
s.store.GetLatestRevision(
objectUUID,
)
if err == nil {
rendered :=
s.expandMacros(
latest.Body,
homePath,
)
s.render(
w,
"page.gohtml",
map[string]any{
"Title": homePath,
"Path": homePath,
"ObjectUUID": objectUUID,
"RenderedHTML": template.HTML(
rendered,
),
},
)
return
}
}
}
rendered :=
s.expandMacros(
"{{children}}",
"/",
)
s.render(
w,
"home.gohtml",
map[string]any{
"Objects": objects,
"RenderedHTML": template.HTML(
rendered,
),
},
)
}
+38 -20
View File
@@ -2,25 +2,49 @@ package server
import (
"fmt"
"html"
"regexp"
"strings"
)
var childrenMacroRe = regexp.MustCompile(`\{\{children(?::([^}]+))?\}\}`)
func (s *Server) expandMacros(
body string,
currentPath string,
) string {
if strings.Contains(
return childrenMacroRe.ReplaceAllStringFunc(
body,
"{{children}}",
) {
func(m string) string {
children, err :=
s.store.ListChildPaths(
currentPath,
)
matches :=
childrenMacroRe.FindStringSubmatch(m)
if err == nil {
targetPath :=
currentPath
if len(matches) >= 2 &&
strings.TrimSpace(matches[1]) != "" {
targetPath =
strings.TrimSpace(
matches[1],
)
}
if !strings.HasPrefix(targetPath, "/") {
targetPath = "/" + targetPath
}
children, err :=
s.store.ListChildPaths(
targetPath,
)
if err != nil {
return ""
}
var b strings.Builder
@@ -35,7 +59,7 @@ func (s *Server) expandMacros(
name :=
strings.TrimPrefix(
child,
currentPath,
targetPath,
)
name =
@@ -47,8 +71,8 @@ func (s *Server) expandMacros(
b.WriteString(
fmt.Sprintf(
`<li><a href="%s">%s</a></li>`,
child,
name,
html.EscapeString(child),
html.EscapeString(name),
),
)
}
@@ -56,13 +80,7 @@ func (s *Server) expandMacros(
b.WriteString("</ul>")
b.WriteString("</section>")
body = strings.ReplaceAll(
body,
"{{children}}",
b.String(),
)
}
}
return body
return b.String()
},
)
}
+8 -2
View File
@@ -11,6 +11,7 @@ type UIHandlers struct {
LinkManager http.HandlerFunc
Health http.HandlerFunc
Tree http.HandlerFunc
Config http.HandlerFunc
}
func RegisterUIRoutes(
@@ -24,10 +25,15 @@ func RegisterUIRoutes(
)
mux.HandleFunc(
"/tree",
"/v/tree",
h.Tree,
)
mux.HandleFunc(
"/-/sys/config",
h.Config,
)
mux.HandleFunc(
"/new",
h.NewPage,
@@ -77,7 +83,7 @@ func RegisterUIRoutes(
switch r.URL.Path {
case "/tree":
case "/v/tree":
h.Tree(
w,
+1
View File
@@ -167,6 +167,7 @@ func (s *Server) registerRoutes() {
LinkManager: s.handleLinkManager,
Health: s.handleHealth,
Tree: s.handleTree,
Config: s.handleConfig,
},
)
+64
View File
@@ -0,0 +1,64 @@
package store
import "database/sql"
func (s *Store) EnsureConfigTable() error {
_, err := s.DB.Exec(`
CREATE TABLE IF NOT EXISTS config (
key TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at INTEGER NOT NULL
)
`)
return err
}
func (s *Store) GetConfig(
key string,
) (string, error) {
var value string
err := s.DB.QueryRow(`
SELECT value
FROM config
WHERE key = ?
`, key).Scan(&value)
if err == sql.ErrNoRows {
return "", nil
}
if err != nil {
return "", err
}
return value, nil
}
func (s *Store) SetConfig(
key string,
value string,
) error {
_, err := s.DB.Exec(`
INSERT INTO config (
key,
value,
updated_at
)
VALUES (
?,
?,
strftime('%s','now')
)
ON CONFLICT(key)
DO UPDATE SET
value = excluded.value,
updated_at = excluded.updated_at
`, key, value)
return err
}
+336
View File
@@ -0,0 +1,336 @@
{{ define "config.gohtml" }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Config</title>
<link rel="stylesheet" href="/o/path:/system/static/css/main.css">
<link rel="stylesheet" href="/o/path:/system/static/css/modes.css">
</head>
<body class="mode-config">
<header class="topbar">
<div class="brand">
<a href="/">Pages Wiki</a>
</div>
<nav>
<a href="/">Home</a>
<a href="/v/tree">Tree</a>
</nav>
</header>
<main class="config-layout">
<aside
id="config-sidebar"
class="config-sidebar"
>
<button
type="button"
class="sidebar-toggle"
onclick="toggleSidebar()"
>
☰
</button>
<nav class="sidebar-nav">
<a href="#" data-panel="home" onclick="showConfigPanel('home'); return false;">
Home
</a>
<a href="#" data-panel="objects" onclick="showConfigPanel('objects'); return false;">
Recent Objects
</a>
</nav>
</aside>
<section class="config-main">
<section
id="panel-home"
class="config-section config-panel active"
>
<h1>Config</h1>
<h2>Home</h2>
<form
method="post"
action="/-/sys/config"
>
<label>
Home semantic path
</label>
<input
name="home_path"
type="text"
value="{{ .HomePath }}"
placeholder="/pageswiki/home"
>
<p class="muted">
If empty, the top page shows child pages like an Apache-style index.
</p>
<button type="submit">
Save
</button>
</form>
</section>
<section
id="panel-objects"
class="config-section config-panel"
>
<h2>Recent Objects</h2>
<table class="recent-table">
<thead>
<tr>
<th>Primary Link</th>
<th>Size</th>
<th>Updated</th>
<th>Object</th>
</tr>
</thead>
<tbody>
{{ range .Objects }}
<tr>
<td>
{{ if .Path }}
<a href="{{ .Path }}">{{ .Path }}</a>
{{ else }}
<span class="muted">unlinked</span>
{{ end }}
</td>
<td class="mono">
{{ formatBytes .Size }}
</td>
<td class="mono">
{{ formatUnix .UpdatedAt }}
</td>
<td class="mono">
<a href="/o/uuid:{{ .UUID }}">{{ .UUID }}</a>
</td>
</tr>
{{ end }}
</tbody>
</table>
</section>
</section>
</main>
<script>
function toggleSidebar() {
const sidebar =
document.getElementById(
"config-sidebar"
)
sidebar.classList.toggle(
"collapsed"
)
}
function showConfigPanel(name) {
for (
const panel
of document.querySelectorAll(
".config-panel"
)
) {
panel.classList.remove(
"active"
)
}
const target =
document.getElementById(
"panel-" + name
)
if (target) {
target.classList.add(
"active"
)
}
for (
const link
of document.querySelectorAll(
".sidebar-nav a"
)
) {
link.classList.toggle(
"active",
link.dataset.panel === name
)
}
history.replaceState(
null,
"",
"/-/sys/config?panel=" + encodeURIComponent(name)
)
}
document.addEventListener(
"DOMContentLoaded",
() => {
const params =
new URLSearchParams(
location.search
)
showConfigPanel(
params.get("panel") || "home"
)
},
)
</script>
<style>
.config-layout {
display: grid;
grid-template-columns: 14rem 1fr;
min-height: calc(100vh - 48px);
}
.config-sidebar {
border-right: 1px solid rgba(255,255,255,0.06);
padding: 0.75rem;
transition: width 0.12s linear;
}
.config-sidebar.collapsed {
width: 2.5rem;
overflow: hidden;
}
.sidebar-toggle {
background: transparent;
color: #aaa;
border: 1px solid rgba(255,255,255,0.08);
padding: 0.25rem 0.5rem;
cursor: pointer;
}
.sidebar-nav {
display: flex;
flex-direction: column;
margin-top: 1rem;
gap: 0.5rem;
}
.sidebar-nav a {
text-decoration: none;
}
.config-main {
padding: 1.25rem;
}
.config-section {
margin-bottom: 2rem;
}
.config-section input[type="text"] {
width: 100%;
max-width: 42rem;
background: #0b0b0b;
color: #eee;
border: 1px solid #222;
padding: 0.6rem;
font-family: monospace;
}
.recent-table {
width: 100%;
border-collapse: collapse;
}
.recent-table th {
text-align: left;
font-weight: normal;
color: #888;
border-bottom: 1px solid rgba(255,255,255,0.08);
padding: 0.45rem 0.35rem;
}
.recent-table td {
border-bottom: 1px solid rgba(255,255,255,0.035);
padding: 0.38rem 0.35rem;
}
.recent-table tr:hover {
background: rgba(255,255,255,0.025);
}
.mono {
font-family:
"JetBrains Mono",
monospace;
}
.muted {
color: #777;
}
.config-panel {
display: none;
}
.config-panel.active {
display: block;
}
.sidebar-nav a.active {
color: #fff;
text-shadow:
0 0 0.45rem rgba(180,120,255,0.35);
}
.config-sidebar.collapsed .sidebar-nav {
display: none;
}
</style>
</body>
</html>
{{ end }}
+10 -230
View File
@@ -1,261 +1,41 @@
{{ define "home.gohtml" }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pages Wiki</title>
<link
rel="stylesheet"
href="/o/path:/system/static/css/main.css"
>
<link
rel="stylesheet"
href="/o/path:/system/static/css/modes.css"
>
<link rel="stylesheet" href="/o/path:/system/static/css/main.css">
<link rel="stylesheet" href="/o/path:/system/static/css/modes.css">
</head>
<body class="mode-home">
<header class="topbar">
<div class="brand">
<a href="/">Pages Wiki</a>
<a href="/">Pages Wiki</a>
</div>
<nav>
<a href="/tree">
Tree
</a>
<a href="/new">
Draft
</a>
<a href="/v/tree">Tree</a>
<a href="/-/sys/config">Config</a>
<a href="/new">Draft</a>
</nav>
</header>
<main class="container">
<section>
<h1>Pages Wiki</h1>
<h1>Index of /</h1>
<p>
Path-first semantic wiki.
</p>
<div class="create-path-box">
<input
id="new-path"
type="text"
placeholder="/pageswiki/hogehoge"
autofocus
>
<button
type="button"
onclick="createOrEditPath()"
>
Create / Edit
</button>
</div>
</section>
<section>
<h2>Recent Objects</h2>
<table class="recent-table">
<thead>
<tr>
<th>Primary Link</th>
<th>Size</th>
<th>Updated</th>
<th>Object UUID</th>
</tr>
</thead>
<tbody>
{{ range .Objects }}
<tr>
<td>
{{ if .Path }}
<a href="{{ .Path }}">
{{ .Path }}
</a>
{{ else }}
<span class="muted">
unlinked
</span>
{{ end }}
</td>
<td class="mono">
{{ formatBytes .Size }}
</td>
<td class="mono">
{{ formatUnix .UpdatedAt }}
</td>
<td class="mono muted">
<a href="/o/uuid:{{ .UUID }}">
{{ .UUID }}
</a>
</td>
</tr>
{{ end }}
</tbody>
</table>
<article class="page-html">
{{ .RenderedHTML }}
</article>
</section>
</main>
<script>
function normalizePath(input) {
let p = input.trim()
if (!p.startsWith("/")) {
p = "/" + p
}
p = p.replace(/\/+/g, "/")
return p
}
function createOrEditPath() {
const input =
document.getElementById(
"new-path",
)
if (!input.value.trim()) {
return
}
const path =
normalizePath(
input.value,
)
location.href =
"/e" + path
}
document
.getElementById(
"new-path",
)
.addEventListener(
"keydown",
(ev) => {
if (ev.key === "Enter") {
ev.preventDefault()
createOrEditPath()
}
},
)
</script>
<style>
main.container {
padding: 1.25rem;
}
section {
margin-bottom: 2rem;
}
.create-path-box {
display: flex;
gap: 0.5rem;
margin: 1rem 0;
}
.create-path-box input {
flex: 1;
background: #0b0b0b;
color: #eee;
border: 1px solid #222;
padding: 0.6rem;
font-family: monospace;
}
.create-path-box button {
padding: 0.6rem 1rem;
}
.recent-table {
width: 100%;
border-collapse: collapse;
}
.recent-table th {
text-align: left;
font-weight: normal;
color: #888;
border-bottom: 1px solid rgba(255,255,255,0.08);
padding: 0.45rem 0.35rem;
}
.recent-table td {
border-bottom: 1px solid rgba(255,255,255,0.035);
padding: 0.38rem 0.35rem;
}
.recent-table tr:hover {
background: rgba(255,255,255,0.025);
}
.mono {
font-family:
"JetBrains Mono",
monospace;
}
.muted {
color: #777;
}
</style>
</body>
</html>
{{ end }}