unioffice/document/paragraph.go

86 lines
2.0 KiB
Go

// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting sales@baliance.com.
package document
import (
wml "baliance.com/gooxml/schema/schemas.openxmlformats.org/wordprocessingml"
)
// Paragraph is a paragraph within a document.
type Paragraph struct {
d *Document
x *wml.CT_P
}
// X returns the inner wrapped XML type.
func (p Paragraph) X() *wml.CT_P {
return p.x
}
func (p Paragraph) ensurePPr() {
if p.x.PPr == nil {
p.x.PPr = wml.NewCT_PPr()
}
}
// ParagraphProperties returnst the paragraph properties.
func (p Paragraph) Properties() ParagraphProperties {
p.ensurePPr()
return ParagraphProperties{p.d, p.x.PPr}
}
// Style returns the style for a paragraph, or an empty string if it is unset.
func (p Paragraph) Style() string {
if p.x.PPr != nil && p.x.PPr.PStyle != nil {
return p.x.PPr.PStyle.ValAttr
}
return ""
}
// SetStyle sets the style of a paragraph.
func (p Paragraph) SetStyle(s string) {
p.ensurePPr()
if s == "" {
p.x.PPr.PStyle = nil
} else {
p.x.PPr.PStyle = wml.NewCT_String()
p.x.PPr.PStyle.ValAttr = s
}
}
// AddRun adds a run to a paragraph.
func (p Paragraph) AddRun() Run {
var pc *wml.EG_PContent
// no need to add a new EG_PContent if we already have one
if len(p.x.EG_PContent) > 0 {
pc = p.x.EG_PContent[len(p.x.EG_PContent)-1]
} else {
pc = wml.NewEG_PContent()
p.x.EG_PContent = append(p.x.EG_PContent, pc)
}
rc := wml.NewEG_ContentRunContent()
pc.EG_ContentRunContent = append(pc.EG_ContentRunContent, rc)
r := wml.NewCT_R()
rc.R = r
return Run{p.d, r}
}
// Runs returns all of the runs in a paragraph.
func (p Paragraph) Runs() []Run {
ret := []Run{}
for _, c := range p.x.EG_PContent {
for _, rc := range c.EG_ContentRunContent {
if rc.R != nil {
ret = append(ret, Run{p.d, rc.R})
}
}
}
return ret
}