From 2dcb1a5fe7a90072ef457ba41eca976d6e39773b Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Sat, 2 Apr 2022 19:58:57 +0300 Subject: [PATCH] Backlinks: Isolate --- {hyphae/backlinks => backlinks}/backlinks.go | 6 +- {hyphae/backlinks => backlinks}/hooks.go | 0 backlinks/view_backlinks.html | 17 ++ backlinks/web.go | 62 ++++++ l18n/en/ui.json | 4 - l18n/l18n.go | 6 +- main.go | 3 +- misc/handlers.go | 2 +- shroom/delete.go | 3 +- shroom/rename.go | 2 +- shroom/upload.go | 2 +- views/nav.qtpl | 2 +- views/nav.qtpl.go | 2 +- views/stuff.qtpl | 28 --- views/stuff.qtpl.go | 222 ++++++------------- web/backlinks.go | 30 --- web/web.go | 3 +- 17 files changed, 162 insertions(+), 232 deletions(-) rename {hyphae/backlinks => backlinks}/backlinks.go (95%) rename {hyphae/backlinks => backlinks}/hooks.go (100%) create mode 100644 backlinks/view_backlinks.html create mode 100644 backlinks/web.go delete mode 100644 web/backlinks.go diff --git a/hyphae/backlinks/backlinks.go b/backlinks/backlinks.go similarity index 95% rename from hyphae/backlinks/backlinks.go rename to backlinks/backlinks.go index 6ed43b0..605d8bd 100644 --- a/hyphae/backlinks/backlinks.go +++ b/backlinks/backlinks.go @@ -9,8 +9,8 @@ import ( "github.com/bouncepaw/mycorrhiza/util" ) -// YieldHyphaBacklinks gets backlinks for the desired hypha, sorts and yields them one by one. -func YieldHyphaBacklinks(hyphaName string) <-chan string { +// yieldHyphaBacklinks gets backlinks for the desired hypha, sorts and yields them one by one. +func yieldHyphaBacklinks(hyphaName string) <-chan string { hyphaName = util.CanonicalName(hyphaName) out := make(chan string) sorted := hyphae.PathographicSort(out) @@ -147,7 +147,7 @@ type backlinkIndexRenaming struct { links []string } -// Apply changes backlink index respective to the operation data +// apply changes backlink index respective to the operation data func (op backlinkIndexRenaming) apply() { for _, link := range op.links { if lSet, exists := backlinkIndex[link]; exists { diff --git a/hyphae/backlinks/hooks.go b/backlinks/hooks.go similarity index 100% rename from hyphae/backlinks/hooks.go rename to backlinks/hooks.go diff --git a/backlinks/view_backlinks.html b/backlinks/view_backlinks.html new file mode 100644 index 0000000..597fe32 --- /dev/null +++ b/backlinks/view_backlinks.html @@ -0,0 +1,17 @@ +{{define "backlinks to text"}}Backlinks to {{.}}{{end}} +{{define "title"}}{{template "backlinks to text" .HyphaName}}{{end}} +{{define "body"}} +
+
+

{{block "backlinks to link" .HyphaName}}Backlinks to {{beautifulName .}}{{end}}

+

{{block "description" .}}Hyphae which have a link to this hypha, embed it as an image or transclude it are listed below.{{end}}

