From 2769a096e53ecf2eb4bc2ee012e2513ada75df01 Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Wed, 30 Mar 2022 00:11:34 +0300 Subject: [PATCH] Alias views.Base to a wrapper to viewutil.Base --- views/base.go | 8 +- views/stuff.qtpl | 77 ------- views/stuff.qtpl.go | 536 +++++++++++++------------------------------ viewutil/meta.go | 2 +- viewutil/viewutil.go | 13 +- 5 files changed, 176 insertions(+), 460 deletions(-) diff --git a/views/base.go b/views/base.go index 1fc4ccd..4b7e50b 100644 --- a/views/base.go +++ b/views/base.go @@ -2,7 +2,11 @@ package views import ( "embed" + "github.com/bouncepaw/mycorrhiza/viewutil" ) -//go:embed *.html -var fs embed.FS +var ( + //go:embed *.html + fs embed.FS + Base = viewutil.Base +) diff --git a/views/stuff.qtpl b/views/stuff.qtpl index 3c164d5..9536c04 100644 --- a/views/stuff.qtpl +++ b/views/stuff.qtpl @@ -3,86 +3,9 @@ {% import "github.com/bouncepaw/mycorrhiza/cfg" %} {% import "github.com/bouncepaw/mycorrhiza/hyphae" %} -{% import "github.com/bouncepaw/mycorrhiza/user" %} {% import "github.com/bouncepaw/mycorrhiza/util" %} {% import "github.com/bouncepaw/mycorrhiza/l18n" %} -{% func Base(title, body string, lc *l18n.Localizer, u *user.User, headElements ...string) %} - - - - - - {%s title %} - - - - {% for _, el := range headElements %}{%s= el %}{% endfor %} - - -
- -
- {%s= body %} - - {%= commonScripts() %} - - - -{% endfunc %} - {% func TitleSearch(query string, generator func(string) <-chan string, lc *l18n.Localizer) %}
diff --git a/views/stuff.qtpl.go b/views/stuff.qtpl.go index ee439b0..043876f 100644 --- a/views/stuff.qtpl.go +++ b/views/stuff.qtpl.go @@ -17,318 +17,102 @@ import "github.com/bouncepaw/mycorrhiza/cfg" import "github.com/bouncepaw/mycorrhiza/hyphae" //line views/stuff.qtpl:6 -import "github.com/bouncepaw/mycorrhiza/user" - -//line views/stuff.qtpl:7 import "github.com/bouncepaw/mycorrhiza/util" -//line views/stuff.qtpl:8 +//line views/stuff.qtpl:7 import "github.com/bouncepaw/mycorrhiza/l18n" -//line views/stuff.qtpl:10 +//line views/stuff.qtpl:9 import ( qtio422016 "io" qt422016 "github.com/valyala/quicktemplate" ) -//line views/stuff.qtpl:10 +//line views/stuff.qtpl:9 var ( _ = qtio422016.Copy _ = qt422016.AcquireByteBuffer ) -//line views/stuff.qtpl:10 -func StreamBase(qw422016 *qt422016.Writer, title, body string, lc *l18n.Localizer, u *user.User, headElements ...string) { -//line views/stuff.qtpl:10 - qw422016.N().S(` - - - - - - `) -//line views/stuff.qtpl:16 - qw422016.E().S(title) -//line views/stuff.qtpl:16 - qw422016.N().S(` - - - - `) -//line views/stuff.qtpl:20 - for _, el := range headElements { -//line views/stuff.qtpl:20 - qw422016.N().S(el) -//line views/stuff.qtpl:20 - } -//line views/stuff.qtpl:20 - qw422016.N().S(` - - -
- -
- `) -//line views/stuff.qtpl:68 - qw422016.N().S(body) -//line views/stuff.qtpl:68 - qw422016.N().S(` - - `) -//line views/stuff.qtpl:80 - streamcommonScripts(qw422016) -//line views/stuff.qtpl:80 - qw422016.N().S(` - - - -`) -//line views/stuff.qtpl:84 -} - -//line views/stuff.qtpl:84 -func WriteBase(qq422016 qtio422016.Writer, title, body string, lc *l18n.Localizer, u *user.User, headElements ...string) { -//line views/stuff.qtpl:84 - qw422016 := qt422016.AcquireWriter(qq422016) -//line views/stuff.qtpl:84 - StreamBase(qw422016, title, body, lc, u, headElements...) -//line views/stuff.qtpl:84 - qt422016.ReleaseWriter(qw422016) -//line views/stuff.qtpl:84 -} - -//line views/stuff.qtpl:84 -func Base(title, body string, lc *l18n.Localizer, u *user.User, headElements ...string) string { -//line views/stuff.qtpl:84 - qb422016 := qt422016.AcquireByteBuffer() -//line views/stuff.qtpl:84 - WriteBase(qb422016, title, body, lc, u, headElements...) -//line views/stuff.qtpl:84 - qs422016 := string(qb422016.B) -//line views/stuff.qtpl:84 - qt422016.ReleaseByteBuffer(qb422016) -//line views/stuff.qtpl:84 - return qs422016 -//line views/stuff.qtpl:84 -} - -//line views/stuff.qtpl:86 +//line views/stuff.qtpl:9 func StreamTitleSearch(qw422016 *qt422016.Writer, query string, generator func(string) <-chan string, lc *l18n.Localizer) { -//line views/stuff.qtpl:86 +//line views/stuff.qtpl:9 qw422016.N().S(`

