1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-04 23:30:25 +08:00

feat: chage template name (due2 #17)

This commit is contained in:
源文雨
2024-03-15 13:07:58 +09:00
parent b3e1a27db9
commit 047c5e5f03
16 changed files with 52 additions and 25 deletions

View File

@@ -67,7 +67,7 @@ import (
)
func main() {
w := docx.NewA4()
w := docx.New().WithDefaultTheme()
// add new paragraph
para1 := w.AddParagraph()
// add text

View File

@@ -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)
}

13
docx.go
View File

@@ -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
}

View File

@@ -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

6
fs.go
View File

@@ -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",

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)

36
theme.go Normal file
View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
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)
}