From cff7dafceaea50d3a533bdac7cd57f0a8fd692a4 Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Fri, 1 Jul 2022 20:51:22 +0500 Subject: [PATCH] Implement redirection hyphae #152 --- categories/categories.go | 5 +++-- categories/handlers.go | 2 +- cfg/config.go | 22 +++++++++++++--------- hypview/hypview.go | 3 ++- hypview/view_rename.html | 5 ++++- shroom/rename.go | 33 +++++++++++++++++++++++++++++++-- web/mutators.go | 9 +++++---- 7 files changed, 59 insertions(+), 20 deletions(-) diff --git a/categories/categories.go b/categories/categories.go index 61d1345..b004e18 100644 --- a/categories/categories.go +++ b/categories/categories.go @@ -57,8 +57,8 @@ func hyphaeInCategory(catName string) (hyphaList []string) { var mutex sync.RWMutex -// addHyphaToCategory adds the hypha to the category and updates the records on the disk. If the hypha is already in the category, nothing happens. Pass canonical names. -func addHyphaToCategory(hyphaName, catName string) { +// AddHyphaToCategory adds the hypha to the category and updates the records on the disk. If the hypha is already in the category, nothing happens. Pass canonical names. +func AddHyphaToCategory(hyphaName, catName string) { mutex.Lock() if node, ok := hyphaToCategories[hyphaName]; ok { node.storeCategory(catName) @@ -109,4 +109,5 @@ func RenameHyphaInAllCategories(oldName, newName string) { } } } + go saveToDisk() } diff --git a/categories/handlers.go b/categories/handlers.go index 3c30319..625ce66 100644 --- a/categories/handlers.go +++ b/categories/handlers.go @@ -74,6 +74,6 @@ func handlerAddToCategory(w http.ResponseWriter, rq *http.Request) { return } log.Println(user.FromRequest(rq).Name, "added", hyphaName, "to", catName) - addHyphaToCategory(hyphaName, catName) + AddHyphaToCategory(hyphaName, catName) http.Redirect(w, rq, redirectTo, http.StatusSeeOther) } diff --git a/cfg/config.go b/cfg/config.go index af1f071..e12f775 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -21,9 +21,10 @@ var ( NaviTitleIcon string UseSiblingHyphaeSidebar bool - HomeHypha string - UserHypha string - HeaderLinksHypha string + HomeHypha string + UserHypha string + HeaderLinksHypha string + RedirectionCategory string ListenAddr string URL string @@ -64,9 +65,10 @@ type Config struct { // Hyphae is a section of Config which has fields related to special hyphae. type Hyphae struct { - HomeHypha string `comment:"This hypha will be the main (index) page of your wiki, served on /."` - UserHypha string `comment:"This hypha is used as a prefix for user hyphae."` - HeaderLinksHypha string `comment:"You can also specify a hypha to populate your own custom header links from."` + HomeHypha string `comment:"This hypha will be the main (index) page of your wiki, served on /."` + UserHypha string `comment:"This hypha is used as a prefix for user hyphae."` + HeaderLinksHypha string `comment:"You can also specify a hypha to populate your own custom header links from."` + RedirectionCategory string `comment:"Redirection hyphae will be added to this category. Default: redirection."` } // Network is a section of Config that has fields related to network stuff. @@ -113,9 +115,10 @@ func ReadConfigFile(path string) error { NaviTitleIcon: "πŸ„", UseSiblingHyphaeSidebar: false, Hyphae: Hyphae{ - HomeHypha: "home", - UserHypha: "u", - HeaderLinksHypha: "", + HomeHypha: "home", + UserHypha: "u", + HeaderLinksHypha: "", + RedirectionCategory: "redirection", }, Network: Network{ ListenAddr: "127.0.0.1:1737", @@ -175,6 +178,7 @@ func ReadConfigFile(path string) error { HomeHypha = cfg.HomeHypha UserHypha = cfg.UserHypha HeaderLinksHypha = cfg.HeaderLinksHypha + RedirectionCategory = cfg.RedirectionCategory if ListenAddr == "" { ListenAddr = cfg.ListenAddr } diff --git a/hypview/hypview.go b/hypview/hypview.go index 1e10598..216aebc 100644 --- a/hypview/hypview.go +++ b/hypview/hypview.go @@ -33,7 +33,8 @@ var ( {{define "rename [[hypha]]?"}}ΠŸΠ΅Ρ€Π΅ΠΈΠΌΠ΅Π½ΠΎΠ²Π°Ρ‚ΡŒ {{beautifulName .}}?{{end}} {{define "new name"}}НовоС Π½Π°Π·Π²Π°Π½ΠΈΠ΅:{{end}} {{define "rename recursively"}}Π’Π°ΠΊΠΆΠ΅ ΠΏΠ΅Ρ€Π΅ΠΈΠΌΠ΅Π½ΠΎΠ²Π°Ρ‚ΡŒ ΠΏΠΎΠ΄Π³ΠΈΡ„Ρ‹{{end}} -{{define "rename tip"}}Если Π²Ρ‹ ΠΏΠ΅Ρ€Π΅ΠΈΠΌΠ΅Π½ΡƒΠ΅Ρ‚Π΅ эту Π³ΠΈΡ„Ρƒ, ΡΠ»ΠΎΠΌΠ°ΡŽΡ‚ΡΡ всС ссылки, Π²Π΅Π΄ΡƒΡ‰ΠΈΠ΅ Π½Π° Π½Π΅Ρ‘, Π° Ρ‚Π°ΠΊΠΆΠ΅ исходящиС ΠΎΡ‚Π½ΠΎΡΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ ссылки. Π’Π°ΠΊΠΆΠ΅ Π²Ρ‹ потСряСтС всю Ρ‚Π΅ΠΊΡƒΡ‰ΡƒΡŽ ΠΈΡΡ‚ΠΎΡ€ΠΈΡŽ для Π½ΠΎΠ²ΠΎΠ³ΠΎ названия. ΠŸΠ΅Ρ€Π΅ΠΈΠΌΠ΅Π½ΠΎΠ²Ρ‹Π²Π°ΠΉΡ‚Π΅ Π°ΠΊΠΊΡƒΡ€Π°Ρ‚Π½ΠΎ.{{end}} +{{define "rename tip"}}ΠŸΠ΅Ρ€Π΅ΠΈΠΌΠ΅Π½ΠΎΠ²Ρ‹Π²Π°ΠΉΡ‚Π΅ Π°ΠΊΠΊΡƒΡ€Π°Ρ‚Π½ΠΎ. ДокумСнтация Π½Π° английском.{{end}} +{{define "leave redirections"}}ΠžΡΡ‚Π°Π²ΠΈΡ‚ΡŒ пСрСнаправлСния{{end}} ` chainNaviTitle viewutil.Chain chainEmptyHypha viewutil.Chain diff --git a/hypview/view_rename.html b/hypview/view_rename.html index 11c5b55..8a49dd1 100644 --- a/hypview/view_rename.html +++ b/hypview/view_rename.html @@ -12,8 +12,11 @@ +
+ + -

