Skip to content

Commit 047c5e5

Browse files
committed
feat: chage template name (due2 #17)
1 parent b3e1a27 commit 047c5e5

File tree

16 files changed

+52
-25
lines changed

16 files changed

+52
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ import (
6767
)
6868

6969
func main() {
70-
w := docx.NewA4()
70+
w := docx.New().WithDefaultTheme()
7171
// add new paragraph
7272
para1 := w.AddParagraph()
7373
// add text

cmd/main/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func main() {
4646
if !*analyzeOnly {
4747
fmt.Printf("Preparing new document to write at %s\n", *fileLocation)
4848

49-
w = docx.NewA4()
49+
w = docx.New().WithDefaultTheme()
5050
// add new paragraph
5151
para1 := w.AddParagraph().Justification("distribute")
5252
r, err := para1.AddAnchorDrawingFrom("testdata/fumiama.JPG")
@@ -253,7 +253,7 @@ func main() {
253253
if err != nil {
254254
panic(err)
255255
}
256-
newFile := docx.NewA4()
256+
newFile := docx.New().WithDefaultTheme()
257257
for i := 0; i < int(*dupnum); i++ {
258258
newFile.AppendFile(doc)
259259
}

docx.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ type Docx struct {
5555
io.WriterTo
5656
}
5757

58-
// NewA4 generates a new empty A4 docx file that we can manipulate and
58+
// New generates a new empty docx file that we can manipulate and
5959
// later on, save
60-
func NewA4() *Docx {
61-
return newEmptyA4File()
60+
func New() *Docx {
61+
return newEmptyFile()
6262
}
6363

6464
// 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) {
160160
func (f *Docx) Read(_ []byte) (int, error) {
161161
return 0, os.ErrInvalid
162162
}
163-
164-
// UseTemplate will replace template files
165-
func (f *Docx) UseTemplate(template string, tmpfslst []string, tmplfs fs.FS) {
166-
f.template = template
167-
f.tmplfs = tmplfs
168-
f.tmpfslst = tmpfslst
169-
}

empty.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"encoding/xml"
2525
)
2626

27-
func newEmptyA4File() *Docx {
27+
func newEmptyFile() *Docx {
2828
docx := &Docx{
2929
Document: Document{
3030
XMLName: xml.Name{
@@ -68,8 +68,6 @@ func newEmptyA4File() *Docx {
6868
mediaNameIdx: make(map[string]int, 64),
6969
rID: 3,
7070
slowIDs: make(map[string]uintptr, 64),
71-
template: "a4",
72-
tmpfslst: A4TemplateFilesList,
7371
}
7472
docx.Document.Body.file = docx
7573
return docx

fs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ import "embed"
2525
var (
2626
// TemplateXMLFS stores template docx files
2727
//go:embed xml
28-
//go:embed xml/a4/_rels/*
28+
//go:embed xml/default/_rels/*
2929
TemplateXMLFS embed.FS
3030

31-
// A4TemplateFilesList is the files list under TemplateXMLFS/xml/a4
32-
A4TemplateFilesList = []string{
31+
// DefaultTemplateFilesList is the files list under TemplateXMLFS/xml/default
32+
DefaultTemplateFilesList = []string{
3333
"_rels/.rels",
3434
"docProps/app.xml",
3535
"docProps/core.xml",

structdrawing_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
func TestDrawingStructure(t *testing.T) {
32-
w := NewA4()
32+
w := New().WithDefaultTheme()
3333
// add new paragraph
3434
para1 := w.AddParagraph()
3535
// add text
@@ -66,7 +66,7 @@ func TestDrawingStructure(t *testing.T) {
6666
if err != nil {
6767
t.Fatal(err)
6868
}
69-
w = NewA4()
69+
w = New().WithDefaultTheme()
7070
err = xml.NewDecoder(f).Decode(&w.Document)
7171
if err != nil {
7272
t.Fatal(err)

structshape_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
func TestShapeStructure(t *testing.T) {
32-
w := NewA4()
32+
w := New().WithDefaultTheme()
3333
// add new paragraph
3434
para1 := w.AddParagraph()
3535
// add text
@@ -69,7 +69,7 @@ func TestShapeStructure(t *testing.T) {
6969
if err != nil {
7070
t.Fatal(err)
7171
}
72-
w = NewA4()
72+
w = New().WithDefaultTheme()
7373
err = xml.NewDecoder(f).Decode(&w.Document)
7474
if err != nil {
7575
t.Fatal(err)

structtable_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
func TestTableStructure(t *testing.T) {
32-
w := NewA4()
32+
w := New().WithDefaultTheme()
3333
// add new paragraph
3434
para1 := w.AddParagraph()
3535
// add text
@@ -63,7 +63,7 @@ func TestTableStructure(t *testing.T) {
6363
if err != nil {
6464
t.Fatal(err)
6565
}
66-
w = NewA4()
66+
w = New().WithDefaultTheme()
6767
err = xml.NewDecoder(f).Decode(&w.Document)
6868
if err != nil {
6969
t.Fatal(err)

theme.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright (c) 2020 gingfrederik
3+
Copyright (c) 2021 Gonzalo Fernandez-Victorio
4+
Copyright (c) 2021 Basement Crowd Ltd (https://www.basementcrowd.com)
5+
Copyright (c) 2024 Fumiama Minamoto (源文雨)
6+
7+
This program is free software: you can redistribute it and/or modify
8+
it under the terms of the GNU Affero General Public License as published
9+
by the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU Affero General Public License for more details.
16+
17+
You should have received a copy of the GNU Affero General Public License
18+
along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
package docx
22+
23+
import "io/fs"
24+
25+
// UseTemplate will replace template files
26+
func (f *Docx) UseTemplate(template string, tmpfslst []string, tmplfs fs.FS) *Docx {
27+
f.template = template
28+
f.tmplfs = tmplfs
29+
f.tmpfslst = tmpfslst
30+
return f
31+
}
32+
33+
// WithDefaultTheme use default theme embeded
34+
func (f *Docx) WithDefaultTheme() *Docx {
35+
return f.UseTemplate("default", DefaultTemplateFilesList, TemplateXMLFS)
36+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)