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