From a9c72d91be4b38b87ea6a910a6324c423a93eba3 Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Fri, 26 Jun 2020 01:31:58 +0500 Subject: [PATCH] Reimplement editor --- cfg/config.go | 7 ++- fs/html.go | 54 +++++++++++++++++ fs/hypha.go | 60 +++++++++---------- handlers.go | 16 +++-- main.go | 6 +- render/render.go | 15 +++++ w/config.json | 2 +- .../default-dark/Hypha/edit/index.html/1.html | 3 +- .../Hypha/edit/index.html/1.html | 20 +------ .../Hypha/edit/sidebar.html/1.html | 19 ++++++ .../Hypha/edit/sidebar.html/meta.json | 19 ++++++ 11 files changed, 154 insertions(+), 67 deletions(-) create mode 100644 fs/html.go create mode 100644 w/m/Templates/default-light/Hypha/edit/sidebar.html/1.html create mode 100644 w/m/Templates/default-light/Hypha/edit/sidebar.html/meta.json diff --git a/cfg/config.go b/cfg/config.go index 7c0db8f..f5e6251 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -16,9 +16,10 @@ const ( ) var ( - WikiDir string - TemplatesDir string - configJsonPath string + DescribeHyphaHerePattern = "Describe %s here" + WikiDir string + TemplatesDir string + configJsonPath string // Default values that can be overriden in config.json Address = "127.0.0.1:80" diff --git a/fs/html.go b/fs/html.go new file mode 100644 index 0000000..f64059d --- /dev/null +++ b/fs/html.go @@ -0,0 +1,54 @@ +package fs + +import ( + "fmt" + "io/ioutil" + "log" + + "github.com/gomarkdown/markdown" + "github.com/gomarkdown/markdown/html" + "github.com/gomarkdown/markdown/parser" +) + +func markdownToHtml(md string) string { + extensions := parser.CommonExtensions | parser.AutoHeadingIDs + p := parser.NewWithExtensions(extensions) + + htmlFlags := html.CommonFlags | html.HrefTargetBlank + opts := html.RendererOptions{Flags: htmlFlags} + renderer := html.NewRenderer(opts) + + return string(markdown.ToHTML([]byte(md), p, renderer)) +} + +func (h *Hypha) asHtml() (string, error) { + rev := h.actual + ret := `
+

` + rev.FullName + `

+` + // What about using
? + if h.hasBinaryData() { + ret += fmt.Sprintf(``, rev.FullName, rev.Id) + } + + contents, err := ioutil.ReadFile(rev.TextPath) + if err != nil { + log.Println("Failed to render", rev.FullName, ":", err) + return "", err + } + + // TODO: support more markups. + // TODO: support mycorrhiza extensions like transclusion. + switch rev.TextMime { + case "text/markdown": + html := markdown.ToHTML(contents, nil, nil) + ret += string(html) + default: + ret += fmt.Sprintf(`
%s
`, contents) + } + + ret += ` +
` + + return ret, nil +} diff --git a/fs/hypha.go b/fs/hypha.go index 737a070..3f198f2 100644 --- a/fs/hypha.go +++ b/fs/hypha.go @@ -9,9 +9,9 @@ import ( "net/http" "path/filepath" "strconv" + "strings" "github.com/bouncepaw/mycorrhiza/cfg" - "github.com/gomarkdown/markdown" ) type Hypha struct { @@ -27,6 +27,32 @@ func (h *Hypha) TextPath() string { return h.actual.TextPath } +func (h *Hypha) TagsJoined() string { + if h.Exists { + return strings.Join(h.actual.Tags, ", ") + } + return "" +} + +func (h *Hypha) TextMime() string { + if h.Exists { + return h.actual.TextMime + } + return "text/markdown" +} + +func (h *Hypha) TextContent() string { + if h.Exists { + contents, err := ioutil.ReadFile(h.TextPath()) + if err != nil { + log.Println("Could not read", h.FullName) + return "Error: could not hypha text content file. It is recommended to cancel editing. Please contact the wiki admin. If you are the admin, see the logs." + } + return string(contents) + } + return fmt.Sprintf(cfg.DescribeHyphaHerePattern, h.FullName) +} + func (s *Storage) Open(name string) (*Hypha, error) { h := &Hypha{ Exists: true, @@ -123,38 +149,6 @@ func (h *Hypha) hasBinaryData() bool { return h.actual.BinaryMime != "" } -func (h *Hypha) asHtml() (string, error) { - rev := h.actual - ret := `
-

` + rev.FullName + `

-` - // What about using
? - if h.hasBinaryData() { - ret += fmt.Sprintf(``, rev.FullName, rev.Id) - } - - contents, err := ioutil.ReadFile(rev.TextPath) - if err != nil { - log.Println("Failed to render", rev.FullName, ":", err) - return "", err - } - - // TODO: support more markups. - // TODO: support mycorrhiza extensions like transclusion. - switch rev.TextMime { - case "text/markdown": - html := markdown.ToHTML(contents, nil, nil) - ret += string(html) - default: - ret += fmt.Sprintf(`
%s
`, contents) - } - - ret += ` -
` - - return ret, nil -} - // ActionRaw is used with `?action=raw`. // It writes text content of the revision without any parsing or rendering. func (h *Hypha) ActionRaw(w http.ResponseWriter) { diff --git a/handlers.go b/handlers.go index d8a1ada..6f1fe71 100644 --- a/handlers.go +++ b/handlers.go @@ -29,8 +29,6 @@ func HandlerBase(w http.ResponseWriter, rq *http.Request) (*fs.Hypha, bool) { if err != nil { log.Println(err) } - - log.Println(*h) return h, true } @@ -60,14 +58,20 @@ func HandlerView(w http.ResponseWriter, rq *http.Request) { } } -/* func HandlerEdit(w http.ResponseWriter, rq *http.Request) { vars := mux.Vars(rq) - ActionEdit(vars["hypha"], w) + h, err := fs.Hs.Open(vars["hypha"]) + // How could this happen? + if err != nil { + log.Println(err) + return + } + h.OnRevision("0") + w.Header().Set("Content-Type", "text/html;charset=utf-8") + w.WriteHeader(http.StatusOK) + w.Write([]byte(render.HyphaEdit(h))) } -*/ - /* // makeTagsSlice turns strings like `"foo,, bar,kek"` to slice of strings that represent tag names. Whitespace around commas is insignificant. // Expected output for string above: []string{"foo", "bar", "kek"} diff --git a/main.go b/main.go index 4cbcb8d..09a3682 100644 --- a/main.go +++ b/main.go @@ -63,10 +63,10 @@ func main() { r.Queries("action", "view").Path(cfg.HyphaUrl). HandlerFunc(HandlerView) - /* - r.Queries("action", "edit").Path(hyphaUrl). - HandlerFunc(HandlerEdit) + r.Queries("action", "edit").Path(cfg.HyphaUrl). + HandlerFunc(HandlerEdit) + /* r.Queries("action", "update").Path(hyphaUrl).Methods("POST"). HandlerFunc(HandlerUpdate) */ diff --git a/render/render.go b/render/render.go index 75c3e86..9af9434 100644 --- a/render/render.go +++ b/render/render.go @@ -26,6 +26,21 @@ func EditHyphaPage(name, textMime, content, tags string) string { return renderBase(renderFromMap(page, "Hypha/edit/index.html"), keys) } +func HyphaEdit(h *fs.Hypha) string { + page := map[string]string{ + "Name": h.FullName, + "Tags": h.TagsJoined(), + "TextMime": h.TextMime(), + "Text": h.TextContent(), + } + keys := map[string]string{ + "Title": fmt.Sprintf(cfg.TitleEditTemplate, h.FullName), + "Header": renderFromString(h.FullName, "Hypha/edit/header.html"), + "Sidebar": renderFromMap(page, "Hypha/edit/sidebar.html"), + } + return renderBase(renderFromMap(page, "Hypha/edit/index.html"), keys) +} + // Hypha404 returns 404 page for nonexistent page. func Hypha404(name, _ string) string { return HyphaGeneric(name, name, "Hypha/view/404.html") diff --git a/w/config.json b/w/config.json index 409bcd2..3497a5a 100644 --- a/w/config.json +++ b/w/config.json @@ -1,6 +1,6 @@ { "address": "127.0.0.1:1737", - "theme": "default-light", + "theme": "default-dark", "site-title": "🍄 MycorrhizaWiki", "title-templates": { "edit-hypha": "Edit %s at MycorrhizaWiki", diff --git a/w/m/Templates/default-dark/Hypha/edit/index.html/1.html b/w/m/Templates/default-dark/Hypha/edit/index.html/1.html index 842b43b..a102c1e 100644 --- a/w/m/Templates/default-dark/Hypha/edit/index.html/1.html +++ b/w/m/Templates/default-dark/Hypha/edit/index.html/1.html @@ -9,8 +9,7 @@

Edit box

+{{ .Text }} diff --git a/w/m/Templates/default-light/Hypha/edit/index.html/1.html b/w/m/Templates/default-light/Hypha/edit/index.html/1.html index 49ab078..943cf5f 100644 --- a/w/m/Templates/default-light/Hypha/edit/index.html/1.html +++ b/w/m/Templates/default-light/Hypha/edit/index.html/1.html @@ -3,34 +3,16 @@ method="POST" enctype="multipart/form-data" action="?action=update"> -

Edit box

+{{ .Text }}

Upload file

If this hypha has a file like that, the text above is meant to be a description of it

- -
-

Text MIME-type

-

Good types are text/markdown and text/plain

- - -

Revision comment

-

Please make your comment helpful

- - -

Edit tags

-

Tags are separated by commas, whitespace is ignored

- -
diff --git a/w/m/Templates/default-light/Hypha/edit/sidebar.html/1.html b/w/m/Templates/default-light/Hypha/edit/sidebar.html/1.html new file mode 100644 index 0000000..3f257cf --- /dev/null +++ b/w/m/Templates/default-light/Hypha/edit/sidebar.html/1.html @@ -0,0 +1,19 @@ +

Text MIME-type

+

Good types are text/markdown and text/plain

+ + +

Revision comment

+

Please make your comment helpful

+ + +

Edit tags

+

Tags are separated by commas, whitespace is ignored

+ + +

Upload file

+

If this hypha has a file like that, the text above is meant to be a description of it

+ + + +

+
diff --git a/w/m/Templates/default-light/Hypha/edit/sidebar.html/meta.json b/w/m/Templates/default-light/Hypha/edit/sidebar.html/meta.json new file mode 100644 index 0000000..1fe3342 --- /dev/null +++ b/w/m/Templates/default-light/Hypha/edit/sidebar.html/meta.json @@ -0,0 +1,19 @@ +{ + "views": 0, + "deleted": false, + "revisions": { + "1": { + "tags": [ + "" + ], + "name": "sidebar.html", + "comment": "Create Templates/default-dark/Hypha/edit/sidebar.html", + "author": "", + "time": 1593003792, + "text_mime": "text/html", + "binary_mime": "", + "text_name": "1.html", + "binary_name": "" + } + } +}