-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumbering_definition.go
More file actions
74 lines (63 loc) · 1.54 KB
/
numbering_definition.go
File metadata and controls
74 lines (63 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"fmt"
"sort"
"strconv"
"strings"
)
type NumberingDefinition struct {
AbstractNumID string
Levels map[string]*NumberingLevel
}
func NewNumberingDefinition(abstractNumID string) *NumberingDefinition {
return &NumberingDefinition{
AbstractNumID: abstractNumID,
Levels: make(map[string]*NumberingLevel),
}
}
func (nd *NumberingDefinition) AddLevel(levelID string, level *NumberingLevel) {
nd.Levels[levelID] = level
}
func (nd *NumberingDefinition) ResetLevelsBelow(currentLevelID string) {
currentLevelInt, err := strconv.Atoi(currentLevelID)
if err != nil {
return
}
for levelIDStr, level := range nd.Levels {
levelIDInt, err := strconv.Atoi(levelIDStr)
if err != nil {
continue
}
if levelIDInt > currentLevelInt {
level.Reset()
}
}
}
func (nd *NumberingDefinition) GetFormattedNumber(levelID string) string {
level, ok := nd.Levels[levelID]
if !ok {
return ""
}
text := level.TextTemplate
var levelIDs []string
for id := range nd.Levels {
levelIDs = append(levelIDs, id)
}
sort.Slice(levelIDs, func(i, j int) bool {
id1, _ := strconv.Atoi(levelIDs[i])
id2, _ := strconv.Atoi(levelIDs[j])
return id1 < id2
})
for _, subLevelIDStr := range levelIDs {
subLevel, exists := nd.Levels[subLevelIDStr]
if !exists {
continue
}
subLevelNum, _ := strconv.Atoi(subLevelIDStr)
placeholder := fmt.Sprintf("%%%d", subLevelNum+1)
if strings.Contains(text, placeholder) {
text = strings.ReplaceAll(text, placeholder, subLevel.FormatCurrentValue())
}
}
return text
}