1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-05 07:40:24 +08:00

add more attrs

This commit is contained in:
源文雨
2023-03-06 15:35:46 +08:00
parent e3d6e419d5
commit 32cb2064cd
6 changed files with 136 additions and 13 deletions

View File

@@ -70,6 +70,22 @@ func (c *WordprocessingCanvas) UnmarshalXML(d *xml.Decoder, start xml.StartEleme
return err
}
c.Items = append(c.Items, &value)
case "pic":
var value Picture
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
value.XMLPIC = getAtt(tt.Attr, "pic")
c.Items = append(c.Items, &value)
case "wgp":
var value WordprocessingGroup
value.file = c.file
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
c.Items = append(c.Items, &value)
default:
err = d.Skip() // skip unsupported tags
if err != nil {

View File

@@ -271,3 +271,68 @@ func (r *NonVisualProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElemen
_, err = d.Token()
return
}
// Spacing ...
type Spacing struct {
XMLName xml.Name `xml:"w:spacing,omitempty"`
Line int `xml:"w:line,attr"`
LineRule string `xml:"w:lineRule,attr"`
}
// UnmarshalXML ...
func (s *Spacing) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "line":
s.Line, err = strconv.Atoi(attr.Value)
if err != nil {
return
}
case "lineRule":
s.LineRule = attr.Value
default:
// ignore other attributes
}
}
// Consume the end element
_, err = d.Token()
return
}
// Ind ...
type Ind struct {
XMLName xml.Name `xml:"w:ind,omitempty"`
FirstLineChars int `xml:"w:firstLineChars,attr"`
FirstLine int `xml:"w:firstLine,attr"`
}
// UnmarshalXML ...
func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "firstLineChars":
if attr.Value == "" {
continue
}
i.FirstLineChars, err = strconv.Atoi(attr.Value)
if err != nil {
return
}
case "firstLine":
if attr.Value == "" {
continue
}
i.FirstLine, err = strconv.Atoi(attr.Value)
if err != nil {
return
}
default:
// ignore other attributes
}
}
// Consume the end element
_, err = d.Token()
return
}

View File

@@ -32,6 +32,8 @@ import (
// ParagraphProperties <w:pPr>
type ParagraphProperties struct {
XMLName xml.Name `xml:"w:pPr,omitempty"`
Spacing *Spacing
Ind *Ind
Justification *Justification
Shade *Shade
Kern *Kern
@@ -57,6 +59,20 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElemen
}
if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "spacing":
var value Spacing
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
p.Spacing = &value
case "ind":
var value Ind
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
p.Ind = &value
case "jc":
p.Justification = &Justification{Val: getAtt(tt.Attr, "val")}
case "shd":
@@ -183,6 +199,8 @@ func (p *Paragraph) String() string {
sb.WriteString(x.Text)
case *Tab:
sb.WriteByte('\t')
case *BarterRabbet:
sb.WriteByte('\n')
case *Drawing:
if x.Inline != nil && x.Inline.Graphic != nil && x.Inline.Graphic.GraphicData != nil {
if x.Inline.Graphic.GraphicData.Pic != nil {

View File

@@ -31,6 +31,8 @@ import (
// 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"`
@@ -46,6 +48,10 @@ type Run struct {
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:
@@ -110,6 +116,8 @@ func (r *Run) parse(d *xml.Decoder, tt xml.StartElement) (child interface{}, err
child = &value
case "tab":
child = &Tab{}
case "br":
child = &BarterRabbet{}
case "AlternateContent":
/*var value AlternateContent
value.file = r.file
@@ -174,7 +182,6 @@ type RunProperties struct {
Bold *Bold
ICs *struct{} `xml:"w:iCs,omitempty"`
Italic *Italic
Underline *Underline
Highlight *Highlight
Color *Color
Size *Size
@@ -183,6 +190,7 @@ type RunProperties struct {
Style *Style
Shade *Shade
Kern *Kern
Underline *Underline
VertAlign *VertAlign
}
@@ -277,10 +285,11 @@ func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err
// 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"`
HAnsi string `xml:"w:hAnsi,attr,omitempty"`
Hint string `xml:"w:hint,attr,omitempty"`
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 ...
@@ -289,6 +298,8 @@ func (f *RunFonts) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
switch attr.Name.Local {
case "ascii":
f.ASCII = attr.Value
case "eastAsia":
f.EastAsia = attr.Value
case "hAnsi":
f.HAnsi = attr.Value
case "hint":

View File

@@ -621,16 +621,16 @@ func (c *WTextBoxContent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) e
// WPSBodyPr represents the body properties for a WordprocessingML DrawingML shape.
type WPSBodyPr struct {
XMLName xml.Name `xml:"wps:bodyPr,omitempty"`
Rot int `xml:"rot,attr,omitempty"`
Rot int `xml:"rot,attr"`
Vert string `xml:"vert,attr,omitempty"`
Wrap string `xml:"wrap,attr,omitempty"`
LIns int64 `xml:"lIns,attr,omitempty"`
TIns int64 `xml:"tIns,attr,omitempty"`
RIns int64 `xml:"rIns,attr,omitempty"`
BIns int64 `xml:"bIns,attr,omitempty"`
LIns int64 `xml:"lIns,attr"`
TIns int64 `xml:"tIns,attr"`
RIns int64 `xml:"rIns,attr"`
BIns int64 `xml:"bIns,attr"`
Anchor string `xml:"anchor,attr,omitempty"`
AnchorCtr int `xml:"anchorCtr,attr,omitempty"`
Upright int `xml:"upright,attr,omitempty"`
AnchorCtr int `xml:"anchorCtr,attr"`
Upright int `xml:"upright,attr"`
NoAutofit *struct{} `xml:"a:noAutofit,omitempty"`
}

View File

@@ -30,17 +30,30 @@ type Tab struct {
XMLName xml.Name `xml:"w:tab,omitempty"`
}
// BarterRabbet is <br>
type BarterRabbet struct {
XMLName xml.Name `xml:"w:br,omitempty"`
}
// Text object contains the actual text
type Text struct {
XMLName xml.Name `xml:"w:t,omitempty"`
// XMLSpace string `xml:"xml:space,attr,omitempty"`
XMLSpace string `xml:"xml:space,attr,omitempty"`
Text string `xml:",chardata"`
}
// UnmarshalXML ...
func (r *Text) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "space":
r.XMLSpace = attr.Value
default:
// ignore other attributes
}
}
for {
t, err := d.Token()
if err == io.EOF {