-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcharset.go
114 lines (94 loc) · 2.26 KB
/
charset.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package generator
import (
"bytes"
"io/ioutil"
"log"
"os"
"sort"
"strings"
"text/template"
"github.com/wingcd/go-xlsx-exporter/settings"
"github.com/wingcd/go-xlsx-exporter/utils"
)
var charsetTemplate = ""
type charsetFileDesc struct {
commonFileDesc
Words []string
}
type charsetGenerator struct {
}
func (g *charsetGenerator) Generate(info *BuildInfo) (save bool, data *bytes.Buffer) {
utils.CheckPath(info.Output)
if charsetTemplate == "" {
temp := getTemplate(info, "./template/charset.gtpl")
log.Printf("[提示] 加载模板: %s \n", temp)
data, err := ioutil.ReadFile(temp)
if err != nil {
log.Println(err)
return false, nil
}
charsetTemplate = string(data)
}
tpl, err := template.New("charset").Funcs(funcs).Parse(charsetTemplate)
if err != nil {
log.Println(err.Error())
return false, nil
}
var fd = charsetFileDesc{}
fd.Version = settings.TOOL_VERSION
fd.GoProtoVersion = settings.GO_PROTO_VERTION
lanTables := settings.LANG_TABLES
if lanTables != nil && len(lanTables) > 0 {
// 所有语言表数据
datas := make([][]string, 0)
for _, table := range lanTables {
datas = append(datas, table.Data...)
}
var tempFileName = info.Output + ".tpl"
var content = ""
if ok, _ := utils.PathExists(tempFileName); ok {
bts, err := os.ReadFile(tempFileName)
if err == nil {
content = content + string(bts)
}
}
var tplfile = "./template/charset.tpl"
if ok, _ := utils.PathExists(tplfile); ok {
bts, err := os.ReadFile(tplfile)
if err == nil {
content = content + string(bts)
}
}
var allChars = make(map[string]bool, 0)
var cntStrs = strings.Split(content, "")
for i := 0; i < len(cntStrs); i++ {
allChars[cntStrs[i]] = true
}
for i := 0; i < len(datas); i++ {
row := datas[i]
for j := 0; j < len(row); j++ {
var cell = row[j]
var strs = strings.Split(cell, "")
for k := 0; k < len(strs); k++ {
allChars[strs[k]] = true
}
}
}
strs := make([]string, 0)
for k, _ := range allChars {
strs = append(strs, k)
}
sort.Strings(strs)
fd.Words = strs
}
var buf = bytes.NewBufferString("")
err = tpl.Execute(buf, &fd)
if err != nil {
log.Println(err)
return false, nil
}
return true, buf
}
func init() {
Regist("charset", &charsetGenerator{})
}