1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-13 04:13:15 +08:00

feat: add support for number properties in paragraph unmarshaling (#50)

Co-authored-by: zhihhwang <zhihhwang@tencent.com>
Co-authored-by: 源文雨 <41315874+fumiama@users.noreply.github.com>
This commit is contained in:
sanbai
2025-05-06 16:50:32 +08:00
committed by GitHub
parent c6b0eac317
commit 0c30fd0930

View File

@@ -24,6 +24,7 @@ import (
"encoding/xml" "encoding/xml"
"io" "io"
"reflect" "reflect"
"strconv"
"strings" "strings"
) )
@@ -109,6 +110,13 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e
p.RunProperties = &value p.RunProperties = &value
case "pStyle": case "pStyle":
p.Style = &Style{Val: getAtt(tt.Attr, "val")} p.Style = &Style{Val: getAtt(tt.Attr, "val")}
case "numPr":
var value NumProperties
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
p.NumProperties = &value
case "textAlignment": case "textAlignment":
p.TextAlignment = &TextAlignment{Val: getAtt(tt.Attr, "val")} p.TextAlignment = &TextAlignment{Val: getAtt(tt.Attr, "val")}
case "adjustRightInd": case "adjustRightInd":
@@ -155,6 +163,7 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e
return err return err
} }
p.OverflowPunct = &value p.OverflowPunct = &value
default: default:
err = d.Skip() // skip unsupported tags err = d.Skip() // skip unsupported tags
if err != nil { if err != nil {
@@ -184,6 +193,12 @@ type Paragraph struct {
func (p *Paragraph) String() string { func (p *Paragraph) String() string {
sb := strings.Builder{} sb := strings.Builder{}
if p.Properties != nil && p.Properties.NumProperties != nil && p.Properties.NumProperties.Ilvl != nil {
indent, err := strconv.Atoi(p.Properties.NumProperties.Ilvl.Val)
if err == nil {
sb.WriteString(strings.Repeat(" ", indent*2))
}
}
for _, c := range p.Children { for _, c := range p.Children {
switch o := c.(type) { switch o := c.(type) {
case *Hyperlink: case *Hyperlink: