mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-11 02:50:27 +08:00
refactor: Paragraph.Children to []interface{}
This commit is contained in:
@@ -35,16 +35,10 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElemen
|
||||
|
||||
}
|
||||
|
||||
type ParagraphChild struct {
|
||||
Link *Hyperlink `xml:"w:hyperlink,omitempty"`
|
||||
Run *Run `xml:"w:r,omitempty"`
|
||||
Properties *RunProperties `xml:"w:rPr,omitempty"`
|
||||
}
|
||||
|
||||
type Paragraph struct {
|
||||
XMLName xml.Name `xml:"w:p,omitempty"`
|
||||
Properties *ParagraphProperties
|
||||
Children []ParagraphChild // Children will generate an unnecessary tag <Children> ... </Children> and we skip it by a self-defined xml.Marshaler
|
||||
Children []interface{} // Children will generate an unnecessary tag <Children> ... </Children> and we skip it by a self-defined xml.Marshaler
|
||||
|
||||
file *Docx
|
||||
}
|
||||
@@ -52,10 +46,10 @@ type Paragraph struct {
|
||||
func (p *Paragraph) String() string {
|
||||
sb := strings.Builder{}
|
||||
for _, c := range p.Children {
|
||||
switch {
|
||||
case c.Link != nil:
|
||||
id := c.Link.ID
|
||||
text := c.Link.Run.InstrText
|
||||
switch o := c.(type) {
|
||||
case *Hyperlink:
|
||||
id := o.ID
|
||||
text := o.Run.InstrText
|
||||
link, err := p.file.ReferTarget(id)
|
||||
sb.WriteString(text)
|
||||
sb.WriteByte('(')
|
||||
@@ -65,9 +59,9 @@ func (p *Paragraph) String() string {
|
||||
sb.WriteString(link)
|
||||
}
|
||||
sb.WriteByte(')')
|
||||
case c.Run != nil:
|
||||
case *Run:
|
||||
sb.WriteString("run") //TODO: implement
|
||||
case c.Properties != nil:
|
||||
case *RunProperties:
|
||||
sb.WriteString("prop") //TODO: implement
|
||||
default:
|
||||
continue
|
||||
@@ -88,16 +82,7 @@ func (p *Paragraph) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||
}
|
||||
}
|
||||
for _, c := range p.Children {
|
||||
switch {
|
||||
case c.Link != nil:
|
||||
err = e.Encode(c.Link)
|
||||
case c.Run != nil:
|
||||
err = e.Encode(c.Run)
|
||||
case c.Properties != nil:
|
||||
err = e.Encode(c.Properties)
|
||||
default:
|
||||
continue
|
||||
}
|
||||
e.Encode(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -106,7 +91,7 @@ func (p *Paragraph) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||
}
|
||||
|
||||
func (p *Paragraph) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
children := make([]ParagraphChild, 0, 64)
|
||||
children := make([]interface{}, 0, 64)
|
||||
for {
|
||||
t, err := d.Token()
|
||||
if err == io.EOF {
|
||||
@@ -117,7 +102,7 @@ func (p *Paragraph) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
}
|
||||
switch tt := t.(type) {
|
||||
case xml.StartElement:
|
||||
var elem ParagraphChild
|
||||
var elem interface{}
|
||||
switch tt.Name.Local {
|
||||
case "hyperlink":
|
||||
var value Hyperlink
|
||||
@@ -130,15 +115,15 @@ func (p *Paragraph) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
if anchor != "" {
|
||||
value.ID = anchor
|
||||
}
|
||||
elem.Link = &value
|
||||
elem = &value
|
||||
case "r":
|
||||
var value Run
|
||||
d.DecodeElement(&value, &tt)
|
||||
elem.Run = &value
|
||||
elem = &value
|
||||
case "rPr":
|
||||
var value RunProperties
|
||||
d.DecodeElement(&value, &tt)
|
||||
elem.Properties = &value
|
||||
elem = &value
|
||||
case "pPr":
|
||||
var value ParagraphProperties
|
||||
d.DecodeElement(&value, &tt)
|
||||
|
||||
Reference in New Issue
Block a user