From b9c91642503ddfdbe4bbfb4a5ac9d0dab14eb70d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Thu, 23 Feb 2023 15:22:56 +0800 Subject: [PATCH] make lint happy --- apidrawing.go | 10 +++++----- docxlib.go | 9 +++++---- empty.go | 4 ++-- fs.go | 3 ++- link.go | 20 ++++++++++---------- pack.go | 6 +++--- structdoc.go | 4 ++++ structdrawing.go | 27 +++++++++++++++++++++++---- structlink.go | 1 + structpara.go | 4 ++++ structrel.go | 2 ++ structrun.go | 4 +++- structtext.go | 12 +++++++----- unpack.go | 8 +++++--- 14 files changed, 76 insertions(+), 38 deletions(-) diff --git a/apidrawing.go b/apidrawing.go index 45ba8e8..d14bbf2 100644 --- a/apidrawing.go +++ b/apidrawing.go @@ -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 { diff --git a/docxlib.go b/docxlib.go index 44d74cb..94e44d3 100644 --- a/docxlib.go +++ b/docxlib.go @@ -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 diff --git a/empty.go b/empty.go index eaa511b..0b2f6ff 100644 --- a/empty.go +++ b/empty.go @@ -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", diff --git a/fs.go b/fs.go index beccd49..5a3d528 100644 --- a/fs.go +++ b/fs.go @@ -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 ) diff --git a/link.go b/link.go index 6e37be7..4981747 100644 --- a/link.go +++ b/link.go @@ -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 } diff --git a/pack.go b/pack.go index 0699933..856263e 100644 --- a/pack.go +++ b/pack.go @@ -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 { diff --git a/structdoc.go b/structdoc.go index 3f617d4..47e75fb 100644 --- a/structdoc.go +++ b/structdoc.go @@ -25,6 +25,7 @@ func getAtt(atts []xml.Attr, name string) string { return "" } +// 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 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() diff --git a/structdrawing.go b/structdrawing.go index 0f03030..d04850e 100644 --- a/structdrawing.go +++ b/structdrawing.go @@ -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 { diff --git a/structlink.go b/structlink.go index 3aba9a0..2267eaf 100644 --- a/structlink.go +++ b/structlink.go @@ -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() diff --git a/structpara.go b/structpara.go index c4495b3..3c99e61 100644 --- a/structpara.go +++ b/structpara.go @@ -6,11 +6,13 @@ import ( "strings" ) +// ParagraphProperties 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 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 { diff --git a/structrel.go b/structrel.go index aff043f..db3752e 100644 --- a/structrel.go +++ b/structrel.go @@ -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"` diff --git a/structrun.go b/structrun.go index f3a997c..b9a96b7 100644 --- a/structrun.go +++ b/structrun.go @@ -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() diff --git a/structtext.go b/structtext.go index 87be9fd..15a69bb 100644 --- a/structtext.go +++ b/structtext.go @@ -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 } diff --git a/unpack.go b/unpack.go index 35cc7ae..fe01c1c 100644 --- a/unpack.go +++ b/unpack.go @@ -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 {