2024-04-29 09:30:05 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
// TODO: read a file line by line
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-04-30 10:50:58 +02:00
|
|
|
type heading struct {
|
2024-04-30 23:52:13 +02:00
|
|
|
text string
|
|
|
|
level int
|
|
|
|
line int
|
|
|
|
parent *heading
|
2024-04-30 10:50:58 +02:00
|
|
|
}
|
|
|
|
|
2024-04-29 09:30:05 +02:00
|
|
|
func is_heading(line string) bool {
|
|
|
|
if strings.HasPrefix(line, "#") {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 10:50:58 +02:00
|
|
|
func get_heading_level(heading_text string) int {
|
2024-04-29 09:30:05 +02:00
|
|
|
level := 0
|
2024-04-30 10:50:58 +02:00
|
|
|
for i := 0; heading_text[i] == '#'; i++ {
|
2024-04-29 09:30:05 +02:00
|
|
|
level += 1
|
|
|
|
}
|
|
|
|
return level
|
|
|
|
}
|
|
|
|
|
2024-04-30 23:52:13 +02:00
|
|
|
func count_levels(headings []heading) [6]int {
|
|
|
|
level_count := [6]int{}
|
2024-04-30 14:30:54 +02:00
|
|
|
|
|
|
|
for index, _ := range headings {
|
|
|
|
switch headings[index].level {
|
|
|
|
case 1:
|
2024-04-30 23:52:13 +02:00
|
|
|
level_count[0]++
|
2024-04-30 14:30:54 +02:00
|
|
|
case 2:
|
2024-04-30 23:52:13 +02:00
|
|
|
level_count[1]++
|
2024-04-30 14:30:54 +02:00
|
|
|
case 3:
|
2024-04-30 23:52:13 +02:00
|
|
|
level_count[2]++
|
2024-04-30 14:30:54 +02:00
|
|
|
case 4:
|
2024-04-30 23:52:13 +02:00
|
|
|
level_count[3]++
|
2024-04-30 14:30:54 +02:00
|
|
|
case 5:
|
2024-04-30 23:52:13 +02:00
|
|
|
level_count[4]++
|
2024-04-30 14:30:54 +02:00
|
|
|
case 6:
|
2024-04-30 23:52:13 +02:00
|
|
|
level_count[5]++
|
2024-04-30 14:30:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return level_count
|
|
|
|
}
|
|
|
|
|
2024-04-30 23:52:13 +02:00
|
|
|
func get_parents(headings []heading) {
|
|
|
|
for index, _ := range headings {
|
|
|
|
if index == 0 {
|
|
|
|
headings[index].parent = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := (index - 1); i >= 0; i-- {
|
|
|
|
if headings[i].level < headings[index].level {
|
|
|
|
headings[index].parent = &headings[i]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-01 15:42:23 +02:00
|
|
|
func print_children(heading *heading, headings []heading) {
|
|
|
|
fmt.Printf("pointer of parent: %p\n", heading)
|
|
|
|
|
|
|
|
for index, _ := range headings {
|
|
|
|
if headings[index].parent == heading {
|
|
|
|
fmt.Printf("Header of parent %p: %s\n", heading, headings[index].text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-01 15:57:29 +02:00
|
|
|
func print_children_recursive(heading *heading, headings []heading) {
|
|
|
|
for index, _ := range headings {
|
|
|
|
if headings[index].parent == heading {
|
2024-05-05 09:08:12 +02:00
|
|
|
fmt.Printf("Child of parent %p: %s (%p)\n", heading, headings[index].text, &headings[index])
|
2024-05-01 15:57:29 +02:00
|
|
|
print_children_recursive(&headings[index], headings)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-05 08:17:49 +02:00
|
|
|
func parse_file(file_name string) []heading {
|
|
|
|
file_content_raw, err := os.ReadFile(file_name)
|
2024-04-29 09:30:05 +02:00
|
|
|
|
2024-05-05 08:17:49 +02:00
|
|
|
var headings []heading
|
2024-04-29 09:30:05 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-05-05 08:17:49 +02:00
|
|
|
file_lines := strings.Split(string(file_content_raw), "\n")
|
2024-04-29 09:30:05 +02:00
|
|
|
|
2024-04-30 10:59:47 +02:00
|
|
|
for index, value := range file_lines {
|
2024-04-29 09:30:05 +02:00
|
|
|
if is_heading(value) {
|
2024-04-30 18:43:20 +02:00
|
|
|
headings = append(
|
2024-04-30 23:52:13 +02:00
|
|
|
headings, heading{value, get_heading_level(value), (index + 1), nil})
|
2024-04-29 09:30:05 +02:00
|
|
|
}
|
|
|
|
}
|
2024-05-05 08:17:49 +02:00
|
|
|
return headings
|
|
|
|
}
|
2024-04-29 09:30:05 +02:00
|
|
|
|
2024-05-05 08:17:49 +02:00
|
|
|
func main() {
|
|
|
|
file_name := os.Args[1]
|
2024-04-30 23:52:13 +02:00
|
|
|
|
2024-05-05 08:17:49 +02:00
|
|
|
var headings []heading = nil
|
2024-05-01 15:42:23 +02:00
|
|
|
|
2024-05-05 08:17:49 +02:00
|
|
|
headings = parse_file(file_name)
|
|
|
|
|
|
|
|
get_parents(headings)
|
2024-05-01 15:57:29 +02:00
|
|
|
|
|
|
|
print_children_recursive(nil, headings)
|
2024-05-01 15:42:23 +02:00
|
|
|
|
2024-04-30 18:43:20 +02:00
|
|
|
fmt.Printf("%v\n", headings)
|
2024-04-30 14:30:54 +02:00
|
|
|
|
2024-04-30 18:43:20 +02:00
|
|
|
header_level_count := count_levels(headings)
|
2024-04-30 14:30:54 +02:00
|
|
|
|
2024-04-30 23:52:13 +02:00
|
|
|
for i := 0; i < 6; i++ {
|
2024-04-30 19:22:42 +02:00
|
|
|
if header_level_count[i] != 0 {
|
2024-04-30 23:52:13 +02:00
|
|
|
fmt.Printf("Header %d occurs %d times.\n", (i + 1), header_level_count[i])
|
2024-04-30 19:22:42 +02:00
|
|
|
}
|
2024-04-30 18:43:20 +02:00
|
|
|
}
|
2024-05-05 16:38:35 +02:00
|
|
|
|
2024-05-05 19:54:43 +02:00
|
|
|
my_children := get_child_indices(0, headings)
|
2024-05-05 16:38:35 +02:00
|
|
|
fmt.Printf("%v\n", my_children)
|
2024-04-29 09:30:05 +02:00
|
|
|
}
|