{{block "rename tip" .}}If you rename this hypha, all incoming links and all relative outcoming links will break. You will also lose all history for the new name. Rename carefully.{{end}}

+

{{block "rename tip" .}}Rename carefully. Documentation.{{end}}

diff --git a/shroom/rename.go b/shroom/rename.go index fc22fe6..24fb5bd 100644 --- a/shroom/rename.go +++ b/shroom/rename.go @@ -5,6 +5,9 @@ import ( "fmt" "github.com/bouncepaw/mycorrhiza/backlinks" "github.com/bouncepaw/mycorrhiza/categories" + "github.com/bouncepaw/mycorrhiza/cfg" + "github.com/bouncepaw/mycorrhiza/files" + "path/filepath" "regexp" "github.com/bouncepaw/mycorrhiza/history" @@ -14,7 +17,8 @@ import ( ) // Rename renames the old hypha to the new name and makes a history record about that. Call if and only if the user has the permission to rename. -func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, u *user.User) error { +func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, leaveRedirections bool, u *user.User) error { + // * bouncepaw hates this function and related renaming functions if newName == "" { rejectRenameLog(oldHypha, u, "no new name given") return errors.New("ui.rename_noname_tip") @@ -62,7 +66,7 @@ func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, u *us newName)) } - hop.WithFilesRenamed(renameMap).Apply() + hop.WithFilesRenamed(renameMap) if len(hop.Errs) != 0 { return hop.Errs[0] @@ -76,11 +80,36 @@ func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, u *us hyphae.RenameHyphaTo(h, newName, replaceName) backlinks.UpdateBacklinksAfterRename(h, oldName) categories.RenameHyphaInAllCategories(oldName, newName) + if leaveRedirections { + if err := leaveRedirection(oldName, newName, hop); err != nil { + hop.WithErrAbort(err) + return err + } + } } + hop.Apply() + return nil } +func leaveRedirection(oldName, newName string, hop *history.Op) error { + var ( + text = fmt.Sprintf("=> %s | ✏️ %s\n", newName, util.BeautifulName(newName)) + emptyHypha = hyphae.ByName(oldName) + ) + switch emptyHypha := emptyHypha.(type) { + case *hyphae.EmptyHypha: + h := hyphae.ExtendEmptyToTextual(emptyHypha, filepath.Join(files.HyphaeDir(), oldName+".myco")) + hyphae.Insert(h) + categories.AddHyphaToCategory(oldName, cfg.RedirectionCategory) + defer backlinks.UpdateBacklinksAfterEdit(h, "") + return writeTextToDisk(h, []byte(text), hop) + default: + return errors.New("invalid state for hypha " + oldName + " renamed to " + newName) + } +} + func findHyphaeToRename(superhypha hyphae.ExistingHypha, recursive bool) []hyphae.ExistingHypha { hyphaList := []hyphae.ExistingHypha{superhypha} if recursive { diff --git a/web/mutators.go b/web/mutators.go index 43c846f..d1632b7 100644 --- a/web/mutators.go +++ b/web/mutators.go @@ -124,9 +124,10 @@ func handlerRename(w http.ResponseWriter, rq *http.Request) { } var ( - oldHypha = h.(hyphae.ExistingHypha) - newName = util.CanonicalName(rq.PostFormValue("new-name")) - recursive = rq.PostFormValue("recursive") == "true" + oldHypha = h.(hyphae.ExistingHypha) + newName = util.CanonicalName(rq.PostFormValue("new-name")) + recursive = rq.PostFormValue("recursive") == "true" + leaveRedirections = rq.PostFormValue("redirection") == "true" ) if rq.Method == "GET" { @@ -134,7 +135,7 @@ func handlerRename(w http.ResponseWriter, rq *http.Request) { return } - if err := shroom.Rename(oldHypha, newName, recursive, u); err != nil { + if err := shroom.Rename(oldHypha, newName, recursive, leaveRedirections, u); err != nil { log.Printf("%s tries to rename β€˜%s’: %s", u.Name, oldHypha.CanonicalName(), err.Error()) viewutil.HttpErr(meta, http.StatusForbidden, oldHypha.CanonicalName(), lc.Get(err.Error())) // TODO: localize return