mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-12 19:58:36 +08:00
add more attrs
This commit is contained in:
@@ -38,6 +38,7 @@ func main() {
|
|||||||
clean := flag.Bool("c", false, "clean mode (keep text and picture only)")
|
clean := flag.Bool("c", false, "clean mode (keep text and picture only)")
|
||||||
unm := flag.Bool("u", false, "lease unmarshalled file")
|
unm := flag.Bool("u", false, "lease unmarshalled file")
|
||||||
splitre := flag.String("s", "", "split file into many docxs by matching regex")
|
splitre := flag.String("s", "", "split file into many docxs by matching regex")
|
||||||
|
droppp := flag.Bool("p", false, "drop all paragraph properties")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
var w *docx.Docx
|
var w *docx.Docx
|
||||||
if !*analyzeOnly {
|
if !*analyzeOnly {
|
||||||
@@ -180,6 +181,22 @@ func main() {
|
|||||||
if *clean {
|
if *clean {
|
||||||
doc.Document.Body.DropDrawingOf("NilPicture")
|
doc.Document.Body.DropDrawingOf("NilPicture")
|
||||||
}
|
}
|
||||||
|
if *droppp {
|
||||||
|
for _, it := range doc.Document.Body.Items {
|
||||||
|
switch o := it.(type) {
|
||||||
|
case *docx.Paragraph: // printable
|
||||||
|
o.Properties = nil
|
||||||
|
case *docx.Table: // printable
|
||||||
|
for _, tr := range o.TableRows {
|
||||||
|
for _, tc := range tr.TableCells {
|
||||||
|
for _, p := range tc.Paragraphs {
|
||||||
|
p.Properties = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if *unm {
|
if *unm {
|
||||||
i := strings.LastIndex(*fileLocation, "/")
|
i := strings.LastIndex(*fileLocation, "/")
|
||||||
name := (*fileLocation)[:i+1] + "unmarshal_" + (*fileLocation)[i+1:]
|
name := (*fileLocation)[:i+1] + "unmarshal_" + (*fileLocation)[i+1:]
|
||||||
|
|||||||
@@ -276,14 +276,33 @@ func (r *NonVisualProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElemen
|
|||||||
type Spacing struct {
|
type Spacing struct {
|
||||||
XMLName xml.Name `xml:"w:spacing,omitempty"`
|
XMLName xml.Name `xml:"w:spacing,omitempty"`
|
||||||
|
|
||||||
Line int `xml:"w:line,attr"`
|
Val int `xml:"w:val,attr,omitempty"`
|
||||||
LineRule string `xml:"w:lineRule,attr"`
|
|
||||||
|
BeforeLines int `xml:"w:beforeLines,attr,omitempty"`
|
||||||
|
Before int `xml:"w:before,attr,omitempty"`
|
||||||
|
Line int `xml:"w:line,attr,omitempty"`
|
||||||
|
LineRule string `xml:"w:lineRule,attr,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalXML ...
|
// UnmarshalXML ...
|
||||||
func (s *Spacing) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
|
func (s *Spacing) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
|
||||||
for _, attr := range start.Attr {
|
for _, attr := range start.Attr {
|
||||||
switch attr.Name.Local {
|
switch attr.Name.Local {
|
||||||
|
case "val":
|
||||||
|
s.Val, err = strconv.Atoi(attr.Value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case "beforeLines":
|
||||||
|
s.BeforeLines, err = strconv.Atoi(attr.Value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case "before":
|
||||||
|
s.Before, err = strconv.Atoi(attr.Value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
case "line":
|
case "line":
|
||||||
s.Line, err = strconv.Atoi(attr.Value)
|
s.Line, err = strconv.Atoi(attr.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -304,14 +323,34 @@ func (s *Spacing) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err erro
|
|||||||
type Ind struct {
|
type Ind struct {
|
||||||
XMLName xml.Name `xml:"w:ind,omitempty"`
|
XMLName xml.Name `xml:"w:ind,omitempty"`
|
||||||
|
|
||||||
FirstLineChars int `xml:"w:firstLineChars,attr"`
|
LeftChars int `xml:"w:leftChars,attr,omitempty"`
|
||||||
FirstLine int `xml:"w:firstLine,attr"`
|
Left int `xml:"w:left,attr,omitempty"`
|
||||||
|
FirstLineChars int `xml:"w:firstLineChars,attr,omitempty"`
|
||||||
|
FirstLine int `xml:"w:firstLine,attr,omitempty"`
|
||||||
|
HangingChars int `xml:"w:hangingChars,attr,omitempty"`
|
||||||
|
Hanging int `xml:"w:hanging,attr,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalXML ...
|
// UnmarshalXML ...
|
||||||
func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
|
func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
|
||||||
for _, attr := range start.Attr {
|
for _, attr := range start.Attr {
|
||||||
switch attr.Name.Local {
|
switch attr.Name.Local {
|
||||||
|
case "leftChars":
|
||||||
|
if attr.Value == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
i.LeftChars, err = strconv.Atoi(attr.Value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case "left":
|
||||||
|
if attr.Value == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
i.Left, err = strconv.Atoi(attr.Value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
case "firstLineChars":
|
case "firstLineChars":
|
||||||
if attr.Value == "" {
|
if attr.Value == "" {
|
||||||
continue
|
continue
|
||||||
@@ -328,6 +367,22 @@ func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
case "hangingChars":
|
||||||
|
if attr.Value == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
i.HangingChars, err = strconv.Atoi(attr.Value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case "hanging":
|
||||||
|
if attr.Value == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
i.Hanging, err = strconv.Atoi(attr.Value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
// ignore other attributes
|
// ignore other attributes
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import (
|
|||||||
// ParagraphProperties <w:pPr>
|
// ParagraphProperties <w:pPr>
|
||||||
type ParagraphProperties struct {
|
type ParagraphProperties struct {
|
||||||
XMLName xml.Name `xml:"w:pPr,omitempty"`
|
XMLName xml.Name `xml:"w:pPr,omitempty"`
|
||||||
|
Tabs *Tabs
|
||||||
Spacing *Spacing
|
Spacing *Spacing
|
||||||
Ind *Ind
|
Ind *Ind
|
||||||
Justification *Justification
|
Justification *Justification
|
||||||
@@ -58,6 +59,13 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElemen
|
|||||||
}
|
}
|
||||||
if tt, ok := t.(xml.StartElement); ok {
|
if tt, ok := t.(xml.StartElement); ok {
|
||||||
switch tt.Name.Local {
|
switch tt.Name.Local {
|
||||||
|
case "tabs":
|
||||||
|
var value Tabs
|
||||||
|
err = d.DecodeElement(&value, &tt)
|
||||||
|
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
p.Tabs = &value
|
||||||
case "spacing":
|
case "spacing":
|
||||||
var value Spacing
|
var value Spacing
|
||||||
err = d.DecodeElement(&value, &tt)
|
err = d.DecodeElement(&value, &tt)
|
||||||
|
|||||||
@@ -205,6 +205,7 @@ type RunProperties struct {
|
|||||||
Color *Color
|
Color *Color
|
||||||
Size *Size
|
Size *Size
|
||||||
SizeCs *SizeCs
|
SizeCs *SizeCs
|
||||||
|
Spacing *Spacing
|
||||||
RunStyle *RunStyle
|
RunStyle *RunStyle
|
||||||
Style *Style
|
Style *Style
|
||||||
Shade *Shade
|
Shade *Shade
|
||||||
@@ -255,6 +256,13 @@ func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err
|
|||||||
var value Size
|
var value Size
|
||||||
value.Val = getAtt(tt.Attr, "val")
|
value.Val = getAtt(tt.Attr, "val")
|
||||||
r.Size = &value
|
r.Size = &value
|
||||||
|
case "spacing":
|
||||||
|
var value Spacing
|
||||||
|
err = d.DecodeElement(&value, &tt)
|
||||||
|
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
r.Spacing = &value
|
||||||
case "szCs":
|
case "szCs":
|
||||||
var value SizeCs
|
var value SizeCs
|
||||||
value.Val = getAtt(tt.Attr, "val")
|
value.Val = getAtt(tt.Attr, "val")
|
||||||
|
|||||||
@@ -773,9 +773,9 @@ func (w *WTableBorders) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err
|
|||||||
|
|
||||||
// WTableBorder is a structure representing a single border of a Word table.
|
// WTableBorder is a structure representing a single border of a Word table.
|
||||||
type WTableBorder struct {
|
type WTableBorder struct {
|
||||||
Val string `xml:"w:val,attr"`
|
Val string `xml:"w:val,attr,omitempty"`
|
||||||
Size int `xml:"w:sz,attr"`
|
Size int `xml:"w:sz,attr,omitempty"`
|
||||||
Space int `xml:"w:space,attr"`
|
Space int `xml:"w:space,attr,omitempty"`
|
||||||
Color string `xml:"w:color,attr,omitempty"`
|
Color string `xml:"w:color,attr,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,11 +24,69 @@ import (
|
|||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"io"
|
"io"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Tabs ...
|
||||||
|
type Tabs struct {
|
||||||
|
XMLName xml.Name `xml:"w:tabs,omitempty"`
|
||||||
|
Tabs []*Tab
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalXML ...
|
||||||
|
func (tb *Tabs) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||||
|
for {
|
||||||
|
t, err := d.Token()
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if tt, ok := t.(xml.StartElement); ok {
|
||||||
|
if tt.Name.Local == "tab" {
|
||||||
|
var value Tab
|
||||||
|
err := d.DecodeElement(&value, &tt)
|
||||||
|
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tb.Tabs = append(tb.Tabs, &value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Tab is the literal tab
|
// Tab is the literal tab
|
||||||
type Tab struct {
|
type Tab struct {
|
||||||
XMLName xml.Name `xml:"w:tab,omitempty"`
|
XMLName xml.Name `xml:"w:tab,omitempty"`
|
||||||
|
Val string `xml:"w:val,attr,omitempty"`
|
||||||
|
Position int `xml:"w:pos,attr,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalXML ...
|
||||||
|
func (t *Tab) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||||
|
var err error
|
||||||
|
for _, attr := range start.Attr {
|
||||||
|
switch attr.Name.Local {
|
||||||
|
case "val":
|
||||||
|
t.Val = attr.Value
|
||||||
|
case "pos":
|
||||||
|
if attr.Value == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
t.Position, err = strconv.Atoi(attr.Value)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Consume the end element
|
||||||
|
_, err = d.Token()
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// BarterRabbet is <br>
|
// BarterRabbet is <br>
|
||||||
|
|||||||
Reference in New Issue
Block a user