mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-06 00:00:24 +08:00
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package docxlib
|
|
|
|
import (
|
|
"strconv"
|
|
"sync/atomic"
|
|
)
|
|
|
|
// when adding an hyperlink we need to store a reference in the relationship field
|
|
//
|
|
// this func is not thread-safe
|
|
func (f *Docx) addLinkRelation(link string) string {
|
|
rel := &Relationship{
|
|
ID: "rId" + strconv.Itoa(int(atomic.AddUintptr(&f.rId, 1))),
|
|
Type: REL_HYPERLINK,
|
|
Target: link,
|
|
TargetMode: REL_TARGETMODE,
|
|
}
|
|
|
|
f.DocRelation.Relationships = append(f.DocRelation.Relationships, rel)
|
|
|
|
return rel.ID
|
|
}
|
|
|
|
// when adding an image we need to store a reference in the relationship field
|
|
//
|
|
// this func is not thread-safe
|
|
func (f *Docx) addImageRelation(m Media) string {
|
|
rel := &Relationship{
|
|
ID: "rId" + strconv.Itoa(int(atomic.AddUintptr(&f.rId, 1))),
|
|
Type: REL_IMAGE,
|
|
Target: "media/" + m.Name,
|
|
}
|
|
|
|
f.DocRelation.Relationships = append(f.DocRelation.Relationships, rel)
|
|
|
|
return rel.ID
|
|
}
|
|
|
|
// ReferHref gets the url for a reference
|
|
func (f *Docx) ReferHref(id string) (href string, err error) {
|
|
f.DocRelation.mu.RLock()
|
|
defer f.DocRelation.mu.RUnlock()
|
|
for _, a := range f.DocRelation.Relationships {
|
|
if a.ID == id {
|
|
href = a.Target
|
|
return
|
|
}
|
|
}
|
|
err = ErrRefIDNotFound
|
|
return
|
|
}
|