From 53ae1a6e7a874989290a3a875ec411c853743a45 Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Sun, 11 Jul 2021 01:02:16 +0500 Subject: [PATCH] Add the help system No documentation yet. This branch will eventually be merged into master --- help/en/index.myco | 7 +++++++ help/help.go | 15 +++++++++++++++ web/stuff.go | 25 +++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 help/en/index.myco create mode 100644 help/help.go diff --git a/help/en/index.myco b/help/en/index.myco new file mode 100644 index 0000000..90d5dc1 --- /dev/null +++ b/help/en/index.myco @@ -0,0 +1,7 @@ +This is Mycorrhiza Wiki built-in documentation. + +Hope you are doing well ☺️ + +Here are the topics covered by the documentation: + +Thanks for reading! \ No newline at end of file diff --git a/help/help.go b/help/help.go new file mode 100644 index 0000000..06fc6f1 --- /dev/null +++ b/help/help.go @@ -0,0 +1,15 @@ +// Package help contains help messages and the utilities for retrieving them. +package help + +import ( + "embed" +) + +//go:embed en +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: +// / +func Get(path string) ([]byte, error) { + return fs.ReadFile(path + ".myco") +} diff --git a/web/stuff.go b/web/stuff.go index bf9ed49..b757a97 100644 --- a/web/stuff.go +++ b/web/stuff.go @@ -2,6 +2,9 @@ package web // stuff.go is used for meta stuff about the wiki or all hyphae at once. import ( + "github.com/bouncepaw/mycomarkup" + "github.com/bouncepaw/mycomarkup/mycocontext" + "github.com/bouncepaw/mycorrhiza/help" "io" "log" "math/rand" @@ -18,6 +21,7 @@ import ( ) func initStuff() { + http.HandleFunc("/help/", handlerHelp) http.HandleFunc("/list/", handlerList) http.HandleFunc("/reindex/", handlerReindex) http.HandleFunc("/update-header-links/", handlerUpdateHeaderLinks) @@ -28,6 +32,27 @@ func initStuff() { }) } +// handlerHelp gets the appropriate documentation or tells you where you (personally) have failed. +func handlerHelp(w http.ResponseWriter, rq *http.Request) { + if shown := user.FromRequest(rq).ShowLockMaybe(w, rq); shown { + return + } + + content, err := help.Get(rq.URL.Path[6:]) // Drop /help/ + if err != nil { + // TODO: proper error reporting that makes sense + httpErr(w, http.StatusForbidden, cfg.HomeHypha, err.Error(), err.Error()) + return + } + + // TODO: change for the function that uses byte array when there is such function in mycomarkup. + ctx, _ := mycocontext.ContextFromStringInput(rq.URL.Path[1:3], string(content)) + ast := mycomarkup.BlockTree(ctx) + result := mycomarkup.BlocksToHTML(ctx, ast) + // TODO: styled output idk + _, _ = io.WriteString(w, result) +} + // handlerList shows a list of all hyphae in the wiki in random order. func handlerList(w http.ResponseWriter, rq *http.Request) { u := user.FromRequest(rq)