From a15eeca1178822c77c8504bab292ed6afa4230d9 Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Sat, 9 Apr 2022 11:28:57 +0300 Subject: [PATCH] Isolate help --- help/help.go | 2 +- help/view_help.html | 16 +++++ help/web.go | 96 +++++++++++++++++++++++++++ views/help.go | 2 +- views/stuff.qtpl | 18 ----- views/stuff.qtpl.go | 156 +++++++------------------------------------- web/help.go | 80 ----------------------- web/web.go | 3 +- 8 files changed, 139 insertions(+), 234 deletions(-) create mode 100644 help/view_help.html create mode 100644 help/web.go delete mode 100644 web/help.go diff --git a/help/help.go b/help/help.go index bc12477..057d3c1 100644 --- a/help/help.go +++ b/help/help.go @@ -5,7 +5,7 @@ import ( "embed" ) -//go:embed en en.myco +//go:embed en en.myco *.html var fs embed.FS // Get determines what help text you need and returns it. The path is a substring from URL, it follows this form: diff --git a/help/view_help.html b/help/view_help.html new file mode 100644 index 0000000..17f1d7f --- /dev/null +++ b/help/view_help.html @@ -0,0 +1,16 @@ +{{define "title"}}Help{{end}} +{{define "body"}} +
+
+
+ {{if .ContentsHTML}} + {{.ContentsHTML}} + {{else}} +

{{block "entry not found" .}}Entry not found{{end}}

+

{{block "entry not found invitation" .}}If you want to write this entry by yourself, consider contributing it directly.{{end}}

+ {{end}} +
+
+ {{.HelpTopicsHTML}} +
+{{end}} diff --git a/help/web.go b/help/web.go new file mode 100644 index 0000000..8c904cd --- /dev/null +++ b/help/web.go @@ -0,0 +1,96 @@ +package help + +// stuff.go is used for meta stuff about the wiki or all hyphae at once. +import ( + "github.com/bouncepaw/mycomarkup/v4" + "github.com/bouncepaw/mycorrhiza/cfg" + "github.com/bouncepaw/mycorrhiza/shroom" + "github.com/bouncepaw/mycorrhiza/viewutil" + "io" + "log" + "net/http" + "strings" + "text/template" + + "github.com/gorilla/mux" + + "github.com/bouncepaw/mycorrhiza/views" + + "github.com/bouncepaw/mycomarkup/v4/mycocontext" +) + +var ( + chain viewutil.Chain + ruTranslation = ` +{{define "title"}}Справка{{end}} +{{define "entry not found"}}Статья не найдена{{end}} +{{define "entry not found invitation"}}Если вы хотите написать эту статью сами, то будем рады вашим правкам в репозитории Миокризы.{{end}} +` +) + +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))) +} + +// handlerHelp gets the appropriate documentation or tells you where you (personally) have failed. +func handlerHelp(w http.ResponseWriter, rq *http.Request) { + // See the history of this file to resurrect the old algorithm that supported multiple languages + var ( + meta = viewutil.MetaFrom(w, rq) + articlePath = strings.TrimPrefix(strings.TrimPrefix(rq.URL.Path, "/help/"), "/help") + lang = "en" + ) + if articlePath == "" { + articlePath = "en" + } + + if !strings.HasPrefix(articlePath, "en") { + w.WriteHeader(http.StatusNotFound) + _, _ = io.WriteString(w, "404 Not found") + return + } + + content, err := Get(articlePath) + if err != nil && strings.HasPrefix(err.Error(), "open") { + w.WriteHeader(http.StatusNotFound) + viewHelp(meta, lang, "") + return + } + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + viewHelp(meta, lang, err.Error()) + return + } + + // TODO: change for the function that uses byte array when there is such function in mycomarkup. + ctx, _ := mycocontext.ContextFromStringInput(string(content), shroom.MarkupOptions(articlePath)) + ast := mycomarkup.BlockTree(ctx) + result := mycomarkup.BlocksToHTML(ctx, ast) + w.WriteHeader(http.StatusOK) + viewHelp(meta, lang, result) +} + +type helpData struct { + viewutil.BaseData + ContentsHTML string + HelpTopicsHTML string + Lang string +} + +func viewHelp(meta viewutil.Meta, lang, contentsHTML string) { + if err := chain.Get(meta).ExecuteTemplate(meta.W, "page", helpData{ + BaseData: viewutil.BaseData{ + Meta: meta, + HeaderLinks: cfg.HeaderLinks, + CommonScripts: cfg.CommonScripts, + }, + ContentsHTML: contentsHTML, + HelpTopicsHTML: views.HelpTopics(lang, meta.Lc), + Lang: lang, + }); err != nil { + log.Println(err) + } +} diff --git a/views/help.go b/views/help.go index e637c88..41de7b7 100644 --- a/views/help.go +++ b/views/help.go @@ -81,7 +81,7 @@ func helpTopicsLocalizedTopic(lang string) func(string) string { } } -func helpTopics(lang string, lc *l18n.Localizer) string { +func HelpTopics(lang string, lc *l18n.Localizer) string { temp, err := template. New("help topics"). Funcs(template.FuncMap{ diff --git a/views/stuff.qtpl b/views/stuff.qtpl index 980c9bd..acb68a5 100644 --- a/views/stuff.qtpl +++ b/views/stuff.qtpl @@ -1,22 +1,4 @@ {% import "github.com/bouncepaw/mycorrhiza/cfg" %} -{% import "github.com/bouncepaw/mycorrhiza/l18n" %} - -{% func Help(content, lang string, lc *l18n.Localizer) %} -
-
-
- {%s= content %} -
-
-{%s= helpTopics(lang, lc) %} -
-{% endfunc %} - -{% func HelpEmptyError(lc *l18n.Localizer) %} -

