diff --git a/.gitignore b/.gitignore index ce4461a..70f0a4b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ docxlib +.vscode/ diff --git a/structdoc_test.go b/structdoc_test.go index 13a684e..58c4e0a 100644 --- a/structdoc_test.go +++ b/structdoc_test.go @@ -19,7 +19,7 @@ func TestStructure(t *testing.T) { decoded_doc) } if len(doc.Body.Paragraphs) != NUM_PARAGRAPHS { - t.Errorf("We expected %d paragraph, we got %d", + t.Errorf("We expected %d paragraphs, we got %d", NUM_PARAGRAPHS, len(doc.Body.Paragraphs)) } for _, p := range doc.Body.Paragraphs { @@ -27,5 +27,10 @@ func TestStructure(t *testing.T) { t.Errorf("We were not able to parse paragraph %v", p) } + for _, child := range p.Children() { + if child.Link == nil && child.Properties == nil && child.Run == nil { + t.Errorf("There are children with all fields nil") + } + } } } diff --git a/structnodes.go b/structnodes.go index d9c9ebd..72d7368 100644 --- a/structnodes.go +++ b/structnodes.go @@ -1,6 +1,9 @@ package docxlib -import "encoding/xml" +import ( + "encoding/xml" + "io" +) type ParagraphChild struct { Link *Hyperlink `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main hyperlink,omitempty"` @@ -14,3 +17,37 @@ type Paragraph struct { file *DocxLib } + +func (p *Paragraph) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + children := make([]ParagraphChild, 0) + for { + t, err := d.Token() + if err == io.EOF { + break + } + switch tt := t.(type) { + case xml.StartElement: + var elem ParagraphChild + if tt.Name.Local == "hyperlink" { + var value Hyperlink + d.DecodeElement(&value, &start) + elem = ParagraphChild{Link: &value} + } else if tt.Name.Local == "r" { + var value Run + d.DecodeElement(&value, &start) + elem = ParagraphChild{Run: &value} + } else if tt.Name.Local == "rPr" { + var value RunProperties + d.DecodeElement(&value, &start) + elem = ParagraphChild{Properties: &value} + } else { + continue + } + children = append(children, elem) + } + + } + *p = Paragraph{Data: children} + return nil + +}