+ +
+
+{{end}} \ No newline at end of file diff --git a/backlinks/web.go b/backlinks/web.go new file mode 100644 index 0000000..278b936 --- /dev/null +++ b/backlinks/web.go @@ -0,0 +1,62 @@ +package backlinks + +import ( + "embed" + "github.com/bouncepaw/mycorrhiza/cfg" + "github.com/bouncepaw/mycorrhiza/util" + "github.com/bouncepaw/mycorrhiza/viewutil" + "github.com/gorilla/mux" + "log" + "net/http" + "text/template" +) + +func InitHandlers(rtr *mux.Router) { + rtr.PathPrefix("/backlinks/").HandlerFunc(handlerBacklinks) + chain = viewutil. + En(viewutil.CopyEnWith(fs, "view_backlinks.html")). + Ru(template.Must(viewutil.CopyRuWith(fs, "view_backlinks.html").Parse(ruTranslation))) +} + +// handlerBacklinks lists all backlinks to a hypha. +func handlerBacklinks(w http.ResponseWriter, rq *http.Request) { + var ( + hyphaName = util.HyphaNameFromRq(rq, "backlinks") + backlinks []string + ) + for b := range yieldHyphaBacklinks(hyphaName) { + backlinks = append(backlinks, b) + } + viewBacklinks(viewutil.MetaFrom(w, rq), hyphaName, backlinks) +} + +var ( + //go:embed *.html + fs embed.FS + ruTranslation = ` +{{define "backlinks to text"}}Обратные ссылки на {{.}}{{end}} +{{define "backlinks to link"}}Обратные ссылки на {{beautifulName .}}{{end}} +{{define "description"}}Ниже перечислены гифы, на которых есть ссылка на эту гифу, трансклюзия этой гифы или эта гифа вставлена как изображение.{{end}} +` + chain viewutil.Chain +) + +type backlinksData struct { + viewutil.BaseData + HyphaName string + Backlinks []string +} + +func viewBacklinks(meta viewutil.Meta, hyphaName string, backlinks []string) { + if err := chain.Get(meta).ExecuteTemplate(meta.W, "page", backlinksData{ + BaseData: viewutil.BaseData{ + Meta: meta, + HeaderLinks: cfg.HeaderLinks, + CommonScripts: cfg.CommonScripts, + }, + HyphaName: hyphaName, + Backlinks: backlinks, + }); err != nil { + log.Println(err) + } +} diff --git a/l18n/en/ui.json b/l18n/en/ui.json index 800bf0b..485429a 100644 --- a/l18n/en/ui.json +++ b/l18n/en/ui.json @@ -4,10 +4,6 @@ "title_search": "Search by title", "admin_panel": "Admin panel", - "backlinks_title": "Backlinks to {{.hypha_name}}", - "backlinks_heading": "Backlinks to {{.hypha_link}}", - "backlinks_desc": "Hyphae which have a link to this hypha, embed it as an image or transclude it are listed below.", - "edit_link": "Edit text", "logout_link": "Log out", "history_link": "View history", diff --git a/l18n/l18n.go b/l18n/l18n.go index da87f53..b557de0 100644 --- a/l18n/l18n.go +++ b/l18n/l18n.go @@ -118,7 +118,7 @@ func (t Localizer) GetWithLocale(locale, key string, replacements ...*Replacemen // If the str doesn't have any substitutions, no need to // template.Execute. - if strings.Contains(str, "}}") { + if !strings.Contains(str, "}}") { return str } @@ -145,7 +145,7 @@ func (t Localizer) GetPlural(key string, n int, replacements ...*Replacements) s // As in the original, we skip templating if have nothing to replace // (however, it's strange case for plurals) - if strings.Contains(str, "}}") { + if !strings.Contains(str, "}}") { return str } @@ -164,7 +164,7 @@ func (t Localizer) GetPlural64(key string, n int64, replacements ...*Replacement // As in the original, we skip templating if have nothing to replace // (however, it's strange case for plurals) - if strings.Contains(str, "}}") { + if !strings.Contains(str, "}}") { return str } diff --git a/main.go b/main.go index 3fec678..a67ac4c 100644 --- a/main.go +++ b/main.go @@ -5,14 +5,13 @@ package main import ( + "github.com/bouncepaw/mycorrhiza/backlinks" "github.com/bouncepaw/mycorrhiza/categories" "github.com/bouncepaw/mycorrhiza/migration" "github.com/bouncepaw/mycorrhiza/viewutil" "log" "os" - "github.com/bouncepaw/mycorrhiza/hyphae/backlinks" - "github.com/bouncepaw/mycorrhiza/cfg" "github.com/bouncepaw/mycorrhiza/files" "github.com/bouncepaw/mycorrhiza/history" diff --git a/misc/handlers.go b/misc/handlers.go index cdf81c1..f15a129 100644 --- a/misc/handlers.go +++ b/misc/handlers.go @@ -2,10 +2,10 @@ package misc import ( + "github.com/bouncepaw/mycorrhiza/backlinks" "github.com/bouncepaw/mycorrhiza/cfg" "github.com/bouncepaw/mycorrhiza/files" "github.com/bouncepaw/mycorrhiza/hyphae" - "github.com/bouncepaw/mycorrhiza/hyphae/backlinks" "github.com/bouncepaw/mycorrhiza/l18n" "github.com/bouncepaw/mycorrhiza/shroom" "github.com/bouncepaw/mycorrhiza/static" diff --git a/shroom/delete.go b/shroom/delete.go index 1227dcb..958262e 100644 --- a/shroom/delete.go +++ b/shroom/delete.go @@ -2,8 +2,7 @@ package shroom import ( "fmt" - "github.com/bouncepaw/mycorrhiza/hyphae/backlinks" - + "github.com/bouncepaw/mycorrhiza/backlinks" "github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/hyphae" "github.com/bouncepaw/mycorrhiza/user" diff --git a/shroom/rename.go b/shroom/rename.go index b232b63..71b4e9c 100644 --- a/shroom/rename.go +++ b/shroom/rename.go @@ -3,7 +3,7 @@ package shroom import ( "errors" "fmt" - "github.com/bouncepaw/mycorrhiza/hyphae/backlinks" + "github.com/bouncepaw/mycorrhiza/backlinks" "regexp" "github.com/bouncepaw/mycorrhiza/history" diff --git a/shroom/upload.go b/shroom/upload.go index df04d7c..54d34cf 100644 --- a/shroom/upload.go +++ b/shroom/upload.go @@ -4,10 +4,10 @@ import ( "bytes" "errors" "fmt" + "github.com/bouncepaw/mycorrhiza/backlinks" "github.com/bouncepaw/mycorrhiza/files" "github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/hyphae" - "github.com/bouncepaw/mycorrhiza/hyphae/backlinks" "github.com/bouncepaw/mycorrhiza/mimetype" "github.com/bouncepaw/mycorrhiza/user" "io" diff --git a/views/nav.qtpl b/views/nav.qtpl index 132beb3..58f94f4 100644 --- a/views/nav.qtpl +++ b/views/nav.qtpl @@ -1,6 +1,6 @@ {% import "strings" %} {% import "github.com/bouncepaw/mycorrhiza/cfg" %} -{% import "github.com/bouncepaw/mycorrhiza/hyphae/backlinks" %} +{% import "github.com/bouncepaw/mycorrhiza/backlinks" %} {% import "github.com/bouncepaw/mycorrhiza/l18n" %} {% import "github.com/bouncepaw/mycorrhiza/user" %} {% import "github.com/bouncepaw/mycorrhiza/hyphae" %} diff --git a/views/nav.qtpl.go b/views/nav.qtpl.go index 3965210..83cc255 100644 --- a/views/nav.qtpl.go +++ b/views/nav.qtpl.go @@ -11,7 +11,7 @@ import "strings" import "github.com/bouncepaw/mycorrhiza/cfg" //line views/nav.qtpl:3 -import "github.com/bouncepaw/mycorrhiza/hyphae/backlinks" +import "github.com/bouncepaw/mycorrhiza/backlinks" //line views/nav.qtpl:4 import "github.com/bouncepaw/mycorrhiza/l18n" diff --git a/views/stuff.qtpl b/views/stuff.qtpl index 99a28db..980c9bd 100644 --- a/views/stuff.qtpl +++ b/views/stuff.qtpl @@ -1,34 +1,6 @@ -{% import "fmt" %} - {% import "github.com/bouncepaw/mycorrhiza/cfg" %} -{% import "github.com/bouncepaw/mycorrhiza/util" %} {% import "github.com/bouncepaw/mycorrhiza/l18n" %} -{% func Backlinks(hyphaName string, generator func(string) <-chan string, lc *l18n.Localizer) %} -
-
-

