tree finally works on all 3 test files
This commit is contained in:
parent
1ef07c59c6
commit
67b64d113d
3
main.go
3
main.go
@ -131,6 +131,5 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
my_children := get_child_indices(0, headings)
|
tree(-1, "", headings)
|
||||||
fmt.Printf("%v\n", my_children)
|
|
||||||
}
|
}
|
||||||
|
32
tree.go
32
tree.go
@ -1,5 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
func get_children(header_index int, headings []heading) []heading {
|
func get_children(header_index int, headings []heading) []heading {
|
||||||
var children []heading
|
var children []heading
|
||||||
|
|
||||||
@ -47,17 +51,21 @@ func get_root_children(headings []heading) []int {
|
|||||||
return root_children
|
return root_children
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: -1 for root -> get indices for headers lying directly under root
|
|
||||||
func get_child_indices(header_index int, headings []heading) []int {
|
func get_child_indices(header_index int, headings []heading) []int {
|
||||||
var children []int
|
var children []int
|
||||||
|
|
||||||
parent_level := get_heading_level(headings[header_index].text)
|
if header_index == -1 {
|
||||||
|
children = get_root_children(headings)
|
||||||
|
return children
|
||||||
|
}
|
||||||
|
|
||||||
|
parent_level := headings[header_index].level
|
||||||
|
|
||||||
if header_index >= (len(headings) - 1) {
|
if header_index >= (len(headings) - 1) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
next_level := get_heading_level(headings[header_index+1].text)
|
next_level := headings[header_index+1].level
|
||||||
|
|
||||||
child_level := 0
|
child_level := 0
|
||||||
|
|
||||||
@ -69,7 +77,7 @@ func get_child_indices(header_index int, headings []heading) []int {
|
|||||||
cur_level := 0
|
cur_level := 0
|
||||||
|
|
||||||
for i := (header_index + 1); i < len(headings); i++ {
|
for i := (header_index + 1); i < len(headings); i++ {
|
||||||
cur_level = get_heading_level(headings[i].text)
|
cur_level = headings[i].level
|
||||||
|
|
||||||
if cur_level == child_level {
|
if cur_level == child_level {
|
||||||
children = append(children, i)
|
children = append(children, i)
|
||||||
@ -82,3 +90,19 @@ func get_child_indices(header_index int, headings []heading) []int {
|
|||||||
|
|
||||||
return children
|
return children
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tree(index int, prefix string, headings []heading) {
|
||||||
|
children := get_child_indices(index, headings)
|
||||||
|
//fmt.Printf("children: %v\n", children)
|
||||||
|
|
||||||
|
for index, child_index := range children {
|
||||||
|
//fmt.Printf("child_index: %d\n", child_index)
|
||||||
|
if index == (len(children) - 1) {
|
||||||
|
fmt.Println(prefix+"`--", headings[child_index].text)
|
||||||
|
tree(child_index, prefix+" ", headings)
|
||||||
|
} else {
|
||||||
|
fmt.Println(prefix+"|--", headings[child_index].text)
|
||||||
|
tree(child_index, prefix+"| ", headings)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user