Skip to content

Commit

Permalink
paragraph style function and preserve space fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gomutex committed Feb 8, 2024
1 parent f35ee90 commit 990a831
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
8 changes: 6 additions & 2 deletions oxml/elements/paragraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ func DefaultParagraphChild() *ParagraphChild {
return &ParagraphChild{}
}

func (p *Paragraph) GetType() string {
return "p"
func (p *Paragraph) Style(value string) *Paragraph {
if p.Property == nil {
p.Property = DefaultParaProperty()
}
p.Property.Style = NewParagraphStyle(value)
return p
}

func (p *Paragraph) Numbering(id int, level int) {
Expand Down
5 changes: 5 additions & 0 deletions oxml/elements/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ func (r *Run) Underline(value UnderlineStyle) *Run {
return r
}

func (r *Run) Style(value string) *Run {
r.RunProperty.Style = NewRunStyle(value)
return r
}

func (r *Run) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
start.Name.Local = "w:r"

Expand Down
21 changes: 18 additions & 3 deletions oxml/elements/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ import (
)

type Text struct {
text string
text string
preserveSpace bool
}

func NewText() *Text {
return &Text{}
return &Text{
preserveSpace: true,
}
}

func TextFromString(text string) *Text {
return &Text{text: text}
return &Text{text: text, preserveSpace: true}
}

func (t *Text) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
elem := xml.StartElement{Name: xml.Name{Local: "w:t"}}

if t.preserveSpace {
elem.Attr = append(elem.Attr, xml.Attr{Name: xml.Name{Local: "xml:space"}, Value: "preserve"})
}

if err = e.EncodeElement(t.text, elem); err != nil {
return err
}
Expand All @@ -29,6 +37,13 @@ func (t *Text) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
func (t *Text) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
var buf bytes.Buffer

for _, attr := range start.Attr {
if attr.Name.Local == "space" && attr.Value == "preserve" {
t.preserveSpace = true
break
}
}

for {
token, err := d.Token()
if err != nil {
Expand Down

0 comments on commit 990a831

Please sign in to comment.