Skip to content

Commit 57dfdbe

Browse files
authored
Merge pull request #34 from gomutex/comments-update
doc update and moved API funcs of Run to docx
2 parents 9a0aaf6 + ecd8b34 commit 57dfdbe

18 files changed

Lines changed: 421 additions & 248 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737

3838
func main() {
3939
// Open an existing DOCX document
40-
// docx, err := godocx.OpenDocument("./testdata/test.docx")
40+
// document, err := godocx.OpenDocument("./testdata/test.docx")
4141

4242
// Create New Document
4343
document, err := godocx.NewDocument()

dml/dmlct/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Package dmlct provides complex types related to DrawingML (Drawing Markup Language),
2+
// which is part of the Office Open XML (OOXML) standard. DrawingML is used to represent
3+
// graphics and drawings in WordprocessingML, SpreadsheetML, and PresentationML documents.
4+
package dmlct

dml/dmlst/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Package dml provides simple types used in DrawingML (Drawing Markup Language),
2+
// part of the Office Open XML (OOXML) standard for representing graphical elements
3+
// in documents.
4+
package dmlst

dml/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Package dml provides functionality for working with DrawingML (Drawing Markup Language)
2+
// which is part of the Office Open XML (OOXML) standard. DrawingML is used to represent
3+
// graphics and drawings in WordprocessingML, SpreadsheetML, and PresentationML documents.
4+
package dml

docx/body.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// This element specifies the contents of the body of the document – the main document editing surface.
1010
type Body struct {
11-
Root *RootDoc
11+
root *RootDoc
1212
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main body"`
1313
Children []DocumentChild
1414
SectPr *ctypes.SectionProp
@@ -23,7 +23,7 @@ type DocumentChild struct {
2323
// Use this function to initialize a new Body before adding content to it.
2424
func NewBody(root *RootDoc) *Body {
2525
return &Body{
26-
Root: root,
26+
root: root,
2727
}
2828
}
2929

@@ -40,13 +40,13 @@ func (b Body) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
4040
if b.Children != nil {
4141
for _, child := range b.Children {
4242
if child.Para != nil {
43-
if err = child.Para.CT.MarshalXML(e, xml.StartElement{}); err != nil {
43+
if err = child.Para.ct.MarshalXML(e, xml.StartElement{}); err != nil {
4444
return err
4545
}
4646
}
4747

4848
if child.Table != nil {
49-
if err = child.Table.CT.MarshalXML(e, xml.StartElement{}); err != nil {
49+
if err = child.Table.ct.MarshalXML(e, xml.StartElement{}); err != nil {
5050
return err
5151
}
5252
}
@@ -76,14 +76,14 @@ func (body *Body) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err erro
7676
case xml.StartElement:
7777
switch elem.Name.Local {
7878
case "p":
79-
para := NewParagraph(body.Root)
80-
if err := d.DecodeElement(&para.CT, &elem); err != nil {
79+
para := newParagraph(body.root)
80+
if err := d.DecodeElement(&para.ct, &elem); err != nil {
8181
return err
8282
}
8383
body.Children = append(body.Children, DocumentChild{Para: para})
8484
case "tbl":
85-
tbl := NewTable(body.Root)
86-
if err := d.DecodeElement(&tbl.CT, &elem); err != nil {
85+
tbl := NewTable(body.root)
86+
if err := d.DecodeElement(&tbl.ct, &elem); err != nil {
8787
return err
8888
}
8989
body.Children = append(body.Children, DocumentChild{Table: tbl})

docx/core.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// Package oxml provides utilities for working with Office Open XML (OOXML) documents,
2-
// including functions related to encoding and decoding XML elements.
31
package docx
42

53
import (

docx/doc.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Package docx provides a comprehensive set of functions and structures
2+
// for manipulating DOCX documents. It allows for the creation, modification,
3+
// and retrieval of document elements such as paragraphs, styles, and images.
4+
// The package is designed to be accessed through the RootDoc element or
5+
// instances of inner elements, providing a flexible and intuitive API for
6+
// working with Office Open XML (OOXML) documents.
7+
//
8+
// // The RootDoc structure is initialized from the main godocx package,
9+
// which provides methods for creating a new document from a default template or
10+
// opening an existing document.
11+
package docx

docx/document.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,15 @@ func (d *Document) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) (e
110110

111111
}
112112

113-
// Return newly Paragraph object containing only a page break.
113+
// AddPageBreak adds a page break to the document by inserting a paragraph containing only a page break.
114+
//
115+
// Returns:
116+
// - *Paragraph: A pointer to the newly created Paragraph object containing the page break.
117+
//
118+
// Example:
119+
//
120+
// document := godocx.NewDocument()
121+
// para := document.AddPageBreak()
114122
func (rd *RootDoc) AddPageBreak() *Paragraph {
115123
p := rd.AddEmptyParagraph()
116124
p.AddRun().AddBreak(internal.ToPtr(stypes.BreakTypePage))

docx/heading.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ func (rd *RootDoc) AddHeading(text string, level uint) (*Paragraph, error) {
1717
return nil, errors.New("Heading level not supported")
1818
}
1919

20-
p := NewParagraph(rd)
21-
p.CT.Property = ctypes.DefaultParaProperty()
20+
p := newParagraph(rd)
21+
p.ct.Property = ctypes.DefaultParaProperty()
2222

2323
style := "Title"
2424
if level != 0 {
2525
style = fmt.Sprintf("Heading%d", level)
2626
}
2727

28-
p.CT.Property.Style = ctypes.NewParagraphStyle(style)
28+
p.ct.Property.Style = ctypes.NewParagraphStyle(style)
2929

3030
bodyElem := DocumentChild{
3131
Para: p,

docx/link.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import (
66
"github.com/gomutex/godocx/common/constants"
77
)
88

9+
// addLinkRelation adds a hyperlink relationship to the document's relationships collection.
10+
//
11+
// Parameters:
12+
// - link: A string representing the target URL or location of the hyperlink.
13+
//
14+
// Returns:
15+
// - string: The ID ("rId" + relation ID) of the added relationship.
16+
//
17+
// This function generates a new relationship ID, creates a Relationship object with the specified link as the target,
18+
// and appends it to the document's relationships collection (DocRels.Relationships). It returns the generated ID of the relationship.
919
func (doc *Document) addLinkRelation(link string) string {
1020

1121
rID := doc.IncRelationID()
@@ -22,6 +32,17 @@ func (doc *Document) addLinkRelation(link string) string {
2232
return "rId" + strconv.Itoa(rID)
2333
}
2434

35+
// addRelation adds a generic relationship to the document's relationships collection.
36+
//
37+
// Parameters:
38+
// - relType: A string representing the type of relationship (e.g., constants.SourceRelationshipImage).
39+
// - fileName: A string representing the target file name or location related to the relationship.
40+
//
41+
// Returns:
42+
// - string: The ID ("rId" + relation ID) of the added relationship.
43+
//
44+
// This function generates a new relationship ID, creates a Relationship object with the specified type and target,
45+
// and appends it to the document's relationships collection (DocRels.Relationships). It returns the generated ID of the relationship.
2546
func (doc *Document) addRelation(relType string, fileName string) string {
2647
rID := doc.IncRelationID()
2748
rel := &Relationship{

0 commit comments

Comments
 (0)