`) -//line views/stuff.qtpl:89 +//line views/stuff.qtpl:12 qw422016.E().S(lc.Get("ui.search_results_query", &l18n.Replacements{"query": query})) -//line views/stuff.qtpl:89 +//line views/stuff.qtpl:12 qw422016.N().S(`

`) -//line views/stuff.qtpl:90 +//line views/stuff.qtpl:13 qw422016.E().S(lc.Get("ui.search_results_desc")) -//line views/stuff.qtpl:90 +//line views/stuff.qtpl:13 qw422016.N().S(`

`) -//line views/stuff.qtpl:99 +//line views/stuff.qtpl:22 } -//line views/stuff.qtpl:99 +//line views/stuff.qtpl:22 func WriteTitleSearch(qq422016 qtio422016.Writer, query string, generator func(string) <-chan string, lc *l18n.Localizer) { -//line views/stuff.qtpl:99 +//line views/stuff.qtpl:22 qw422016 := qt422016.AcquireWriter(qq422016) -//line views/stuff.qtpl:99 +//line views/stuff.qtpl:22 StreamTitleSearch(qw422016, query, generator, lc) -//line views/stuff.qtpl:99 +//line views/stuff.qtpl:22 qt422016.ReleaseWriter(qw422016) -//line views/stuff.qtpl:99 +//line views/stuff.qtpl:22 } -//line views/stuff.qtpl:99 +//line views/stuff.qtpl:22 func TitleSearch(query string, generator func(string) <-chan string, lc *l18n.Localizer) string { -//line views/stuff.qtpl:99 +//line views/stuff.qtpl:22 qb422016 := qt422016.AcquireByteBuffer() -//line views/stuff.qtpl:99 +//line views/stuff.qtpl:22 WriteTitleSearch(qb422016, query, generator, lc) -//line views/stuff.qtpl:99 +//line views/stuff.qtpl:22 qs422016 := string(qb422016.B) -//line views/stuff.qtpl:99 +//line views/stuff.qtpl:22 qt422016.ReleaseByteBuffer(qb422016) -//line views/stuff.qtpl:99 +//line views/stuff.qtpl:22 return qs422016 -//line views/stuff.qtpl:99 +//line views/stuff.qtpl:22 } -//line views/stuff.qtpl:101 +//line views/stuff.qtpl:24 func StreamBacklinks(qw422016 *qt422016.Writer, hyphaName string, generator func(string) <-chan string, lc *l18n.Localizer) { -//line views/stuff.qtpl:101 +//line views/stuff.qtpl:24 qw422016.N().S(`

`) -//line views/stuff.qtpl:104 +//line views/stuff.qtpl:27 qw422016.N().S(lc.Get( "ui.backlinks_heading", &l18n.Replacements{ @@ -339,194 +123,194 @@ func StreamBacklinks(qw422016 *qt422016.Writer, hyphaName string, generator func ), }, )) -//line views/stuff.qtpl:113 +//line views/stuff.qtpl:36 qw422016.N().S(`

