/
funcs.go
56 lines (51 loc) · 1.25 KB
/
funcs.go
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
package main
import (
"fmt"
"github.com/Masterminds/sprig/v3"
"github.com/Wing924/d2r-wing/tools/lib/model"
"regexp"
"strconv"
"strings"
"text/template"
"unicode"
)
func CreateFuncs(entries []model.Entry) template.FuncMap {
funcs := sprig.HermeticTxtFuncMap()
funcs["mkAbbr"] = mkAbbr
funcs["lookupString"] = lookupString(entries)
return funcs
}
func mkAbbr(text string, abbr string) string {
if idx := strings.IndexAny(text, ":,"); idx >= 0 {
return fmt.Sprint(text[0:idx], "(", abbr+")", text[idx:])
} else {
runes := []rune(text)
for i := len(runes) - 1; i >= 0; i-- {
if unicode.In(runes[i], unicode.Han) {
return fmt.Sprint(string(runes[0:i+1]), "(", abbr+")", string(runes[i+1:]))
}
}
}
return text
}
var reInline = regexp.MustCompile(`\$\{\d+:[^}]+}`)
func lookupString(entries []model.Entry) func(str string) string {
table := map[int]string{}
for _, e := range entries {
table[e.ID] = e.ZhTW
}
return func(str string) string {
return reInline.ReplaceAllStringFunc(str, func(s string) string {
idx := strings.IndexRune(s, ':')
idStr := s[2:idx]
id, err := strconv.Atoi(idStr)
if err != nil {
panic(err)
}
if v, ok := table[id]; ok {
return v
}
panic("cannot lookup by id: " + idStr)
})
}
}