forked from tenkoh/go-docc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocc.go
227 lines (202 loc) · 4.83 KB
/
docc.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package docc
import (
"archive/zip"
"bytes"
"encoding/xml"
"errors"
"io"
"os"
"path/filepath"
"strings"
)
var (
ErrNotSupportFormat = errors.New("the file is not supported")
xmlParagraph = "p"
xmlTab = "t"
)
type FileReader struct {
fileName string
xml io.ReadCloser
decoder *xml.Decoder
}
type Reader struct {
docxPath string
fromDoc bool
docx *zip.ReadCloser
fileReaders []FileReader
}
func getFileReaders(zipReader *zip.Reader) ([]FileReader, error) {
var fileReaders []FileReader
for _, file := range zipReader.File {
if file.Name == "word/document.xml" ||
strings.Contains(file.Name, "header") ||
strings.Contains(file.Name, "footer") ||
strings.Contains(file.Name, "footnotes") {
openedFile, err := zipReader.Open(file.Name)
if err != nil {
return nil, err
}
fileReaders = append(fileReaders, FileReader{
fileName: file.Name,
xml: openedFile,
decoder: xml.NewDecoder(openedFile),
})
}
}
return fileReaders, nil
}
// NewReader generetes a Reader struct.
// After reading, the Reader struct shall be Close().
func NewReaderFromBytes(byteValues []byte) (*Reader, error) {
reader := new(Reader)
byteReader := bytes.NewReader(byteValues)
zipReader, err := zip.NewReader(byteReader, int64(len(byteValues)))
if err != nil {
return nil, err
}
fileReaders, err := getFileReaders(zipReader)
if err != nil {
return nil, err
}
reader.fileReaders = fileReaders
return reader, nil
}
// NewReader generetes a Reader struct.
// After reading, the Reader struct shall be Close().
func NewReader(docxPath string) (*Reader, error) {
reader := new(Reader)
reader.docxPath = docxPath
ext := strings.ToLower(filepath.Ext(docxPath))
if ext != ".docx" {
return nil, ErrNotSupportFormat
}
zipReadCloser, err := zip.OpenReader(reader.docxPath)
if err != nil {
return nil, err
}
fileReaders, err := getFileReaders(&zipReadCloser.Reader)
if err != nil {
return nil, err
}
reader.fileReaders = fileReaders
reader.docx = zipReadCloser
return reader, nil
}
func (r *Reader) read(decoder *xml.Decoder) (string, error) {
err := seekNextTag(decoder, xmlParagraph)
if err != nil {
return "", err
}
paragraph, err := seekParagraph(decoder)
if err != nil {
return "", err
}
return paragraph, nil
}
func (r *Reader) readSingleFile(decoder *xml.Decoder) (string, error) {
var content strings.Builder
for {
paragraph, err := r.read(decoder)
if err == io.EOF {
return content.String(), nil
} else if err != nil {
return "", err
}
content.WriteString(paragraph)
content.WriteString(" ")
}
}
// ReadAllFiles reads all header, footer, footnote and content related files in
// the zip archive and returns it's raw contents
func (r *Reader) ReadAllFiles() (headerValue, contentValue, footerValue, footnotesValue string, err error) {
var header, footer, footnotes, content strings.Builder
for _, fileReader := range r.fileReaders {
fileContent, err := r.readSingleFile(fileReader.decoder)
if err != nil {
return "", "", "", "", err
}
if strings.Contains(fileReader.fileName, "header") {
header.WriteString(fileContent)
} else if strings.Contains(fileReader.fileName, "footer") {
footer.WriteString(fileContent)
} else if strings.Contains(fileReader.fileName, "footnotes") {
footnotes.WriteString(fileContent)
} else {
content.WriteString(fileContent)
}
}
return trimmedString(header.String()),
trimmedString(content.String()),
trimmedString(footer.String()),
trimmedString(footnotes.String()),
nil
}
func trimmedString(input string) string {
return strings.TrimSpace(input)
}
func (r *Reader) Close() error {
for _, fileReader := range r.fileReaders {
fileReader.xml.Close()
}
if r.docx != nil {
r.docx.Close()
}
if r.fromDoc {
os.Remove(r.docxPath)
}
return nil
}
func seekText(decoder *xml.Decoder) (string, error) {
for {
token, err := decoder.Token()
if err != nil {
return "", err
}
switch tokenType := token.(type) {
case xml.CharData:
return string(tokenType), nil
case xml.EndElement:
return "", nil
}
}
}
func seekParagraph(decoder *xml.Decoder) (string, error) {
var paragraph strings.Builder
for {
token, err := decoder.Token()
if err != nil {
return "", err
}
switch tokenType := token.(type) {
case xml.EndElement:
if tokenType.Name.Local == xmlParagraph {
return paragraph.String(), nil
}
case xml.StartElement:
if tokenType.Name.Local == xmlTab {
text, err := seekText(decoder)
if err != nil {
return "", err
}
paragraph.WriteString(text)
}
}
}
}
func seekNextTag(decoder *xml.Decoder, tag string) error {
for {
token, err := decoder.Token()
if err != nil {
return err
}
tokenTag, ok := token.(xml.StartElement)
if !ok {
continue
}
if tokenTag.Name.Local != tag {
continue
}
break
}
return nil
}