-
Notifications
You must be signed in to change notification settings - Fork 9
/
structnodes.go
67 lines (59 loc) · 1.64 KB
/
structnodes.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
package docxlib
import (
"encoding/xml"
"io"
"github.com/golang/glog"
)
type ParagraphChild struct {
Link *Hyperlink `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main hyperlink,omitempty"`
Run *Run `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main r,omitempty"`
Properties *RunProperties `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main rPr,omitempty"`
}
type Paragraph struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main p"`
Data []ParagraphChild
file *DocxLib
}
func (p *Paragraph) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
children := make([]ParagraphChild, 0)
for {
t, err := d.Token()
if err == io.EOF {
break
}
switch tt := t.(type) {
case xml.StartElement:
var elem ParagraphChild
if tt.Name.Local == "hyperlink" {
var value Hyperlink
d.DecodeElement(&value, &start)
id := getAtt(tt.Attr, "id")
anchor := getAtt(tt.Attr, "anchor")
if id != "" {
value.ID = id
}
if anchor != "" {
value.ID = anchor
}
elem = ParagraphChild{Link: &value}
} else if tt.Name.Local == "r" {
var value Run
d.DecodeElement(&value, &start)
elem = ParagraphChild{Run: &value}
if value.InstrText == "" && value.Text == nil {
glog.V(0).Infof("Empty run, we ignore")
continue
}
} else if tt.Name.Local == "rPr" {
var value RunProperties
d.DecodeElement(&value, &start)
elem = ParagraphChild{Properties: &value}
} else {
continue
}
children = append(children, elem)
}
}
*p = Paragraph{Data: children}
return nil
}