1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-01-05 17:40:26 +00:00

Viewutil: Introduce CopyEnRuWith

Reducing boilerplate as usual
This commit is contained in:
Timur Ismagilov 2022-05-18 19:58:24 +03:00
parent 9a540ba022
commit 5bc704b404
8 changed files with 33 additions and 62 deletions

View File

@ -8,18 +8,13 @@ import (
"github.com/gorilla/mux"
"net/http"
"sort"
"text/template"
)
func InitHandlers(rtr *mux.Router) {
rtr.PathPrefix("/backlinks/").HandlerFunc(handlerBacklinks)
rtr.PathPrefix("/orphans").HandlerFunc(handlerOrphans)
chainBacklinks = viewutil.
En(viewutil.CopyEnWith(fs, "view_backlinks.html")).
Ru(template.Must(viewutil.CopyRuWith(fs, "view_backlinks.html").Parse(ruTranslation)))
chainOrphans = viewutil.
En(viewutil.CopyEnWith(fs, "view_orphans.html")).
Ru(template.Must(viewutil.CopyRuWith(fs, "view_orphans.html").Parse(ruTranslation)))
chainBacklinks = viewutil.CopyEnRuWith(fs, "view_backlinks.html", ruTranslation)
chainOrphans = viewutil.CopyEnRuWith(fs, "view_orphans.html", ruTranslation)
}
// handlerBacklinks lists all backlinks to a hypha.

View File