{%s= lc.Get( - "ui.backlinks_heading", - &l18n.Replacements{ - "hypha_link": fmt.Sprintf( - `%s`, - hyphaName, - util.BeautifulName(hyphaName), - ), - }, - )%}

-

{%s lc.Get("ui.backlinks_desc")%}

- -
-
-{% endfunc %} - {% func Help(content, lang string, lc *l18n.Localizer) %}
diff --git a/views/stuff.qtpl.go b/views/stuff.qtpl.go index 5abfcf6..0298658 100644 --- a/views/stuff.qtpl.go +++ b/views/stuff.qtpl.go @@ -5,262 +5,176 @@ package views //line views/stuff.qtpl:1 -import "fmt" - -//line views/stuff.qtpl:3 import "github.com/bouncepaw/mycorrhiza/cfg" -//line views/stuff.qtpl:4 -import "github.com/bouncepaw/mycorrhiza/util" - -//line views/stuff.qtpl:5 +//line views/stuff.qtpl:2 import "github.com/bouncepaw/mycorrhiza/l18n" -//line views/stuff.qtpl:7 +//line views/stuff.qtpl:4 import ( qtio422016 "io" qt422016 "github.com/valyala/quicktemplate" ) -//line views/stuff.qtpl:7 +//line views/stuff.qtpl:4 var ( _ = qtio422016.Copy _ = qt422016.AcquireByteBuffer ) -//line views/stuff.qtpl:7 -func StreamBacklinks(qw422016 *qt422016.Writer, hyphaName string, generator func(string) <-chan string, lc *l18n.Localizer) { -//line views/stuff.qtpl:7 - qw422016.N().S(` -
-
-

