1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-06 08:10:25 +08:00

make lint happy

This commit is contained in:
源文雨
2023-02-23 15:22:56 +08:00
parent 958a125885
commit b9c9164250
14 changed files with 76 additions and 38 deletions

View File

@@ -15,9 +15,9 @@ func (p *Paragraph) AddInlineDrawing(pic []byte) (*Run, error) {
if err != nil {
return nil, err
}
idn := int(atomic.AddUintptr(&p.file.imageId, 1))
idn := int(atomic.AddUintptr(&p.file.imageID, 1))
id := strconv.Itoa(idn)
rId := p.file.addImage(Media{Name: "image" + id + "." + format, Data: pic})
rid := p.file.addImage(Media{Name: "image" + id + "." + format, Data: pic})
w, h := int64(sz.Width), int64(sz.Height)
if float64(w)/float64(h) > 1.2 {
h = A4_EMU_MAX_WIDTH * h / w
@@ -59,7 +59,7 @@ func (p *Paragraph) AddInlineDrawing(pic []byte) (*Run, error) {
},
BlipFill: &PICBlipFill{
Blip: ABlip{
Embed: rId,
Embed: rid,
Cstate: "print",
},
},
@@ -114,7 +114,7 @@ func (p *Paragraph) AddAnchorDrawing(pic []byte) (*Run, error) {
if err != nil {
return nil, err
}
idn := int(atomic.AddUintptr(&p.file.imageId, 1))
idn := int(atomic.AddUintptr(&p.file.imageID, 1))
id := strconv.Itoa(idn)
rId := p.file.addImage(Media{Name: "image" + id + "." + format, Data: pic})
w, h := int64(sz.Width), int64(sz.Height)
@@ -195,7 +195,7 @@ func (p *Paragraph) AddAnchorDrawing(pic []byte) (*Run, error) {
return run, nil
}
// AddInlineDrawingFrom adds drawing from file to paragraph
// AddAnchorDrawingFrom adds drawing from file to paragraph
func (p *Paragraph) AddAnchorDrawingFrom(file string) (*Run, error) {
data, err := os.ReadFile(file)
if err != nil {

View File

@@ -12,14 +12,15 @@ import (
// Docx is the structure that allow to access the internal represntation
// in memory of the doc (either read or about to be written)
type Docx struct {
Document Document
DocRelation Relationships
Document Document // Document is word/document.xml
docRelation Relationships // docRelation is word/_rels/document.xml.rels
media []Media
mediaNameIdx map[string]int
rId uintptr
imageId uintptr
rID uintptr
imageID uintptr
template string
tmplfs fs.FS

View File

@@ -19,7 +19,7 @@ func newEmptyA4File() *Docx {
Paragraphs: make([]Paragraph, 0, 64),
},
},
DocRelation: Relationships{
docRelation: Relationships{
Xmlns: XMLNS_REL,
Relationship: []Relationship{
{
@@ -51,7 +51,7 @@ func newEmptyA4File() *Docx {
},
media: make([]Media, 0, 64),
mediaNameIdx: make(map[string]int, 64),
rId: 5,
rID: 5,
template: "a4",
tmpfslst: []string{
"_rels/.rels",

3
fs.go
View File

@@ -3,7 +3,8 @@ package docxlib
import "embed"
var (
// TemplateXMLFS stores template docx files
//go:embed xml
//go:embed xml/a4/_rels/*
TEMP_XML_FS embed.FS
TemplateXMLFS embed.FS
)

20
link.go
View File

@@ -18,13 +18,13 @@ var (
// 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))),
ID: "rId" + strconv.Itoa(int(atomic.AddUintptr(&f.rID, 1))),
Type: REL_HYPERLINK,
Target: link,
TargetMode: REL_TARGETMODE,
}
f.DocRelation.Relationship = append(f.DocRelation.Relationship, rel)
f.docRelation.Relationship = append(f.docRelation.Relationship, rel)
return rel.ID
}
@@ -34,21 +34,21 @@ func (f *Docx) addLinkRelation(link string) string {
// 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))),
ID: "rId" + strconv.Itoa(int(atomic.AddUintptr(&f.rID, 1))),
Type: REL_IMAGE,
Target: "media/" + m.Name,
}
f.DocRelation.Relationship = append(f.DocRelation.Relationship, rel)
f.docRelation.Relationship = append(f.docRelation.Relationship, rel)
return rel.ID
}
// ReferTarget gets the target for a reference
func (f *Docx) ReferTarget(id string) (string, error) {
f.DocRelation.mu.RLock()
defer f.DocRelation.mu.RUnlock()
for _, a := range f.DocRelation.Relationship {
f.docRelation.mu.RLock()
defer f.docRelation.mu.RUnlock()
for _, a := range f.docRelation.Relationship {
if a.ID == id {
return a.Target, nil
}
@@ -58,9 +58,9 @@ func (f *Docx) ReferTarget(id string) (string, error) {
// ReferID gets the rId from target
func (f *Docx) ReferID(target string) (string, error) {
f.DocRelation.mu.RLock()
defer f.DocRelation.mu.RUnlock()
for _, a := range f.DocRelation.Relationship {
f.docRelation.mu.RLock()
defer f.docRelation.mu.RUnlock()
for _, a := range f.docRelation.Relationship {
if a.Target == target {
return a.ID, nil
}

View File

@@ -8,7 +8,7 @@ import (
"os"
)
// This receives a zip file writer (word documents are a zip with multiple xml inside)
// pack receives a zip file writer (word documents are a zip with multiple xml inside)
// and writes the relevant files. Some of them come from the empty_constants file,
// others from the actual in-memory structure
func (f *Docx) pack(zipWriter *zip.Writer) (err error) {
@@ -16,7 +16,7 @@ func (f *Docx) pack(zipWriter *zip.Writer) (err error) {
if f.template != "" {
for _, name := range f.tmpfslst {
files[name], err = TEMP_XML_FS.Open("xml/" + f.template + "/" + name)
files[name], err = TemplateXMLFS.Open("xml/" + f.template + "/" + name)
if err != nil {
return
}
@@ -30,7 +30,7 @@ func (f *Docx) pack(zipWriter *zip.Writer) (err error) {
}
}
files["word/_rels/document.xml.rels"] = marshaller{data: &f.DocRelation}
files["word/_rels/document.xml.rels"] = marshaller{data: &f.docRelation}
files["word/document.xml"] = marshaller{data: &f.Document}
for _, m := range f.media {

View File

@@ -25,6 +25,7 @@ func getAtt(atts []xml.Attr, name string) string {
return ""
}
// Body <w:body>
type Body struct {
mu sync.Mutex
Paragraphs []Paragraph `xml:"w:p,omitempty"`
@@ -32,6 +33,7 @@ type Body struct {
file *Docx
}
// UnmarshalXML ...
func (b *Body) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
@@ -68,6 +70,7 @@ func (b *Body) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
}
// Document <w:document>
type Document struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/wordprocessingml/2006/main document"`
XMLW string `xml:"xmlns:w,attr"` // cannot be unmarshalled in
@@ -78,6 +81,7 @@ type Document struct {
Body Body `xml:"w:body"`
}
// UnmarshalXML ...
func (doc *Document) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()

View File

@@ -24,6 +24,7 @@ type Drawing struct {
Anchor *WPAnchor
}
// UnmarshalXML ...
func (r *Drawing) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
@@ -75,6 +76,7 @@ type WPInline struct {
Graphic *AGraphic
}
// UnmarshalXML ...
func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
for _, attr := range start.Attr {
switch attr.Name.Local {
@@ -180,6 +182,7 @@ type WPExtent struct {
CY int64 `xml:"cy,attr"`
}
// UnmarshalXML ...
func (r *WPExtent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error
for _, attr := range start.Attr {
@@ -213,6 +216,7 @@ type WPEffectExtent struct {
B int64 `xml:"b,attr"`
}
// UnmarshalXML ...
func (r *WPEffectExtent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error
for _, attr := range start.Attr {
@@ -254,6 +258,7 @@ type WPDocPr struct {
Name string `xml:"name,attr,omitempty"`
}
// UnmarshalXML ...
func (r *WPDocPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
switch attr.Name.Local {
@@ -284,6 +289,7 @@ type WPCNvGraphicFramePr struct {
Locks *AGraphicFrameLocks
}
// UnmarshalXML ...
func (w *WPCNvGraphicFramePr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
@@ -329,6 +335,7 @@ type AGraphic struct {
GraphicData *AGraphicData
}
// UnmarshalXML ...
func (a *AGraphic) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
switch attr.Name.Local {
@@ -373,6 +380,7 @@ type AGraphicData struct {
Pic *PICPic
}
// UnmarshalXML ...
func (a *AGraphicData) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
@@ -411,6 +419,7 @@ type PICPic struct {
SpPr *PICSpPr
}
// UnmarshalXML ...
func (p *PICPic) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
@@ -460,6 +469,7 @@ type PICNonVisualPicProperties struct {
CNvPicPr PicCNvPicPr
}
// UnmarshalXML ...
func (p *PICNonVisualPicProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
@@ -495,6 +505,7 @@ type PicCNvPicPr struct {
Locks *APicLocks
}
// UnmarshalXML ...
func (p *PicCNvPicPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
// Loop through XML tokens
for {
@@ -544,6 +555,7 @@ type PICBlipFill struct {
Stretch AStretch
}
// UnmarshalXML ...
func (p *PICBlipFill) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
@@ -583,6 +595,7 @@ type ABlip struct {
AlphaModFix *AAlphaModFix
}
// UnmarshalXML ...
func (a *ABlip) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
switch attr.Name.Local {
@@ -645,6 +658,7 @@ type PICSpPr struct {
PrstGeom APrstGeom
}
// UnmarshalXML ...
func (p *PICSpPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
@@ -688,6 +702,7 @@ type AXfrm struct {
Ext AExt
}
// UnmarshalXML ...
func (a *AXfrm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
for _, attr := range start.Attr {
switch attr.Name.Local {
@@ -779,6 +794,7 @@ type AAvLst struct {
RawXML string `xml:",innerxml"`
}
// UnmarshalXML ...
func (a *AAvLst) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
var content []byte
@@ -838,6 +854,7 @@ type WPAnchor struct {
Graphic *AGraphic
}
// UnmarshalXML ...
func (r *WPAnchor) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
for _, tt := range start.Attr {
switch tt.Name.Local {
@@ -985,11 +1002,12 @@ type WPPositionH struct {
PosOffset int64 `xml:"wp:posOffset"`
}
// UnmarshalXML ...
func (r *WPPositionH) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "relativeFrom":
if attr.Name.Local == "relativeFrom" {
r.RelativeFrom = attr.Value
break
}
}
for {
@@ -1023,11 +1041,12 @@ type WPPositionV struct {
PosOffset int64 `xml:"wp:posOffset"`
}
// UnmarshalXML ...
func (r *WPPositionV) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "relativeFrom":
if attr.Name.Local == "relativeFrom" {
r.RelativeFrom = attr.Value
break
}
}
for {

View File

@@ -13,6 +13,7 @@ type Hyperlink struct {
Run Run
}
// UnmarshalXML ...
func (r *Hyperlink) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()

View File

@@ -6,11 +6,13 @@ import (
"strings"
)
// ParagraphProperties <w:pPr>
type ParagraphProperties struct {
XMLName xml.Name `xml:"w:pPr,omitempty"`
Justification *Justification `xml:"w:jc,omitempty"`
}
// UnmarshalXML ...
func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
@@ -34,6 +36,7 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElemen
}
// Paragraph <w:p>
type Paragraph struct {
// XMLName xml.Name `xml:"w:p,omitempty"`
Properties *ParagraphProperties
@@ -89,6 +92,7 @@ func (p *Paragraph) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeToken(start.End())
}
// UnmarshalXML ...
func (p *Paragraph) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
children := make([]interface{}, 0, 64)
for {

View File

@@ -12,12 +12,14 @@ const (
REL_TARGETMODE = "External"
)
// Relationships ...
type Relationships struct {
mu sync.RWMutex
Xmlns string `xml:"xmlns,attr"`
Relationship []Relationship
}
// Relationship ...
type Relationship struct {
ID string `xml:"Id,attr"`
Type string `xml:"Type,attr"`

View File

@@ -11,7 +11,7 @@ import (
type Run struct {
XMLName xml.Name `xml:"w:r,omitempty"`
RunProperties *RunProperties `xml:"w:rPr,omitempty"`
FrontTab []struct { // TODO: replace with variable []RunChild
FrontTab []struct { //TODO: replace with variable []RunChild
XMLName xml.Name `xml:"w:tab,omitempty"`
}
InstrText string `xml:"w:instrText,omitempty"`
@@ -22,6 +22,7 @@ type Run struct {
}
}
// UnmarshalXML ...
func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
@@ -97,6 +98,7 @@ type RunProperties struct {
Style *Style `xml:"w:pStyle,omitempty"`
}
// UnmarshalXML ...
func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()

View File

@@ -7,11 +7,14 @@ import (
// Text object contains the actual text
type Text struct {
XMLName xml.Name `xml:"w:t,omitempty"`
XMLSpace string `xml:"xml:space,attr,omitempty"`
Text string `xml:",chardata"`
XMLName xml.Name `xml:"w:t,omitempty"`
// XMLSpace string `xml:"xml:space,attr,omitempty"`
Text string `xml:",chardata"`
}
// UnmarshalXML ...
func (r *Text) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
t, err := d.Token()
@@ -22,8 +25,7 @@ func (r *Text) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
return err
}
switch tt := t.(type) {
case xml.CharData:
if tt, ok := t.(xml.CharData); ok {
r.Text = string(tt) // implicitly copy
}

View File

@@ -76,12 +76,14 @@ func (f *Docx) parseDocRelation(file *zip.File) error {
}
defer zf.Close()
f.DocRelation.Xmlns = XMLNS_R
//TODO: find last rId & imageId
return xml.NewDecoder(zf).Decode(&f.DocRelation)
f.docRelation.Xmlns = XMLNS_R
//TODO: find last rID
return xml.NewDecoder(zf).Decode(&f.docRelation)
}
// parseMedia add the media into Docx struct
func (f *Docx) parseMedia(file *zip.File) error {
//TODO: find last imageID
name := file.Name[len(MEDIA_FOLDER):]
zf, err := file.Open()
if err != nil {