From 9cb29541daab8f72ec25679ac013e3f36d334186 Mon Sep 17 00:00:00 2001 From: Gonzalo Fernandez-Victorio Date: Fri, 30 Apr 2021 13:51:52 +0100 Subject: [PATCH] Fix links --- apipara.go | 10 ++-------- apirun.go | 2 +- docxlib.go | 13 +++++++++++++ main/main.go | 17 +++++++++++++++-- structrun.go | 2 +- unpack.go | 2 +- 6 files changed, 33 insertions(+), 13 deletions(-) diff --git a/apipara.go b/apipara.go index 93cf807..6eff353 100644 --- a/apipara.go +++ b/apipara.go @@ -15,12 +15,6 @@ func (f *DocxLib) Paragraphs() []*Paragraph { return f.Document.Body.Paragraphs } -func (p *Paragraph) Runs() (ret []*Run) { - data := p.Data - for _, d := range data { - if d.Run != nil { - ret = append(ret, d.Run) - } - } - return +func (p *Paragraph) Children() (ret []ParagraphChild) { + return p.Data } diff --git a/apirun.go b/apirun.go index 0fc9c02..25a9110 100644 --- a/apirun.go +++ b/apirun.go @@ -17,7 +17,7 @@ func (r *Run) Size(size int) *Run { return r } -// AddText add text to paragraph +// AddText adds text to paragraph func (p *Paragraph) AddText(text string) *Run { t := &Text{ Text: text, diff --git a/docxlib.go b/docxlib.go index 6b749c6..2ed58d0 100644 --- a/docxlib.go +++ b/docxlib.go @@ -2,6 +2,7 @@ package docxlib import ( "archive/zip" + "errors" "io" ) @@ -37,3 +38,15 @@ func (f *DocxLib) Write(writer io.Writer) (err error) { return f.pack(zipWriter) } + +// References gets the url for a reference +func (f *DocxLib) References(id string) (href string, err error) { + for _, a := range f.DocRelation.Relationships { + if a.ID == id { + href = a.Target + return + } + } + err = errors.New("id not found") + return +} diff --git a/main/main.go b/main/main.go index 2dac108..5609c61 100644 --- a/main/main.go +++ b/main/main.go @@ -48,8 +48,21 @@ func main() { panic(err) } for _, para := range doc.Paragraphs() { - for _, run := range para.Runs() { - fmt.Printf("\tWe've found a new run with the text ->%s\n", run.Text.Text) + for _, child := range para.Children() { + if child.Run != nil { + fmt.Printf("\tWe've found a new run with the text ->%s\n", child.Run.Text.Text) + } + if child.Link != nil { + id := child.Link.ID + text := child.Link.Run.InstrText + link, err := doc.References(id) + if err != nil { + fmt.Printf("\tWe found a link with id %s and text %s without target\n", id, text) + } else { + fmt.Printf("\tWe've found a new hyperlink with ref %s and the text %s\n", link, text) + } + + } } } fmt.Println("End of main") diff --git a/structrun.go b/structrun.go index 55fbb8b..f145165 100644 --- a/structrun.go +++ b/structrun.go @@ -11,7 +11,7 @@ const ( type Run struct { XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main r"` RunProperties *RunProperties `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main rPr,omitempty"` - InstrText string `xml:"w:instrText,omitempty"` + InstrText string `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main instrText,omitempty"` Text *Text } diff --git a/unpack.go b/unpack.go index 945360f..b97a641 100644 --- a/unpack.go +++ b/unpack.go @@ -63,7 +63,7 @@ func processRelations(file *zip.File) (*Relationships, error) { fmt.Println("Error reading from internal zip file") return nil, err } - rels := Relationships{Xmlns: "none"} + rels := Relationships{Xmlns: XMLNS_R} err = xml.Unmarshal(filebytes, &rels) if err != nil { fmt.Println("Error unmarshalling relationships")