1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-04 18:19:54 +00:00

Isolate help

This commit is contained in:
Timur Ismagilov 2022-04-09 11:28:57 +03:00
parent 17e006322f
commit a15eeca117
8 changed files with 139 additions and 234 deletions

View File

@ -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:

16
help/view_help.html Normal file
View File

@ -0,0 +1,16 @@
{{define "title"}}Help{{end}}
{{define "body"}}
<div class="layout">
<main class="main-width help">
<article>
{{if .ContentsHTML}}
{{.ContentsHTML}}
{{else}}
<h1>{{block "entry not found" .}}Entry not found{{end}}</h1>
<p>{{block "entry not found invitation" .}}If you want to write this entry by yourself, consider <a class="wikilink wikilink_external wikilink_https" href="https://github.com/bouncepaw/mycorrhiza">contributing</a> it directly.{{end}}</p>
{{end}}
</article>
</main>
{{.HelpTopicsHTML}}
</div>
{{end}}

96
help/web.go Normal file
View File

@ -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"}}Если вы хотите написать эту статью сами, то будем рады вашим правкам <a class="wikilink wikilink_external wikilink_https" href="https://github.com/bouncepaw/mycorrhiza">в репозитории Миокризы</a>.{{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)
}
}

View File

@ -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{

View File

@ -1,22 +1,4 @@
{% import "github.com/bouncepaw/mycorrhiza/cfg" %}
{% import "github.com/bouncepaw/mycorrhiza/l18n" %}
{% func Help(content, lang string, lc *l18n.Localizer) %}
<div class="layout">
<main class="main-width help">
<article>
{%s= content %}
</article>
</main>
{%s= helpTopics(lang, lc) %}
</div>
{% endfunc %}
{% func HelpEmptyError(lc *l18n.Localizer) %}
<h1>{%s lc.Get("help.empty_error_title") %}</h1>
<p>{%s lc.Get("help.empty_error_line_1") %}</p>
<p>{%s lc.Get("help.empty_error_line_2a") %} <a class="wikilink wikilink_external wikilink_https" href="https://github.com/bouncepaw/mycorrhiza">{%s lc.Get("help.empty_error_link") %}</a> {%s lc.Get("help.empty_error_line_2b") %}</p>
{% endfunc %}
{% func commonScripts() %}
{% for _, scriptPath := range cfg.CommonScripts %}

View File

@ -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(`
<div class="layout">
<main class="main-width help">
<article>
`)
//line views/stuff.qtpl:8
qw422016.N().S(content)
//line views/stuff.qtpl:8
qw422016.N().S(`
</article>
</main>
`)
//line views/stuff.qtpl:11
qw422016.N().S(helpTopics(lang, lc))
//line views/stuff.qtpl:11
qw422016.N().S(`
</div>
`)
//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(`
<h1>`)
//line views/stuff.qtpl:16
qw422016.E().S(lc.Get("help.empty_error_title"))
//line views/stuff.qtpl:16
qw422016.N().S(`</h1>
<p>`)
//line views/stuff.qtpl:17
qw422016.E().S(lc.Get("help.empty_error_line_1"))
//line views/stuff.qtpl:17
qw422016.N().S(`</p>
<p>`)
//line views/stuff.qtpl:18
qw422016.E().S(lc.Get("help.empty_error_line_2a"))
//line views/stuff.qtpl:18
qw422016.N().S(` <a class="wikilink wikilink_external wikilink_https" href="https://github.com/bouncepaw/mycorrhiza">`)
//line views/stuff.qtpl:18
qw422016.E().S(lc.Get("help.empty_error_link"))
//line views/stuff.qtpl:18
qw422016.N().S(`</a> `)
//line views/stuff.qtpl:18
qw422016.E().S(lc.Get("help.empty_error_line_2b"))
//line views/stuff.qtpl:18
qw422016.N().S(`</p>
`)
//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(`
<script src="`)
//line views/stuff.qtpl:23
//line views/stuff.qtpl:5
qw422016.E().S(scriptPath)
//line views/stuff.qtpl:23
//line views/stuff.qtpl:5
qw422016.N().S(`"></script>
`)
//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
}

View File

@ -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),
),
)
}

View File

@ -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)