From 65da4dc8f893347f01780349685bbff7f3d37ea7 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, 9 Mar 2023 22:42:16 +0800 Subject: [PATCH] fix: para.copymedia --- cmd/main/main.go | 21 +++++++++++++++++++++ structdoc.go | 6 +++--- structpara.go | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/cmd/main/main.go b/cmd/main/main.go index 10b040a..bea06f1 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -25,6 +25,8 @@ import ( "flag" "fmt" "os" + "regexp" + "strconv" "strings" "github.com/fumiama/go-docx" @@ -35,6 +37,7 @@ func main() { analyzeOnly := flag.Bool("a", false, "analyze file only") clean := flag.Bool("c", false, "clean mode (keep text and picture only)") unm := flag.Bool("u", false, "lease unmarshalled file") + splitre := flag.String("s", "", "split file into many docxs by matching regex") flag.Parse() var w *docx.Docx if !*analyzeOnly { @@ -202,5 +205,23 @@ func main() { fmt.Println(o.String()) } } + if *splitre != "" { + i := strings.LastIndex(*fileLocation, "/") + for j, splitteddoc := range doc.SplitByParagraph(docx.SplitDocxByPlainTextRegex(regexp.MustCompile(*splitre))) { + name := (*fileLocation)[:i+1] + "unmarshal_split" + strconv.Itoa(j) + "_" + (*fileLocation)[i+1:] + f, err := os.Create(name) + if err != nil { + panic(err) + } + _, err = splitteddoc.WriteTo(f) + if err != nil { + panic(err) + } + err = f.Close() + if err != nil { + panic(err) + } + } + } fmt.Println("End of main") } diff --git a/structdoc.go b/structdoc.go index 7da7491..15f46bd 100644 --- a/structdoc.go +++ b/structdoc.go @@ -26,7 +26,6 @@ import ( "reflect" "regexp" "strings" - "unsafe" ) //nolint:revive,stylecheck @@ -127,7 +126,7 @@ func (b *Body) DropDrawingOf(name string) { switch o := item.(type) { case *Paragraph: f := reflect.ValueOf(o).MethodByName("Drop" + name) - if *(*uintptr)(unsafe.Pointer(&f)) == 0 { + if !f.IsValid() { continue } _ = f.Call(nil) @@ -136,7 +135,7 @@ func (b *Body) DropDrawingOf(name string) { for _, tc := range tr.TableCells { for _, p := range tc.Paragraphs { f := reflect.ValueOf(p).MethodByName("Drop" + name) - if *(*uintptr)(unsafe.Pointer(&f)) == 0 { + if !f.IsValid() { continue } _ = f.Call(nil) @@ -301,6 +300,7 @@ func (p *Paragraph) copymedia(to *Docx) (np Paragraph) { } nr.Children = append(nr.Children, rc) } + np.Children = append(np.Children, &nr) continue } np.Children = append(np.Children, pc) diff --git a/structpara.go b/structpara.go index a7f2dc8..5c79a37 100644 --- a/structpara.go +++ b/structpara.go @@ -169,7 +169,7 @@ type Paragraph struct { RsidP string `xml:"w:rsidP,attr,omitempty"` Properties *ParagraphProperties - Children []interface{} // Children will generate an unnecessary tag ... and we skip it by a self-defined xml.Marshaler + Children []interface{} file *Docx }