From 4cb605c38b20ec5ef124ece0bf6b212e59b73d74 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Fri, 16 Oct 2020 20:34:34 +0200 Subject: [PATCH] Post translations, little progress on templates --- posts.go | 57 ++++++++++++++++++++---------------- postsFuncs.go | 25 ++++++++++++++++ render.go | 15 +++++++++- templates/post.gohtml | 3 ++ templates/summary.gohtml | 17 +++++++---- templates/summarymeta.gohtml | 11 +++++++ 6 files changed, 95 insertions(+), 33 deletions(-) create mode 100644 templates/summarymeta.gohtml diff --git a/posts.go b/posts.go index 952a017..34ba867 100644 --- a/posts.go +++ b/posts.go @@ -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 + "'))" diff --git a/postsFuncs.go b/postsFuncs.go index 0462ed3..2ae6ca1 100644 --- a/postsFuncs.go +++ b/postsFuncs.go @@ -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 +} diff --git a/render.go b/render.go index 3e90213..575ad63 100644 --- a/render.go +++ b/render.go @@ -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 } diff --git a/templates/post.gohtml b/templates/post.gohtml index a8ff606..c9fe58e 100644 --- a/templates/post.gohtml +++ b/templates/post.gohtml @@ -26,6 +26,9 @@

{{ end }} {{ end }} + {{ range $i, $t := (translations .Data) }} +

{{ (blog $t.Blog).Title }}

+ {{ end }} {{ end }} diff --git a/templates/summary.gohtml b/templates/summary.gohtml index 89b8ba0..ffa19b0 100644 --- a/templates/summary.gohtml +++ b/templates/summary.gohtml @@ -1,9 +1,14 @@ {{ define "summary" }} -
- {{ with p .Data "title" }}

{{ . }}

{{ end }} - {{ if .Data.Published }}

{{ longDate .Data.Published .Blog.Lang }}

{{ end }} -

{{ summary .Data }}

- {{ string .Blog.Lang "view" }} + -
{{ end }} \ No newline at end of file diff --git a/templates/summarymeta.gohtml b/templates/summarymeta.gohtml new file mode 100644 index 0000000..af560b8 --- /dev/null +++ b/templates/summarymeta.gohtml @@ -0,0 +1,11 @@ +{{ define "summarymeta" }} +
+ {{ $section := (index .Blog.Sections .Data.Section) }} + {{ if .Data.Published }} +
{{ if $section }} in {{ $section.Title }}{{ end }}
+ {{ end }} + {{ if .Data.Updated }} +
{{ string .Blog.Lang "updatedon" }}
+ {{ end }} +
+{{ end }} \ No newline at end of file