-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocx.go
More file actions
251 lines (236 loc) · 5.46 KB
/
docx.go
File metadata and controls
251 lines (236 loc) · 5.46 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package parser
import (
"archive/zip"
"bytes"
"context"
"encoding/xml"
"fmt"
"io"
"strconv"
"strings"
)
// DOCX parses Microsoft Word .docx files.
//
// A .docx is a ZIP archive; the main payload lives at word/document.xml.
// Paragraphs with a style name like "Heading 1", "Heading 2", … become
// section boundaries. Paragraphs with no heading style are body text for
// the enclosing section.
//
// We use encoding/xml directly (no third-party dependency) because the
// WordprocessingML subset we care about is small and stable.
type DOCX struct{}
// NewDOCX returns a new DOCX parser.
func NewDOCX() *DOCX { return &DOCX{} }
// Name implements Parser.
func (*DOCX) Name() string { return "docx" }
// Accepts implements Parser.
func (*DOCX) Accepts(contentType, filename string) bool {
if contentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" {
return true
}
return HasExt(filename, ".docx")
}
// Parse implements Parser.
func (*DOCX) Parse(_ context.Context, r io.Reader) (*ParsedDoc, error) {
// zip.Reader needs a ReaderAt + size; buffer into memory. DOCX files
// are almost always small (< a few MB) in practice.
buf, err := io.ReadAll(r)
if err != nil {
return nil, err
}
zr, err := zip.NewReader(bytes.NewReader(buf), int64(len(buf)))
if err != nil {
return nil, fmt.Errorf("docx: not a valid zip: %w", err)
}
var body []byte
for _, f := range zr.File {
if f.Name == "word/document.xml" {
rc, err := f.Open()
if err != nil {
return nil, err
}
body, err = io.ReadAll(rc)
rc.Close()
if err != nil {
return nil, err
}
break
}
}
if body == nil {
return nil, fmt.Errorf("docx: missing word/document.xml")
}
paras, err := extractParagraphs(body)
if err != nil {
return nil, err
}
// Group paragraphs into sections. A heading paragraph opens a new
// section; non-heading paragraphs append to the current section's body.
type flat struct {
level int
title string
body []string
}
flats := []*flat{{level: 0, title: ""}}
current := flats[0]
for _, p := range paras {
text := strings.TrimSpace(p.text)
if text == "" {
continue
}
if p.headingLevel > 0 {
current = &flat{level: p.headingLevel, title: text}
flats = append(flats, current)
continue
}
current.body = append(current.body, text)
}
if len(flats) > 1 && flats[0].level == 0 && len(flats[0].body) == 0 {
flats = flats[1:]
}
var title string
for _, f := range flats {
if f.level == 1 {
title = f.title
break
}
}
if title == "" && len(flats) > 0 {
title = flats[0].title
}
// Build hierarchy via level stack.
rootSec := &Section{Level: 0, Title: title}
stack := []*Section{rootSec}
for _, f := range flats {
sec := Section{
Level: f.level,
Title: f.title,
Content: strings.Join(f.body, "\n\n"),
}
if f.level == 0 {
if sec.Content == "" {
continue
}
sec.Level = 1
sec.Title = "Introduction"
}
for len(stack) > 1 && stack[len(stack)-1].Level >= sec.Level {
stack = stack[:len(stack)-1]
}
parent := stack[len(stack)-1]
parent.Children = append(parent.Children, sec)
tail := &parent.Children[len(parent.Children)-1]
stack = append(stack, tail)
}
return &ParsedDoc{
Title: title,
Sections: rootSec.Children,
}, nil
}
type docxPara struct {
headingLevel int
text string
}
// extractParagraphs streams through document.xml and yields each <w:p>
// as a (headingLevel, text) pair.
//
// Heading detection: a paragraph is a heading if its <w:pStyle w:val>
// value is "Heading1".."Heading9" or "Heading 1".."Heading 9" (Word
// writes both spellings). Level is the trailing digit.
func extractParagraphs(body []byte) ([]docxPara, error) {
dec := xml.NewDecoder(bytes.NewReader(body))
var out []docxPara
var (
inPara bool
level int
textBuf strings.Builder
capturing bool // inside <w:t>
)
for {
tok, err := dec.Token()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
switch t := tok.(type) {
case xml.StartElement:
switch t.Name.Local {
case "p":
if t.Name.Space == "" || strings.HasSuffix(t.Name.Space, "wordprocessingml/2006/main") {
inPara = true
level = 0
textBuf.Reset()
}
case "pStyle":
if inPara {
for _, a := range t.Attr {
if a.Name.Local == "val" {
level = headingLevelFromStyle(a.Value)
}
}
}
case "t":
if inPara {
capturing = true
}
case "tab":
if inPara {
textBuf.WriteByte('\t')
}
case "br":
if inPara {
textBuf.WriteByte('\n')
}
}
case xml.CharData:
if capturing {
textBuf.Write(t)
}
case xml.EndElement:
switch t.Name.Local {
case "t":
capturing = false
case "p":
if inPara {
out = append(out, docxPara{
headingLevel: level,
text: textBuf.String(),
})
inPara = false
}
}
}
}
return out, nil
}
// headingLevelFromStyle parses the pStyle @val attribute. Word writes:
//
// "Heading1", "Heading2", ... (Word 2007+, most common)
// "Heading 1", "Heading 2", ... (with space, some variants)
// "heading 1" (LibreOffice)
// "Title" — treat as level 1
//
// Returns 0 when the style is not a heading.
func headingLevelFromStyle(s string) int {
v := strings.TrimSpace(strings.ToLower(s))
if v == "title" {
return 1
}
v = strings.TrimSpace(strings.TrimPrefix(v, "heading"))
if v == "" {
return 0
}
n, err := strconv.Atoi(v)
if err != nil {
return 0
}
if n < 1 {
return 0
}
if n > 6 {
n = 6
}
return n
}