-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
62 lines (51 loc) · 1.41 KB
/
render.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
package onepagestaticsite
import (
"fmt"
"os"
"text/template"
)
// Render generate the HTML file on the outfp file path using the Go HTML
// template on the tplfp file path and placing the HTML content in the
// {{.Content}} of the template, which is obtained from the Markdown file in the
// cntfp file path.
// NOTE that if the outfp file exists, it will be overwritten.
// It returns an errors if the template file doesn't exits, if any of the
// files paths cntfp or outfp don't exists or any error happens during the
// operations performed.
func Render(tplfp string, ctnfp string, outfp string) error {
tplfp, err := resolveFilepath(tplfp)
if err != nil {
return err
}
tpl, err := template.ParseFiles(tplfp)
if err != nil {
return fmt.Errorf("error parsing the templage: %+v", err)
}
fpa, err := resolveFilepath(ctnfp)
if err != nil {
return err
}
ctnf, err := os.Open(fpa) // nolint: gosec
if err != nil {
return fmt.Errorf("error opening the content file. %+v", err)
}
ctn, err := contentToHTML(ctnf)
if err != nil {
return err
}
outfp, err = resolveFilepath(outfp)
if err != nil {
return err
}
outf, err := os.Create(outfp)
if err != nil {
return fmt.Errorf("error creating the output file: %+v", err)
}
if err = tpl.Execute(outf, tplData{Content: ctn}); err != nil {
return fmt.Errorf("error rendering the template: %+v", err)
}
return nil
}
type tplData struct {
Content string
}