/
function.go
189 lines (147 loc) · 4.2 KB
/
function.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package fileconvt
import (
"os"
"os/exec"
"io"
"io/ioutil"
"fmt"
"regexp"
"strings"
)
const SizeLimit = 20 * 1024 * 1024
// https://www.fileformat.info/info/unicode/char/3000/index.htm
// UTF-8 (hex) 0xE3 0x80 0x80 (e38080)
func GenSection(sep string) string {
return string('\u3000') + "[[" + sep + "]]" + string('\u3000')
}
// time format https://zetcode.com/golang/datetime-parse/
// https://stackoverflow.com/questions/71979579/how-to-remove-multiple-line-break-n-from-a-string-but-keep-only-one
func PrintFileText(header string, footer string, body string, meta map[string]string) bool {
var text = ""
t := strings.TrimSpace(header)
if (len(t) > 0){
text += GenSection("header") + header
}
t = strings.TrimSpace(footer)
if (len(t) > 0){
text += GenSection("footer") + footer
}
t = strings.TrimSpace(body)
if (len(t) > 0){
text += GenSection("body") + "\n" + body
}
// remove long digit string
re := regexp.MustCompile(`(\d){21,}`)
text = re.ReplaceAllString(text, "")
// remove multiple returns
re = regexp.MustCompile(`(\r\n?|\n){2,}`)
text = re.ReplaceAllString(text, "$1$1")
fmt.Println(text)
if len(meta) > 0 {
fmt.Println(GenSection("meta"))
}
for key, value := range meta {
v := strings.TrimSpace(value)
length := len([]rune(v))
if(length > 0){
fmt.Printf("%20s\t %s\n", key, value)
}
}
if (len(text) > 0){
return true
}
return false
}
// cleanCell returns a cleaned cell text without new-lines
func cleanCell(text string) string {
text = strings.ReplaceAll(text, "\n", " ")
text = strings.ReplaceAll(text, "\r", "")
text = strings.TrimSpace(text)
return text
}
func xlGenerateSheetTitle(name string, number, rows int) (title string) {
if number > 0 {
title += "\n"
}
title += fmt.Sprintf("Sheet \"%s\" (%d rows):\n", name, rows)
return title
}
func writeOutput(writer io.Writer, output []byte, alreadyWritten *int64, size *int64) (err error) {
if int64(len(output)) > *size {
output = output[:*size]
}
*size -= int64(len(output))
writtenOut, err := writer.Write(output)
*alreadyWritten += int64(writtenOut)
return err
}
// wraps an *os.File
type LocalFile struct {
*os.File
unlink bool
}
// NewLocalFile ensures that there is a file which contains the data provided by r. If r is
// actually an instance of *os.File then this file is used, otherwise a temporary file is
// created and the data from r copied into it. Callers must call Done() when
// the LocalFile is no longer needed to ensure all resources are cleaned up.
func NewLocalFile(r io.Reader) (*LocalFile, error) {
if f, ok := r.(*os.File); ok {
return &LocalFile{
File: f,
}, nil
}
f, err := ioutil.TempFile(os.TempDir(), "docconv")
if err != nil {
return nil, fmt.Errorf("error creating temporary file: %v", err)
}
_, err = io.Copy(f, r)
if err != nil {
f.Close()
os.Remove(f.Name())
return nil, fmt.Errorf("error copying data into temporary file: %v", err)
}
return &LocalFile{
File: f,
unlink: true,
}, nil
}
// Done cleans up all resources.
func (l *LocalFile) Done() {
l.Close()
if l.unlink {
os.Remove(l.Name())
}
}
func check(e error) {
if e != nil {
panic(e)
}
}
func ConvertGB2312toUTF8(meta map[string]string) (map[string]string, error) {
var text string
for key, value := range meta {
v := strings.TrimSpace(value)
length := len([]rune(v))
if(length > 0){
text = text + fmt.Sprintf("%20s\t %s\n$7$7$7", key, value)
}
}
f, err := os.CreateTemp(os.TempDir(), "encconv")
check(err)
defer f.Close()
_, err = f.WriteString(text)
check(err)
f.Sync()
txtUTF8, err := exec.Command("iconv", "-f", "gb2312", "-t", "UTF-8", f.Name()).Output()
if err = os.Remove(f.Name()); err != nil {
fmt.Errorf("error delete temp: %v", err)
}
utxt := strings.Split(string(txtUTF8), "\n$7$7$7")
mutf := make(map[string]string)
for i := 0; i < len(utxt); i++ {
if len( strings.TrimSpace(utxt[i]) ) < 2 {continue}
rs := strings.SplitAfter(utxt[i], "\t")
mutf[ strings.TrimSpace(rs[0]) ] = strings.TrimSpace(rs[1])
}
return mutf, err
}