mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-23 20:16:38 +08:00
🎨 改进代码样式 (#23)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
d74423910a
commit
843de6e909
170
structsect.go
170
structsect.go
@@ -1,85 +1,85 @@
|
|||||||
/*
|
/*
|
||||||
Copyright (c) 2024 mabiao0525 (马飚)
|
Copyright (c) 2024 mabiao0525 (马飚)
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU Affero General Public License as published
|
it under the terms of the GNU Affero General Public License as published
|
||||||
by the Free Software Foundation, either version 3 of the License, or
|
by the Free Software Foundation, either version 3 of the License, or
|
||||||
(at your option) any later version.
|
(at your option) any later version.
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
GNU Affero General Public License for more details.
|
GNU Affero General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU Affero General Public License
|
You should have received a copy of the GNU Affero General Public License
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package docx
|
package docx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SectPr show the properties of the document, like paper size
|
// SectPr show the properties of the document, like paper size
|
||||||
type SectPr struct {
|
type SectPr struct {
|
||||||
XMLName xml.Name `xml:"w:sectPr,omitempty"` // properties of the document, including paper size
|
XMLName xml.Name `xml:"w:sectPr,omitempty"` // properties of the document, including paper size
|
||||||
PgSz *PgSz `xml:"w:pgSz,omitempty"`
|
PgSz *PgSz `xml:"w:pgSz,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// PgSz show the paper size
|
// PgSz show the paper size
|
||||||
type PgSz struct {
|
type PgSz struct {
|
||||||
W xml.Attr `xml:"w:w,attr"` // width of paper
|
W xml.Attr `xml:"w:w,attr"` // width of paper
|
||||||
H xml.Attr `xml:"w:h,attr"` // high of paper
|
H xml.Attr `xml:"w:h,attr"` // high of paper
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalXML ...
|
// UnmarshalXML ...
|
||||||
func (sect *SectPr) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
|
func (sect *SectPr) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
|
||||||
for {
|
for {
|
||||||
t, err := d.Token()
|
t, err := d.Token()
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if tt, ok := t.(xml.StartElement); ok {
|
if tt, ok := t.(xml.StartElement); ok {
|
||||||
switch tt.Name.Local {
|
switch tt.Name.Local {
|
||||||
case "pgSz":
|
case "pgSz":
|
||||||
var value PgSz
|
var value PgSz
|
||||||
err = d.DecodeElement(&value, &tt)
|
err = d.DecodeElement(&value, &tt)
|
||||||
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
|
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
sect.PgSz = &value
|
sect.PgSz = &value
|
||||||
default:
|
default:
|
||||||
err = d.Skip() // skip unsupported tags
|
err = d.Skip() // skip unsupported tags
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalXML ...
|
// UnmarshalXML ...
|
||||||
func (pgsz *PgSz) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
func (pgsz *PgSz) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
for _, attr := range start.Attr {
|
for _, attr := range start.Attr {
|
||||||
switch attr.Name.Local {
|
switch attr.Name.Local {
|
||||||
case "w":
|
case "w":
|
||||||
pgsz.W = xml.Attr{Name: xml.Name{Local: "w:w"}, Value: attr.Value}
|
pgsz.W = xml.Attr{Name: xml.Name{Local: "w:w"}, Value: attr.Value}
|
||||||
case "h":
|
case "h":
|
||||||
pgsz.H = xml.Attr{Name: xml.Name{Local: "w:w"}, Value: attr.Value}
|
pgsz.H = xml.Attr{Name: xml.Name{Local: "w:w"}, Value: attr.Value}
|
||||||
default:
|
default:
|
||||||
//ignore other attributes now
|
//ignore other attributes now
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Consume the end element
|
// Consume the end element
|
||||||
_, err = d.Token()
|
_, err = d.Token()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user