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

refactor: Run use Children

This commit is contained in:
源文雨
2023-02-25 21:18:29 +08:00
parent e32b907c6a
commit fafd4c9fc3
14 changed files with 101 additions and 129 deletions

View File

@@ -20,17 +20,20 @@
package docxlib
import "encoding/xml"
import "strings"
// AddTab adds tab to para
func (p *Paragraph) AddTab() *Run {
c := make([]interface{}, 1, 64)
c[0] = &WTab{}
run := &Run{
RunProperties: &RunProperties{},
FrontTab: []struct {
XMLName xml.Name "xml:\"w:tab,omitempty\""
}{{}},
Children: c,
}
p.Children = append(p.Children, run)
return run
}
@@ -40,13 +43,22 @@ func (p *Paragraph) AddText(text string) *Run {
return p.AddTab()
}
t := &Text{
Text: text,
c := make([]interface{}, 0, 64)
for i, s := range strings.Split(text, "\t") {
if i > 0 {
c = append(c, &WTab{})
}
if s != "" {
c = append(c, &Text{
Text: s,
})
}
}
run := &Run{
Text: t,
RunProperties: &RunProperties{},
Children: c,
}
p.Children = append(p.Children, run)