`) -//line views/stuff.qtpl:10 - qw422016.N().S(lc.Get( - "ui.backlinks_heading", - &l18n.Replacements{ - "hypha_link": fmt.Sprintf( - `%s`, - hyphaName, - util.BeautifulName(hyphaName), - ), - }, - )) -//line views/stuff.qtpl:19 - qw422016.N().S(`

-

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

- -
-
-`) -//line views/stuff.qtpl:30 -} - -//line views/stuff.qtpl:30 -func WriteBacklinks(qq422016 qtio422016.Writer, hyphaName string, generator func(string) <-chan string, lc *l18n.Localizer) { -//line views/stuff.qtpl:30 - qw422016 := qt422016.AcquireWriter(qq422016) -//line views/stuff.qtpl:30 - StreamBacklinks(qw422016, hyphaName, generator, lc) -//line views/stuff.qtpl:30 - qt422016.ReleaseWriter(qw422016) -//line views/stuff.qtpl:30 -} - -//line views/stuff.qtpl:30 -func Backlinks(hyphaName string, generator func(string) <-chan string, lc *l18n.Localizer) string { -//line views/stuff.qtpl:30 - qb422016 := qt422016.AcquireByteBuffer() -//line views/stuff.qtpl:30 - WriteBacklinks(qb422016, hyphaName, generator, lc) -//line views/stuff.qtpl:30 - qs422016 := string(qb422016.B) -//line views/stuff.qtpl:30 - qt422016.ReleaseByteBuffer(qb422016) -//line views/stuff.qtpl:30 - return qs422016 -//line views/stuff.qtpl:30 -} - -//line views/stuff.qtpl:32 +//line views/stuff.qtpl:4 func StreamHelp(qw422016 *qt422016.Writer, content, lang string, lc *l18n.Localizer) { -//line views/stuff.qtpl:32 +//line views/stuff.qtpl:4 qw422016.N().S(`
`) -//line views/stuff.qtpl:36 +//line views/stuff.qtpl:8 qw422016.N().S(content) -//line views/stuff.qtpl:36 +//line views/stuff.qtpl:8 qw422016.N().S(`
`) -//line views/stuff.qtpl:39 +//line views/stuff.qtpl:11 qw422016.N().S(helpTopics(lang, lc)) -//line views/stuff.qtpl:39 +//line views/stuff.qtpl:11 qw422016.N().S(`
`) -//line views/stuff.qtpl:41 +//line views/stuff.qtpl:13 } -//line views/stuff.qtpl:41 +//line views/stuff.qtpl:13 func WriteHelp(qq422016 qtio422016.Writer, content, lang string, lc *l18n.Localizer) { -//line views/stuff.qtpl:41 +//line views/stuff.qtpl:13 qw422016 := qt422016.AcquireWriter(qq422016) -//line views/stuff.qtpl:41 +//line views/stuff.qtpl:13 StreamHelp(qw422016, content, lang, lc) -//line views/stuff.qtpl:41 +//line views/stuff.qtpl:13 qt422016.ReleaseWriter(qw422016) -//line views/stuff.qtpl:41 +//line views/stuff.qtpl:13 } -//line views/stuff.qtpl:41 +//line views/stuff.qtpl:13 func Help(content, lang string, lc *l18n.Localizer) string { -//line views/stuff.qtpl:41 +//line views/stuff.qtpl:13 qb422016 := qt422016.AcquireByteBuffer() -//line views/stuff.qtpl:41 +//line views/stuff.qtpl:13 WriteHelp(qb422016, content, lang, lc) -//line views/stuff.qtpl:41 +//line views/stuff.qtpl:13 qs422016 := string(qb422016.B) -//line views/stuff.qtpl:41 +//line views/stuff.qtpl:13 qt422016.ReleaseByteBuffer(qb422016) -//line views/stuff.qtpl:41 +//line views/stuff.qtpl:13 return qs422016 -//line views/stuff.qtpl:41 +//line views/stuff.qtpl:13 } -//line views/stuff.qtpl:43 +//line views/stuff.qtpl:15 func StreamHelpEmptyError(qw422016 *qt422016.Writer, lc *l18n.Localizer) { -//line views/stuff.qtpl:43 +//line views/stuff.qtpl:15 qw422016.N().S(`

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

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

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

