forked from nguyenthenguyen/docx
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbase.go
70 lines (62 loc) · 1.76 KB
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package docTemp
import (
"bytes"
"errors"
"log"
"path/filepath"
"text/template"
"github.com/opencontrol/doc-template/docx"
)
// Document interface is a combintation of methods use for generic data files
type Document interface {
ReadFile(string) error
UpdateContent(string)
GetContent() string
WriteToFile(string, string) error
Close() error
}
// DocTemplate struct combines data and methods from both the Document interface
// and golang's templating library
type DocTemplate struct {
Template *template.Template
Document Document
}
// GetTemplate uses the file extension to determine the correct document struct to use
func GetTemplate(filePath string) (*DocTemplate, error) {
var document Document
switch filepath.Ext(filePath) {
case ".docx":
document = new(docx.Docx)
default:
return nil, errors.New("Unsupported Document Type")
}
err := document.ReadFile(filePath)
if err != nil {
return nil, err
}
return &DocTemplate{Document: document, Template: template.New("docTemp")}, nil
}
// Execute func runs the template and sends the output to the export path
func (docTemplate *DocTemplate) Execute(exportPath string, data interface{}) error {
buf := new(bytes.Buffer)
err := docTemplate.Template.Execute(buf, data)
if err != nil {
log.Println(err)
return err
}
err = docTemplate.Document.WriteToFile(exportPath, buf.String())
return err
}
// AddFunctions adds functions to the template
func (docTemplate *DocTemplate) AddFunctions(funcMap template.FuncMap) {
docTemplate.Template = docTemplate.Template.Funcs(funcMap)
}
// Parse parses the template
func (docTemplate *DocTemplate) Parse() {
temp, err := docTemplate.Template.Parse(docTemplate.Document.GetContent())
if err != nil {
log.Println(err)
} else {
docTemplate.Template = temp
}
}