seems to work, but might need unit tests
This commit is contained in:
parent
0539436231
commit
8b4e9a9e22
47
main.go
47
main.go
@ -10,9 +10,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type heading struct {
|
type heading struct {
|
||||||
text string
|
text string
|
||||||
level int
|
level int
|
||||||
line int
|
line int
|
||||||
|
parent *heading
|
||||||
}
|
}
|
||||||
|
|
||||||
func is_heading(line string) bool {
|
func is_heading(line string) bool {
|
||||||
@ -31,29 +32,45 @@ func get_heading_level(heading_text string) int {
|
|||||||
return level
|
return level
|
||||||
}
|
}
|
||||||
|
|
||||||
func count_levels(headings []heading) map[int]int {
|
func count_levels(headings []heading) [6]int {
|
||||||
level_count := make(map[int]int)
|
level_count := [6]int{}
|
||||||
|
|
||||||
for index, _ := range headings {
|
for index, _ := range headings {
|
||||||
switch headings[index].level {
|
switch headings[index].level {
|
||||||
case 1:
|
case 1:
|
||||||
level_count[1]++
|
level_count[0]++
|
||||||
case 2:
|
case 2:
|
||||||
level_count[2]++
|
level_count[1]++
|
||||||
case 3:
|
case 3:
|
||||||
level_count[3]++
|
level_count[2]++
|
||||||
case 4:
|
case 4:
|
||||||
level_count[4]++
|
level_count[3]++
|
||||||
case 5:
|
case 5:
|
||||||
level_count[5]++
|
level_count[4]++
|
||||||
case 6:
|
case 6:
|
||||||
level_count[6]++
|
level_count[5]++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return level_count
|
return level_count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
file_content_raw, err := os.ReadFile(os.Args[1])
|
file_content_raw, err := os.ReadFile(os.Args[1])
|
||||||
|
|
||||||
@ -70,17 +87,19 @@ func main() {
|
|||||||
for index, value := range file_lines {
|
for index, value := range file_lines {
|
||||||
if is_heading(value) {
|
if is_heading(value) {
|
||||||
headings = append(
|
headings = append(
|
||||||
headings, heading{value, get_heading_level(value), (index + 1)})
|
headings, heading{value, get_heading_level(value), (index + 1), nil})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get_parents(headings)
|
||||||
|
|
||||||
fmt.Printf("%v\n", headings)
|
fmt.Printf("%v\n", headings)
|
||||||
|
|
||||||
header_level_count := count_levels(headings)
|
header_level_count := count_levels(headings)
|
||||||
|
|
||||||
for i := 0; i <= 6; i++ {
|
for i := 0; i < 6; i++ {
|
||||||
if header_level_count[i] != 0 {
|
if header_level_count[i] != 0 {
|
||||||
fmt.Printf("Header %d occurs %d times.\n", i, header_level_count[i])
|
fmt.Printf("Header %d occurs %d times.\n", (i + 1), header_level_count[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
33
tests/weird_headers.md
Normal file
33
tests/weird_headers.md
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
some text.
|
||||||
|
|
||||||
|
#### test markdown doc
|
||||||
|
|
||||||
|
## header2
|
||||||
|
|
||||||
|
#### header4
|
||||||
|
|
||||||
|
some text.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
echo 'hi'
|
||||||
|
```
|
||||||
|
|
||||||
|
#### another header4
|
||||||
|
|
||||||
|
more text.
|
||||||
|
|
||||||
|
####third header4
|
||||||
|
|
||||||
|
####fourth #header4
|
||||||
|
|
||||||
|
## this is not a header anymore
|
||||||
|
|
||||||
|
##### header5
|
||||||
|
|
||||||
|
## a second header2
|
||||||
|
|
||||||
|
#### testing
|
||||||
|
|
||||||
|
```
|
||||||
|
cool code
|
||||||
|
```
|
Reference in New Issue
Block a user