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"
|
"html/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed embed/user-list.html
|
|
||||||
var userListTemplateStr string
|
|
||||||
|
|
||||||
//go:embed embed/post.html
|
|
||||||
var postTemplateStr string
|
|
||||||
|
|
||||||
type PageContent struct {
|
type PageContent struct {
|
||||||
Title string
|
Title string
|
||||||
Content template.HTML
|
Content template.HTML
|
||||||
|
@ -22,6 +16,27 @@ type PostRenderData struct {
|
||||||
Post template.HTML
|
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) {
|
func renderIntoBaseTemplate(user User, data PageContent) (string, error) {
|
||||||
baseTemplate, _ := user.Template()
|
baseTemplate, _ := user.Template()
|
||||||
t, err := template.New("index").Parse(baseTemplate)
|
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) {
|
func RenderPost(post Post) (string, error) {
|
||||||
buf, _ := post.MarkdownData()
|
buf, _ := post.MarkdownData()
|
||||||
|
postHtml, err := renderEmbedTemplate("embed/post.html", PostRenderData{
|
||||||
postTemplate, _ := template.New("post").Parse(postTemplateStr)
|
|
||||||
var postHtml bytes.Buffer
|
|
||||||
err := postTemplate.Execute(&postHtml, PostRenderData{
|
|
||||||
Title: post.Title(),
|
Title: post.Title(),
|
||||||
Post: template.HTML(buf.String()),
|
Post: template.HTML(buf.String()),
|
||||||
})
|
})
|
||||||
|
@ -65,7 +77,7 @@ func RenderPost(post Post) (string, error) {
|
||||||
|
|
||||||
data := PageContent{
|
data := PageContent{
|
||||||
Title: post.Title(),
|
Title: post.Title(),
|
||||||
Content: template.HTML(postHtml.String()),
|
Content: template.HTML(postHtml),
|
||||||
}
|
}
|
||||||
|
|
||||||
return renderIntoBaseTemplate(*post.user, data)
|
return renderIntoBaseTemplate(*post.user, data)
|
||||||
|
@ -91,27 +103,15 @@ func RenderIndexPage(user User) (string, error) {
|
||||||
func RenderUserList(repo Repository) (string, error) {
|
func RenderUserList(repo Repository) (string, error) {
|
||||||
baseTemplate, _ := repo.Template()
|
baseTemplate, _ := repo.Template()
|
||||||
users, _ := repo.Users()
|
users, _ := repo.Users()
|
||||||
t, err := template.New("user_list").Parse(userListTemplateStr)
|
userHtml, err := renderEmbedTemplate("embed/user-list.html", users)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
var userHtml bytes.Buffer
|
|
||||||
t.Execute(&userHtml, users)
|
|
||||||
|
|
||||||
data := PageContent{
|
data := PageContent{
|
||||||
Title: "Index",
|
Title: "Index",
|
||||||
Content: template.HTML(userHtml.String()),
|
Content: template.HTML(userHtml),
|
||||||
}
|
}
|
||||||
|
|
||||||
var html bytes.Buffer
|
return renderTemplateStr([]byte(baseTemplate), data)
|
||||||
t, err = template.New("index").Parse(baseTemplate)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Execute(&html, data)
|
|
||||||
|
|
||||||
return html.String(), nil
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package owl
|
package owl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -14,9 +13,6 @@ import (
|
||||||
//go:embed embed/initial/base.html
|
//go:embed embed/initial/base.html
|
||||||
var base_template string
|
var base_template string
|
||||||
|
|
||||||
//go:embed embed/*
|
|
||||||
var static_files embed.FS
|
|
||||||
|
|
||||||
var VERSION = "0.0.1"
|
var VERSION = "0.0.1"
|
||||||
|
|
||||||
type Repository struct {
|
type Repository struct {
|
||||||
|
@ -41,22 +37,22 @@ func CreateRepository(name string) (Repository, error) {
|
||||||
os.Mkdir(newRepo.StaticDir(), 0755)
|
os.Mkdir(newRepo.StaticDir(), 0755)
|
||||||
|
|
||||||
// copy all files from static_files embed.FS to StaticDir
|
// 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 {
|
for _, file := range staticFiles {
|
||||||
if file.IsDir() {
|
if file.IsDir() {
|
||||||
continue
|
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)
|
os.WriteFile(newRepo.StaticDir()+"/"+file.Name(), src_data, 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy repo/ to newRepo.Dir()
|
// 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 {
|
for _, file := range init_files {
|
||||||
if file.IsDir() {
|
if file.IsDir() {
|
||||||
continue
|
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)
|
os.WriteFile(newRepo.Dir()+"/"+file.Name(), src_data, 0644)
|
||||||
}
|
}
|
||||||
return newRepo, nil
|
return newRepo, nil
|
||||||
|
|
Loading…
Reference in New Issue