-
Notifications
You must be signed in to change notification settings - Fork 25
/
structrun.go
348 lines (326 loc) · 8.07 KB
/
structrun.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
Copyright (c) 2020 gingfrederik
Copyright (c) 2021 Gonzalo Fernandez-Victorio
Copyright (c) 2021 Basement Crowd Ltd (https://www.basementcrowd.com)
Copyright (c) 2023 Fumiama Minamoto (源文雨)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package docx
import (
"encoding/xml"
"io"
"reflect"
"strings"
)
// Run is part of a paragraph that has its own style. It could be
// a piece of text in bold, or a link
type Run struct {
XMLName xml.Name `xml:"w:r,omitempty"`
Space string `xml:"xml:space,attr,omitempty"`
// RsidR string `xml:"w:rsidR,attr,omitempty"`
// RsidRPr string `xml:"w:rsidRPr,attr,omitempty"`
RunProperties *RunProperties `xml:"w:rPr,omitempty"`
InstrText string `xml:"w:instrText,omitempty"`
Children []interface{}
file *Docx
}
// UnmarshalXML ...
func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "space":
r.Space = attr.Value
/*case "rsidR":
r.RsidR = attr.Value
case "rsidRPr":
r.RsidRPr = attr.Value*/
default:
// ignore other attributes
}
}
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
if tt, ok := t.(xml.StartElement); ok {
child, err := r.parse(d, tt)
if err != nil {
return err
}
if child != nil {
r.Children = append(r.Children, child)
}
}
}
return nil
}
func (r *Run) parse(d *xml.Decoder, tt xml.StartElement) (child interface{}, err error) {
switch tt.Name.Local {
case "rPr":
var value RunProperties
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return nil, err
}
r.RunProperties = &value
return nil, nil
case "instrText":
var value string
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return nil, err
}
r.InstrText = value
return nil, nil
case "t":
var value Text
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return nil, err
}
child = &value
case "drawing":
var value Drawing
value.file = r.file
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return nil, err
}
child = &value
case "tab":
child = &Tab{}
case "br":
var value BarterRabbet
err = d.DecodeElement(&value, &tt)
if err != nil {
return nil, err
}
child = &value
case "AlternateContent":
/*var value AlternateContent
value.file = r.file
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return nil, err
}
if value.Choice == nil {
return nil, nil
}
if value.Choice.Requires != "wps" && value.Choice.Requires != "wpc" && value.Choice.Requires != "wpg" {
return nil, nil
}
child = &value*/
altcont:
for {
tok, err1 := d.Token()
if err1 == io.EOF {
break
}
if err1 != nil {
return nil, err1
}
if ttt, ok := tok.(xml.StartElement); ok && ttt.Name.Local == "Choice" {
for _, attr := range ttt.Attr {
if attr.Name.Local == "Requires" {
if attr.Value == "wps" || attr.Value == "wpc" || attr.Value == "wpg" {
tok, err = d.Token() // go into choice
if err != nil {
return nil, err
}
if ttt, ok := tok.(xml.StartElement); ok {
child, err = r.parse(d, ttt)
}
break altcont
}
break
}
}
}
if et, ok := tok.(xml.EndElement); ok {
if et.Name.Local == "AlternateContent" {
break
}
}
err = d.Skip() // skip unsupported tags
if err != nil {
return nil, err
}
}
default:
err = d.Skip() // skip unsupported tags
}
return
}
// KeepElements keep named elems amd removes others
//
// names: *docx.Text *docx.Drawing *docx.Tab *docx.BarterRabbet
func (r *Run) KeepElements(name ...string) {
items := make([]interface{}, 0, len(r.Children))
namemap := make(map[string]struct{}, len(name)*2)
for _, n := range name {
namemap[n] = struct{}{}
}
for _, item := range r.Children {
_, ok := namemap[reflect.ValueOf(item).Type().String()]
if ok {
items = append(items, item)
}
}
r.Children = items
}
// RunProperties encapsulates visual properties of a run
type RunProperties struct {
XMLName xml.Name `xml:"w:rPr,omitempty"`
Fonts *RunFonts
Bold *Bold
ICs *struct{} `xml:"w:iCs,omitempty"`
Italic *Italic
Highlight *Highlight
Color *Color
Size *Size
SizeCs *SizeCs
Spacing *Spacing
RunStyle *RunStyle
Style *Style
Shade *Shade
Kern *Kern
Underline *Underline
VertAlign *VertAlign
Strike *Strike
}
// UnmarshalXML ...
func (r *RunProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "rFonts":
var value RunFonts
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
r.Fonts = &value
case "b":
r.Bold = &Bold{}
case "iCs":
r.ICs = &struct{}{}
case "i":
r.Italic = &Italic{}
case "u":
var value Underline
value.Val = getAtt(tt.Attr, "val")
r.Underline = &value
case "highlight":
var value Highlight
value.Val = getAtt(tt.Attr, "val")
r.Highlight = &value
case "color":
var value Color
value.Val = getAtt(tt.Attr, "val")
r.Color = &value
case "sz":
var value Size
value.Val = getAtt(tt.Attr, "val")
r.Size = &value
case "spacing":
var value Spacing
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
r.Spacing = &value
case "szCs":
var value SizeCs
value.Val = getAtt(tt.Attr, "val")
r.SizeCs = &value
case "rStyle":
var value RunStyle
value.Val = getAtt(tt.Attr, "val")
r.RunStyle = &value
case "pStyle":
var value Style
value.Val = getAtt(tt.Attr, "val")
r.Style = &value
case "shd":
var value Shade
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
r.Shade = &value
case "kern":
var value Kern
v := getAtt(tt.Attr, "val")
if v == "" {
continue
}
value.Val, err = GetInt64(v)
if err != nil {
return err
}
r.Kern = &value
case "vertAlign":
var value VertAlign
value.Val = getAtt(tt.Attr, "val")
r.VertAlign = &value
case "strike":
var value Strike
value.Val = getAtt(tt.Attr, "val")
r.Strike = &value
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}
return nil
}
// RunFonts specifies the fonts used in the text of a run.
type RunFonts struct {
XMLName xml.Name `xml:"w:rFonts,omitempty"`
ASCII string `xml:"w:ascii,attr,omitempty"`
EastAsia string `xml:"w:eastAsia,attr,omitempty"`
HAnsi string `xml:"w:hAnsi,attr,omitempty"`
Hint string `xml:"w:hint,attr,omitempty"`
}
// UnmarshalXML ...
func (f *RunFonts) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "ascii":
f.ASCII = attr.Value
case "eastAsia":
f.EastAsia = attr.Value
case "hAnsi":
f.HAnsi = attr.Value
case "hint":
f.Hint = attr.Value
}
}
// Consume the end element
_, err := d.Token()
return err
}