-
Notifications
You must be signed in to change notification settings - Fork 14
/
structrun.go
130 lines (113 loc) · 2.64 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
package docxlib
import (
"encoding/xml"
"io"
)
// 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"`
RunProperties *RunProperties `xml:"w:rPr,omitempty"`
InstrText string `xml:"w:instrText,omitempty"`
Text *Text
Drawing *Drawing
}
func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
switch tt := t.(type) {
case xml.StartElement:
switch tt.Name.Local {
case "rPr":
var value RunProperties
d.DecodeElement(&value, &start)
r.RunProperties = &value
case "instrText":
var value string
d.DecodeElement(&value, &start)
r.InstrText = value
case "t":
var value Text
d.DecodeElement(&value, &start)
r.Text = &value
case "drawing":
var value Drawing
d.DecodeElement(&value, &start)
r.Drawing = &value
default:
continue
}
}
}
return nil
}
// RunProperties encapsulates visual properties of a run
type RunProperties struct {
XMLName xml.Name `xml:"w:rPr,omitempty"`
Color *Color `xml:"w:color,omitempty"`
Size *Size `xml:"w:sz,omitempty"`
RunStyle *RunStyle `xml:"w:rStyle,omitempty"`
Style *Style `xml:"w:pStyle,omitempty"`
}
func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
switch tt := t.(type) {
case xml.StartElement:
switch tt.Name.Local {
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 "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
default:
continue
}
}
}
return nil
}
// RunStyle contains styling for a run
type RunStyle struct {
XMLName xml.Name `xml:"w:rStyle,omitempty"`
Val string `xml:"w:val,attr"`
}
// Style contains styling for a paragraph
type Style struct {
XMLName xml.Name `xml:"w:pStyle,omitempty"`
Val string `xml:"w:val,attr"`
}
// Color contains the sound of music. :D
// I'm kidding. It contains the color
type Color struct {
XMLName xml.Name `xml:"w:color"`
Val string `xml:"w:val,attr"`
}
// Size contains the font size
type Size struct {
XMLName xml.Name `xml:"w:sz"`
Val string `xml:"w:val,attr"`
}