2022-05-27 13:43:40 +00:00
|
|
|
package interwiki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/viewutil"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"net/http"
|
2022-06-21 21:39:56 +00:00
|
|
|
"strings"
|
2022-05-27 13:43:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
//go:embed *html
|
2022-08-02 09:29:25 +00:00
|
|
|
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/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}}
|
|
|
|
`
|
2022-05-27 13:43:40 +00:00
|
|
|
chainInterwiki viewutil.Chain
|
2022-06-21 21:39:56 +00:00
|
|
|
chainNameTaken viewutil.Chain
|
2022-05-27 13:43:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func InitHandlers(rtr *mux.Router) {
|
|
|
|
chainInterwiki = viewutil.CopyEnRuWith(fs, "view_interwiki.html", ruTranslation)
|
2022-06-21 21:39:56 +00:00
|
|
|
chainNameTaken = viewutil.CopyEnRuWith(fs, "view_name_taken.html", ruTranslation)
|
2022-05-27 13:43:40 +00:00
|
|
|
rtr.HandleFunc("/interwiki", handlerInterwiki)
|
2022-06-21 21:39:56 +00:00
|
|
|
rtr.HandleFunc("/interwiki/add-entry", handlerAddEntry).Methods(http.MethodPost)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlerAddEntry(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
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()
|
|
|
|
if err := addEntry(&wiki); err != nil {
|
|
|
|
viewNameTaken(viewutil.MetaFrom(w, rq), &wiki, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
saveInterwikiJson()
|
|
|
|
http.Redirect(w, rq, "/interwiki", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
type nameTakenData struct {
|
|
|
|
*viewutil.BaseData
|
|
|
|
*Wiki
|
|
|
|
TakenName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func viewNameTaken(meta viewutil.Meta, wiki *Wiki, takenName string) {
|
|
|
|
viewutil.ExecutePage(meta, chainNameTaken, nameTakenData{
|
|
|
|
BaseData: &viewutil.BaseData{},
|
|
|
|
Wiki: wiki,
|
|
|
|
TakenName: takenName,
|
|
|
|
})
|
2022-05-27 13:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerInterwiki(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
viewInterwiki(viewutil.MetaFrom(w, rq))
|
|
|
|
}
|
|
|
|
|
|
|
|
type interwikiData struct {
|
|
|
|
*viewutil.BaseData
|
|
|
|
Entries []*Wiki
|
|
|
|
CanEdit bool
|
2022-06-06 16:12:56 +00:00
|
|
|
Error string
|
2022-05-27 13:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func viewInterwiki(meta viewutil.Meta) {
|
|
|
|
viewutil.ExecutePage(meta, chainInterwiki, interwikiData{
|
|
|
|
BaseData: &viewutil.BaseData{},
|
2022-06-21 21:39:56 +00:00
|
|
|
Entries: listOfEntries,
|
2022-05-27 13:43:40 +00:00
|
|
|
CanEdit: meta.U.Group == "admin",
|
2022-06-06 16:12:56 +00:00
|
|
|
Error: "",
|
2022-05-27 13:43:40 +00:00
|
|
|
})
|
|
|
|
}
|