Write HTML files and link from the index

This commit is contained in:
Johannes Rothe 2022-09-06 22:32:35 +02:00
parent 2160605775
commit 57fac8b7b7

29
main.go
View File

@ -23,14 +23,14 @@ type Node struct {
Data NodeData Data NodeData
} }
func (n Node) writeHTML(w io.Writer) { func (n *Node) writeIndex(w io.Writer) {
tmpl, err := template.New("root").Parse(` tmpl, err := template.New("root").Parse(`
{{ define "tree" }} {{ define "tree" }}
{{ if gt (len .Children) 0 }} {{ if gt (len .Children) 0 }}
<ul> <ul>
{{ range $index, $element := .Children}} {{ range $index, $element := .Children}}
<li> <li>
{{ $element.Data.Title }} <a href="{{$element.Data.Id}}.html">{{ $element.Data.Title }}</a>
{{ template "tree" $element }} {{ template "tree" $element }}
</li> </li>
{{ end }} {{ end }}
@ -50,6 +50,17 @@ func (n Node) writeHTML(w io.Writer) {
} }
} }
func (n *Node) writeHTMLFiles() {
if len(n.Children) > 0 {
for _, child := range n.Children {
writeHTML(n)
child.writeHTMLFiles()
}
}
// leaf node
writeHTML(n)
}
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 {
@ -64,6 +75,15 @@ func (n *Node) addChild(child *Node) {
n.Children = append(n.Children, child) n.Children = append(n.Children, child)
} }
func writeHTML(n *Node) {
f, err := os.Create(n.Data.Id + ".html")
if err != nil {
log.Fatalf("Error creating file for ID %v", n.Data.Id)
}
defer f.Close()
f.Write([]byte(n.Data.Title))
}
type spaceResult struct { type spaceResult struct {
Results []struct { Results []struct {
ID string `json:"id"` ID string `json:"id"`
@ -191,13 +211,14 @@ func createPageTree(s *spaceResult) {
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)
} }
fname := "tree.html" fname := "index.html"
f, err := os.Create(fname) f, err := os.Create(fname)
if err != nil { if err != nil {
log.Fatalf("Failed to open %v", fname) log.Fatalf("Failed to open %v", fname)
} }
defer f.Close() defer f.Close()
root.writeHTML(f) root.writeIndex(f)
root.writeHTMLFiles()
} }
func main() { func main() {