mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-08 09:10:24 +08:00
Keep unmarshalling
This commit is contained in:
13
main/main.go
13
main/main.go
@@ -1,16 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/gonfva/docxlib"
|
||||
)
|
||||
|
||||
const FILE_PATH = "/tmp/new-file.docx"
|
||||
var fileLocation *string
|
||||
|
||||
func init() {
|
||||
fileLocation = flag.String("file", "/tmp/new-file.docx", "file location")
|
||||
flag.Parse()
|
||||
}
|
||||
func main() {
|
||||
fmt.Printf("Preparing new document to write at %s\n", FILE_PATH)
|
||||
fmt.Printf("Preparing new document to write at %s\n", *fileLocation)
|
||||
|
||||
w := docxlib.New()
|
||||
// add new paragraph
|
||||
@@ -26,7 +31,7 @@ func main() {
|
||||
nextPara := w.AddParagraph()
|
||||
nextPara.AddLink("google", `http://google.com`)
|
||||
|
||||
f, err := os.Create(FILE_PATH)
|
||||
f, err := os.Create(*fileLocation)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -34,7 +39,7 @@ func main() {
|
||||
w.Write(f)
|
||||
fmt.Println("Document writen. \nNow trying to read it")
|
||||
// Now let's try to read the file
|
||||
readFile, err := os.Open(FILE_PATH)
|
||||
readFile, err := os.Open(*fileLocation)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -29,11 +29,17 @@ func TestStructure(t *testing.T) {
|
||||
}
|
||||
for _, child := range p.Children() {
|
||||
if child.Link == nil && child.Properties == nil && child.Run == nil {
|
||||
t.Errorf("There are children with all fields nil")
|
||||
t.Errorf("There are Paragraph children with all fields nil")
|
||||
}
|
||||
if child.Run != nil && child.Run.Text == nil {
|
||||
t.Errorf("We have a run with no text")
|
||||
}
|
||||
if child.Run != nil && child.Run.Text != nil && child.Run.Text.Text == "" {
|
||||
t.Errorf("We have a text with no text")
|
||||
}
|
||||
if child.Link != nil && child.Link.ID == "" {
|
||||
t.Errorf("We have a link without ID")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ func (p *Paragraph) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
if tt.Name.Local == "hyperlink" {
|
||||
var value Hyperlink
|
||||
d.DecodeElement(&value, &start)
|
||||
value.ID = getAtt(tt.Attr, "id")
|
||||
elem = ParagraphChild{Link: &value}
|
||||
} else if tt.Name.Local == "r" {
|
||||
var value Run
|
||||
|
||||
76
structrun.go
76
structrun.go
@@ -75,10 +75,6 @@ func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
}
|
||||
|
||||
switch tt := t.(type) {
|
||||
case xml.CharData:
|
||||
var value string
|
||||
d.DecodeElement(&value, &start)
|
||||
elem.Text = &Text{Text: value}
|
||||
case xml.StartElement:
|
||||
if tt.Name.Local == "rPr" {
|
||||
var value RunProperties
|
||||
@@ -88,6 +84,10 @@ func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
var value string
|
||||
d.DecodeElement(&value, &start)
|
||||
elem.InstrText = value
|
||||
} else if tt.Name.Local == "t" {
|
||||
var value Text
|
||||
d.DecodeElement(&value, &start)
|
||||
elem.Text = &value
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
@@ -98,3 +98,71 @@ func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
return nil
|
||||
|
||||
}
|
||||
func (r *Text) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
var elem Text
|
||||
for {
|
||||
t, err := d.Token()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
|
||||
switch tt := t.(type) {
|
||||
case xml.CharData:
|
||||
cd := tt.Copy()
|
||||
elem.Text = string(cd)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
*r = elem
|
||||
return nil
|
||||
}
|
||||
func (r *Hyperlink) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
var elem Hyperlink
|
||||
for {
|
||||
t, err := d.Token()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
|
||||
switch tt := t.(type) {
|
||||
case xml.StartElement:
|
||||
if tt.Name.Local == "r" {
|
||||
d.DecodeElement(&elem.Run, &start)
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
*r = elem
|
||||
return nil
|
||||
|
||||
}
|
||||
func (r *RunStyle) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
var elem RunStyle
|
||||
for {
|
||||
t, err := d.Token()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
|
||||
switch tt := t.(type) {
|
||||
case xml.StartElement:
|
||||
elem.Val = getAtt(tt.Attr, "val")
|
||||
}
|
||||
|
||||
}
|
||||
*r = elem
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func getAtt(atts []xml.Attr, name string) string {
|
||||
for _, at := range atts {
|
||||
if at.Name.Local == name {
|
||||
return at.Value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user