diff --git a/README.md b/README.md index 900c545..8448164 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ import ( ) func main() { - w := docx.NewA4() + w := docx.New().WithDefaultTheme() // add new paragraph para1 := w.AddParagraph() // add text diff --git a/cmd/main/main.go b/cmd/main/main.go index 202f3a4..9974872 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -46,7 +46,7 @@ func main() { if !*analyzeOnly { fmt.Printf("Preparing new document to write at %s\n", *fileLocation) - w = docx.NewA4() + w = docx.New().WithDefaultTheme() // add new paragraph para1 := w.AddParagraph().Justification("distribute") r, err := para1.AddAnchorDrawingFrom("testdata/fumiama.JPG") @@ -253,7 +253,7 @@ func main() { if err != nil { panic(err) } - newFile := docx.NewA4() + newFile := docx.New().WithDefaultTheme() for i := 0; i < int(*dupnum); i++ { newFile.AppendFile(doc) } diff --git a/docx.go b/docx.go index b0e6a00..c7753f4 100644 --- a/docx.go +++ b/docx.go @@ -55,10 +55,10 @@ type Docx struct { io.WriterTo } -// NewA4 generates a new empty A4 docx file that we can manipulate and +// New generates a new empty docx file that we can manipulate and // later on, save -func NewA4() *Docx { - return newEmptyA4File() +func New() *Docx { + return newEmptyFile() } // Parse generates a new docx file in memory from a reader @@ -160,10 +160,3 @@ func (f *Docx) WriteTo(writer io.Writer) (_ int64, err error) { func (f *Docx) Read(_ []byte) (int, error) { return 0, os.ErrInvalid } - -// UseTemplate will replace template files -func (f *Docx) UseTemplate(template string, tmpfslst []string, tmplfs fs.FS) { - f.template = template - f.tmplfs = tmplfs - f.tmpfslst = tmpfslst -} diff --git a/empty.go b/empty.go index 5688ebe..394486c 100644 --- a/empty.go +++ b/empty.go @@ -24,7 +24,7 @@ import ( "encoding/xml" ) -func newEmptyA4File() *Docx { +func newEmptyFile() *Docx { docx := &Docx{ Document: Document{ XMLName: xml.Name{ @@ -68,8 +68,6 @@ func newEmptyA4File() *Docx { mediaNameIdx: make(map[string]int, 64), rID: 3, slowIDs: make(map[string]uintptr, 64), - template: "a4", - tmpfslst: A4TemplateFilesList, } docx.Document.Body.file = docx return docx diff --git a/fs.go b/fs.go index 782a8fc..598da95 100644 --- a/fs.go +++ b/fs.go @@ -25,11 +25,11 @@ import "embed" var ( // TemplateXMLFS stores template docx files //go:embed xml - //go:embed xml/a4/_rels/* + //go:embed xml/default/_rels/* TemplateXMLFS embed.FS - // A4TemplateFilesList is the files list under TemplateXMLFS/xml/a4 - A4TemplateFilesList = []string{ + // DefaultTemplateFilesList is the files list under TemplateXMLFS/xml/default + DefaultTemplateFilesList = []string{ "_rels/.rels", "docProps/app.xml", "docProps/core.xml", diff --git a/structdrawing_test.go b/structdrawing_test.go index a2d82f8..0744e63 100644 --- a/structdrawing_test.go +++ b/structdrawing_test.go @@ -29,7 +29,7 @@ import ( ) func TestDrawingStructure(t *testing.T) { - w := NewA4() + w := New().WithDefaultTheme() // add new paragraph para1 := w.AddParagraph() // add text @@ -66,7 +66,7 @@ func TestDrawingStructure(t *testing.T) { if err != nil { t.Fatal(err) } - w = NewA4() + w = New().WithDefaultTheme() err = xml.NewDecoder(f).Decode(&w.Document) if err != nil { t.Fatal(err) diff --git a/structshape_test.go b/structshape_test.go index d70fad5..2850de3 100644 --- a/structshape_test.go +++ b/structshape_test.go @@ -29,7 +29,7 @@ import ( ) func TestShapeStructure(t *testing.T) { - w := NewA4() + w := New().WithDefaultTheme() // add new paragraph para1 := w.AddParagraph() // add text @@ -69,7 +69,7 @@ func TestShapeStructure(t *testing.T) { if err != nil { t.Fatal(err) } - w = NewA4() + w = New().WithDefaultTheme() err = xml.NewDecoder(f).Decode(&w.Document) if err != nil { t.Fatal(err) diff --git a/structtable_test.go b/structtable_test.go index 8c5b8e5..373642b 100644 --- a/structtable_test.go +++ b/structtable_test.go @@ -29,7 +29,7 @@ import ( ) func TestTableStructure(t *testing.T) { - w := NewA4() + w := New().WithDefaultTheme() // add new paragraph para1 := w.AddParagraph() // add text @@ -63,7 +63,7 @@ func TestTableStructure(t *testing.T) { if err != nil { t.Fatal(err) } - w = NewA4() + w = New().WithDefaultTheme() err = xml.NewDecoder(f).Decode(&w.Document) if err != nil { t.Fatal(err) diff --git a/theme.go b/theme.go new file mode 100644 index 0000000..6748b49 --- /dev/null +++ b/theme.go @@ -0,0 +1,36 @@ +/* + Copyright (c) 2020 gingfrederik + Copyright (c) 2021 Gonzalo Fernandez-Victorio + Copyright (c) 2021 Basement Crowd Ltd (https://www.basementcrowd.com) + Copyright (c) 2024 Fumiama Minamoto (源文雨) + + 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 + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package docx + +import "io/fs" + +// UseTemplate will replace template files +func (f *Docx) UseTemplate(template string, tmpfslst []string, tmplfs fs.FS) *Docx { + f.template = template + f.tmplfs = tmplfs + f.tmpfslst = tmpfslst + return f +} + +// WithDefaultTheme use default theme embeded +func (f *Docx) WithDefaultTheme() *Docx { + return f.UseTemplate("default", DefaultTemplateFilesList, TemplateXMLFS) +} diff --git a/xml/a4/[Content_Types].xml b/xml/default/[Content_Types].xml similarity index 100% rename from xml/a4/[Content_Types].xml rename to xml/default/[Content_Types].xml diff --git a/xml/a4/_rels/.rels b/xml/default/_rels/.rels similarity index 100% rename from xml/a4/_rels/.rels rename to xml/default/_rels/.rels diff --git a/xml/a4/docProps/app.xml b/xml/default/docProps/app.xml similarity index 100% rename from xml/a4/docProps/app.xml rename to xml/default/docProps/app.xml diff --git a/xml/a4/docProps/core.xml b/xml/default/docProps/core.xml similarity index 100% rename from xml/a4/docProps/core.xml rename to xml/default/docProps/core.xml diff --git a/xml/a4/word/fontTable.xml b/xml/default/word/fontTable.xml similarity index 100% rename from xml/a4/word/fontTable.xml rename to xml/default/word/fontTable.xml diff --git a/xml/a4/word/styles.xml b/xml/default/word/styles.xml similarity index 100% rename from xml/a4/word/styles.xml rename to xml/default/word/styles.xml diff --git a/xml/a4/word/theme/theme1.xml b/xml/default/word/theme/theme1.xml similarity index 100% rename from xml/a4/word/theme/theme1.xml rename to xml/default/word/theme/theme1.xml