Browse Source

Move strings out of template files and embedded in app, improve notifications admin a bit

master
Jan-Lukas Else 3 months ago
parent
commit
e32cb518af
  1. 1
      Dockerfile
  2. 7
      go.mod
  3. 14
      go.sum
  4. 0
      strings/de.yaml
  5. 0
      strings/default.yaml
  6. 0
      strings/pt-br.yaml
  7. 7
      templateStrings.go
  8. 8
      ui.go
  9. 13
      utils.go
  10. 10
      utils_test.go

1
Dockerfile

@ -10,6 +10,7 @@ ADD testdata/ /app/testdata/
ADD templates/ /app/templates/
ADD leaflet/ /app/leaflet/
ADD dbmigrations/ /app/dbmigrations/
ADD strings/ /app/strings/
FROM buildbase as build

7
go.mod

@ -6,7 +6,7 @@ require (
git.jlel.se/jlelse/go-geouri v0.0.0-20210525190615-a9c1d50f42d6
git.jlel.se/jlelse/go-shutdowner v0.0.0-20210707065515-773db8099c30
git.jlel.se/jlelse/goldmark-mark v0.0.0-20210522162520-9788c89266a4
git.jlel.se/jlelse/template-strings v0.0.0-20210617205924-cfa3bd35ae40
git.jlel.se/jlelse/template-strings v0.0.0-20220210101539-6a4e1cfaf1b4
github.com/PuerkitoBio/goquery v1.8.0
github.com/alecthomas/chroma v0.10.0
github.com/andybalholm/brotli v1.0.4
@ -17,7 +17,7 @@ require (
github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f
github.com/dgraph-io/ristretto v0.1.0
github.com/dmulholl/mp3lib v1.0.0
github.com/elnormous/contenttype v1.0.0
github.com/elnormous/contenttype v1.0.1
github.com/emersion/go-sasl v0.0.0-20211008083017-0b9dcfb154ac
github.com/emersion/go-smtp v0.15.0
github.com/go-chi/chi/v5 v5.0.7
@ -36,6 +36,7 @@ require (
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/lopezator/migrator v0.3.0
github.com/mattn/go-sqlite3 v1.14.11
github.com/mergestat/timediff v0.0.3
github.com/microcosm-cc/bluemonday v1.0.18
github.com/mmcdole/gofeed v1.1.3
github.com/paulmach/go.geojson v1.4.0
@ -55,7 +56,7 @@ require (
// master
github.com/yuin/goldmark-emoji v1.0.2-0.20210607094911-0487583eca38
github.com/yuin/goldmark-highlighting v0.0.0-20220208100518-594be1970594
golang.org/x/crypto v0.0.0-20220209195652-db638375bc3a
golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/text v0.3.7

14
go.sum

@ -38,8 +38,8 @@ git.jlel.se/jlelse/go-shutdowner v0.0.0-20210707065515-773db8099c30 h1:+U313KydO
git.jlel.se/jlelse/go-shutdowner v0.0.0-20210707065515-773db8099c30/go.mod h1:C4E+E1LpDuayNCX7fJKUx5ERKpBw//2NSna9aeiS5yE=
git.jlel.se/jlelse/goldmark-mark v0.0.0-20210522162520-9788c89266a4 h1:p3c/vCY6MUADNXWCYI/hsZ8ZEvN7LtvwHzK2B8Y6BSk=
git.jlel.se/jlelse/goldmark-mark v0.0.0-20210522162520-9788c89266a4/go.mod h1:ZFhxwbX+afhgbzh5rpkSJUp6vIduNPtIGDrsWpIcHTE=
git.jlel.se/jlelse/template-strings v0.0.0-20210617205924-cfa3bd35ae40 h1:xFR5EREcB9FHX2/HXgM3KueC7EJEFiPcuoC+jIZttQw=
git.jlel.se/jlelse/template-strings v0.0.0-20210617205924-cfa3bd35ae40/go.mod h1:KA7sVmsYAMxpPUBdDSSzShP8+sKLM16wnpKVk8ltaA0=
git.jlel.se/jlelse/template-strings v0.0.0-20220210101539-6a4e1cfaf1b4 h1:EZZfPCTCExRhcgx0W3naHrfqY7Rk1CPIL4Moda1YUBA=
git.jlel.se/jlelse/template-strings v0.0.0-20220210101539-6a4e1cfaf1b4/go.mod h1:UNLE8cup2GTHbsE89xezRwq3GhKspPI9NyckPbgJEmw=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v0.4.1 h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw=
github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
@ -106,8 +106,8 @@ github.com/dmulholl/mp3lib v1.0.0/go.mod h1:4RoA+iht/khfwxmH1ieoxZTzYVbb0am/zdvF
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dvyukov/go-fuzz v0.0.0-20210103155950-6a8e9d1f2415/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw=
github.com/elnormous/contenttype v1.0.0 h1:cTLou7K7uQMsPEmRiTJosAznsPcYuoBmXMrFAf86t2A=
github.com/elnormous/contenttype v1.0.0/go.mod h1:ngVcyGGU8pnn4QJ5sL4StrNgc/wmXZXy5IQSBuHOFPg=
github.com/elnormous/contenttype v1.0.1 h1:SfPyQht9GlfIRC6ywjxZV6ks9S58q1665j3ms3eG5DA=
github.com/elnormous/contenttype v1.0.1/go.mod h1:ngVcyGGU8pnn4QJ5sL4StrNgc/wmXZXy5IQSBuHOFPg=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-sasl v0.0.0-20211008083017-0b9dcfb154ac h1:tn/OQ2PmwQ0XFVgAHfjlLyqMewry25Rz7jWnVoh4Ggs=
github.com/emersion/go-sasl v0.0.0-20211008083017-0b9dcfb154ac/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
@ -339,6 +339,8 @@ github.com/mdlayher/socket v0.0.0-20210307095302-262dc9984e00/go.mod h1:GAFlyu4/
github.com/mdlayher/socket v0.0.0-20211007213009-516dcbdf0267/go.mod h1:nFZ1EtZYK8Gi/k6QNu7z7CgO20i/4ExeQswwWuPmG/g=
github.com/mdlayher/socket v0.0.0-20211102153432-57e3fa563ecb h1:2dC7L10LmTqlyMVzFJ00qM25lqESg9Z4u3GuEXN5iHY=
github.com/mdlayher/socket v0.0.0-20211102153432-57e3fa563ecb/go.mod h1:nFZ1EtZYK8Gi/k6QNu7z7CgO20i/4ExeQswwWuPmG/g=
github.com/mergestat/timediff v0.0.3 h1:ucCNh4/ZrTPjFZ081PccNbhx9spymCJkFxSzgVuPU+Y=
github.com/mergestat/timediff v0.0.3/go.mod h1:yvMUaRu2oetc+9IbPLYBJviz6sA7xz8OXMDfhBl7YSI=
github.com/microcosm-cc/bluemonday v1.0.18 h1:6HcxvXDAi3ARt3slx6nTesbvorIc3QeTzBNRvWktHBo=
github.com/microcosm-cc/bluemonday v1.0.18/go.mod h1:Z0r70sCuXHig8YpBzCc5eGHAap2K7e/u082ZUpDRRqM=
github.com/miekg/dns v1.1.43 h1:JKfpVSCB84vrAmHzyrsxB5NAr5kLoMXZArPSw7Qlgyg=
@ -471,8 +473,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220209195652-db638375bc3a h1:atOEWVSedO4ksXBe/UrlbSLVxQQ9RxM/tT2Jy10IaHo=
golang.org/x/crypto v0.0.0-20220209195652-db638375bc3a/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2 h1:XdAboW3BNMv9ocSCOk/u1MFioZGzCNkiJZ19v9Oe3Ig=
golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=

0
templates/strings/de.yaml → strings/de.yaml

0
templates/strings/default.yaml → strings/default.yaml

0
templates/strings/pt-br.yaml → strings/pt-br.yaml

7
templateStrings.go

@ -1,14 +1,19 @@
package main
import (
"embed"
ts "git.jlel.se/jlelse/template-strings"
)
//go:embed strings/*
var stringsFiles embed.FS
func (a *goBlog) initTemplateStrings() (err error) {
var blogLangs []string
for _, b := range a.cfg.Blogs {
blogLangs = append(blogLangs, b.Lang)
}
a.ts, err = ts.InitTemplateStrings("templates/strings", ".yaml", "default", blogLangs...)
a.ts, err = ts.InitTemplateStringsFS(stringsFiles, "strings", ".yaml", "default", blogLangs...)
return err
}

8
ui.go

@ -7,6 +7,7 @@ import (
"github.com/hacdias/indieauth"
"github.com/kaorimatz/go-opml"
"github.com/mergestat/timediff"
"github.com/thoas/go-funk"
)
@ -1192,16 +1193,19 @@ func (a *goBlog) renderNotificationsAdmin(hb *htmlBuilder, rd *renderData) {
hb.writeEscaped(a.ts.GetTemplateStringVariant(rd.Blog.Lang, "notifications"))
hb.writeElementClose("h1")
// Notifications
tdLocale := matchTimeDiffLocale(rd.Blog.Lang)
for _, n := range nrd.notifications {
hb.writeElementOpen("div", "class", "p")
// Date
hb.writeElementOpen("p")
hb.writeElementOpen("i")
hb.writeEscaped(unixToLocalDateString(n.Time))
hb.writeEscaped(timediff.TimeDiff(time.Unix(n.Time, 0), timediff.WithLocale(tdLocale)))
hb.writeElementClose("i")
hb.writeElementClose("p")
// Message
_ = a.renderMarkdownToWriter(hb, n.Text, false)
hb.writeElementOpen("pre")
hb.writeEscaped(n.Text)
hb.writeElementClose("pre")
// Delete form
hb.writeElementOpen("form", "class", "actions", "method", "post", "action", "/notifications/delete")
hb.writeElementOpen("input", "type", "hidden", "name", "notificationid", "value", n.ID)

13
utils.go

@ -20,8 +20,10 @@ import (
"github.com/PuerkitoBio/goquery"
"github.com/araddon/dateparse"
"github.com/c2h5oh/datasize"
tdl "github.com/mergestat/timediff/locale"
"github.com/microcosm-cc/bluemonday"
"github.com/thoas/go-funk"
"golang.org/x/text/language"
)
type contextKey string
@ -363,3 +365,14 @@ func (valueOnlyContext) Done() <-chan struct{} {
func (valueOnlyContext) Err() error {
return nil
}
func matchTimeDiffLocale(lang string) tdl.Locale {
supportedLangs := []string{"en", "de", "es", "hi", "pt", "ru", "zh-CN"}
supportedTags := []language.Tag{}
for _, lang := range supportedLangs {
supportedTags = append(supportedTags, language.Make(lang))
}
matcher := language.NewMatcher(supportedTags)
_, idx, _ := matcher.Match(language.Make(lang))
return tdl.Locale(supportedLangs[idx])
}

10
utils_test.go

@ -114,3 +114,13 @@ func Test_defaultIfEmpty(t *testing.T) {
assert.Equal(t, "def", defaultIfEmpty("", "def"))
assert.Equal(t, "first", defaultIfEmpty("first", "def"))
}
func Test_matchTimeDiffLocale(t *testing.T) {
assert.Equal(t, "en", string(matchTimeDiffLocale("en-US")))
assert.Equal(t, "en", string(matchTimeDiffLocale("en")))
assert.Equal(t, "de", string(matchTimeDiffLocale("de")))
assert.Equal(t, "de", string(matchTimeDiffLocale("de-DE")))
assert.Equal(t, "de", string(matchTimeDiffLocale("de-AT")))
assert.Equal(t, "pt", string(matchTimeDiffLocale("pt-BR")))
assert.Equal(t, "pt", string(matchTimeDiffLocale("pt")))
}

Loading…
Cancel
Save