add configuration page, semantic uri fix, improve children macro
This commit is contained in:
1 parent
4da7d4e42d
commit
627466c935
9 files changed
+581
-263
No files matched your search
@@ -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
@@ -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,
|
||||
),
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -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()
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -167,6 +167,7 @@ func (s *Server) registerRoutes() {
|
||||
LinkManager: s.handleLinkManager,
|
||||
Health: s.handleHealth,
|
||||
Tree: s.handleTree,
|
||||
Config: s.handleConfig,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user