mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-11 02:50:27 +08:00
New way of marshalling
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
docxlib
|
docxlib
|
||||||
|
.vscode/
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func TestStructure(t *testing.T) {
|
|||||||
decoded_doc)
|
decoded_doc)
|
||||||
}
|
}
|
||||||
if len(doc.Body.Paragraphs) != NUM_PARAGRAPHS {
|
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))
|
NUM_PARAGRAPHS, len(doc.Body.Paragraphs))
|
||||||
}
|
}
|
||||||
for _, p := range 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",
|
t.Errorf("We were not able to parse paragraph %v",
|
||||||
p)
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package docxlib
|
package docxlib
|
||||||
|
|
||||||
import "encoding/xml"
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
type ParagraphChild struct {
|
type ParagraphChild struct {
|
||||||
Link *Hyperlink `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main hyperlink,omitempty"`
|
Link *Hyperlink `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main hyperlink,omitempty"`
|
||||||
@@ -14,3 +17,37 @@ type Paragraph struct {
|
|||||||
|
|
||||||
file *DocxLib
|
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
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user