forked from gonfva/docxlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structdoc.go
159 lines (140 loc) · 7.6 KB
/
structdoc.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
package docx
import (
"encoding/xml"
"fmt"
"strconv"
"strings"
)
const (
XMLNS_W = `http://schemas.openxmlformats.org/wordprocessingml/2006/main`
XMLNS_R = `http://schemas.openxmlformats.org/officeDocument/2006/relationships`
)
type Body struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main body"`
Paragraphs []*Paragraph `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main p"`
}
type Document struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main document"`
XMLW string `xml:"xmlns:w,attr"`
XMLR string `xml:"xmlns:r,attr"`
Body *Body `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main body"`
Styles *DocumentStyle `xml:"-"`
}
type DocumentStyle struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main styles"`
XMLW string `xml:"xmlns:w,attr"`
XMLR string `xml:"xmlns:r,attr"`
DocumentDefault *DocumentDefault `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main docDefaults"`
LatentStyles *LatentStyles `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main latentStyles"`
Styles []*DefinedStyle `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main style"`
styleMap map[string]*DefinedStyle
}
func (d *DocumentStyle) GetStyleById(styleId string) *DefinedStyle {
if d.styleMap == nil {
d.styleMap = make(map[string]*DefinedStyle)
}
if s, ok := d.styleMap[styleId]; ok {
return s
}
for _, s := range d.Styles {
d.styleMap[s.StyleId] = s
if s.StyleId == styleId {
return s
}
}
return nil
}
type DocumentDefault struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main docDefaults"`
RPrDefault *RPrDefault `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main rPrDefault"`
PPrDefault *PPrDefault `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main pPrDefault"`
}
type RPrDefault struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main rPrDefault"`
RPr *RunProperties `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main rPr"`
}
type PPrDefault struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main pPrDefault"`
PPr *ParagraphProperties `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main pPr"`
}
type LatentStyles struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main latentStyles"`
LsdExceptions []*LsdException `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main lsdException"`
Count int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main count,attr"`
DefQFormat int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main defQFormat,attr"`
DefUnhideWhenUsed int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main defUnhideWhenUsed,attr"`
DefUIPriority int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main defUIPriority,attr"`
DefLockedState int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main defLockedState,attr"`
DefSemiHidden int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main defSemiHidden,attr"`
DefPrimaryStyle int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main defPrimaryStyle,attr"`
}
type LsdException struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main lsdException"`
Name string `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main name,attr"`
Locked int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main locked,attr"`
SemiHidden int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main semiHidden,attr"`
UnhideWhenUsed int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main unhideWhenUsed,attr"`
QFormat int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main qFormat,attr"`
UIPriority int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main uiPriority,attr"`
PrimaryStyle int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main primaryStyle,attr"`
}
type DefinedStyle struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main style"`
Type string `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main type,attr"`
StyleId string `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main styleId,attr"`
Name *StrValueNode `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main name"`
BasedOn *StrValueNode `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main basedOn"`
Next *StrValueNode `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main next"`
Link string `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main link"`
RPr *RunProperties `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main rPr"`
PPr *ParagraphProperties `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main pPr"`
AutoRedefine *StrValueNode `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main autoRedefine"`
SemiHidden *StrValueNode `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main semiHidden"`
QFormat *StrValueNode `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main qFormat"`
UiPriority *StrValueNode `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main uiPriority"`
TblPr *TblPr `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main tblPr"`
}
func (d *DefinedStyle) GetName() string {
if d.Name != nil {
return d.Name.Val
}
return ""
}
func (d *DefinedStyle) HeadingLevel() int {
if d.PPr == nil {
return 0
}
if strings.HasPrefix(d.Name.Val, "heading ") {
level := d.Name.Val[8:]
if level != "" {
iLevel, err := strconv.Atoi(level)
if err == nil {
return iLevel
}
}
}
return 0
}
type StrValueNode struct {
XMLName xml.Name
Val string `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main val,attr"`
}
func (v *StrValueNode) String() string {
return fmt.Sprintf("%s:'%s'", v.XMLName.Local, v.Val)
}
type IntValueNode struct {
XMLName xml.Name
Val int64 `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main val,attr"`
}
type TblPr struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main tblPr"`
TblStyle string `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main tblStyle,attr"`
TblCellMar *TblCellMar `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main tblCellMar"`
}
type TblCellMar struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main tblCellMar"`
Top int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main top,attr"`
Left int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main left,attr"`
Bottom int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main bottom,attr"`
Right int `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main right,attr"`
}