`) -//line views/stuff.qtpl:114 +//line views/stuff.qtpl:37 qw422016.E().S(lc.Get("ui.backlinks_desc")) -//line views/stuff.qtpl:114 +//line views/stuff.qtpl:37 qw422016.N().S(`

`) -//line views/stuff.qtpl:124 +//line views/stuff.qtpl:47 } -//line views/stuff.qtpl:124 +//line views/stuff.qtpl:47 func WriteBacklinks(qq422016 qtio422016.Writer, hyphaName string, generator func(string) <-chan string, lc *l18n.Localizer) { -//line views/stuff.qtpl:124 +//line views/stuff.qtpl:47 qw422016 := qt422016.AcquireWriter(qq422016) -//line views/stuff.qtpl:124 +//line views/stuff.qtpl:47 StreamBacklinks(qw422016, hyphaName, generator, lc) -//line views/stuff.qtpl:124 +//line views/stuff.qtpl:47 qt422016.ReleaseWriter(qw422016) -//line views/stuff.qtpl:124 +//line views/stuff.qtpl:47 } -//line views/stuff.qtpl:124 +//line views/stuff.qtpl:47 func Backlinks(hyphaName string, generator func(string) <-chan string, lc *l18n.Localizer) string { -//line views/stuff.qtpl:124 +//line views/stuff.qtpl:47 qb422016 := qt422016.AcquireByteBuffer() -//line views/stuff.qtpl:124 +//line views/stuff.qtpl:47 WriteBacklinks(qb422016, hyphaName, generator, lc) -//line views/stuff.qtpl:124 +//line views/stuff.qtpl:47 qs422016 := string(qb422016.B) -//line views/stuff.qtpl:124 +//line views/stuff.qtpl:47 qt422016.ReleaseByteBuffer(qb422016) -//line views/stuff.qtpl:124 +//line views/stuff.qtpl:47 return qs422016 -//line views/stuff.qtpl:124 +//line views/stuff.qtpl:47 } -//line views/stuff.qtpl:126 +//line views/stuff.qtpl:49 func StreamHelp(qw422016 *qt422016.Writer, content, lang string, lc *l18n.Localizer) { -//line views/stuff.qtpl:126 +//line views/stuff.qtpl:49 qw422016.N().S(`
`) -//line views/stuff.qtpl:130 +//line views/stuff.qtpl:53 qw422016.N().S(content) -//line views/stuff.qtpl:130 +//line views/stuff.qtpl:53 qw422016.N().S(`
`) -//line views/stuff.qtpl:133 +//line views/stuff.qtpl:56 qw422016.N().S(helpTopics(lang, lc)) -//line views/stuff.qtpl:133 +//line views/stuff.qtpl:56 qw422016.N().S(`
`) -//line views/stuff.qtpl:135 +//line views/stuff.qtpl:58 } -//line views/stuff.qtpl:135 +//line views/stuff.qtpl:58 func WriteHelp(qq422016 qtio422016.Writer, content, lang string, lc *l18n.Localizer) { -//line views/stuff.qtpl:135 +//line views/stuff.qtpl:58 qw422016 := qt422016.AcquireWriter(qq422016) -//line views/stuff.qtpl:135 +//line views/stuff.qtpl:58 StreamHelp(qw422016, content, lang, lc) -//line views/stuff.qtpl:135 +//line views/stuff.qtpl:58 qt422016.ReleaseWriter(qw422016) -//line views/stuff.qtpl:135 +//line views/stuff.qtpl:58 } -//line views/stuff.qtpl:135 +//line views/stuff.qtpl:58 func Help(content, lang string, lc *l18n.Localizer) string { -//line views/stuff.qtpl:135 +//line views/stuff.qtpl:58 qb422016 := qt422016.AcquireByteBuffer() -//line views/stuff.qtpl:135 +//line views/stuff.qtpl:58 WriteHelp(qb422016, content, lang, lc) -//line views/stuff.qtpl:135 +//line views/stuff.qtpl:58 qs422016 := string(qb422016.B) -//line views/stuff.qtpl:135 +//line views/stuff.qtpl:58 qt422016.ReleaseByteBuffer(qb422016) -//line views/stuff.qtpl:135 +//line views/stuff.qtpl:58 return qs422016 -//line views/stuff.qtpl:135 +//line views/stuff.qtpl:58 } -//line views/stuff.qtpl:137 +//line views/stuff.qtpl:60 func StreamHelpEmptyError(qw422016 *qt422016.Writer, lc *l18n.Localizer) { -//line views/stuff.qtpl:137 +//line views/stuff.qtpl:60 qw422016.N().S(`

