package main import ( "fmt" ) func get_children(header_index int, headings []heading) []heading { var children []heading parent_level := get_heading_level(headings[header_index].text) next_level := get_heading_level(headings[header_index+1].text) child_level := 0 if next_level <= parent_level { return nil } else { child_level = next_level } cur_level := 0 for i := (header_index + 1); i < len(headings); i++ { cur_level = get_heading_level(headings[i].text) if cur_level == child_level { children = append(children, headings[i]) } if cur_level < child_level { break } } return children } func get_root_children(headings []heading) []int { var root_children []int lowest_level := 0 for index, value := range headings { cur_level := value.level if index == 0 || cur_level <= lowest_level { root_children = append(root_children, index) lowest_level = cur_level } } return root_children } func get_child_indices(header_index int, headings []heading) []int { var children []int 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 := headings[header_index+1].level child_level := 0 if next_level <= parent_level { return nil } else { child_level = next_level } cur_level := 0 for i := (header_index + 1); i < len(headings); i++ { cur_level = headings[i].level if cur_level == child_level { children = append(children, i) } if cur_level < child_level { break } } 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) } } }