1package markdown
2
3import "strconv"
4
5type headingNumbering struct {
6 levels [6]int
7}
8
9// Observe register the event of a new level with the given depth and
10// adjust the numbering accordingly
11func (hn *headingNumbering) Observe(level int) {
12 if level <= 0 {
13 panic("level start at 1, ask blackfriday why")
14 }
15 if level > 6 {
16 panic("Markdown is limited to 6 levels of heading")
17 }
18
19 hn.levels[level-1]++
20 for i := level; i < 6; i++ {
21 hn.levels[i] = 0
22 }
23}
24
25// Render render the current headings numbering.
26func (hn *headingNumbering) Render() string {
27 slice := hn.levels[:]
28
29 // pop the last zero levels
30 for i := 5; i >= 0; i-- {
31 if hn.levels[i] != 0 {
32 break
33 }
34 slice = slice[:len(slice)-1]
35 }
36
37 var result string
38
39 for i := range slice {
40 if i > 0 {
41 result += "."
42 }
43 result += strconv.Itoa(slice[i])
44 }
45
46 return result
47}