From 7bd415bd4a8d5baa709cd952d969ede3a3527845 Mon Sep 17 00:00:00 2001 From: Manuel Odendahl Date: Thu, 4 Apr 2024 12:58:37 -0400 Subject: [PATCH] :sparkles: Actually use a separate simpler render structure with sorted children --- .../datatables/templates/commands.tmpl.html | 51 ++++++++++++++++++ .../datatables/templates/index.tmpl.html | 54 ------------------- pkg/handlers/command-dir/command-dir.go | 31 ++++++++--- pkg/handlers/template-dir/template-dir.go | 3 ++ pkg/handlers/template/template.go | 1 - pkg/render/renderer.go | 3 +- 6 files changed, 81 insertions(+), 62 deletions(-) create mode 100644 pkg/glazed/handlers/datatables/templates/commands.tmpl.html delete mode 100644 pkg/glazed/handlers/datatables/templates/index.tmpl.html diff --git a/pkg/glazed/handlers/datatables/templates/commands.tmpl.html b/pkg/glazed/handlers/datatables/templates/commands.tmpl.html new file mode 100644 index 0000000..34a499e --- /dev/null +++ b/pkg/glazed/handlers/datatables/templates/commands.tmpl.html @@ -0,0 +1,51 @@ + + + + + + Commands Tree + + + +
+ +
+ + +{{ define "node" }} +{{ $ = . }} +{{ $node := $.node }} +
  • {{ $node.Name }} + {{ if $node.Command }} + {{ with $node.Command }} + + {{ .Name }} + (text, + json + ) + {{ if .Short }}- {{ .Short }} {{ end }} + {{ if .Long }} +
    + Details + {{ .Long }} +
    + {{ end }} + {{ end}} + {{end}} + + {{ if $node.Children }} + + {{ end }} + {{ end }} +
  • + + \ No newline at end of file diff --git a/pkg/glazed/handlers/datatables/templates/index.tmpl.html b/pkg/glazed/handlers/datatables/templates/index.tmpl.html deleted file mode 100644 index a201b02..0000000 --- a/pkg/glazed/handlers/datatables/templates/index.tmpl.html +++ /dev/null @@ -1,54 +0,0 @@ - - - - - - Commands Tree - - - -
    - {{ template "node" (dict "node" .commands "path" .path "showTitle" true) }} -
    - - -{{ define "node" }} - {{ $ = . }} - -{{ end }} - - \ No newline at end of file diff --git a/pkg/handlers/command-dir/command-dir.go b/pkg/handlers/command-dir/command-dir.go index 2d82bdf..b24fbe6 100644 --- a/pkg/handlers/command-dir/command-dir.go +++ b/pkg/handlers/command-dir/command-dir.go @@ -306,18 +306,37 @@ func (cd *CommandDirHandler) Serve(server *parka.Server, path string) error { } }) - server.Router.GET(path+"/", func(c *gin.Context) { - //commands := cd.Repository.CollectCommands(nil, true) - rootNode := cd.Repository.FindNode(nil) - templ, err := cd.TemplateLookup.Lookup("index.tmpl.html") + server.Router.GET(path+"/commands/*path", func(c *gin.Context) { + path_ := c.Param("path") + path_ = strings.TrimPrefix(path_, "/") + path_ = strings.TrimSuffix(path_, "/") + splitPath := strings.Split(path_, "/") + if path_ == "" { + splitPath = []string{} + } + renderNode, ok := cd.Repository.GetRenderNode(splitPath) + if !ok { + c.JSON(404, gin.H{"error": fmt.Sprintf("command %s not found", path_)}) + return + } + templ, err := cd.TemplateLookup.Lookup("commands.tmpl.html") if err != nil { c.JSON(500, gin.H{"error": errors.Wrapf(err, "could not load index template").Error()}) return } + var nodes []*repositories.RenderNode + + if renderNode.Command != nil { + nodes = append(nodes, renderNode) + } else { + for _, v := range renderNode.Children { + nodes = append(nodes, v) + } + } err = templ.Execute(c.Writer, gin.H{ - "commands": rootNode, - "path": path, + "nodes": nodes, + "path": path, }) if err != nil { c.JSON(500, gin.H{"error": errors.Wrapf(err, "could not execute index template").Error()}) diff --git a/pkg/handlers/template-dir/template-dir.go b/pkg/handlers/template-dir/template-dir.go index ddea417..efb8db0 100644 --- a/pkg/handlers/template-dir/template-dir.go +++ b/pkg/handlers/template-dir/template-dir.go @@ -129,6 +129,9 @@ func NewTemplateDirHandlerFromConfig(td *config.TemplateDir, options ...Template func (td *TemplateDirHandler) Serve(server *server.Server, path string) error { // TODO(manuel, 2023-05-26) This is a hack because we currently mix and match content with commands. + // The use of a middleware to handle something that could be handled by the routing framework itself + // is because gin (which really should get replaced because we actually go against its grain heavily) + // does not allow routes to overlap. server.Router.Use(td.renderer.HandleWithTrimPrefix(path, nil)) return nil diff --git a/pkg/handlers/template/template.go b/pkg/handlers/template/template.go index c66bb89..ad66d40 100644 --- a/pkg/handlers/template/template.go +++ b/pkg/handlers/template/template.go @@ -87,7 +87,6 @@ func NewTemplateHandlerFromConfig( func (t *TemplateHandler) Serve(server_ *server.Server, path string) error { server_.Router.Handle("GET", path, t.renderer.HandleWithTrimPrefix("", nil)) - //server_.Router.Use(t.renderer.Handle(nil)) return nil } diff --git a/pkg/render/renderer.go b/pkg/render/renderer.go index ef4824e..ed2552a 100644 --- a/pkg/render/renderer.go +++ b/pkg/render/renderer.go @@ -292,6 +292,7 @@ func (r *Renderer) HandleWithTrimPrefix(prefix string, data map[string]interface return } } - c.Next() + // we're done and have written our piece, so we can return + return } }