mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-22 11:30:36 +08:00
Fix links
This commit is contained in:
10
apipara.go
10
apipara.go
@@ -15,12 +15,6 @@ func (f *DocxLib) Paragraphs() []*Paragraph {
|
|||||||
return f.Document.Body.Paragraphs
|
return f.Document.Body.Paragraphs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Paragraph) Runs() (ret []*Run) {
|
func (p *Paragraph) Children() (ret []ParagraphChild) {
|
||||||
data := p.Data
|
return p.Data
|
||||||
for _, d := range data {
|
|
||||||
if d.Run != nil {
|
|
||||||
ret = append(ret, d.Run)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ func (r *Run) Size(size int) *Run {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddText add text to paragraph
|
// AddText adds text to paragraph
|
||||||
func (p *Paragraph) AddText(text string) *Run {
|
func (p *Paragraph) AddText(text string) *Run {
|
||||||
t := &Text{
|
t := &Text{
|
||||||
Text: text,
|
Text: text,
|
||||||
|
|||||||
13
docxlib.go
13
docxlib.go
@@ -2,6 +2,7 @@ package docxlib
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,3 +38,15 @@ func (f *DocxLib) Write(writer io.Writer) (err error) {
|
|||||||
|
|
||||||
return f.pack(zipWriter)
|
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
|
||||||
|
}
|
||||||
|
|||||||
17
main/main.go
17
main/main.go
@@ -48,8 +48,21 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
for _, para := range doc.Paragraphs() {
|
for _, para := range doc.Paragraphs() {
|
||||||
for _, run := range para.Runs() {
|
for _, child := range para.Children() {
|
||||||
fmt.Printf("\tWe've found a new run with the text ->%s\n", run.Text.Text)
|
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")
|
fmt.Println("End of main")
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ const (
|
|||||||
type Run struct {
|
type Run struct {
|
||||||
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main r"`
|
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main r"`
|
||||||
RunProperties *RunProperties `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main rPr,omitempty"`
|
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
|
Text *Text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ func processRelations(file *zip.File) (*Relationships, error) {
|
|||||||
fmt.Println("Error reading from internal zip file")
|
fmt.Println("Error reading from internal zip file")
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
rels := Relationships{Xmlns: "none"}
|
rels := Relationships{Xmlns: XMLNS_R}
|
||||||
err = xml.Unmarshal(filebytes, &rels)
|
err = xml.Unmarshal(filebytes, &rels)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error unmarshalling relationships")
|
fmt.Println("Error unmarshalling relationships")
|
||||||
|
|||||||
Reference in New Issue
Block a user