`) -//line views/stuff.qtpl:138 +//line views/stuff.qtpl:61 qw422016.E().S(lc.Get("help.empty_error_title")) -//line views/stuff.qtpl:138 +//line views/stuff.qtpl:61 qw422016.N().S(`

`) -//line views/stuff.qtpl:139 +//line views/stuff.qtpl:62 qw422016.E().S(lc.Get("help.empty_error_line_1")) -//line views/stuff.qtpl:139 +//line views/stuff.qtpl:62 qw422016.N().S(`

`) -//line views/stuff.qtpl:140 +//line views/stuff.qtpl:63 qw422016.E().S(lc.Get("help.empty_error_line_2a")) -//line views/stuff.qtpl:140 +//line views/stuff.qtpl:63 qw422016.N().S(` `) -//line views/stuff.qtpl:140 +//line views/stuff.qtpl:63 qw422016.E().S(lc.Get("help.empty_error_link")) -//line views/stuff.qtpl:140 +//line views/stuff.qtpl:63 qw422016.N().S(` `) -//line views/stuff.qtpl:140 +//line views/stuff.qtpl:63 qw422016.E().S(lc.Get("help.empty_error_line_2b")) -//line views/stuff.qtpl:140 +//line views/stuff.qtpl:63 qw422016.N().S(`

`) -//line views/stuff.qtpl:141 +//line views/stuff.qtpl:64 } -//line views/stuff.qtpl:141 +//line views/stuff.qtpl:64 func WriteHelpEmptyError(qq422016 qtio422016.Writer, lc *l18n.Localizer) { -//line views/stuff.qtpl:141 +//line views/stuff.qtpl:64 qw422016 := qt422016.AcquireWriter(qq422016) -//line views/stuff.qtpl:141 +//line views/stuff.qtpl:64 StreamHelpEmptyError(qw422016, lc) -//line views/stuff.qtpl:141 +//line views/stuff.qtpl:64 qt422016.ReleaseWriter(qw422016) -//line views/stuff.qtpl:141 +//line views/stuff.qtpl:64 } -//line views/stuff.qtpl:141 +//line views/stuff.qtpl:64 func HelpEmptyError(lc *l18n.Localizer) string { -//line views/stuff.qtpl:141 +//line views/stuff.qtpl:64 qb422016 := qt422016.AcquireByteBuffer() -//line views/stuff.qtpl:141 +//line views/stuff.qtpl:64 WriteHelpEmptyError(qb422016, lc) -//line views/stuff.qtpl:141 +//line views/stuff.qtpl:64 qs422016 := string(qb422016.B) -//line views/stuff.qtpl:141 +//line views/stuff.qtpl:64 qt422016.ReleaseByteBuffer(qb422016) -//line views/stuff.qtpl:141 +//line views/stuff.qtpl:64 return qs422016 -//line views/stuff.qtpl:141 +//line views/stuff.qtpl:64 } -//line views/stuff.qtpl:143 +//line views/stuff.qtpl:66 func StreamHyphaList(qw422016 *qt422016.Writer, lc *l18n.Localizer) { -//line views/stuff.qtpl:143 +//line views/stuff.qtpl:66 qw422016.N().S(`

`) -//line views/stuff.qtpl:146 +//line views/stuff.qtpl:69 qw422016.E().S(lc.Get("ui.list_heading")) -//line views/stuff.qtpl:146 +//line views/stuff.qtpl:69 qw422016.N().S(`

