1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 11:46:16 +00:00
mycorrhiza/web/stuff.go
Timur Ismagilov f8fbc23202 Misc: Make the new package
It contains things like hypha list or about wiki pages
2022-04-02 09:38:16 +03:00

80 lines
2.0 KiB
Go

package web
// stuff.go is used for meta stuff about the wiki or all hyphae at once.
import (
"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/v3"
"github.com/bouncepaw/mycomarkup/v3/mycocontext"
)
func initStuff(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(articlePath, string(content))
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),
),
)
}