{%s lc.Get("help.empty_error_title") %}

-

{%s lc.Get("help.empty_error_line_1") %}

-

{%s lc.Get("help.empty_error_line_2a") %} {%s lc.Get("help.empty_error_link") %} {%s lc.Get("help.empty_error_line_2b") %}

-{% endfunc %} {% func commonScripts() %} {% for _, scriptPath := range cfg.CommonScripts %} diff --git a/views/stuff.qtpl.go b/views/stuff.qtpl.go index 0298658..c0b4e29 100644 --- a/views/stuff.qtpl.go +++ b/views/stuff.qtpl.go @@ -7,174 +7,64 @@ package views //line views/stuff.qtpl:1 import "github.com/bouncepaw/mycorrhiza/cfg" -//line views/stuff.qtpl:2 -import "github.com/bouncepaw/mycorrhiza/l18n" - -//line views/stuff.qtpl:4 +//line views/stuff.qtpl:3 import ( qtio422016 "io" qt422016 "github.com/valyala/quicktemplate" ) -//line views/stuff.qtpl:4 +//line views/stuff.qtpl:3 var ( _ = qtio422016.Copy _ = qt422016.AcquireByteBuffer ) -//line views/stuff.qtpl:4 -func StreamHelp(qw422016 *qt422016.Writer, content, lang string, lc *l18n.Localizer) { -//line views/stuff.qtpl:4 - qw422016.N().S(` -
-
-
- `) -//line views/stuff.qtpl:8 - qw422016.N().S(content) -//line views/stuff.qtpl:8 - qw422016.N().S(` -
-
-`) -//line views/stuff.qtpl:11 - qw422016.N().S(helpTopics(lang, lc)) -//line views/stuff.qtpl:11 - qw422016.N().S(` -
-`) -//line views/stuff.qtpl:13 -} - -//line views/stuff.qtpl:13 -func WriteHelp(qq422016 qtio422016.Writer, content, lang string, lc *l18n.Localizer) { -//line views/stuff.qtpl:13 - qw422016 := qt422016.AcquireWriter(qq422016) -//line views/stuff.qtpl:13 - StreamHelp(qw422016, content, lang, lc) -//line views/stuff.qtpl:13 - qt422016.ReleaseWriter(qw422016) -//line views/stuff.qtpl:13 -} - -//line views/stuff.qtpl:13 -func Help(content, lang string, lc *l18n.Localizer) string { -//line views/stuff.qtpl:13 - qb422016 := qt422016.AcquireByteBuffer() -//line views/stuff.qtpl:13 - WriteHelp(qb422016, content, lang, lc) -//line views/stuff.qtpl:13 - qs422016 := string(qb422016.B) -//line views/stuff.qtpl:13 - qt422016.ReleaseByteBuffer(qb422016) -//line views/stuff.qtpl:13 - return qs422016 -//line views/stuff.qtpl:13 -} - -//line views/stuff.qtpl:15 -func StreamHelpEmptyError(qw422016 *qt422016.Writer, lc *l18n.Localizer) { -//line views/stuff.qtpl:15 - qw422016.N().S(` -

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