`) -//line views/stuff.qtpl:147 +//line views/stuff.qtpl:70 qw422016.E().S(lc.GetPlural("ui.list_desc", hyphae.Count())) -//line views/stuff.qtpl:147 +//line views/stuff.qtpl:70 qw422016.N().S(`

    `) -//line views/stuff.qtpl:150 +//line views/stuff.qtpl:73 hyphaNames := make(chan string) sortedHypha := hyphae.PathographicSort(hyphaNames) for hypha := range hyphae.YieldExistingHyphae() { @@ -534,134 +318,134 @@ func StreamHyphaList(qw422016 *qt422016.Writer, lc *l18n.Localizer) { } close(hyphaNames) -//line views/stuff.qtpl:156 +//line views/stuff.qtpl:79 qw422016.N().S(` `) -//line views/stuff.qtpl:157 +//line views/stuff.qtpl:80 for hyphaName := range sortedHypha { -//line views/stuff.qtpl:157 +//line views/stuff.qtpl:80 qw422016.N().S(` `) -//line views/stuff.qtpl:158 +//line views/stuff.qtpl:81 h := hyphae.ByName(hyphaName) -//line views/stuff.qtpl:158 +//line views/stuff.qtpl:81 qw422016.N().S(`
  • `) -//line views/stuff.qtpl:161 +//line views/stuff.qtpl:84 qw422016.E().S(util.BeautifulName(h.CanonicalName())) -//line views/stuff.qtpl:161 +//line views/stuff.qtpl:84 qw422016.N().S(` `) -//line views/stuff.qtpl:163 +//line views/stuff.qtpl:86 switch h := h.(type) { -//line views/stuff.qtpl:164 +//line views/stuff.qtpl:87 case *hyphae.MediaHypha: -//line views/stuff.qtpl:164 +//line views/stuff.qtpl:87 qw422016.N().S(` `) -//line views/stuff.qtpl:166 +//line views/stuff.qtpl:89 qw422016.E().S(filepath.Ext(h.MediaFilePath())[1:]) -//line views/stuff.qtpl:166 +//line views/stuff.qtpl:89 qw422016.N().S(` `) -//line views/stuff.qtpl:168 +//line views/stuff.qtpl:91 } -//line views/stuff.qtpl:168 +//line views/stuff.qtpl:91 qw422016.N().S(`
  • `) -//line views/stuff.qtpl:170 +//line views/stuff.qtpl:93 } -//line views/stuff.qtpl:170 +//line views/stuff.qtpl:93 qw422016.N().S(`
