diff --git a/main.go b/main.go index 5f08976..4512618 100644 --- a/main.go +++ b/main.go @@ -131,6 +131,5 @@ func main() { } } - my_children := get_child_indices(0, headings) - fmt.Printf("%v\n", my_children) + tree(-1, "", headings) } diff --git a/tree.go b/tree.go index 9ad5acb..67bd79b 100644 --- a/tree.go +++ b/tree.go @@ -1,5 +1,9 @@ package main +import ( + "fmt" +) + func get_children(header_index int, headings []heading) []heading { var children []heading @@ -47,17 +51,21 @@ func get_root_children(headings []heading) []int { 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 { 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) { return nil } - next_level := get_heading_level(headings[header_index+1].text) + next_level := headings[header_index+1].level child_level := 0 @@ -69,7 +77,7 @@ func get_child_indices(header_index int, headings []heading) []int { cur_level := 0 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 { children = append(children, i) @@ -82,3 +90,19 @@ func get_child_indices(header_index int, headings []heading) []int { 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) + } + } +}