diff --git a/cmd/pageswiki/main.go b/cmd/pageswiki/main.go index e53c30f..56453f5 100644 --- a/cmd/pageswiki/main.go +++ b/cmd/pageswiki/main.go @@ -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") diff --git a/internal/server/config_handler.go b/internal/server/config_handler.go new file mode 100644 index 0000000..c8ecf8a --- /dev/null +++ b/internal/server/config_handler.go @@ -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, + }, + ) +} diff --git a/internal/server/handlers.go b/internal/server/handlers.go index b681daa..f964170 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -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, + ), }, ) } diff --git a/internal/server/macro_expand.go b/internal/server/macro_expand.go index 5d6d67a..09eae95 100644 --- a/internal/server/macro_expand.go +++ b/internal/server/macro_expand.go @@ -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( `
  • %s
  • `, - child, - name, + html.EscapeString(child), + html.EscapeString(name), ), ) } @@ -56,13 +80,7 @@ func (s *Server) expandMacros( b.WriteString("") b.WriteString("") - body = strings.ReplaceAll( - body, - "{{children}}", - b.String(), - ) - } - } - - return body + return b.String() + }, + ) } diff --git a/internal/server/routes/ui.go b/internal/server/routes/ui.go index 35fbf37..ddeff52 100644 --- a/internal/server/routes/ui.go +++ b/internal/server/routes/ui.go @@ -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, diff --git a/internal/server/server.go b/internal/server/server.go index 0bfeb27..f22c532 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -167,6 +167,7 @@ func (s *Server) registerRoutes() { LinkManager: s.handleLinkManager, Health: s.handleHealth, Tree: s.handleTree, + Config: s.handleConfig, }, ) diff --git a/internal/store/config.go b/internal/store/config.go new file mode 100644 index 0000000..fcc3754 --- /dev/null +++ b/internal/store/config.go @@ -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 +} diff --git a/templates/pages/config.gohtml b/templates/pages/config.gohtml new file mode 100644 index 0000000..2405431 --- /dev/null +++ b/templates/pages/config.gohtml @@ -0,0 +1,336 @@ +{{ define "config.gohtml" }} + + + + +Config + + + + + + +
    +
    +Pages Wiki +
    + + +
    + +
    + + + +
    + +
    + +

    Config

    + +

    Home

    + +
    + + + + + +

    +If empty, the top page shows child pages like an Apache-style index. +

    + + + +
    + +
    + +
    + +

    Recent Objects

    + + + + + + + + + + + + + + +{{ range .Objects }} + + + + + + + + + + + + + +{{ end }} + + + +
    Primary LinkSizeUpdatedObject
    +{{ if .Path }} +{{ .Path }} +{{ else }} +unlinked +{{ end }} + +{{ formatBytes .Size }} + +{{ formatUnix .UpdatedAt }} + +{{ .UUID }} +
    + +
    + +
    + +
    + + + + + + + +{{ end }} diff --git a/templates/pages/home.gohtml b/templates/pages/home.gohtml index 05a6ccc..eb55fa1 100644 --- a/templates/pages/home.gohtml +++ b/templates/pages/home.gohtml @@ -1,261 +1,41 @@ {{ define "home.gohtml" }} - - - Pages Wiki - - - - - + +
    -
    - Pages Wiki +Pages Wiki
    -
    -

    Pages Wiki

    +

    Index of /

    -

    -Path-first semantic wiki. -

    - -
    - - - - - -
    - -
    - -
    - -

    Recent Objects

    - - - - - - - - - - - - - - - - -{{ range .Objects }} - - - - - - - - - - - - - -{{ end }} - - - -
    Primary LinkSizeUpdatedObject UUID
    - -{{ if .Path }} - - -{{ .Path }} - - -{{ else }} - - -unlinked - - -{{ end }} - - -{{ formatBytes .Size }} - -{{ formatUnix .UpdatedAt }} - - -{{ .UUID }} - -
    +
    +{{ .RenderedHTML }} +
    - - - - {{ end }}