`) -//line views/stuff.qtpl:47 +//line views/stuff.qtpl:19 } -//line views/stuff.qtpl:47 +//line views/stuff.qtpl:19 func WriteHelpEmptyError(qq422016 qtio422016.Writer, lc *l18n.Localizer) { -//line views/stuff.qtpl:47 +//line views/stuff.qtpl:19 qw422016 := qt422016.AcquireWriter(qq422016) -//line views/stuff.qtpl:47 +//line views/stuff.qtpl:19 StreamHelpEmptyError(qw422016, lc) -//line views/stuff.qtpl:47 +//line views/stuff.qtpl:19 qt422016.ReleaseWriter(qw422016) -//line views/stuff.qtpl:47 +//line views/stuff.qtpl:19 } -//line views/stuff.qtpl:47 +//line views/stuff.qtpl:19 func HelpEmptyError(lc *l18n.Localizer) string { -//line views/stuff.qtpl:47 +//line views/stuff.qtpl:19 qb422016 := qt422016.AcquireByteBuffer() -//line views/stuff.qtpl:47 +//line views/stuff.qtpl:19 WriteHelpEmptyError(qb422016, lc) -//line views/stuff.qtpl:47 +//line views/stuff.qtpl:19 qs422016 := string(qb422016.B) -//line views/stuff.qtpl:47 +//line views/stuff.qtpl:19 qt422016.ReleaseByteBuffer(qb422016) -//line views/stuff.qtpl:47 +//line views/stuff.qtpl:19 return qs422016 -//line views/stuff.qtpl:47 +//line views/stuff.qtpl:19 } -//line views/stuff.qtpl:49 +//line views/stuff.qtpl:21 func streamcommonScripts(qw422016 *qt422016.Writer) { -//line views/stuff.qtpl:49 +//line views/stuff.qtpl:21 qw422016.N().S(` `) -//line views/stuff.qtpl:50 +//line views/stuff.qtpl:22 for _, scriptPath := range cfg.CommonScripts { -//line views/stuff.qtpl:50 +//line views/stuff.qtpl:22 qw422016.N().S(` `) -//line views/stuff.qtpl:52 +//line views/stuff.qtpl:24 } -//line views/stuff.qtpl:52 +//line views/stuff.qtpl:24 qw422016.N().S(` `) -//line views/stuff.qtpl:53 +//line views/stuff.qtpl:25 } -//line views/stuff.qtpl:53 +//line views/stuff.qtpl:25 func writecommonScripts(qq422016 qtio422016.Writer) { -//line views/stuff.qtpl:53 +//line views/stuff.qtpl:25 qw422016 := qt422016.AcquireWriter(qq422016) -//line views/stuff.qtpl:53 +//line views/stuff.qtpl:25 streamcommonScripts(qw422016) -//line views/stuff.qtpl:53 +//line views/stuff.qtpl:25 qt422016.ReleaseWriter(qw422016) -//line views/stuff.qtpl:53 +//line views/stuff.qtpl:25 } -//line views/stuff.qtpl:53 +//line views/stuff.qtpl:25 func commonScripts() string { -//line views/stuff.qtpl:53 +//line views/stuff.qtpl:25 qb422016 := qt422016.AcquireByteBuffer() -//line views/stuff.qtpl:53 +//line views/stuff.qtpl:25 writecommonScripts(qb422016) -//line views/stuff.qtpl:53 +//line views/stuff.qtpl:25 qs422016 := string(qb422016.B) -//line views/stuff.qtpl:53 +//line views/stuff.qtpl:25 qt422016.ReleaseByteBuffer(qb422016) -//line views/stuff.qtpl:53 +//line views/stuff.qtpl:25 return qs422016 -//line views/stuff.qtpl:53 +//line views/stuff.qtpl:25 } diff --git a/web/backlinks.go b/web/backlinks.go deleted file mode 100644 index 1bb10d7..0000000 --- a/web/backlinks.go +++ /dev/null @@ -1,30 +0,0 @@ -package web - -import ( - "github.com/bouncepaw/mycorrhiza/hyphae/backlinks" - "github.com/bouncepaw/mycorrhiza/viewutil" - "net/http" - - "github.com/gorilla/mux" - - "github.com/bouncepaw/mycorrhiza/l18n" - "github.com/bouncepaw/mycorrhiza/util" - "github.com/bouncepaw/mycorrhiza/views" -) - -func initBacklinks(r *mux.Router) { - r.PathPrefix("/backlinks/").HandlerFunc(handlerBacklinks) -} - -// handlerBacklinks lists all backlinks to a hypha. -func handlerBacklinks(w http.ResponseWriter, rq *http.Request) { - var ( - hyphaName = util.HyphaNameFromRq(rq, "backlinks") - lc = l18n.FromRequest(rq) - ) - util.HTTP200Page(w, views.Base( - viewutil.MetaFrom(w, rq), - lc.Get("ui.backlinks_title", &l18n.Replacements{"query": util.BeautifulName(hyphaName)}), - views.Backlinks(hyphaName, backlinks.YieldHyphaBacklinks, lc), - )) -} diff --git a/web/web.go b/web/web.go index 9801a17..845a037 100644 --- a/web/web.go +++ b/web/web.go @@ -2,6 +2,7 @@ package web import ( + "github.com/bouncepaw/mycorrhiza/backlinks" "github.com/bouncepaw/mycorrhiza/categories" "github.com/bouncepaw/mycorrhiza/misc" "io" @@ -47,7 +48,7 @@ func Handler() http.Handler { initMutators(wikiRouter) initHistory(wikiRouter) initHelp(wikiRouter) - initBacklinks(wikiRouter) + backlinks.InitHandlers(wikiRouter) categories.InitHandlers(wikiRouter) misc.InitHandlers(wikiRouter)