Write pagetree as html

This commit is contained in:
Johannes Rothe 2022-09-06 22:13:42 +02:00
parent 82e36eacc1
commit 2160605775
2 changed files with 44 additions and 19 deletions

1
.gitignore vendored
View File

@ -21,3 +21,4 @@
# Go workspace file # Go workspace file
go.work go.work
*.html

62
main.go
View File

@ -3,38 +3,56 @@ package main
import ( import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"log" "log"
"net/http" "net/http"
"strings" "os"
"text/template"
"github.com/alexflint/go-arg" "github.com/alexflint/go-arg"
) )
type NodeData struct { type NodeData struct {
id string Id string
title string Title string
} }
type Node struct { type Node struct {
children []*Node Children []*Node
data NodeData Data NodeData
} }
func (n Node) printChildren(level int) { func (n Node) writeHTML(w io.Writer) {
log.Printf("%v%v", strings.Repeat("\t", level), n.data.title) tmpl, err := template.New("root").Parse(`
// leaf note {{ define "tree" }}
if len(n.children) == 0 { {{ if gt (len .Children) 0 }}
return <ul>
{{ range $index, $element := .Children}}
<li>
{{ $element.Data.Title }}
{{ template "tree" $element }}
</li>
{{ end }}
</ul>
{{ end }}
{{ end }}
<html>
{{ template "tree" . }}
</html>
`)
if err != nil {
fmt.Println("ERROR creating template")
} }
for _, child := range n.children { err = tmpl.Execute(w, n)
child.printChildren(level + 1) if err != nil {
fmt.Printf("ERROR executing template: %v", err)
} }
} }
func (n *Node) getChildWithID(id string) (node *Node, hasChild bool) { func (n *Node) getChildWithID(id string) (node *Node, hasChild bool) {
for _, c := range n.children { for _, c := range n.Children {
if c.data.id == id { if c.Data.Id == id {
return c, true return c, true
} }
} }
@ -43,7 +61,7 @@ func (n *Node) getChildWithID(id string) (node *Node, hasChild bool) {
} }
func (n *Node) addChild(child *Node) { func (n *Node) addChild(child *Node) {
n.children = append(n.children, child) n.Children = append(n.Children, child)
} }
type spaceResult struct { type spaceResult struct {
@ -147,7 +165,7 @@ func getSpaceChildren(url, user, token string) spaceResult {
func createPageTree(s *spaceResult) { func createPageTree(s *spaceResult) {
var parent *Node var parent *Node
root := Node{data: NodeData{title: "root"}} root := Node{Data: NodeData{Title: "root"}}
for _, page := range s.Results { for _, page := range s.Results {
parent = &root parent = &root
// ignore the root node // ignore the root node
@ -165,15 +183,21 @@ func createPageTree(s *spaceResult) {
parent = node parent = node
continue continue
} }
n := Node{data: NodeData{id: ancestor.ID, title: ancestor.Title}} n := Node{Data: NodeData{Id: ancestor.ID, Title: ancestor.Title}}
parent.addChild(&n) parent.addChild(&n)
parent = &n parent = &n
} }
// create leaf node // create leaf node
n := Node{data: NodeData{id: page.ID, title: page.Title}} n := Node{Data: NodeData{Id: page.ID, Title: page.Title}}
parent.addChild(&n) parent.addChild(&n)
} }
root.printChildren(0) fname := "tree.html"
f, err := os.Create(fname)
if err != nil {
log.Fatalf("Failed to open %v", fname)
}
defer f.Close()
root.writeHTML(f)
} }
func main() { func main() {