Skip to content

Commit

Permalink
✨ Actually use a separate simpler render structure with sorted children
Browse files Browse the repository at this point in the history
  • Loading branch information
wesen committed Apr 4, 2024
1 parent d010646 commit 7bd415b
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 62 deletions.
51 changes: 51 additions & 0 deletions pkg/glazed/handlers/datatables/templates/commands.tmpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Commands Tree</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.4.1/milligram.min.css">
</head>
<body>
<div class="container">
<ul>
{{ $path := .path }}
{{ range $name, $node := .nodes }}
{{ template "node" (dict "node" $node "path" $path) }}
{{ end }}
</ul>
</div>


{{ define "node" }}
{{ $ = . }}
{{ $node := $.node }}
<li><strong>{{ $node.Name }}</strong>
{{ if $node.Command }}
{{ with $node.Command }}
<a href="{{$.path}}/datatables/{{ join "/" .Parents }}/{{ .Name }}">
<strong>{{ .Name }}</strong>
</a> (<a href="{{$.path}}/text/{{ join "/" .Parents }}/{{ .Name }}">text</a>,
<a href="{{$.path}}/data/{{ join "/" .Parents }}/{{ .Name }}">json</a>
)
{{ if .Short }}- {{ .Short }} {{ end }}
{{ if .Long }}
<details>
<summary>Details</summary>
{{ .Long }}
</details>
{{ end }}
{{ end}}
{{end}}

{{ if $node.Children }}
<ul>
{{ range $name, $child := $node.Children }}
{{ template "node" (dict "node" $child "path" $.path) }}
{{end}}
</ul>
{{ end }}
{{ end }}
</li>
</body>
</html>
54 changes: 0 additions & 54 deletions pkg/glazed/handlers/datatables/templates/index.tmpl.html

This file was deleted.

31 changes: 25 additions & 6 deletions pkg/handlers/command-dir/command-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()})
Expand Down
3 changes: 3 additions & 0 deletions pkg/handlers/template-dir/template-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion pkg/handlers/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion pkg/render/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 7bd415b

Please sign in to comment.