diff --git a/categories/handlers.go b/categories/handlers.go index a6ae643..4b6b04e 100644 --- a/categories/handlers.go +++ b/categories/handlers.go @@ -1,14 +1,15 @@ package categories import ( - "github.com/bouncepaw/mycorrhiza/internal/user" - "github.com/bouncepaw/mycorrhiza/util" - "github.com/bouncepaw/mycorrhiza/web/viewutil" - "github.com/gorilla/mux" "io" "log" "net/http" "strings" + + "github.com/bouncepaw/mycorrhiza/internal/user" + "github.com/bouncepaw/mycorrhiza/util" + "github.com/bouncepaw/mycorrhiza/web/viewutil" + "github.com/gorilla/mux" ) // InitHandlers initializes HTTP handlers for the given router. Call somewhere in package web. diff --git a/categories/view_card.html b/categories/view_card.html deleted file mode 100644 index e69de29..0000000 diff --git a/categories/views.go b/categories/views.go index 87242a1..6526ea7 100644 --- a/categories/views.go +++ b/categories/views.go @@ -2,10 +2,9 @@ package categories import ( "embed" - viewutil2 "github.com/bouncepaw/mycorrhiza/web/viewutil" - "log" "sort" - "strings" + + "github.com/bouncepaw/mycorrhiza/web/viewutil" ) const ruTranslation = ` @@ -31,46 +30,26 @@ const ruTranslation = ` var ( //go:embed *.html fs embed.FS - viewListChain, viewPageChain, viewCardChain, viewEditChain viewutil2.Chain + viewListChain, viewPageChain, viewCardChain, viewEditChain viewutil.Chain ) func prepareViews() { - viewCardChain = viewutil2.CopyEnRuWith(fs, "view_card.html", ruTranslation) - viewListChain = viewutil2.CopyEnRuWith(fs, "view_list.html", ruTranslation) - viewPageChain = viewutil2.CopyEnRuWith(fs, "view_page.html", ruTranslation) - viewEditChain = viewutil2.CopyEnRuWith(fs, "view_edit.html", ruTranslation) -} - -type cardData struct { - HyphaName string - Categories []string - GivenPermissionToModify bool -} - -// CategoryCard is the little sidebar that is shown nearby the hypha view. -func CategoryCard(meta viewutil2.Meta, hyphaName string) string { - var buf strings.Builder - err := viewCardChain.Get(meta).ExecuteTemplate(&buf, "category card", cardData{ - HyphaName: hyphaName, - Categories: CategoriesWithHypha(hyphaName), - GivenPermissionToModify: meta.U.CanProceed("add-to-category"), - }) - if err != nil { - log.Println(err) - } - return buf.String() + viewCardChain = viewutil.CopyEnRuWith(fs, "view_card.html", ruTranslation) + viewListChain = viewutil.CopyEnRuWith(fs, "view_list.html", ruTranslation) + viewPageChain = viewutil.CopyEnRuWith(fs, "view_page.html", ruTranslation) + viewEditChain = viewutil.CopyEnRuWith(fs, "view_edit.html", ruTranslation) } type catData struct { - *viewutil2.BaseData + *viewutil.BaseData CatName string Hyphae []string GivenPermissionToModify bool } -func categoryEdit(meta viewutil2.Meta, catName string) { - viewutil2.ExecutePage(meta, viewEditChain, catData{ - BaseData: &viewutil2.BaseData{ +func categoryEdit(meta viewutil.Meta, catName string) { + viewutil.ExecutePage(meta, viewEditChain, catData{ + BaseData: &viewutil.BaseData{ Addr: "/edit-category/" + catName, }, CatName: catName, @@ -79,9 +58,9 @@ func categoryEdit(meta viewutil2.Meta, catName string) { }) } -func categoryPage(meta viewutil2.Meta, catName string) { - viewutil2.ExecutePage(meta, viewPageChain, catData{ - BaseData: &viewutil2.BaseData{ +func categoryPage(meta viewutil.Meta, catName string) { + viewutil.ExecutePage(meta, viewPageChain, catData{ + BaseData: &viewutil.BaseData{ Addr: "/category/" + catName, }, CatName: catName, @@ -91,15 +70,15 @@ func categoryPage(meta viewutil2.Meta, catName string) { } type listData struct { - *viewutil2.BaseData + *viewutil.BaseData Categories []string } -func categoryList(meta viewutil2.Meta) { +func categoryList(meta viewutil.Meta) { cats := listOfCategories() sort.Strings(cats) - viewutil2.ExecutePage(meta, viewListChain, listData{ - BaseData: &viewutil2.BaseData{ + viewutil.ExecutePage(meta, viewListChain, listData{ + BaseData: &viewutil.BaseData{ Addr: "/category", }, Categories: cats, diff --git a/help/web.go b/help/web.go index f66edda..2259fac 100644 --- a/help/web.go +++ b/help/web.go @@ -2,19 +2,21 @@ package help // stuff.go is used for meta stuff about the wiki or all hyphae at once. import ( - "git.sr.ht/~bouncepaw/mycomarkup/v5" - "github.com/bouncepaw/mycorrhiza/mycoopts" - viewutil2 "github.com/bouncepaw/mycorrhiza/web/viewutil" - "github.com/gorilla/mux" "io" "net/http" "strings" + "github.com/bouncepaw/mycorrhiza/mycoopts" + "github.com/bouncepaw/mycorrhiza/web/viewutil" + + "git.sr.ht/~bouncepaw/mycomarkup/v5" "git.sr.ht/~bouncepaw/mycomarkup/v5/mycocontext" + + "github.com/gorilla/mux" ) var ( - chain viewutil2.Chain + chain viewutil.Chain ruTranslation = ` {{define "title"}}Справка{{end}} {{define "entry not found"}}Статья не найдена{{end}} @@ -46,14 +48,14 @@ var ( func InitHandlers(r *mux.Router) { r.PathPrefix("/help").HandlerFunc(handlerHelp) - chain = viewutil2.CopyEnRuWith(fs, "view_help.html", ruTranslation) + chain = viewutil.CopyEnRuWith(fs, "view_help.html", ruTranslation) } // handlerHelp gets the appropriate documentation or tells you where you (personally) have failed. func handlerHelp(w http.ResponseWriter, rq *http.Request) { // See the history of this file to resurrect the old algorithm that supported multiple languages var ( - meta = viewutil2.MetaFrom(w, rq) + meta = viewutil.MetaFrom(w, rq) articlePath = strings.TrimPrefix(strings.TrimPrefix(rq.URL.Path, "/help/"), "/help") lang = "en" ) @@ -88,14 +90,14 @@ func handlerHelp(w http.ResponseWriter, rq *http.Request) { } type helpData struct { - *viewutil2.BaseData + *viewutil.BaseData ContentsHTML string Lang string } -func viewHelp(meta viewutil2.Meta, lang, contentsHTML, articlePath string) { - viewutil2.ExecutePage(meta, chain, helpData{ - BaseData: &viewutil2.BaseData{ +func viewHelp(meta viewutil.Meta, lang, contentsHTML, articlePath string) { + viewutil.ExecutePage(meta, chain, helpData{ + BaseData: &viewutil.BaseData{ Addr: "/help/" + articlePath, }, ContentsHTML: contentsHTML, diff --git a/interwiki/web.go b/interwiki/web.go index 124859f..58ba51d 100644 --- a/interwiki/web.go +++ b/interwiki/web.go @@ -2,7 +2,7 @@ package interwiki import ( "embed" - viewutil2 "github.com/bouncepaw/mycorrhiza/web/viewutil" + "github.com/bouncepaw/mycorrhiza/web/viewutil" "github.com/gorilla/mux" "log" "net/http" @@ -29,13 +29,13 @@ var ( {{define "edit separately."}}Изменяйте записи по отдельности.{{end}} {{define "add interwiki entry"}}Добавить запись в интеркарту{{end}} ` - chainInterwiki viewutil2.Chain - chainNameTaken viewutil2.Chain + chainInterwiki viewutil.Chain + chainNameTaken viewutil.Chain ) func InitHandlers(rtr *mux.Router) { - chainInterwiki = viewutil2.CopyEnRuWith(fs, "view_interwiki.html", ruTranslation) - chainNameTaken = viewutil2.CopyEnRuWith(fs, "view_name_taken.html", ruTranslation) + chainInterwiki = viewutil.CopyEnRuWith(fs, "view_interwiki.html", ruTranslation) + chainNameTaken = viewutil.CopyEnRuWith(fs, "view_name_taken.html", ruTranslation) rtr.HandleFunc("/interwiki", handlerInterwiki) rtr.HandleFunc("/interwiki/add-entry", handlerAddEntry).Methods(http.MethodPost) rtr.HandleFunc("/interwiki/modify-entry/{target}", handlerModifyEntry).Methods(http.MethodPost) @@ -64,13 +64,13 @@ func handlerModifyEntry(w http.ResponseWriter, rq *http.Request) { if oldData, ok = entriesByName[name]; !ok { log.Printf("Could not modify interwiki entry ‘%s’ because it does not exist", name) - viewutil2.HandlerNotFound(w, rq) + viewutil.HandlerNotFound(w, rq) return } if err := replaceEntry(oldData, &newData); err != nil { log.Printf("Could not modify interwiki entry ‘%s’ because one of the proposed aliases/name is taken\n", name) - viewNameTaken(viewutil2.MetaFrom(w, rq), oldData, err.Error(), "modify-entry/"+name) + viewNameTaken(viewutil.MetaFrom(w, rq), oldData, err.Error(), "modify-entry/"+name) return } @@ -82,7 +82,7 @@ func handlerModifyEntry(w http.ResponseWriter, rq *http.Request) { func handlerAddEntry(w http.ResponseWriter, rq *http.Request) { wiki := readInterwikiEntryFromRequest(rq) if err := addEntry(&wiki); err != nil { - viewNameTaken(viewutil2.MetaFrom(w, rq), &wiki, err.Error(), "add-entry") + viewNameTaken(viewutil.MetaFrom(w, rq), &wiki, err.Error(), "add-entry") return } saveInterwikiJson() @@ -90,15 +90,15 @@ func handlerAddEntry(w http.ResponseWriter, rq *http.Request) { } type nameTakenData struct { - *viewutil2.BaseData + *viewutil.BaseData *Wiki TakenName string Action string } -func viewNameTaken(meta viewutil2.Meta, wiki *Wiki, takenName, action string) { - viewutil2.ExecutePage(meta, chainNameTaken, nameTakenData{ - BaseData: &viewutil2.BaseData{}, +func viewNameTaken(meta viewutil.Meta, wiki *Wiki, takenName, action string) { + viewutil.ExecutePage(meta, chainNameTaken, nameTakenData{ + BaseData: &viewutil.BaseData{}, Wiki: wiki, TakenName: takenName, Action: action, @@ -106,19 +106,19 @@ func viewNameTaken(meta viewutil2.Meta, wiki *Wiki, takenName, action string) { } func handlerInterwiki(w http.ResponseWriter, rq *http.Request) { - viewInterwiki(viewutil2.MetaFrom(w, rq)) + viewInterwiki(viewutil.MetaFrom(w, rq)) } type interwikiData struct { - *viewutil2.BaseData + *viewutil.BaseData Entries []*Wiki CanEdit bool Error string } -func viewInterwiki(meta viewutil2.Meta) { - viewutil2.ExecutePage(meta, chainInterwiki, interwikiData{ - BaseData: &viewutil2.BaseData{}, +func viewInterwiki(meta viewutil.Meta) { + viewutil.ExecutePage(meta, chainInterwiki, interwikiData{ + BaseData: &viewutil.BaseData{}, Entries: listOfEntries, CanEdit: meta.U.Group == "admin", Error: "", diff --git a/misc/handlers.go b/misc/handlers.go index 7a5878f..e282ee4 100644 --- a/misc/handlers.go +++ b/misc/handlers.go @@ -2,13 +2,6 @@ package misc import ( - "github.com/bouncepaw/mycorrhiza/internal/backlinks" - "github.com/bouncepaw/mycorrhiza/internal/cfg" - hyphae2 "github.com/bouncepaw/mycorrhiza/internal/hyphae" - shroom2 "github.com/bouncepaw/mycorrhiza/internal/shroom" - "github.com/bouncepaw/mycorrhiza/internal/user" - "github.com/bouncepaw/mycorrhiza/web/static" - viewutil2 "github.com/bouncepaw/mycorrhiza/web/viewutil" "io" "log" "math/rand" @@ -18,9 +11,16 @@ import ( "github.com/gorilla/mux" + "github.com/bouncepaw/mycorrhiza/internal/backlinks" + "github.com/bouncepaw/mycorrhiza/internal/cfg" "github.com/bouncepaw/mycorrhiza/internal/files" + "github.com/bouncepaw/mycorrhiza/internal/hyphae" + "github.com/bouncepaw/mycorrhiza/internal/shroom" + "github.com/bouncepaw/mycorrhiza/internal/user" "github.com/bouncepaw/mycorrhiza/l18n" "github.com/bouncepaw/mycorrhiza/util" + "github.com/bouncepaw/mycorrhiza/web/static" + "github.com/bouncepaw/mycorrhiza/web/viewutil" ) func InitAssetHandlers(rtr *mux.Router) { @@ -49,22 +49,22 @@ func handlerList(w http.ResponseWriter, rq *http.Request) { // TODO: make this more effective, there are too many loops and vars var ( hyphaNames = make(chan string) - sortedHypha = hyphae2.PathographicSort(hyphaNames) + sortedHypha = hyphae.PathographicSort(hyphaNames) entries []listDatum ) - for hypha := range hyphae2.YieldExistingHyphae() { + for hypha := range hyphae.YieldExistingHyphae() { hyphaNames <- hypha.CanonicalName() } close(hyphaNames) for hyphaName := range sortedHypha { - switch h := hyphae2.ByName(hyphaName).(type) { - case *hyphae2.TextualHypha: + switch h := hyphae.ByName(hyphaName).(type) { + case *hyphae.TextualHypha: entries = append(entries, listDatum{h.CanonicalName(), ""}) - case *hyphae2.MediaHypha: + case *hyphae.MediaHypha: entries = append(entries, listDatum{h.CanonicalName(), filepath.Ext(h.MediaFilePath())[1:]}) } } - viewList(viewutil2.MetaFrom(w, rq), entries) + viewList(viewutil.MetaFrom(w, rq), entries) } // handlerReindex reindexes all hyphae by checking the wiki storage directory anew. @@ -72,13 +72,13 @@ func handlerReindex(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) if ok := user.CanProceed(rq, "reindex"); !ok { var lc = l18n.FromRequest(rq) - viewutil2.HttpErr(viewutil2.MetaFrom(w, rq), http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.reindex_no_rights")) + viewutil.HttpErr(viewutil.MetaFrom(w, rq), http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.reindex_no_rights")) log.Println("Rejected", rq.URL) return } - hyphae2.ResetCount() + hyphae.ResetCount() log.Println("Reindexing hyphae in", files.HyphaeDir()) - hyphae2.Index(files.HyphaeDir()) + hyphae.Index(files.HyphaeDir()) backlinks.IndexBacklinks() http.Redirect(w, rq, "/", http.StatusSeeOther) } @@ -88,11 +88,11 @@ func handlerUpdateHeaderLinks(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) if ok := user.CanProceed(rq, "update-header-links"); !ok { var lc = l18n.FromRequest(rq) - viewutil2.HttpErr(viewutil2.MetaFrom(w, rq), http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.header_no_rights")) + viewutil.HttpErr(viewutil.MetaFrom(w, rq), http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.header_no_rights")) log.Println("Rejected", rq.URL) return } - shroom2.SetHeaderLinks() + shroom.SetHeaderLinks() http.Redirect(w, rq, "/", http.StatusSeeOther) } @@ -101,15 +101,15 @@ func handlerRandom(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) var ( randomHyphaName string - amountOfHyphae = hyphae2.Count() + amountOfHyphae = hyphae.Count() ) if amountOfHyphae == 0 { var lc = l18n.FromRequest(rq) - viewutil2.HttpErr(viewutil2.MetaFrom(w, rq), http.StatusNotFound, cfg.HomeHypha, lc.Get("ui.random_no_hyphae_tip")) + viewutil.HttpErr(viewutil.MetaFrom(w, rq), http.StatusNotFound, cfg.HomeHypha, lc.Get("ui.random_no_hyphae_tip")) return } i := rand.Intn(amountOfHyphae) - for h := range hyphae2.YieldExistingHyphae() { + for h := range hyphae.YieldExistingHyphae() { if i == 0 { randomHyphaName = h.CanonicalName() } @@ -126,8 +126,8 @@ func handlerAbout(w http.ResponseWriter, rq *http.Request) { lc = l18n.FromRequest(rq) title = lc.Get("ui.about_title", &l18n.Replacements{"name": cfg.WikiName}) ) - _, err := io.WriteString(w, viewutil2.Base( - viewutil2.MetaFrom(w, rq), + _, err := io.WriteString(w, viewutil.Base( + viewutil.MetaFrom(w, rq), title, AboutHTML(lc), map[string]string{}, @@ -174,12 +174,12 @@ func handlerTitleSearch(w http.ResponseWriter, rq *http.Request) { var ( query = rq.FormValue("q") hyphaName = util.CanonicalName(query) - _, nameFree = hyphae2.AreFreeNames(hyphaName) + _, nameFree = hyphae.AreFreeNames(hyphaName) results []string ) - for hyphaName := range shroom2.YieldHyphaNamesContainingString(query) { + for hyphaName := range shroom.YieldHyphaNamesContainingString(query) { results = append(results, hyphaName) } w.WriteHeader(http.StatusOK) - viewTitleSearch(viewutil2.MetaFrom(w, rq), query, hyphaName, !nameFree, results) + viewTitleSearch(viewutil.MetaFrom(w, rq), query, hyphaName, !nameFree, results) } diff --git a/misc/views.go b/misc/views.go index fe01d95..99f83ff 100644 --- a/misc/views.go +++ b/misc/views.go @@ -3,13 +3,13 @@ package misc import ( "embed" "github.com/bouncepaw/mycorrhiza/internal/hyphae" - viewutil2 "github.com/bouncepaw/mycorrhiza/web/viewutil" + "github.com/bouncepaw/mycorrhiza/web/viewutil" ) var ( //go:embed *html fs embed.FS - chainList, chainTitleSearch viewutil2.Chain + chainList, chainTitleSearch viewutil.Chain ruTranslation = ` {{define "list of hyphae"}}Список гиф{{end}} {{define "search:"}}Поиск: {{.}}{{end}} @@ -21,8 +21,8 @@ var ( ) func initViews() { - chainList = viewutil2.CopyEnRuWith(fs, "view_list.html", ruTranslation) - chainTitleSearch = viewutil2.CopyEnRuWith(fs, "view_title_search.html", ruTranslation) + chainList = viewutil.CopyEnRuWith(fs, "view_list.html", ruTranslation) + chainTitleSearch = viewutil.CopyEnRuWith(fs, "view_title_search.html", ruTranslation) } type listDatum struct { @@ -31,30 +31,30 @@ type listDatum struct { } type listData struct { - *viewutil2.BaseData + *viewutil.BaseData Entries []listDatum HyphaCount int } -func viewList(meta viewutil2.Meta, entries []listDatum) { - viewutil2.ExecutePage(meta, chainList, listData{ - BaseData: &viewutil2.BaseData{}, +func viewList(meta viewutil.Meta, entries []listDatum) { + viewutil.ExecutePage(meta, chainList, listData{ + BaseData: &viewutil.BaseData{}, Entries: entries, HyphaCount: hyphae.Count(), }) } type titleSearchData struct { - *viewutil2.BaseData + *viewutil.BaseData Query string Results []string MatchedHyphaName string HasExactMatch bool } -func viewTitleSearch(meta viewutil2.Meta, query string, hyphaName string, hasExactMatch bool, results []string) { - viewutil2.ExecutePage(meta, chainTitleSearch, titleSearchData{ - BaseData: &viewutil2.BaseData{}, +func viewTitleSearch(meta viewutil.Meta, query string, hyphaName string, hasExactMatch bool, results []string) { + viewutil.ExecutePage(meta, chainTitleSearch, titleSearchData{ + BaseData: &viewutil.BaseData{}, Query: query, Results: results, MatchedHyphaName: hyphaName, diff --git a/mycoopts/mycoopts.go b/mycoopts/mycoopts.go index 48d249c..bd1d9f2 100644 --- a/mycoopts/mycoopts.go +++ b/mycoopts/mycoopts.go @@ -4,7 +4,7 @@ import ( "errors" "git.sr.ht/~bouncepaw/mycomarkup/v5/options" "github.com/bouncepaw/mycorrhiza/internal/cfg" - hyphae2 "github.com/bouncepaw/mycorrhiza/internal/hyphae" + "github.com/bouncepaw/mycorrhiza/internal/hyphae" "github.com/bouncepaw/mycorrhiza/interwiki" "github.com/bouncepaw/mycorrhiza/util" ) @@ -17,26 +17,26 @@ func MarkupOptions(hyphaName string) options.Options { RedLinksSupported: true, InterwikiSupported: true, HyphaExists: func(hyphaName string) bool { - switch hyphae2.ByName(hyphaName).(type) { - case *hyphae2.EmptyHypha: + switch hyphae.ByName(hyphaName).(type) { + case *hyphae.EmptyHypha: return false default: return true } }, IterateHyphaNamesWith: func(λ func(string)) { - for h := range hyphae2.YieldExistingHyphae() { + for h := range hyphae.YieldExistingHyphae() { λ(h.CanonicalName()) } }, HyphaHTMLData: func(hyphaName string) (rawText, binaryBlock string, err error) { - switch h := hyphae2.ByName(hyphaName).(type) { - case *hyphae2.EmptyHypha: + switch h := hyphae.ByName(hyphaName).(type) { + case *hyphae.EmptyHypha: err = errors.New("Hypha " + hyphaName + " does not exist") - case *hyphae2.TextualHypha: - rawText, err = hyphae2.FetchMycomarkupFile(h) - case *hyphae2.MediaHypha: - rawText, err = hyphae2.FetchMycomarkupFile(h) + case *hyphae.TextualHypha: + rawText, err = hyphae.FetchMycomarkupFile(h) + case *hyphae.MediaHypha: + rawText, err = hyphae.FetchMycomarkupFile(h) binaryBlock = mediaRaw(h) } return diff --git a/web/password.go b/web/password.go index 6fc0629..4a74a7b 100644 --- a/web/password.go +++ b/web/password.go @@ -2,7 +2,7 @@ package web import ( "fmt" - user2 "github.com/bouncepaw/mycorrhiza/internal/user" + "github.com/bouncepaw/mycorrhiza/internal/user" "github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/web/viewutil" "mime" @@ -11,9 +11,9 @@ import ( ) func handlerUserChangePassword(w http.ResponseWriter, rq *http.Request) { - u := user2.FromRequest(rq) + u := user.FromRequest(rq) // TODO: is there a better way? - if reflect.DeepEqual(u, user2.EmptyUser()) || u == nil { + if reflect.DeepEqual(u, user.EmptyUser()) || u == nil { util.HTTP404Page(w, "404 page not found") return } @@ -21,7 +21,7 @@ func handlerUserChangePassword(w http.ResponseWriter, rq *http.Request) { f := util.FormDataFromRequest(rq, []string{"current_password", "password", "password_confirm"}) currentPassword := f.Get("current_password") - if user2.CredentialsOK(u.Name, currentPassword) { + if user.CredentialsOK(u.Name, currentPassword) { password := f.Get("password") passwordConfirm := f.Get("password_confirm") // server side validation @@ -34,7 +34,7 @@ func handlerUserChangePassword(w http.ResponseWriter, rq *http.Request) { if err := u.ChangePassword(password); err != nil { f = f.WithError(err) } else { - if err := user2.SaveUserDatabase(); err != nil { + if err := user.SaveUserDatabase(); err != nil { u.Password = previousPassword f = f.WithError(err) } else {