refactoring rendering
This commit is contained in:
parent
54d319e8d7
commit
a9e83e1c60
|
@ -0,0 +1,6 @@
|
|||
package owl
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed embed/*
|
||||
var embed_files embed.FS
|
52
renderer.go
52
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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue