From 8f54b4d4bfe6f2839a22e947d0917370dc5f70bf Mon Sep 17 00:00:00 2001 From: bjt-user Date: Sun, 5 May 2024 08:17:49 +0200 Subject: [PATCH] parse md file in separate func --- main.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 735cd53..9ce108e 100644 --- a/main.go +++ b/main.go @@ -82,8 +82,6 @@ func print_children(heading *heading, headings []heading) { } func print_children_recursive(heading *heading, headings []heading) { - //fmt.Printf("pointer of parent: %p\n", heading) - for index, _ := range headings { if headings[index].parent == heading { fmt.Printf("Child of parent %p: %s\n", heading, headings[index].text) @@ -92,18 +90,16 @@ func print_children_recursive(heading *heading, headings []heading) { } } -func main() { - file_content_raw, err := os.ReadFile(os.Args[1]) +func parse_file(file_name string) []heading { + file_content_raw, err := os.ReadFile(file_name) - var headings []heading = nil + var headings []heading if err != nil { log.Fatal(err) } - var file_content string = string(file_content_raw) - - file_lines := strings.Split(file_content, "\n") + file_lines := strings.Split(string(file_content_raw), "\n") for index, value := range file_lines { if is_heading(value) { @@ -111,15 +107,18 @@ func main() { headings, heading{value, get_heading_level(value), (index + 1), nil}) } } + return headings +} + +func main() { + file_name := os.Args[1] + + var headings []heading = nil + + headings = parse_file(file_name) get_parents(headings) - // print all children that do not have a parent - //print_children(nil, headings) - - // print all children of the second header - //print_children(&headings[1], headings) - print_children_recursive(nil, headings) fmt.Printf("%v\n", headings)