Post translations, little progress on templates

This commit is contained in:
Jan-Lukas Else 2020-10-16 20:34:34 +02:00
parent 5652fab725
commit 4cb605c38b
6 changed files with 95 additions and 33 deletions

View File

@ -133,21 +133,21 @@ func serveTaxonomyValue(blog string, path string, tax *taxonomy, value string) f
func servePhotos(blog string) func(w http.ResponseWriter, r *http.Request) {
return serveIndex(&indexConfig{
blog: blog,
path: appConfig.Blogs[blog].Photos.Path,
onlyWithParameter: appConfig.Blogs[blog].Photos.Parameter,
template: templatePhotos,
blog: blog,
path: appConfig.Blogs[blog].Photos.Path,
parameter: appConfig.Blogs[blog].Photos.Parameter,
template: templatePhotos,
})
}
type indexConfig struct {
blog string
path string
section *section
tax *taxonomy
taxValue string
onlyWithParameter string
template string
blog string
path string
section *section
tax *taxonomy
taxValue string
parameter string
template string
}
func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) {
@ -163,11 +163,11 @@ func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) {
}
}
p := paginator.New(&postPaginationAdapter{context: r.Context(), config: &postsRequestConfig{
blog: ic.blog,
sections: sections,
taxonomy: ic.tax,
taxonomyValue: ic.taxValue,
onlyWithParameter: ic.onlyWithParameter,
blog: ic.blog,
sections: sections,
taxonomy: ic.tax,
taxonomyValue: ic.taxValue,
parameter: ic.parameter,
}}, appConfig.Blogs[ic.blog].Pagination)
p.SetPage(pageNo)
var posts []*post
@ -229,14 +229,15 @@ func getPost(context context.Context, path string) (*post, error) {
}
type postsRequestConfig struct {
blog string
path string
limit int
offset int
sections []string
taxonomy *taxonomy
taxonomyValue string
onlyWithParameter string
blog string
path string
limit int
offset int
sections []string
taxonomy *taxonomy
taxonomyValue string
parameter string
parameterValue string
}
func getPosts(context context.Context, config *postsRequestConfig) (posts []*post, err error) {
@ -247,8 +248,12 @@ func getPosts(context context.Context, config *postsRequestConfig) (posts []*pos
if config.blog != "" {
postsTable = "(select * from " + postsTable + " where blog = '" + config.blog + "')"
}
if config.onlyWithParameter != "" {
postsTable = "(select distinct p.* from " + postsTable + " p left outer join post_parameters pp on p.path = pp.path where pp.parameter = '" + config.onlyWithParameter + "' and length(coalesce(pp.value, '')) > 1)"
if config.parameter != "" {
if config.parameterValue != "" {
postsTable = "(select distinct p.* from " + postsTable + " p left outer join post_parameters pp on p.path = pp.path where pp.parameter = '" + config.parameter + "' and pp.value = '" + config.parameterValue + "')"
} else {
postsTable = "(select distinct p.* from " + postsTable + " p left outer join post_parameters pp on p.path = pp.path where pp.parameter = '" + config.parameter + "' and length(coalesce(pp.value, '')) > 1)"
}
}
if config.taxonomy != nil && len(config.taxonomyValue) > 0 {
postsTable = "(select distinct p.* from " + postsTable + " p left outer join post_parameters pp on p.path = pp.path where pp.parameter = '" + config.taxonomy.Name + "' and lower(pp.value) = lower('" + config.taxonomyValue + "'))"

View File

@ -1,6 +1,7 @@
package main
import (
"context"
"strings"
"github.com/PuerkitoBio/goquery"
@ -33,3 +34,27 @@ func (p *post) summary() (summary string) {
summary = firstSentences(doc.Text(), 3)
return
}
func (p *post) translations() []*post {
translationkey := p.firstParameter("translationkey")
if translationkey == "" {
return nil
}
posts, err := getPosts(context.Background(), &postsRequestConfig{
parameter: "translationkey",
parameterValue: translationkey,
})
if err != nil || len(posts) == 0 {
return nil
}
translations := []*post{}
for _, t := range posts {
if p.Path != t.Path {
translations = append(translations, t)
}
}
if len(translations) == 0 {
return nil
}
return translations
}

View File

@ -33,6 +33,9 @@ var templateFunctions template.FuncMap
func initRendering() error {
templateFunctions = template.FuncMap{
"blog": func(blog string) *configBlog {
return appConfig.Blogs[blog]
},
"menu": func(blog *configBlog, id string) *menu {
return blog.Menus[id]
},
@ -48,16 +51,23 @@ func initRendering() error {
"p": func(p *post, parameter string) string {
return p.firstParameter(parameter)
},
// All parameter values
// Post specific
"ps": func(p *post, parameter string) []string {
return p.Parameters[parameter]
},
"hasp": func(p *post, parameter string) bool {
return len(p.Parameters[parameter]) > 0
},
"title": func(p *post) string {
return p.title()
},
"summary": func(p *post) string {
return p.summary()
},
"translations": func(p *post) []*post {
return p.translations()
},
// Others
"dateformat": func(date string, format string) string {
d, err := dateparse.ParseIn(date, time.Local)
if err != nil {
@ -86,6 +96,9 @@ func initRendering() error {
"urlize": urlize,
"sort": sortedStrings,
"blogRelative": func(blog *configBlog, path string) string {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
if blog.Path != "/" {
return blog.Path + path
}

View File

@ -26,6 +26,9 @@
</p>
{{ end }}
{{ end }}
{{ range $i, $t := (translations .Data) }}
<p><a href="{{ $t.Path }}">{{ (blog $t.Blog).Title }}</a></p>
{{ end }}
</main>
{{ end }}

View File

@ -1,9 +1,14 @@
{{ define "summary" }}
<article>
{{ with p .Data "title" }}<h2>{{ . }}</h2>{{ end }}
{{ if .Data.Published }}<p>{{ longDate .Data.Published .Blog.Lang }}</p>{{ end }}
<p>{{ summary .Data }}</p>
<a href="{{ .Data.Path }}">{{ string .Blog.Lang "view" }}</a>
<article class="h-entry border-bottom">
{{ if p .Data "title" }}
<h2 class="p-name">
<a class="u-url" href="{{ .Data.Path }}">
{{ p .Data "title" }}
</a>
</h2>
{{ end }}
{{ include "summarymeta" .Blog .Data }}
<p class="p-summary">{{ summary .Data }}</p>
<p>{{ if (hasp .Data "images") }}🖼️ {{ end }}<a class="u-url" href="{{ .Data.Path }}">{{ string .Blog.Lang "view" }}</a></p>
</article>
<hr>
{{ end }}

View File

@ -0,0 +1,11 @@
{{ define "summarymeta" }}
<div class="p">
{{ $section := (index .Blog.Sections .Data.Section) }}
{{ if .Data.Published }}
<div><time class="dt-published" datetime="{{ dateformat .Data.Published "2006-01-02T15:04:05Z07:00" }}">{{ longDate .Data.Published .Blog.Lang }}</time>{{ if $section }} in <a href="{{ blogRelative .Blog $section.Name }}">{{ $section.Title }}</a>{{ end }}</div>
{{ end }}
{{ if .Data.Updated }}
<div>{{ string .Blog.Lang "updatedon" }} <time class="dt-updated" datetime="{{ dateformat .Data.Updated "2006-01-02T15:04:05Z07:00" }}">{{ longDate .Data.Updated .Blog.Lang }}</time></div>
{{ end }}
</div>
{{ end }}