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