2022-08-03 14:55:48 +00:00
|
|
|
package owl
|
2022-07-21 17:02:37 +00:00
|
|
|
|
2022-07-23 16:37:41 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
_ "embed"
|
|
|
|
"html/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PageContent struct {
|
2022-10-19 19:14:31 +00:00
|
|
|
Title string
|
|
|
|
Description string
|
|
|
|
Content template.HTML
|
|
|
|
Type string
|
|
|
|
SelfUrl string
|
2022-07-23 16:37:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PostRenderData struct {
|
2022-08-22 19:35:50 +00:00
|
|
|
Title string
|
|
|
|
Post *Post
|
|
|
|
Content template.HTML
|
2022-07-23 16:37:41 +00:00
|
|
|
}
|
2022-07-21 17:44:07 +00:00
|
|
|
|
2022-08-18 19:06:01 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-27 19:53:56 +00:00
|
|
|
func renderIntoBaseTemplate(user User, data PageContent) (string, error) {
|
|
|
|
baseTemplate, _ := user.Template()
|
|
|
|
t, err := template.New("index").Parse(baseTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
full_data := struct {
|
2022-10-19 19:14:31 +00:00
|
|
|
Title string
|
|
|
|
Description string
|
|
|
|
Content template.HTML
|
|
|
|
Type string
|
|
|
|
SelfUrl string
|
|
|
|
User User
|
2022-07-27 19:53:56 +00:00
|
|
|
}{
|
2022-10-19 19:14:31 +00:00
|
|
|
Title: data.Title,
|
|
|
|
Description: data.Description,
|
|
|
|
Content: data.Content,
|
|
|
|
Type: data.Type,
|
|
|
|
SelfUrl: data.SelfUrl,
|
|
|
|
User: user,
|
2022-07-27 19:53:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var html bytes.Buffer
|
|
|
|
t.Execute(&html, full_data)
|
|
|
|
|
|
|
|
return html.String(), nil
|
|
|
|
}
|
|
|
|
|
2022-09-04 13:32:37 +00:00
|
|
|
func renderPostContent(post *Post) (string, error) {
|
2022-08-22 19:15:36 +00:00
|
|
|
buf := post.RenderedContent()
|
2022-08-18 19:06:01 +00:00
|
|
|
postHtml, err := renderEmbedTemplate("embed/post.html", PostRenderData{
|
2022-08-22 19:35:50 +00:00
|
|
|
Title: post.Title(),
|
|
|
|
Post: post,
|
|
|
|
Content: template.HTML(buf.String()),
|
2022-07-23 16:37:41 +00:00
|
|
|
})
|
2022-09-04 13:32:37 +00:00
|
|
|
return postHtml, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func RenderPost(post *Post) (string, error) {
|
|
|
|
postHtml, err := renderPostContent(post)
|
2022-07-23 16:37:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-08-18 19:10:03 +00:00
|
|
|
return renderIntoBaseTemplate(*post.user, PageContent{
|
2022-10-19 19:14:31 +00:00
|
|
|
Title: post.Title(),
|
|
|
|
Description: post.Meta().Description,
|
|
|
|
Content: template.HTML(postHtml),
|
|
|
|
Type: "article",
|
|
|
|
SelfUrl: post.FullUrl(),
|
2022-08-18 19:10:03 +00:00
|
|
|
})
|
2022-07-21 17:02:37 +00:00
|
|
|
}
|
2022-07-23 15:19:47 +00:00
|
|
|
|
|
|
|
func RenderIndexPage(user User) (string, error) {
|
|
|
|
posts, _ := user.Posts()
|
2022-07-23 16:37:41 +00:00
|
|
|
|
2022-08-18 19:09:02 +00:00
|
|
|
postHtml, err := renderEmbedTemplate("embed/post-list.html", posts)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2022-07-23 15:19:47 +00:00
|
|
|
}
|
2022-07-23 16:37:41 +00:00
|
|
|
|
2022-08-18 19:10:03 +00:00
|
|
|
return renderIntoBaseTemplate(user, PageContent{
|
2022-07-23 16:37:41 +00:00
|
|
|
Title: "Index",
|
|
|
|
Content: template.HTML(postHtml),
|
2022-08-18 19:10:03 +00:00
|
|
|
})
|
2022-11-03 20:22:55 +00:00
|
|
|
}
|
2022-07-23 16:37:41 +00:00
|
|
|
|
2022-11-03 20:22:55 +00:00
|
|
|
func RenderUserAuthPage(user User) (string, error) {
|
|
|
|
authHtml, err := renderEmbedTemplate("embed/auth.html", user)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return renderIntoBaseTemplate(user, PageContent{
|
|
|
|
Title: "Auth",
|
|
|
|
Content: template.HTML(authHtml),
|
|
|
|
})
|
2022-07-23 15:19:47 +00:00
|
|
|
}
|
2022-07-23 16:37:41 +00:00
|
|
|
|
2022-07-24 18:29:31 +00:00
|
|
|
func RenderUserList(repo Repository) (string, error) {
|
|
|
|
baseTemplate, _ := repo.Template()
|
|
|
|
users, _ := repo.Users()
|
2022-08-18 19:06:01 +00:00
|
|
|
userHtml, err := renderEmbedTemplate("embed/user-list.html", users)
|
2022-07-24 18:29:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
data := PageContent{
|
|
|
|
Title: "Index",
|
2022-08-18 19:06:01 +00:00
|
|
|
Content: template.HTML(userHtml),
|
2022-07-24 18:29:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 19:06:01 +00:00
|
|
|
return renderTemplateStr([]byte(baseTemplate), data)
|
2022-07-24 18:29:31 +00:00
|
|
|
}
|