-

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

-

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

-`) -//line views/stuff.qtpl:19 -} - -//line views/stuff.qtpl:19 -func WriteHelpEmptyError(qq422016 qtio422016.Writer, lc *l18n.Localizer) { -//line views/stuff.qtpl:19 - qw422016 := qt422016.AcquireWriter(qq422016) -//line views/stuff.qtpl:19 - StreamHelpEmptyError(qw422016, lc) -//line views/stuff.qtpl:19 - qt422016.ReleaseWriter(qw422016) -//line views/stuff.qtpl:19 -} - -//line views/stuff.qtpl:19 -func HelpEmptyError(lc *l18n.Localizer) string { -//line views/stuff.qtpl:19 - qb422016 := qt422016.AcquireByteBuffer() -//line views/stuff.qtpl:19 - WriteHelpEmptyError(qb422016, lc) -//line views/stuff.qtpl:19 - qs422016 := string(qb422016.B) -//line views/stuff.qtpl:19 - qt422016.ReleaseByteBuffer(qb422016) -//line views/stuff.qtpl:19 - return qs422016 -//line views/stuff.qtpl:19 -} - -//line views/stuff.qtpl:21 +//line views/stuff.qtpl:3 func streamcommonScripts(qw422016 *qt422016.Writer) { -//line views/stuff.qtpl:21 +//line views/stuff.qtpl:3 qw422016.N().S(` `) -//line views/stuff.qtpl:22 +//line views/stuff.qtpl:4 for _, scriptPath := range cfg.CommonScripts { -//line views/stuff.qtpl:22 +//line views/stuff.qtpl:4 qw422016.N().S(` `) -//line views/stuff.qtpl:24 +//line views/stuff.qtpl:6 } -//line views/stuff.qtpl:24 +//line views/stuff.qtpl:6 qw422016.N().S(` `) -//line views/stuff.qtpl:25 +//line views/stuff.qtpl:7 } -//line views/stuff.qtpl:25 +//line views/stuff.qtpl:7 func writecommonScripts(qq422016 qtio422016.Writer) { -//line views/stuff.qtpl:25 +//line views/stuff.qtpl:7 qw422016 := qt422016.AcquireWriter(qq422016) -//line views/stuff.qtpl:25 +//line views/stuff.qtpl:7 streamcommonScripts(qw422016) -//line views/stuff.qtpl:25 +//line views/stuff.qtpl:7 qt422016.ReleaseWriter(qw422016) -//line views/stuff.qtpl:25 +//line views/stuff.qtpl:7 } -//line views/stuff.qtpl:25 +//line views/stuff.qtpl:7 func commonScripts() string { -//line views/stuff.qtpl:25 +//line views/stuff.qtpl:7 qb422016 := qt422016.AcquireByteBuffer() -//line views/stuff.qtpl:25 +//line views/stuff.qtpl:7 writecommonScripts(qb422016) -//line views/stuff.qtpl:25 +//line views/stuff.qtpl:7 qs422016 := string(qb422016.B) -//line views/stuff.qtpl:25 +//line views/stuff.qtpl:7 qt422016.ReleaseByteBuffer(qb422016) -//line views/stuff.qtpl:25 +//line views/stuff.qtpl:7 return qs422016 -//line views/stuff.qtpl:25 +//line views/stuff.qtpl:7 } diff --git a/web/help.go b/web/help.go deleted file mode 100644 index 11bccce..0000000 --- a/web/help.go +++ /dev/null @@ -1,80 +0,0 @@ -package web - -// stuff.go is used for meta stuff about the wiki or all hyphae at once. -import ( - "github.com/bouncepaw/mycomarkup/v4" - "github.com/bouncepaw/mycorrhiza/shroom" - "github.com/bouncepaw/mycorrhiza/viewutil" - "io" - "net/http" - "strings" - - "github.com/gorilla/mux" - - "github.com/bouncepaw/mycorrhiza/help" - "github.com/bouncepaw/mycorrhiza/l18n" - "github.com/bouncepaw/mycorrhiza/views" - - "github.com/bouncepaw/mycomarkup/v4/mycocontext" -) - -func initHelp(r *mux.Router) { - r.PathPrefix("/help").HandlerFunc(handlerHelp) -} - -// handlerHelp gets the appropriate documentation or tells you where you (personally) have failed. -func handlerHelp(w http.ResponseWriter, rq *http.Request) { - lc := l18n.FromRequest(rq) - articlePath := strings.TrimPrefix(strings.TrimPrefix(rq.URL.Path, "/help/"), "/help") - // See the history of this file to resurrect the old algorithm that supported multiple languages - lang := "en" - if articlePath == "" { - articlePath = "en" - } - - if !strings.HasPrefix(articlePath, "en") { - w.WriteHeader(http.StatusNotFound) - _, _ = io.WriteString(w, "404 Not found") - return - } - - content, err := help.Get(articlePath) - if err != nil && strings.HasPrefix(err.Error(), "open") { - w.WriteHeader(http.StatusNotFound) - _, _ = io.WriteString( - w, - views.Base( - viewutil.MetaFrom(w, rq), - lc.Get("help.entry_not_found"), - views.Help(views.HelpEmptyError(lc), lang, lc), - ), - ) - return - } - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - _, _ = io.WriteString( - w, - views.Base( - viewutil.MetaFrom(w, rq), - err.Error(), - views.Help(err.Error(), lang, lc), - ), - ) - return - } - - // TODO: change for the function that uses byte array when there is such function in mycomarkup. - ctx, _ := mycocontext.ContextFromStringInput(string(content), shroom.MarkupOptions(articlePath)) - ast := mycomarkup.BlockTree(ctx) - result := mycomarkup.BlocksToHTML(ctx, ast) - w.WriteHeader(http.StatusOK) - _, _ = io.WriteString( - w, - views.Base( - viewutil.MetaFrom(w, rq), - lc.Get("help.title"), - views.Help(result, lang, lc), - ), - ) -} diff --git a/web/web.go b/web/web.go index 845a037..2511e6b 100644 --- a/web/web.go +++ b/web/web.go @@ -4,6 +4,7 @@ package web import ( "github.com/bouncepaw/mycorrhiza/backlinks" "github.com/bouncepaw/mycorrhiza/categories" + "github.com/bouncepaw/mycorrhiza/help" "github.com/bouncepaw/mycorrhiza/misc" "io" "net/http" @@ -47,7 +48,7 @@ func Handler() http.Handler { initReaders(wikiRouter) initMutators(wikiRouter) initHistory(wikiRouter) - initHelp(wikiRouter) + help.InitHandlers(wikiRouter) backlinks.InitHandlers(wikiRouter) categories.InitHandlers(wikiRouter) misc.InitHandlers(wikiRouter)