1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-11-09 20:13:00 +00:00
Files
mycorrhiza/interwiki/web.go
Timur Ismagilov 41733c50bd New templates #117 (#236)
Didn't have the chance to migrate //all// templates just yet. We'll get there.

* Implement yet another template system

* Move orphans to the new system and fix a bug in it

* Link orphans in the admin panel

* Move the backlink handlers to the web package

* Move auth routing to web

* Move /user-list to the new system

* Move change password and translate it

* Move stuff

* Move admin-related stuff to the web

* Move a lot of files into internal dir

Outside of it are web and stuff that needs further refactoring

* Fix static not loading and de-qtpl tree

* Move tree to internal

* Keep the globe on the same line #230

* Revert "Keep the globe on the same line #230"

This reverts commit ae78e5e459b1e980ba89bf29e61f75c0625ed2c7.

* Migrate templates from hypview: delete, edit, start empty and existing WIP

The delete media view was removed, I didn't even know it still existed as a GET. A rudiment.

* Make views multi-file and break compilation

* Megarefactoring of hypha views

* Auth-related stuffs

* Fix some of those weird imports

* Migrate cat views

* Fix cat js

* Lower standards

* Internalize trauma
2024-09-07 21:22:41 +03:00

127 lines
3.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package interwiki
import (
"embed"
"github.com/bouncepaw/mycorrhiza/web/viewutil"
"github.com/gorilla/mux"
"log"
"net/http"
"strings"
)
var (
//go:embed *html
fs embed.FS
ruTranslation = `
{{define "interwiki map"}}Интеркарта{{end}}
{{define "name"}}Название:{{end}}
{{define "aliases"}}Псевдонимы:{{end}}
{{define "aliases (,)"}}Псевдонимы (разделённые запятыми):{{end}}
{{define "engine"}}Движок:{{end}}
{{define "engine/mycorrhiza"}}Микориза{{end}}
{{define "engine/betula"}}Бетула{{end}}
{{define "engine/agora"}}Агора{{end}}
{{define "engine/generic"}}Любой сайт{{end}}
{{define "link href format"}}Строка форматирования атрибута href ссылки:{{end}}
{{define "img src format"}}Строка форматирования атрибута src изображения:{{end}}
{{define "unset map"}}Интеркарта не задана.{{end}}
{{define "documentation."}}Документация.{{end}}
{{define "edit separately."}}Изменяйте записи по отдельности.{{end}}
{{define "add interwiki entry"}}Добавить запись в интеркарту{{end}}
`
chainInterwiki viewutil.Chain
chainNameTaken viewutil.Chain
)
func InitHandlers(rtr *mux.Router) {
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)
}
func readInterwikiEntryFromRequest(rq *http.Request) Wiki {
wiki := Wiki{
Name: rq.PostFormValue("name"),
Aliases: strings.Split(rq.PostFormValue("aliases"), ","),
URL: rq.PostFormValue("url"),
LinkHrefFormat: rq.PostFormValue("link-href-format"),
ImgSrcFormat: rq.PostFormValue("img-src-format"),
Engine: WikiEngine(rq.PostFormValue("engine")),
}
wiki.canonize()
return wiki
}
func handlerModifyEntry(w http.ResponseWriter, rq *http.Request) {
var (
oldData *Wiki
ok bool
name = mux.Vars(rq)["target"]
newData = readInterwikiEntryFromRequest(rq)
)
if oldData, ok = entriesByName[name]; !ok {
log.Printf("Could not modify interwiki entry %s because it does not exist", name)
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(viewutil.MetaFrom(w, rq), oldData, err.Error(), "modify-entry/"+name)
return
}
saveInterwikiJson()
log.Printf("Modified interwiki entry %s\n", name)
http.Redirect(w, rq, "/interwiki", http.StatusSeeOther)
}
func handlerAddEntry(w http.ResponseWriter, rq *http.Request) {
wiki := readInterwikiEntryFromRequest(rq)
if err := addEntry(&wiki); err != nil {
viewNameTaken(viewutil.MetaFrom(w, rq), &wiki, err.Error(), "add-entry")
return
}
saveInterwikiJson()
http.Redirect(w, rq, "/interwiki", http.StatusSeeOther)
}
type nameTakenData struct {
*viewutil.BaseData
*Wiki
TakenName string
Action string
}
func viewNameTaken(meta viewutil.Meta, wiki *Wiki, takenName, action string) {
viewutil.ExecutePage(meta, chainNameTaken, nameTakenData{
BaseData: &viewutil.BaseData{},
Wiki: wiki,
TakenName: takenName,
Action: action,
})
}
func handlerInterwiki(w http.ResponseWriter, rq *http.Request) {
viewInterwiki(viewutil.MetaFrom(w, rq))
}
type interwikiData struct {
*viewutil.BaseData
Entries []*Wiki
CanEdit bool
Error string
}
func viewInterwiki(meta viewutil.Meta) {
viewutil.ExecutePage(meta, chainInterwiki, interwikiData{
BaseData: &viewutil.BaseData{},
Entries: listOfEntries,
CanEdit: meta.U.Group == "admin",
Error: "",
})
}