@ -5,7 +5,6 @@ import (
"github.com/bouncepaw/mycorrhiza/viewutil"
"log"
"strings"
"text/template" // TODO: Fight
)
const ruTranslation = `
@ -29,17 +28,9 @@ var (
)
func prepareViews() {
m := template.Must
viewCardChain = viewutil.
En(viewutil.CopyEnWith(fs, "view_card.html")).
Ru(m(viewutil.CopyRuWith(fs, "view_card.html").Parse(ruTranslation)))
viewListChain = viewutil.
En(viewutil.CopyEnWith(fs, "view_list.html")).
Ru(m(viewutil.CopyRuWith(fs, "view_list.html").Parse(ruTranslation)))
viewPageChain = viewutil.
En(viewutil.CopyEnWith(fs, "view_page.html")).
Ru(m(viewutil.CopyRuWith(fs, "view_page.html").Parse(ruTranslation)))
viewCardChain = viewutil.CopyEnRuWith(fs, "view_card.html", ruTranslation)
viewListChain = viewutil.CopyEnRuWith(fs, "view_list.html", ruTranslation)
viewPageChain = viewutil.CopyEnRuWith(fs, "view_page.html", ruTranslation)
}
type cardData struct {

View File

@ -5,12 +5,10 @@ import (
"github.com/bouncepaw/mycomarkup/v4"
"github.com/bouncepaw/mycorrhiza/shroom"
"github.com/bouncepaw/mycorrhiza/viewutil"
"github.com/gorilla/mux"
"io"
"net/http"
"strings"
"text/template"
"github.com/gorilla/mux"
"github.com/bouncepaw/mycomarkup/v4/mycocontext"
)
@ -46,9 +44,7 @@ var (
func InitHandlers(r *mux.Router) {
r.PathPrefix("/help").HandlerFunc(handlerHelp)
chain = viewutil.
En(viewutil.CopyEnWith(fs, "view_help.html")).
Ru(template.Must(viewutil.CopyRuWith(fs, "view_help.html").Parse(ruTranslation)))
chain = viewutil.CopyEnRuWith(fs, "view_help.html", ruTranslation)
}
// handlerHelp gets the appropriate documentation or tells you where you (personally) have failed.

View File

@ -13,7 +13,6 @@ import (
"net/http"
"strconv"
"strings"
"text/template"
)
func InitHandlers(rtr *mux.Router) {
@ -23,12 +22,8 @@ func InitHandlers(rtr *mux.Router) {
http.Redirect(w, rq, "/recent-changes/20", http.StatusSeeOther)
})
chainPrimitiveDiff = viewutil.
En(viewutil.CopyEnWith(fs, "view_primitive_diff.html")).
Ru(template.Must(viewutil.CopyRuWith(fs, "view_primitive_diff.html").Parse(ruTranslation)))
chainRecentChanges = viewutil.
En(viewutil.CopyEnWith(fs, "view_recent_changes.html")).
Ru(template.Must(viewutil.CopyRuWith(fs, "view_recent_changes.html").Parse(ruTranslation)))
chainPrimitiveDiff = viewutil.CopyEnRuWith(fs, "view_primitive_diff.html", ruTranslation)
chainRecentChanges = viewutil.CopyEnRuWith(fs, "view_recent_changes.html", ruTranslation)
}
func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {

View File

@ -6,7 +6,6 @@ import (
"github.com/bouncepaw/mycorrhiza/viewutil"
"log"
"strings"
"text/template"
)
var (
@ -30,12 +29,8 @@ var (
)
func Init() {
chainNaviTitle = viewutil.
En(viewutil.CopyEnWith(fs, "view_navititle.html")).
Ru(viewutil.CopyRuWith(fs, "view_navititle.html")) // no text inside
chainEmptyHypha = viewutil.
En(viewutil.CopyEnWith(fs, "view_empty_hypha.html")).
Ru(template.Must(viewutil.CopyRuWith(fs, "view_empty_hypha.html").Parse(ruTranslation)))
chainNaviTitle = viewutil.CopyEnRuWith(fs, "view_navititle.html", "")
chainEmptyHypha = viewutil.CopyEnRuWith(fs, "view_empty_hypha.html", ruTranslation)
}
type emptyHyphaData struct {

View File

@ -3,7 +3,6 @@ package misc
import (
"embed"
"github.com/bouncepaw/mycorrhiza/viewutil"
"text/template"
)
var (
@ -20,13 +19,8 @@ var (
)
func initViews() {
m := template.Must
chainList = viewutil.
En(viewutil.CopyEnWith(fs, "view_list.html")).
Ru(m(viewutil.CopyRuWith(fs, "view_list.html").Parse(ruTranslation)))
chainTitleSearch = viewutil.
En(viewutil.CopyEnWith(fs, "view_title_search.html")).
Ru(m(viewutil.CopyRuWith(fs, "view_title_search.html").Parse(ruTranslation)))
chainList = viewutil.CopyEnRuWith(fs, "view_list.html", ruTranslation)
chainTitleSearch = viewutil.CopyEnRuWith(fs, "view_title_search.html", ruTranslation)
}
type listDatum struct {

View File

@ -4,30 +4,30 @@ import "text/template"
// Chain represents a chain of different language versions of the same template.
type Chain struct {
en *template.Template
ru *template.Template
_en *template.Template
_ru *template.Template
}
// En returns a new Chain. This is the only constructor of the type, so every view is forced to have an English representation.
func En(en *template.Template) Chain {
// en returns a new Chain. This is the only constructor of the type, so every view is forced to have an English representation.
func en(en *template.Template) Chain {
return Chain{
en: en,
_en: en,
}
}
// Ru adds a Russian translation to the Chain.
func (c Chain) Ru(ru *template.Template) Chain {
c.ru = ru
// ru adds a Russian translation to the Chain.
func (c Chain) ru(ru *template.Template) Chain {
c._ru = ru
return c
}
// Get returns an appropriate language representation for the given locale in meta.
func (c Chain) Get(meta Meta) *template.Template {
switch meta.Locale() {
case "en":
return c.en
case "ru":
return c.ru
case "_en":
return c._en
case "_ru":
return c._ru
}
panic("unknown language " + meta.Locale())
}

View File

@ -75,7 +75,7 @@ func Init() {
// TODO: get rid of this
func localizedBaseWithWeirdBody(meta Meta) *template.Template {
t := func() *template.Template {
if meta.Locale() == "ru" {
if meta.Locale() == "_ru" {
return BaseRu
}
return BaseEn
@ -121,11 +121,16 @@ func Base(meta Meta, title, body string, headElements ...string) string {
return w.String()
}
func CopyEnWith(fsys fs.FS, f string) *template.Template {
func CopyEnRuWith(fsys fs.FS, filename, ruTranslation string) Chain {
return en(copyEnWith(fsys, filename)).
ru(template.Must(copyRuWith(fsys, filename).Parse(ruTranslation)))
}
func copyEnWith(fsys fs.FS, f string) *template.Template {
return m(m(BaseEn.Clone()).ParseFS(fsys, f))
}
func CopyRuWith(fsys fs.FS, f string) *template.Template {
func copyRuWith(fsys fs.FS, f string) *template.Template {
return m(m(BaseRu.Clone()).ParseFS(fsys, f))
}