`) -//line views/stuff.qtpl:174 +//line views/stuff.qtpl:97 } -//line views/stuff.qtpl:174 +//line views/stuff.qtpl:97 func WriteHyphaList(qq422016 qtio422016.Writer, lc *l18n.Localizer) { -//line views/stuff.qtpl:174 +//line views/stuff.qtpl:97 qw422016 := qt422016.AcquireWriter(qq422016) -//line views/stuff.qtpl:174 +//line views/stuff.qtpl:97 StreamHyphaList(qw422016, lc) -//line views/stuff.qtpl:174 +//line views/stuff.qtpl:97 qt422016.ReleaseWriter(qw422016) -//line views/stuff.qtpl:174 +//line views/stuff.qtpl:97 } -//line views/stuff.qtpl:174 +//line views/stuff.qtpl:97 func HyphaList(lc *l18n.Localizer) string { -//line views/stuff.qtpl:174 +//line views/stuff.qtpl:97 qb422016 := qt422016.AcquireByteBuffer() -//line views/stuff.qtpl:174 +//line views/stuff.qtpl:97 WriteHyphaList(qb422016, lc) -//line views/stuff.qtpl:174 +//line views/stuff.qtpl:97 qs422016 := string(qb422016.B) -//line views/stuff.qtpl:174 +//line views/stuff.qtpl:97 qt422016.ReleaseByteBuffer(qb422016) -//line views/stuff.qtpl:174 +//line views/stuff.qtpl:97 return qs422016 -//line views/stuff.qtpl:174 +//line views/stuff.qtpl:97 } -//line views/stuff.qtpl:176 +//line views/stuff.qtpl:99 func streamcommonScripts(qw422016 *qt422016.Writer) { -//line views/stuff.qtpl:176 +//line views/stuff.qtpl:99 qw422016.N().S(` `) -//line views/stuff.qtpl:177 +//line views/stuff.qtpl:100 for _, scriptPath := range cfg.CommonScripts { -//line views/stuff.qtpl:177 +//line views/stuff.qtpl:100 qw422016.N().S(` `) -//line views/stuff.qtpl:179 +//line views/stuff.qtpl:102 } -//line views/stuff.qtpl:179 +//line views/stuff.qtpl:102 qw422016.N().S(` `) -//line views/stuff.qtpl:180 +//line views/stuff.qtpl:103 } -//line views/stuff.qtpl:180 +//line views/stuff.qtpl:103 func writecommonScripts(qq422016 qtio422016.Writer) { -//line views/stuff.qtpl:180 +//line views/stuff.qtpl:103 qw422016 := qt422016.AcquireWriter(qq422016) -//line views/stuff.qtpl:180 +//line views/stuff.qtpl:103 streamcommonScripts(qw422016) -//line views/stuff.qtpl:180 +//line views/stuff.qtpl:103 qt422016.ReleaseWriter(qw422016) -//line views/stuff.qtpl:180 +//line views/stuff.qtpl:103 } -//line views/stuff.qtpl:180 +//line views/stuff.qtpl:103 func commonScripts() string { -//line views/stuff.qtpl:180 +//line views/stuff.qtpl:103 qb422016 := qt422016.AcquireByteBuffer() -//line views/stuff.qtpl:180 +//line views/stuff.qtpl:103 writecommonScripts(qb422016) -//line views/stuff.qtpl:180 +//line views/stuff.qtpl:103 qs422016 := string(qb422016.B) -//line views/stuff.qtpl:180 +//line views/stuff.qtpl:103 qt422016.ReleaseByteBuffer(qb422016) -//line views/stuff.qtpl:180 +//line views/stuff.qtpl:103 return qs422016 -//line views/stuff.qtpl:180 +//line views/stuff.qtpl:103 } diff --git a/viewutil/meta.go b/viewutil/meta.go index c51db6e..36a4e90 100644 --- a/viewutil/meta.go +++ b/viewutil/meta.go @@ -23,6 +23,6 @@ func MetaFrom(w http.ResponseWriter, rq *http.Request) Meta { } } -func (m *Meta) Locale() string { +func (m Meta) Locale() string { return m.Lc.Locale } diff --git a/viewutil/viewutil.go b/viewutil/viewutil.go index f023101..82b67a0 100644 --- a/viewutil/viewutil.go +++ b/viewutil/viewutil.go @@ -7,13 +7,14 @@ import ( "github.com/bouncepaw/mycorrhiza/cfg" "github.com/bouncepaw/mycorrhiza/l18n" "github.com/bouncepaw/mycorrhiza/user" - "html/template" + "github.com/bouncepaw/mycorrhiza/util" "log" "strings" + "text/template" // TODO: save the world ) var ( - //go:embed viewutil.go + //go:embed *.html fs embed.FS BaseEn *template.Template BaseRu *template.Template @@ -31,7 +32,11 @@ func Init() { dataText := fmt.Sprintf(` {{define "wiki name"}}%s{{end}} `, cfg.WikiName) - BaseEn = m(m(template.ParseFS(fs, "viewutil.go")).Parse(dataText)) + BaseEn = m(m(template.New(""). + Funcs(template.FuncMap{ + "beautifulName": util.BeautifulName, + }).ParseFS(fs, "base.html")). + Parse(dataText)) if !cfg.UseAuth { m(BaseEn.Parse(`{{define "auth"}}{{end}}`)) } @@ -70,7 +75,7 @@ func Base(title, body string, lc *l18n.Localizer, u *user.User, headElements ... W: &w, } t := localizedBaseWithWeirdBody(meta) - err := t.Execute(&w, baseData{ + err := t.ExecuteTemplate(&w, "base", baseData{ Meta: meta, Title: title, HeadElements: headElements,