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 {
|
|
|
|
text string
|
|
|
|
level int
|
|
|
|
line int
|
|
|
|
}
|
|
|
|
|
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 10:50:58 +02:00
|
|
|
func print_toc(headings []heading) {
|
2024-04-29 09:30:05 +02:00
|
|
|
tab_count := 0
|
2024-04-30 10:50:58 +02:00
|
|
|
for index, _ := range headings {
|
2024-04-30 14:30:54 +02:00
|
|
|
cur_level := get_heading_level(headings[index].text)
|
|
|
|
|
|
|
|
headings[index].level = cur_level
|
|
|
|
|
2024-04-29 09:30:05 +02:00
|
|
|
if index > 0 {
|
2024-04-30 10:50:58 +02:00
|
|
|
prev_level := get_heading_level(headings[index-1].text)
|
2024-04-29 09:30:05 +02:00
|
|
|
|
|
|
|
if cur_level > prev_level {
|
|
|
|
tab_count += (cur_level - prev_level)
|
|
|
|
} else if cur_level < prev_level {
|
|
|
|
tab_count -= (prev_level - cur_level)
|
|
|
|
}
|
|
|
|
for i := 0; i < tab_count; i++ {
|
|
|
|
fmt.Printf("\t")
|
|
|
|
}
|
|
|
|
}
|
2024-04-30 10:50:58 +02:00
|
|
|
fmt.Printf("%v\n", headings[index])
|
2024-04-29 09:30:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 14:30:54 +02:00
|
|
|
func count_levels(headings []heading) map[string]int {
|
|
|
|
level_count := map[string]int{
|
|
|
|
"header1": 0,
|
|
|
|
"header2": 0,
|
|
|
|
"header3": 0,
|
|
|
|
"header4": 0,
|
|
|
|
"header5": 0,
|
|
|
|
"header6": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
for index, _ := range headings {
|
|
|
|
switch headings[index].level {
|
|
|
|
case 1:
|
|
|
|
level_count["header1"]++
|
|
|
|
case 2:
|
|
|
|
level_count["header2"]++
|
|
|
|
case 3:
|
|
|
|
level_count["header3"]++
|
|
|
|
case 4:
|
|
|
|
level_count["header4"]++
|
|
|
|
case 5:
|
|
|
|
level_count["header5"]++
|
|
|
|
case 6:
|
|
|
|
level_count["header6"]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return level_count
|
|
|
|
}
|
|
|
|
|
2024-04-29 09:30:05 +02:00
|
|
|
func main() {
|
|
|
|
file_content_raw, err := os.ReadFile(os.Args[1])
|
|
|
|
|
2024-04-30 10:50:58 +02:00
|
|
|
var headings []heading = nil
|
2024-04-29 09:30:05 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var file_content string = string(file_content_raw)
|
|
|
|
|
|
|
|
file_lines := strings.Split(file_content, "\n")
|
|
|
|
|
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 10:59:47 +02:00
|
|
|
headings = append(headings, heading{value, 0, (index + 1)})
|
2024-04-29 09:30:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print_toc(headings)
|
2024-04-30 14:30:54 +02:00
|
|
|
|
|
|
|
header_levels := count_levels(headings)
|
|
|
|
|
|
|
|
fmt.Printf("%v\n", header_levels)
|
2024-04-29 09:30:05 +02:00
|
|
|
}
|