From 17e006322f195aef80158decf11348ae12351409 Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Thu, 7 Apr 2022 20:17:59 +0300 Subject: [PATCH] Mycomarkup: Migrate to v4 --- backlinks/hooks.go | 11 +++++---- cfg/header_links.go | 10 ++++---- go.mod | 4 ++-- go.sum | 6 +++-- migration/rockets.go | 2 +- shroom/mycomarkup_options.go | 46 ++++++++++++++++++++++++++++++++++++ shroom/shroom.go | 38 ----------------------------- util/util.go | 2 +- web/help.go | 7 +++--- web/mutators.go | 6 ++--- web/readers.go | 13 +++++----- 11 files changed, 78 insertions(+), 67 deletions(-) create mode 100644 shroom/mycomarkup_options.go diff --git a/backlinks/hooks.go b/backlinks/hooks.go index 9cc5237..1ad7ebe 100644 --- a/backlinks/hooks.go +++ b/backlinks/hooks.go @@ -1,10 +1,11 @@ package backlinks import ( - "github.com/bouncepaw/mycomarkup/v3" - "github.com/bouncepaw/mycomarkup/v3/links" - "github.com/bouncepaw/mycomarkup/v3/mycocontext" - "github.com/bouncepaw/mycomarkup/v3/tools" + "github.com/bouncepaw/mycomarkup/v4" + "github.com/bouncepaw/mycomarkup/v4/links" + "github.com/bouncepaw/mycomarkup/v4/mycocontext" + "github.com/bouncepaw/mycomarkup/v4/options" + "github.com/bouncepaw/mycomarkup/v4/tools" "github.com/bouncepaw/mycorrhiza/hyphae" ) @@ -34,7 +35,7 @@ func extractHyphaLinks(h hyphae.Hypha) []string { // extractHyphaLinksFromContent extracts local hypha links from the provided text. func extractHyphaLinksFromContent(hyphaName string, contents string) []string { - ctx, _ := mycocontext.ContextFromStringInput(hyphaName, contents) + ctx, _ := mycocontext.ContextFromStringInput(contents, options.Options{HyphaName: hyphaName}.FillTheRest()) linkVisitor, getLinks := tools.LinkVisitor(ctx) // Ignore the result of BlockTree because we call it for linkVisitor. _ = mycomarkup.BlockTree(ctx, linkVisitor) diff --git a/cfg/header_links.go b/cfg/header_links.go index 3d284aa..fcb51c8 100644 --- a/cfg/header_links.go +++ b/cfg/header_links.go @@ -1,10 +1,10 @@ package cfg -// See https://mycorrhiza.wiki/hypha/configuration/header import ( - "github.com/bouncepaw/mycomarkup/v3" - "github.com/bouncepaw/mycomarkup/v3/blocks" - "github.com/bouncepaw/mycomarkup/v3/mycocontext" + "github.com/bouncepaw/mycomarkup/v4" + "github.com/bouncepaw/mycomarkup/v4/blocks" + "github.com/bouncepaw/mycomarkup/v4/mycocontext" + "github.com/bouncepaw/mycomarkup/v4/options" ) // HeaderLinks is a list off current header links. Feel free to iterate it directly but do not modify it by yourself. Call ParseHeaderLinks if you need to set new header links. @@ -24,7 +24,7 @@ func SetDefaultHeaderLinks() { // ParseHeaderLinks extracts all rocketlinks from the given text and saves them as header links. func ParseHeaderLinks(text string) { HeaderLinks = []HeaderLink{} - ctx, _ := mycocontext.ContextFromStringInput("", text) + ctx, _ := mycocontext.ContextFromStringInput(text, options.Options{}.FillTheRest()) // We call for side-effects _ = mycomarkup.BlockTree(ctx, func(block blocks.Block) { switch launchpad := block.(type) { diff --git a/go.mod b/go.mod index f58fb3c..6f52df7 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/bouncepaw/mycorrhiza go 1.18 require ( - github.com/bouncepaw/mycomarkup/v3 v3.6.2 + github.com/bouncepaw/mycomarkup/v4 v4.0.0 github.com/go-ini/ini v1.63.2 github.com/gorilla/feeds v1.1.1 github.com/gorilla/mux v1.8.0 @@ -22,7 +22,7 @@ require ( // Use this trick to test local Mycomarkup changes, replace the path with yours, // but do not commit the change to the path: -// replace github.com/bouncepaw/mycomarkup/v3 v3.6.2 => "/Users/bouncepaw/GolandProjects/mycomarkup" +// replace github.com/bouncepaw/mycomarkup/v4 v4.0.0 => "/Users/bouncepaw/GolandProjects/mycomarkup" // Use this utility every time Mycomarkup gets a major update: // https://github.com/marwan-at-work/mod diff --git a/go.sum b/go.sum index 0103904..bd53870 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,9 @@ github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= -github.com/bouncepaw/mycomarkup/v3 v3.6.2 h1:5zqb12aOw19xg8/0QIvgoA8oEW2doSdWqCbXltPEaPQ= -github.com/bouncepaw/mycomarkup/v3 v3.6.2/go.mod h1:BpiGUVsYCgRZCDxF0iIdc08LJokm/Ab36S/Hif0J6D0= +github.com/bouncepaw/mycomarkup/v4 v3.6.2 h1:5zqb12aOw19xg8/0QIvgoA8oEW2doSdWqCbXltPEaPQ= +github.com/bouncepaw/mycomarkup/v4 v3.6.2/go.mod h1:BpiGUVsYCgRZCDxF0iIdc08LJokm/Ab36S/Hif0J6D0= +github.com/bouncepaw/mycomarkup/v4 v4.0.0 h1:qokseZ+otcFuQ5vARdvxKqjlEZFMvsjFJ7YpJ4sUr8c= +github.com/bouncepaw/mycomarkup/v4 v4.0.0/go.mod h1:y0b8U6Xfnh3KfNUpG3QuAXRJwqFPPpmS2kYvLzaf688= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-ini/ini v1.63.2 h1:kwN3umicd2HF3Tgvap4um1ZG52/WyKT9GGdPx0CJk6Y= diff --git a/migration/rockets.go b/migration/rockets.go index f68ce97..3895aec 100644 --- a/migration/rockets.go +++ b/migration/rockets.go @@ -4,7 +4,7 @@ package migration import ( - "github.com/bouncepaw/mycomarkup/v3/tools" + "github.com/bouncepaw/mycomarkup/v4/tools" "io" "io/ioutil" "log" diff --git a/shroom/mycomarkup_options.go b/shroom/mycomarkup_options.go new file mode 100644 index 0000000..05595e0 --- /dev/null +++ b/shroom/mycomarkup_options.go @@ -0,0 +1,46 @@ +package shroom + +import ( + "errors" + "github.com/bouncepaw/mycomarkup/v4/options" + "github.com/bouncepaw/mycorrhiza/cfg" + "github.com/bouncepaw/mycorrhiza/hyphae" + "github.com/bouncepaw/mycorrhiza/views" +) + +func MarkupOptions(hyphaName string) options.Options { + return fillMycomarkupOptions(options.Options{ + HyphaName: hyphaName, + WebSiteURL: cfg.URL, + TransclusionSupported: true, + }) +} + +func fillMycomarkupOptions(opts options.Options) options.Options { + opts.HyphaExists = func(hyphaName string) bool { + switch hyphae.ByName(hyphaName).(type) { + case *hyphae.EmptyHypha: + return false + default: + return true + } + } + opts.HyphaHTMLData = func(hyphaName string) (rawText, binaryBlock string, err error) { + switch h := hyphae.ByName(hyphaName).(type) { + case *hyphae.EmptyHypha: + err = errors.New("Hypha " + hyphaName + " does not exist") + case *hyphae.TextualHypha: + rawText, err = FetchTextFile(h) + case *hyphae.MediaHypha: + rawText, err = FetchTextFile(h) + binaryBlock = views.MediaRaw(h) + } + return + } + opts.IterateHyphaNamesWith = func(λ func(string)) { + for h := range hyphae.YieldExistingHyphae() { + λ(h.CanonicalName()) + } + } + return opts.FillTheRest() +} diff --git a/shroom/shroom.go b/shroom/shroom.go index 6269694..469307c 100644 --- a/shroom/shroom.go +++ b/shroom/shroom.go @@ -2,41 +2,3 @@ // // Some of them are wrappers around functions provided by package hyphae. They manage history for you. package shroom - -import ( - "errors" - - "github.com/bouncepaw/mycorrhiza/hyphae" - "github.com/bouncepaw/mycorrhiza/views" - - "github.com/bouncepaw/mycomarkup/v3/globals" -) - -func init() { - // TODO: clean this complete and utter mess - globals.HyphaExists = func(hyphaName string) bool { - switch hyphae.ByName(hyphaName).(type) { - case *hyphae.EmptyHypha: - return false - default: - return true - } - } - globals.HyphaAccess = func(hyphaName string) (rawText, binaryBlock string, err error) { - switch h := hyphae.ByName(hyphaName).(type) { - case *hyphae.EmptyHypha: - err = errors.New("Hypha " + hyphaName + " does not exist") - case *hyphae.TextualHypha: - rawText, err = FetchTextFile(h) - case *hyphae.MediaHypha: - rawText, err = FetchTextFile(h) - binaryBlock = views.MediaRaw(h) - } - return - } - globals.HyphaIterate = func(λ func(string)) { - for h := range hyphae.YieldExistingHyphae() { - λ(h.CanonicalName()) - } - } -} diff --git a/util/util.go b/util/util.go index 66fedd0..9af0361 100644 --- a/util/util.go +++ b/util/util.go @@ -8,7 +8,7 @@ import ( "net/http" "strings" - "github.com/bouncepaw/mycomarkup/v3/util" + "github.com/bouncepaw/mycomarkup/v4/util" "github.com/bouncepaw/mycorrhiza/cfg" ) diff --git a/web/help.go b/web/help.go index 8227b5b..11bccce 100644 --- a/web/help.go +++ b/web/help.go @@ -2,6 +2,8 @@ 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" @@ -13,8 +15,7 @@ import ( "github.com/bouncepaw/mycorrhiza/l18n" "github.com/bouncepaw/mycorrhiza/views" - "github.com/bouncepaw/mycomarkup/v3" - "github.com/bouncepaw/mycomarkup/v3/mycocontext" + "github.com/bouncepaw/mycomarkup/v4/mycocontext" ) func initHelp(r *mux.Router) { @@ -64,7 +65,7 @@ func handlerHelp(w http.ResponseWriter, rq *http.Request) { } // TODO: change for the function that uses byte array when there is such function in mycomarkup. - ctx, _ := mycocontext.ContextFromStringInput(articlePath, string(content)) + ctx, _ := mycocontext.ContextFromStringInput(string(content), shroom.MarkupOptions(articlePath)) ast := mycomarkup.BlockTree(ctx) result := mycomarkup.BlocksToHTML(ctx, ast) w.WriteHeader(http.StatusOK) diff --git a/web/mutators.go b/web/mutators.go index 331464d..8c3ad02 100644 --- a/web/mutators.go +++ b/web/mutators.go @@ -2,12 +2,12 @@ package web import ( "fmt" + "github.com/bouncepaw/mycomarkup/v4" "github.com/bouncepaw/mycorrhiza/viewutil" "log" "net/http" - "github.com/bouncepaw/mycomarkup/v3" - "github.com/bouncepaw/mycomarkup/v3/mycocontext" + "github.com/bouncepaw/mycomarkup/v4/mycocontext" "github.com/gorilla/mux" @@ -208,7 +208,7 @@ func handlerUploadText(w http.ResponseWriter, rq *http.Request) { } if action == "Preview" { - ctx, _ := mycocontext.ContextFromStringInput(hyphaName, textData) + ctx, _ := mycocontext.ContextFromStringInput(textData, shroom.MarkupOptions(hyphaName)) util.HTTP200Page( w, diff --git a/web/readers.go b/web/readers.go index c5e1ba6..acccb33 100644 --- a/web/readers.go +++ b/web/readers.go @@ -3,6 +3,8 @@ package web import ( "encoding/hex" "fmt" + "github.com/bouncepaw/mycomarkup/v4" + "github.com/bouncepaw/mycorrhiza/shroom" "github.com/bouncepaw/mycorrhiza/viewutil" "io" "log" @@ -13,7 +15,6 @@ import ( "github.com/gorilla/mux" - "github.com/bouncepaw/mycorrhiza/cfg" "github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/hyphae" "github.com/bouncepaw/mycorrhiza/l18n" @@ -22,9 +23,8 @@ import ( "github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/views" - "github.com/bouncepaw/mycomarkup/v3" - "github.com/bouncepaw/mycomarkup/v3/mycocontext" - "github.com/bouncepaw/mycomarkup/v3/tools" + "github.com/bouncepaw/mycomarkup/v4/mycocontext" + "github.com/bouncepaw/mycomarkup/v4/tools" ) func initReaders(r *mux.Router) { @@ -140,7 +140,7 @@ func handlerRevision(w http.ResponseWriter, rq *http.Request) { var textContents, err = history.FileAtRevision(h.TextFilePath(), revHash) if err == nil { - ctx, _ := mycocontext.ContextFromStringInput(hyphaName, textContents) + ctx, _ := mycocontext.ContextFromStringInput(textContents, shroom.MarkupOptions(hyphaName)) contents = mycomarkup.BlocksToHTML(ctx, mycomarkup.BlockTree(ctx)) } } @@ -213,8 +213,7 @@ func handlerHypha(w http.ResponseWriter, rq *http.Request) { case hyphae.ExistingHypha: fileContentsT, errT := os.ReadFile(h.TextFilePath()) if errT == nil { - ctx, _ := mycocontext.ContextFromStringInput(hyphaName, string(fileContentsT)) - ctx = mycocontext.WithWebSiteURL(ctx, cfg.URL) + ctx, _ := mycocontext.ContextFromStringInput(string(fileContentsT), shroom.MarkupOptions(hyphaName)) getOpenGraph, descVisitor, imgVisitor := tools.OpenGraphVisitors(ctx) ast := mycomarkup.BlockTree(ctx, descVisitor, imgVisitor) contents = mycomarkup.BlocksToHTML(ctx, ast)