mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-12 03:20:23 +08:00
shrink xmlns
This commit is contained in:
2
empty.go
2
empty.go
@@ -23,7 +23,7 @@ func newEmptyFile() *Docx {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
DocRelation: Relationships{
|
DocRelation: Relationships{
|
||||||
Xmlns: XMLNS,
|
Xmlns: XMLNS_REL,
|
||||||
Relationships: []*Relationship{
|
Relationships: []*Relationship{
|
||||||
{
|
{
|
||||||
ID: "rId1",
|
ID: "rId1",
|
||||||
|
|||||||
46
structdoc.go
46
structdoc.go
@@ -1,6 +1,9 @@
|
|||||||
package docxlib
|
package docxlib
|
||||||
|
|
||||||
import "encoding/xml"
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
XMLNS_W = `http://schemas.openxmlformats.org/wordprocessingml/2006/main`
|
XMLNS_W = `http://schemas.openxmlformats.org/wordprocessingml/2006/main`
|
||||||
@@ -19,15 +22,44 @@ func getAtt(atts []xml.Attr, name string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Body struct {
|
type Body struct {
|
||||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main body"`
|
XMLName xml.Name `xml:"w:body"`
|
||||||
Paragraphs []*Paragraph `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main p"`
|
Paragraphs []*Paragraph `xml:"w:p,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Document struct {
|
type Document struct {
|
||||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main document"`
|
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main document"`
|
||||||
XMLW string `xml:"xmlns:w,attr"`
|
XMLW string `xml:"xmlns:w,attr"` // cannot be unmarshalled in
|
||||||
XMLR string `xml:"xmlns:r,attr"`
|
XMLR string `xml:"xmlns:r,attr,omitempty"` // cannot be unmarshalled in
|
||||||
XMLWP string `xml:"xmlns:wp,attr"`
|
XMLWP string `xml:"xmlns:wp,attr,omitempty"` // cannot be unmarshalled in
|
||||||
XMLWP14 string `xml:"xmlns:wp14,attr"`
|
XMLWP14 string `xml:"xmlns:wp14,attr,omitempty"` // cannot be unmarshalled in
|
||||||
Body *Body
|
Body *Body
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (doc *Document) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||||
|
if doc.Body == nil {
|
||||||
|
doc.Body = &Body{
|
||||||
|
Paragraphs: make([]*Paragraph, 0, 64),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
t, err := d.Token()
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
switch tt := t.(type) {
|
||||||
|
case xml.StartElement:
|
||||||
|
switch tt.Name.Local {
|
||||||
|
case "p":
|
||||||
|
var value Paragraph
|
||||||
|
d.DecodeElement(&value, &start)
|
||||||
|
doc.Body.Paragraphs = append(doc.Body.Paragraphs, &value)
|
||||||
|
default:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
// Drawing element contains photos
|
// Drawing element contains photos
|
||||||
type Drawing struct {
|
type Drawing struct {
|
||||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main drawing,omitempty"`
|
XMLName xml.Name `xml:"w:drawing,omitempty"`
|
||||||
Inline *WPInline
|
Inline *WPInline
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,13 +42,17 @@ func (r *Drawing) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|||||||
|
|
||||||
// WPInline wp:inline
|
// WPInline wp:inline
|
||||||
type WPInline struct {
|
type WPInline struct {
|
||||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing inline,omitempty"`
|
XMLName xml.Name `xml:"wp:inline,omitempty"`
|
||||||
DistT string `xml:"distT,attr"`
|
DistT string `xml:"distT,attr"`
|
||||||
DistB string `xml:"distB,attr"`
|
DistB string `xml:"distB,attr"`
|
||||||
DistL string `xml:"distL,attr"`
|
DistL string `xml:"distL,attr"`
|
||||||
DistR string `xml:"distR,attr"`
|
DistR string `xml:"distR,attr"`
|
||||||
AnchorID string `xml:"wp14:anchorId,attr"`
|
AnchorID string `xml:"wp14:anchorId,attr"`
|
||||||
EditID string `xml:"wp14:editId,attr"`
|
EditID string `xml:"wp14:editId,attr"`
|
||||||
|
|
||||||
|
Extent *WPExtent
|
||||||
|
EffectExtent *WPEffectExtent
|
||||||
|
DocPr *WPDocPr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||||
@@ -61,8 +65,19 @@ func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|||||||
switch tt := t.(type) {
|
switch tt := t.(type) {
|
||||||
case xml.StartElement:
|
case xml.StartElement:
|
||||||
switch tt.Name.Local {
|
switch tt.Name.Local {
|
||||||
case "inline":
|
case "extent":
|
||||||
|
r.Extent.CX = getAtt(tt.Attr, "cx")
|
||||||
|
r.Extent.CY = getAtt(tt.Attr, "cy")
|
||||||
|
case "effectExtent":
|
||||||
|
r.EffectExtent.L = getAtt(tt.Attr, "l")
|
||||||
|
r.EffectExtent.T = getAtt(tt.Attr, "t")
|
||||||
|
r.EffectExtent.R = getAtt(tt.Attr, "r")
|
||||||
|
r.EffectExtent.B = getAtt(tt.Attr, "b")
|
||||||
|
case "docPr":
|
||||||
|
r.DocPr.ID = getAtt(tt.Attr, "id")
|
||||||
|
r.DocPr.Name = getAtt(tt.Attr, "name")
|
||||||
|
r.DocPr.Macro = getAtt(tt.Attr, "macro")
|
||||||
|
r.DocPr.Hidden = getAtt(tt.Attr, "hidden")
|
||||||
default:
|
default:
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -72,3 +87,40 @@ func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WPExtent represents the extent of a drawing in a Word document.
|
||||||
|
type WPExtent struct {
|
||||||
|
XMLName xml.Name `xml:"wp:extent,omitempty"`
|
||||||
|
CX string `xml:"cx,attr"`
|
||||||
|
CY string `xml:"cy,attr"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// WPEffectExtent represents the effect extent of a drawing in a Word document.
|
||||||
|
type WPEffectExtent struct {
|
||||||
|
XMLName xml.Name `xml:"wp:effectExtent,omitempty"`
|
||||||
|
L string `xml:"l,attr"`
|
||||||
|
T string `xml:"t,attr"`
|
||||||
|
R string `xml:"r,attr"`
|
||||||
|
B string `xml:"b,attr"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// WPDocPr represents the document properties of a drawing in a Word document.
|
||||||
|
type WPDocPr struct {
|
||||||
|
XMLName xml.Name `xml:"wp:docPr,omitempty"`
|
||||||
|
ID string `xml:"id,attr"`
|
||||||
|
Name string `xml:"name,attr,omitempty"`
|
||||||
|
Macro string `xml:"macro,attr,omitempty"`
|
||||||
|
Hidden string `xml:"hidden,attr,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AGraphic represents a graphic in a Word document.
|
||||||
|
type AGraphic struct {
|
||||||
|
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/drawingml/2006/main graphic,omitempty"`
|
||||||
|
GraphicData *AGraphicData
|
||||||
|
}
|
||||||
|
|
||||||
|
// AGraphicData represents the data of a graphic in a Word document.
|
||||||
|
type AGraphicData struct {
|
||||||
|
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/drawingml/2006/main graphicData,omitempty"`
|
||||||
|
URI string `xml:"uri,attr"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
|
|
||||||
// Hyperlink element contains links
|
// Hyperlink element contains links
|
||||||
type Hyperlink struct {
|
type Hyperlink struct {
|
||||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main hyperlink,omitempty"`
|
XMLName xml.Name `xml:"w:hyperlink,omitempty"`
|
||||||
ID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr"`
|
ID string `xml:"r:id,attr"`
|
||||||
Run Run
|
Run Run
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ParagraphChild struct {
|
type ParagraphChild struct {
|
||||||
Link *Hyperlink `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main hyperlink,omitempty"`
|
Link *Hyperlink `xml:"w:hyperlink,omitempty"`
|
||||||
Run *Run `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main r,omitempty"`
|
Run *Run `xml:"w:r,omitempty"`
|
||||||
Properties *RunProperties `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main rPr,omitempty"`
|
Properties *RunProperties `xml:"w:rPr,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Paragraph struct {
|
type Paragraph struct {
|
||||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main p"`
|
XMLName xml.Name `xml:"w:p,omitempty"`
|
||||||
Children []ParagraphChild // Children will generate an unnecessary tag <Children> ... </Children> and we skip it by a self-defined xml.Marshaler
|
Children []ParagraphChild // Children will generate an unnecessary tag <Children> ... </Children> and we skip it by a self-defined xml.Marshaler
|
||||||
|
|
||||||
file *Docx
|
file *Docx
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package docxlib
|
|||||||
import "encoding/xml"
|
import "encoding/xml"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
XMLNS = `http://schemas.openxmlformats.org/package/2006/relationships`
|
XMLNS_REL = `http://schemas.openxmlformats.org/package/2006/relationships`
|
||||||
REL_HYPERLINK = `http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink`
|
REL_HYPERLINK = `http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink`
|
||||||
|
|
||||||
REL_TARGETMODE = "External"
|
REL_TARGETMODE = "External"
|
||||||
|
|||||||
24
structrun.go
24
structrun.go
@@ -8,9 +8,9 @@ import (
|
|||||||
// Run is part of a paragraph that has its own style. It could be
|
// Run is part of a paragraph that has its own style. It could be
|
||||||
// a piece of text in bold, or a link
|
// a piece of text in bold, or a link
|
||||||
type Run struct {
|
type Run struct {
|
||||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main r,omitempty"`
|
XMLName xml.Name `xml:"w:r,omitempty"`
|
||||||
RunProperties *RunProperties `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main rPr,omitempty"`
|
RunProperties *RunProperties `xml:"w:rPr,omitempty"`
|
||||||
InstrText string `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main instrText,omitempty"`
|
InstrText string `xml:"w:instrText,omitempty"`
|
||||||
Text *Text
|
Text *Text
|
||||||
Drawing *Drawing
|
Drawing *Drawing
|
||||||
}
|
}
|
||||||
@@ -54,11 +54,11 @@ func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|||||||
|
|
||||||
// RunProperties encapsulates visual properties of a run
|
// RunProperties encapsulates visual properties of a run
|
||||||
type RunProperties struct {
|
type RunProperties struct {
|
||||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main rPr,omitempty"`
|
XMLName xml.Name `xml:"w:rPr,omitempty"`
|
||||||
Color *Color `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main color,omitempty"`
|
Color *Color `xml:"w:color,omitempty"`
|
||||||
Size *Size `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main sz,omitempty"`
|
Size *Size `xml:"w:sz,omitempty"`
|
||||||
RunStyle *RunStyle `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main rStyle,omitempty"`
|
RunStyle *RunStyle `xml:"w:rStyle,omitempty"`
|
||||||
Style *Style `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main pStyle,omitempty"`
|
Style *Style `xml:"w:pStyle,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||||
@@ -100,25 +100,25 @@ func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err
|
|||||||
|
|
||||||
// RunStyle contains styling for a run
|
// RunStyle contains styling for a run
|
||||||
type RunStyle struct {
|
type RunStyle struct {
|
||||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main rStyle,omitempty"`
|
XMLName xml.Name `xml:"w:rStyle,omitempty"`
|
||||||
Val string `xml:"w:val,attr"`
|
Val string `xml:"w:val,attr"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Style contains styling for a paragraph
|
// Style contains styling for a paragraph
|
||||||
type Style struct {
|
type Style struct {
|
||||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main pStyle,omitempty"`
|
XMLName xml.Name `xml:"w:pStyle,omitempty"`
|
||||||
Val string `xml:"w:val,attr"`
|
Val string `xml:"w:val,attr"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Color contains the sound of music. :D
|
// Color contains the sound of music. :D
|
||||||
// I'm kidding. It contains the color
|
// I'm kidding. It contains the color
|
||||||
type Color struct {
|
type Color struct {
|
||||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main color"`
|
XMLName xml.Name `xml:"w:color"`
|
||||||
Val string `xml:"w:val,attr"`
|
Val string `xml:"w:val,attr"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size contains the font size
|
// Size contains the font size
|
||||||
type Size struct {
|
type Size struct {
|
||||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main sz"`
|
XMLName xml.Name `xml:"w:sz"`
|
||||||
Val string `xml:"w:val,attr"`
|
Val string `xml:"w:val,attr"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
// Text object contains the actual text
|
// Text object contains the actual text
|
||||||
type Text struct {
|
type Text struct {
|
||||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main t"`
|
XMLName xml.Name `xml:"w:t"`
|
||||||
XMLSpace string `xml:"xml:space,attr,omitempty"`
|
XMLSpace string `xml:"xml:space,attr,omitempty"`
|
||||||
Text string `xml:",chardata"`
|
Text string `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user