commit
message
Using embedded templates
author
Ben Vogt <[email protected]>
date
2023-04-04 18:30:31
stats
5 file(s) changed,
8 insertions(+),
17 deletions(-)
files
commit.template.html
templates/commit.html
file.template.html
templates/file.html
files.template.html
templates/files.html
log.template.html
templates/log.html
main.go
1diff --git a/main.go b/main.go
2index f16a775..5daa906 100644
3--- a/main.go
4+++ b/main.go
5@@ -2,6 +2,7 @@ package main
6
7 import (
8 "bytes"
9+ "embed"
10 _ "embed"
11 "errors"
12 "flag"
13@@ -21,21 +22,10 @@ import (
14 "github.com/go-git/go-git/v5/plumbing/object"
15 )
16
17-//go:embed file.template.html
18-var fileTemplateHtml string
19+//go:embed templates/*
20+var htmlTemplates embed.FS
21
22-//go:embed files.template.html
23-var filesTemplateHtml string
24-
25-//go:embed log.template.html
26-var logTemplateHtml string
27-
28-//go:embed commit.template.html
29-var commitTemplateHtml string
30-
31-var (
32- config Config
33-)
34+var config Config
35
36 type Config struct {
37 DebugOn bool
38@@ -258,7 +248,7 @@ func CloneAndInfo() *git.Repository {
39 }
40
41 func RenderAllCommitPages(r *git.Repository) {
42- t, err := template.New("commit").Parse(commitTemplateHtml)
43+ t, err := template.ParseFS(htmlTemplates, "templates/commit.html")
44 checkErr(err)
45 ref, err := r.Head()
46 checkErr(err)
47@@ -292,7 +282,7 @@ func RenderAllCommitPages(r *git.Repository) {
48 }
49
50 func RenderLogPage(r *git.Repository) {
51- t, err := template.New("log").Parse(logTemplateHtml)
52+ t, err := template.ParseFS(htmlTemplates, "templates/log.html")
53 checkErr(err)
54 commits := make([]Commit, 0)
55 ref, err := r.Head()
56@@ -332,7 +322,7 @@ func RenderLogPage(r *git.Repository) {
57 }
58
59 func RenderAllFilesPage() {
60- t, err := template.New("files").Parse(filesTemplateHtml)
61+ t, err := template.ParseFS(htmlTemplates, "templates/files.html")
62 checkErr(err)
63 trackedFiles := make([]TrackedFileMetaData, 0)
64 err = filepath.Walk(config.CloneDir, func(filename string, info fs.FileInfo, err error) error {
65@@ -365,7 +355,7 @@ func RenderAllFilesPage() {
66 }
67
68 func RenderSingleFilePages() {
69- t, err := template.New("file").Parse(fileTemplateHtml)
70+ t, err := template.ParseFS(htmlTemplates, "templates/file.html")
71 checkErr(err)
72 err = filepath.Walk(config.CloneDir, func(filename string, info fs.FileInfo, err error) error {
73 if info.IsDir() && info.Name() == ".git" {
74diff --git a/commit.template.html b/templates/commit.html
75similarity index 100%
76rename from commit.template.html
77rename to templates/commit.html
78diff --git a/file.template.html b/templates/file.html
79similarity index 100%
80rename from file.template.html
81rename to templates/file.html
82diff --git a/files.template.html b/templates/files.html
83similarity index 100%
84rename from files.template.html
85rename to templates/files.html
86diff --git a/log.template.html b/templates/log.html
87similarity index 100%
88rename from log.template.html
89rename to templates/log.html