diff --git a/main/main.go b/main/main.go index 5609c61..f129a9c 100644 --- a/main/main.go +++ b/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) } diff --git a/structdoc_test.go b/structdoc_test.go index 519f8ea..7b1baa2 100644 --- a/structdoc_test.go +++ b/structdoc_test.go @@ -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") + } } } } diff --git a/structnodes.go b/structnodes.go index 72d7368..ad138a7 100644 --- a/structnodes.go +++ b/structnodes.go @@ -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 diff --git a/structrun.go b/structrun.go index e692934..b861440 100644 --- a/structrun.go +++ b/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 "" +}