From a9e83e1c60bbe6b8cfecdc5dd00e04a863bd6ed4 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Thu, 18 Aug 2022 21:06:01 +0200 Subject: [PATCH] refactoring rendering --- embed.go | 6 +++++ embed/post-list.html | 0 renderer.go | 52 ++++++++++++++++++++++---------------------- repository.go | 12 ++++------ 4 files changed, 36 insertions(+), 34 deletions(-) create mode 100644 embed.go create mode 100644 embed/post-list.html diff --git a/embed.go b/embed.go new file mode 100644 index 0000000..b01283e --- /dev/null +++ b/embed.go @@ -0,0 +1,6 @@ +package owl + +import "embed" + +//go:embed embed/* +var embed_files embed.FS diff --git a/embed/post-list.html b/embed/post-list.html new file mode 100644 index 0000000..e69de29 diff --git a/renderer.go b/renderer.go index 707c32b..8a255d7 100644 --- a/renderer.go +++ b/renderer.go @@ -6,12 +6,6 @@ import ( "html/template" ) -//go:embed embed/user-list.html -var userListTemplateStr string - -//go:embed embed/post.html -var postTemplateStr string - type PageContent struct { Title string Content template.HTML @@ -22,6 +16,27 @@ type PostRenderData struct { Post template.HTML } +func renderEmbedTemplate(templateFile string, data interface{}) (string, error) { + templateStr, err := embed_files.ReadFile(templateFile) + if err != nil { + return "", err + } + return renderTemplateStr(templateStr, data) +} + +func renderTemplateStr(templateStr []byte, data interface{}) (string, error) { + t, err := template.New("_").Parse(string(templateStr)) + if err != nil { + return "", err + } + var html bytes.Buffer + err = t.Execute(&html, data) + if err != nil { + return "", err + } + return html.String(), nil +} + func renderIntoBaseTemplate(user User, data PageContent) (string, error) { baseTemplate, _ := user.Template() t, err := template.New("index").Parse(baseTemplate) @@ -52,10 +67,7 @@ func renderIntoBaseTemplate(user User, data PageContent) (string, error) { func RenderPost(post Post) (string, error) { buf, _ := post.MarkdownData() - - postTemplate, _ := template.New("post").Parse(postTemplateStr) - var postHtml bytes.Buffer - err := postTemplate.Execute(&postHtml, PostRenderData{ + postHtml, err := renderEmbedTemplate("embed/post.html", PostRenderData{ Title: post.Title(), Post: template.HTML(buf.String()), }) @@ -65,7 +77,7 @@ func RenderPost(post Post) (string, error) { data := PageContent{ Title: post.Title(), - Content: template.HTML(postHtml.String()), + Content: template.HTML(postHtml), } return renderIntoBaseTemplate(*post.user, data) @@ -91,27 +103,15 @@ func RenderIndexPage(user User) (string, error) { func RenderUserList(repo Repository) (string, error) { baseTemplate, _ := repo.Template() users, _ := repo.Users() - t, err := template.New("user_list").Parse(userListTemplateStr) + userHtml, err := renderEmbedTemplate("embed/user-list.html", users) if err != nil { return "", err } - var userHtml bytes.Buffer - t.Execute(&userHtml, users) - data := PageContent{ Title: "Index", - Content: template.HTML(userHtml.String()), + Content: template.HTML(userHtml), } - var html bytes.Buffer - t, err = template.New("index").Parse(baseTemplate) - if err != nil { - return "", err - } - - t.Execute(&html, data) - - return html.String(), nil - + return renderTemplateStr([]byte(baseTemplate), data) } diff --git a/repository.go b/repository.go index 201ee74..4a8c96d 100644 --- a/repository.go +++ b/repository.go @@ -1,7 +1,6 @@ package owl import ( - "embed" _ "embed" "fmt" "io/ioutil" @@ -14,9 +13,6 @@ import ( //go:embed embed/initial/base.html var base_template string -//go:embed embed/* -var static_files embed.FS - var VERSION = "0.0.1" type Repository struct { @@ -41,22 +37,22 @@ func CreateRepository(name string) (Repository, error) { os.Mkdir(newRepo.StaticDir(), 0755) // copy all files from static_files embed.FS to StaticDir - staticFiles, _ := static_files.ReadDir("embed/initial/static") + staticFiles, _ := embed_files.ReadDir("embed/initial/static") for _, file := range staticFiles { if file.IsDir() { continue } - src_data, _ := static_files.ReadFile("embed/initial/static/" + file.Name()) + src_data, _ := embed_files.ReadFile("embed/initial/static/" + file.Name()) os.WriteFile(newRepo.StaticDir()+"/"+file.Name(), src_data, 0644) } // copy repo/ to newRepo.Dir() - init_files, _ := static_files.ReadDir("embed/initial/repo") + init_files, _ := embed_files.ReadDir("embed/initial/repo") for _, file := range init_files { if file.IsDir() { continue } - src_data, _ := static_files.ReadFile("embed/initial/repo/" + file.Name()) + src_data, _ := embed_files.ReadFile("embed/initial/repo/" + file.Name()) os.WriteFile(newRepo.Dir()+"/"+file.Name(), src_data, 0644) } return newRepo, nil