added publishing date to post and post list with microformat tags

This commit is contained in:
Niko Abeler 2022-08-22 21:35:50 +02:00
parent 769c4133d5
commit 179559250a
5 changed files with 33 additions and 15 deletions

View File

@ -102,7 +102,7 @@ func postHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request,
return return
} }
html, err := owl.RenderPost(post) html, err := owl.RenderPost(&post)
if err != nil { if err != nil {
println("Error rendering post: ", err.Error()) println("Error rendering post: ", err.Error())
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)

View File

@ -1,10 +1,16 @@
<div class="h-feed"> <div class="h-feed">
{{range .}} {{range .}}
<div class="h-entry"> <div class="h-entry">
<h3 style="margin-bottom:0;"><a class="u-url" href="{{.UrlPath}}">{{.Title}}</a></h3> <hgroup>
<div>Published: {{.Meta.Date}}</div> <h3><a class="u-url" href="{{.UrlPath}}">{{.Title}}</a></h3>
<small>
Published:
<time class="dt-published" datetime="{{.Meta.Date}}">
{{.Meta.Date}}
</time>
</small>
</hgroup>
</div> </div>
<hr> <hr>
<br>
{{end}} {{end}}
</div> </div>

View File

@ -1,6 +1,16 @@
<div class="h-entry"> <div class="h-entry">
<h1 class="p-name">{{.Title}}</h1> <hgroup>
<h1 class="p-name">{{.Title}}</h1>
<small>
Published:
<time class="dt-published" datetime="{{.Post.Meta.Date}}">
{{.Post.Meta.Date}}
</time>
</small>
</hgroup>
<hr>
<br>
<div class="e-content"> <div class="e-content">
{{.Post}} {{.Content}}
</div> </div>
</div> </div>

View File

@ -12,8 +12,9 @@ type PageContent struct {
} }
type PostRenderData struct { type PostRenderData struct {
Title string Title string
Post template.HTML Post *Post
Content template.HTML
} }
func renderEmbedTemplate(templateFile string, data interface{}) (string, error) { func renderEmbedTemplate(templateFile string, data interface{}) (string, error) {
@ -67,11 +68,12 @@ func renderIntoBaseTemplate(user User, data PageContent) (string, error) {
return html.String(), nil return html.String(), nil
} }
func RenderPost(post Post) (string, error) { func RenderPost(post *Post) (string, error) {
buf := post.RenderedContent() buf := post.RenderedContent()
postHtml, err := renderEmbedTemplate("embed/post.html", PostRenderData{ postHtml, err := renderEmbedTemplate("embed/post.html", PostRenderData{
Title: post.Title(), Title: post.Title(),
Post: template.HTML(buf.String()), Post: post,
Content: template.HTML(buf.String()),
}) })
if err != nil { if err != nil {
return "", err return "", err

View File

@ -11,7 +11,7 @@ import (
func TestCanRenderPost(t *testing.T) { func TestCanRenderPost(t *testing.T) {
user := getTestUser() user := getTestUser()
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")
result, err := owl.RenderPost(post) result, err := owl.RenderPost(&post)
if err != nil { if err != nil {
t.Error("Error rendering post: " + err.Error()) t.Error("Error rendering post: " + err.Error())
@ -27,7 +27,7 @@ func TestCanRenderPost(t *testing.T) {
func TestRenderPostHEntry(t *testing.T) { func TestRenderPostHEntry(t *testing.T) {
user := getTestUser() user := getTestUser()
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")
result, _ := owl.RenderPost(post) result, _ := owl.RenderPost(&post)
if !strings.Contains(result, "class=\"h-entry\"") { if !strings.Contains(result, "class=\"h-entry\"") {
t.Error("h-entry container not rendered. Got: " + result) t.Error("h-entry container not rendered. Got: " + result)
} }
@ -43,7 +43,7 @@ func TestRenderPostHEntry(t *testing.T) {
func TestRendererUsesBaseTemplate(t *testing.T) { func TestRendererUsesBaseTemplate(t *testing.T) {
user := getTestUser() user := getTestUser()
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")
result, err := owl.RenderPost(post) result, err := owl.RenderPost(&post)
if err != nil { if err != nil {
t.Error("Error rendering post: " + err.Error()) t.Error("Error rendering post: " + err.Error())
@ -132,7 +132,7 @@ func TestRendersHeaderTitle(t *testing.T) {
}) })
post, _ := user.CreateNewPost("testpost") post, _ := user.CreateNewPost("testpost")
result, _ := owl.RenderPost(post) result, _ := owl.RenderPost(&post)
if !strings.Contains(result, "Test Title") { if !strings.Contains(result, "Test Title") {
t.Error("Header title not rendered. Got: " + result) t.Error("Header title not rendered. Got: " + result)
} }