mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-01-07 02:10:26 +00:00
Viewutil: Introduce CopyEnRuWith
Reducing boilerplate as usual
This commit is contained in:
parent
9a540ba022
commit
5bc704b404
@ -8,18 +8,13 @@ import (
|
|||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
"sort"
|
||||||
"text/template"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitHandlers(rtr *mux.Router) {
|
func InitHandlers(rtr *mux.Router) {
|
||||||
rtr.PathPrefix("/backlinks/").HandlerFunc(handlerBacklinks)
|
rtr.PathPrefix("/backlinks/").HandlerFunc(handlerBacklinks)
|
||||||
rtr.PathPrefix("/orphans").HandlerFunc(handlerOrphans)
|
rtr.PathPrefix("/orphans").HandlerFunc(handlerOrphans)
|
||||||
chainBacklinks = viewutil.
|
chainBacklinks = viewutil.CopyEnRuWith(fs, "view_backlinks.html", ruTranslation)
|
||||||
En(viewutil.CopyEnWith(fs, "view_backlinks.html")).
|
chainOrphans = viewutil.CopyEnRuWith(fs, "view_orphans.html", ruTranslation)
|
||||||
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)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handlerBacklinks lists all backlinks to a hypha.
|
// handlerBacklinks lists all backlinks to a hypha.
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"github.com/bouncepaw/mycorrhiza/viewutil"
|
"github.com/bouncepaw/mycorrhiza/viewutil"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template" // TODO: Fight
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const ruTranslation = `
|
const ruTranslation = `
|
||||||
@ -29,17 +28,9 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func prepareViews() {
|
func prepareViews() {
|
||||||
m := template.Must
|
viewCardChain = viewutil.CopyEnRuWith(fs, "view_card.html", ruTranslation)
|
||||||
|
viewListChain = viewutil.CopyEnRuWith(fs, "view_list.html", ruTranslation)
|
||||||
viewCardChain = viewutil.
|
viewPageChain = viewutil.CopyEnRuWith(fs, "view_page.html", ruTranslation)
|
||||||
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)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type cardData struct {
|
type cardData struct {
|
||||||
|
@ -5,12 +5,10 @@ import (
|
|||||||
"github.com/bouncepaw/mycomarkup/v4"
|
"github.com/bouncepaw/mycomarkup/v4"
|
||||||
"github.com/bouncepaw/mycorrhiza/shroom"
|
"github.com/bouncepaw/mycorrhiza/shroom"
|
||||||
"github.com/bouncepaw/mycorrhiza/viewutil"
|
"github.com/bouncepaw/mycorrhiza/viewutil"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
|
|
||||||
"github.com/bouncepaw/mycomarkup/v4/mycocontext"
|
"github.com/bouncepaw/mycomarkup/v4/mycocontext"
|
||||||
)
|
)
|
||||||
@ -46,9 +44,7 @@ var (
|
|||||||
|
|
||||||
func InitHandlers(r *mux.Router) {
|
func InitHandlers(r *mux.Router) {
|
||||||
r.PathPrefix("/help").HandlerFunc(handlerHelp)
|
r.PathPrefix("/help").HandlerFunc(handlerHelp)
|
||||||
chain = viewutil.
|
chain = viewutil.CopyEnRuWith(fs, "view_help.html", ruTranslation)
|
||||||
En(viewutil.CopyEnWith(fs, "view_help.html")).
|
|
||||||
Ru(template.Must(viewutil.CopyRuWith(fs, "view_help.html").Parse(ruTranslation)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// handlerHelp gets the appropriate documentation or tells you where you (personally) have failed.
|
// handlerHelp gets the appropriate documentation or tells you where you (personally) have failed.
|
||||||
|
@ -13,7 +13,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitHandlers(rtr *mux.Router) {
|
func InitHandlers(rtr *mux.Router) {
|
||||||
@ -23,12 +22,8 @@ func InitHandlers(rtr *mux.Router) {
|
|||||||
http.Redirect(w, rq, "/recent-changes/20", http.StatusSeeOther)
|
http.Redirect(w, rq, "/recent-changes/20", http.StatusSeeOther)
|
||||||
})
|
})
|
||||||
|
|
||||||
chainPrimitiveDiff = viewutil.
|
chainPrimitiveDiff = viewutil.CopyEnRuWith(fs, "view_primitive_diff.html", ruTranslation)
|
||||||
En(viewutil.CopyEnWith(fs, "view_primitive_diff.html")).
|
chainRecentChanges = viewutil.CopyEnRuWith(fs, "view_recent_changes.html", ruTranslation)
|
||||||
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)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
|
func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"github.com/bouncepaw/mycorrhiza/viewutil"
|
"github.com/bouncepaw/mycorrhiza/viewutil"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -30,12 +29,8 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
chainNaviTitle = viewutil.
|
chainNaviTitle = viewutil.CopyEnRuWith(fs, "view_navititle.html", "")
|
||||||
En(viewutil.CopyEnWith(fs, "view_navititle.html")).
|
chainEmptyHypha = viewutil.CopyEnRuWith(fs, "view_empty_hypha.html", ruTranslation)
|
||||||
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)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type emptyHyphaData struct {
|
type emptyHyphaData struct {
|
||||||
|
@ -3,7 +3,6 @@ package misc
|
|||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"github.com/bouncepaw/mycorrhiza/viewutil"
|
"github.com/bouncepaw/mycorrhiza/viewutil"
|
||||||
"text/template"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -20,13 +19,8 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func initViews() {
|
func initViews() {
|
||||||
m := template.Must
|
chainList = viewutil.CopyEnRuWith(fs, "view_list.html", ruTranslation)
|
||||||
chainList = viewutil.
|
chainTitleSearch = viewutil.CopyEnRuWith(fs, "view_title_search.html", ruTranslation)
|
||||||
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)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type listDatum struct {
|
type listDatum struct {
|
||||||
|
@ -4,30 +4,30 @@ import "text/template"
|
|||||||
|
|
||||||
// Chain represents a chain of different language versions of the same template.
|
// Chain represents a chain of different language versions of the same template.
|
||||||
type Chain struct {
|
type Chain struct {
|
||||||
en *template.Template
|
_en *template.Template
|
||||||
ru *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.
|
// 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 {
|
func en(en *template.Template) Chain {
|
||||||
return Chain{
|
return Chain{
|
||||||
en: en,
|
_en: en,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ru adds a Russian translation to the Chain.
|
// ru adds a Russian translation to the Chain.
|
||||||
func (c Chain) Ru(ru *template.Template) Chain {
|
func (c Chain) ru(ru *template.Template) Chain {
|
||||||
c.ru = ru
|
c._ru = ru
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns an appropriate language representation for the given locale in meta.
|
// Get returns an appropriate language representation for the given locale in meta.
|
||||||
func (c Chain) Get(meta Meta) *template.Template {
|
func (c Chain) Get(meta Meta) *template.Template {
|
||||||
switch meta.Locale() {
|
switch meta.Locale() {
|
||||||
case "en":
|
case "_en":
|
||||||
return c.en
|
return c._en
|
||||||
case "ru":
|
case "_ru":
|
||||||
return c.ru
|
return c._ru
|
||||||
}
|
}
|
||||||
panic("unknown language " + meta.Locale())
|
panic("unknown language " + meta.Locale())
|
||||||
}
|
}
|
||||||
|
@ -75,7 +75,7 @@ func Init() {
|
|||||||
// TODO: get rid of this
|
// TODO: get rid of this
|
||||||
func localizedBaseWithWeirdBody(meta Meta) *template.Template {
|
func localizedBaseWithWeirdBody(meta Meta) *template.Template {
|
||||||
t := func() *template.Template {
|
t := func() *template.Template {
|
||||||
if meta.Locale() == "ru" {
|
if meta.Locale() == "_ru" {
|
||||||
return BaseRu
|
return BaseRu
|
||||||
}
|
}
|
||||||
return BaseEn
|
return BaseEn
|
||||||
@ -121,11 +121,16 @@ func Base(meta Meta, title, body string, headElements ...string) string {
|
|||||||
return w.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))
|
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))
|
return m(m(BaseRu.Clone()).ParseFS(fsys, f))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user