-
Notifications
You must be signed in to change notification settings - Fork 2
/
docc.go
172 lines (157 loc) · 2.95 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
package docc
import (
"archive/zip"
"encoding/xml"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
var ErrNotSupportFormat = errors.New("the file is not supported")
type Document struct {
XMLName xml.Name `xml:"document"`
Body struct {
P []struct {
R []struct {
T struct {
Text string `xml:",chardata"`
Space string `xml:"space,attr"`
} `xml:"t"`
} `xml:"r"`
} `xml:"p"`
} `xml:"body"`
}
type Paragraph struct {
R []struct {
T struct {
Text string `xml:",chardata"`
Space string `xml:"space,attr"`
} `xml:"t"`
} `xml:"r"`
}
type Reader struct {
docxPath string
fromDoc bool
docx *zip.ReadCloser
xml io.ReadCloser
dec *xml.Decoder
}
// NewReader generetes a Reader struct.
// After reading, the Reader struct shall be Close().
func NewReader(docxPath string) (*Reader, error) {
r := new(Reader)
r.docxPath = docxPath
ext := strings.ToLower(filepath.Ext(docxPath))
if ext == ".doc" {
xfp, err := docToX(docxPath)
if err != nil {
return nil, fmt.Errorf("fail or not support .doc file: %w", err)
}
r.docxPath = xfp
r.fromDoc = true
} else if ext != ".docx" {
return nil, ErrNotSupportFormat
}
a, err := zip.OpenReader(r.docxPath)
if err != nil {
return nil, err
}
r.docx = a
f, err := a.Open("word/document.xml")
if err != nil {
return nil, err
}
r.xml = f
r.dec = xml.NewDecoder(f)
return r, nil
}
// Read reads the .docx file by a paragraph.
// When no paragraphs are remained to read, io.EOF error is returned.
func (r *Reader) Read() (string, error) {
err := seekNextTag(r.dec, "p")
if err != nil {
return "", err
}
p, err := seekParagraph(r.dec)
if err != nil {
return "", err
}
return p, nil
}
// ReadAll reads the whole .docx file.
func (r *Reader) ReadAll() ([]string, error) {
ps := []string{}
for {
p, err := r.Read()
if err == io.EOF {
return ps, nil
} else if err != nil {
return nil, err
}
ps = append(ps, p)
}
}
func (r *Reader) Close() error {
r.xml.Close()
r.docx.Close()
if r.fromDoc {
os.Remove(r.docxPath)
}
return nil
}
func seekParagraph(dec *xml.Decoder) (string, error) {
var t string
for {
token, err := dec.Token()
if err != nil {
return "", err
}
switch tt := token.(type) {
case xml.EndElement:
if tt.Name.Local == "p" {
return t, nil
}
case xml.StartElement:
if tt.Name.Local == "t" {
text, err := seekText(dec)
if err != nil {
return "", err
}
t = t + text
}
}
}
}
func seekText(dec *xml.Decoder) (string, error) {
for {
token, err := dec.Token()
if err != nil {
return "", err
}
switch tt := token.(type) {
case xml.CharData:
return string(tt), nil
case xml.EndElement:
return "", nil
}
}
}
func seekNextTag(dec *xml.Decoder, tag string) error {
for {
token, err := dec.Token()
if err != nil {
return err
}
t, ok := token.(xml.StartElement)
if !ok {
continue
}
if t.Name.Local != tag {
continue
}
break
}
return nil
}