added function to count header levels and return a map
This commit is contained in:
parent
09a8626a85
commit
9d37c5b519
@ -6,9 +6,6 @@ This seems intended by the creators of the language.
|
|||||||
|
|
||||||
#### TODO
|
#### TODO
|
||||||
|
|
||||||
- count number of headings for each heading level
|
- print header tree
|
||||||
(loop through all headings, find the highest heading level, get all used levels\
|
|
||||||
then create a slice or slice of maps and append after each deeper level discovered...)
|
|
||||||
- print heading tree
|
|
||||||
- delph deeper into different Markdown implementations (like CommonMark)
|
- delph deeper into different Markdown implementations (like CommonMark)
|
||||||
- add support for "Setext-style headers"
|
- add support for "Setext-style headers"
|
||||||
|
43
main.go
43
main.go
@ -1,10 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
// TODO: read a file line by line
|
// TODO: read a file line by line
|
||||||
// use heading struct with
|
|
||||||
// line content
|
|
||||||
// line number
|
|
||||||
// heading level
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -38,12 +34,13 @@ func get_heading_level(heading_text string) int {
|
|||||||
func print_toc(headings []heading) {
|
func print_toc(headings []heading) {
|
||||||
tab_count := 0
|
tab_count := 0
|
||||||
for index, _ := range headings {
|
for index, _ := range headings {
|
||||||
if index > 0 {
|
|
||||||
prev_level := get_heading_level(headings[index-1].text)
|
|
||||||
cur_level := get_heading_level(headings[index].text)
|
cur_level := get_heading_level(headings[index].text)
|
||||||
|
|
||||||
headings[index].level = cur_level
|
headings[index].level = cur_level
|
||||||
|
|
||||||
|
if index > 0 {
|
||||||
|
prev_level := get_heading_level(headings[index-1].text)
|
||||||
|
|
||||||
if cur_level > prev_level {
|
if cur_level > prev_level {
|
||||||
tab_count += (cur_level - prev_level)
|
tab_count += (cur_level - prev_level)
|
||||||
} else if cur_level < prev_level {
|
} else if cur_level < prev_level {
|
||||||
@ -57,6 +54,36 @@ func print_toc(headings []heading) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
file_content_raw, err := os.ReadFile(os.Args[1])
|
file_content_raw, err := os.ReadFile(os.Args[1])
|
||||||
|
|
||||||
@ -77,4 +104,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
print_toc(headings)
|
print_toc(headings)
|
||||||
|
|
||||||
|
header_levels := count_levels(headings)
|
||||||
|
|
||||||
|
fmt.Printf("%v\n", header_levels)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user