1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-03-10 13:38:20 +00:00
mycorrhiza/misc/about.go
Timur Ismagilov a4cc67cd74
Migrate from log to slog #109 (#255)
* Migrate httpd.go

* Migrate history and main

* Migrate hypview

* Migrate interwiki

* Migrate misc

* Migrate utils

* Migrate backlinks

* Migrate categories

* Reformat some imports

* Migrate hyphae

* Migrate migration

* Reformat more imports

* Migrate user

* Migrate shroom

* Migrate viewutil

* Migrate web

* Migrate others

* Migrate main

* Wording concerns
2024-09-07 23:55:39 +03:00

123 lines
3.6 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 misc
import (
"log/slog"
"os"
"strings"
"text/template" // sic! TODO: make it html/template after the template library migration
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/internal/user"
"github.com/bouncepaw/mycorrhiza/internal/version"
"github.com/bouncepaw/mycorrhiza/l18n"
)
type L10nEntry struct {
_en string
_ru string
}
func En(v string) L10nEntry {
return L10nEntry{_en: v}
}
func (e L10nEntry) Ru(v string) L10nEntry {
e._ru = v
return e
}
func (e L10nEntry) Get(lang string) string {
if lang == "ru" && e._ru != "" {
return e._ru
}
return e._en
}
const aboutTemplateString = `
<main class="main-width">
<section class="about-page">
<h1>{{ printf (get .L.Title) .Cfg.WikiName }}</h1>
<dl>
<dt>{{ get .L.Version }}</dt>
<dd>{{ .Version }}</dd>
{{ if .Cfg.UseAuth }}
<dt>{{ get .L.HomeHypha }}</dt>
<dd><a href="/">{{ .Cfg.HomeHypha }}</a></dd>
<dt>{{get .L.Auth}}</dt>
<dd>{{ get .L.AuthOn }}</dd>
{{if .Cfg.TelegramEnabled}}<dd>{{get .L.TelegramOn}}</dd>{{end}}
<dt>{{ get .L.UserCount }}</dt>
<dd>{{ .UserCount }}</dd>
{{if .Cfg.RegistrationLimit}}
<dt>{{get .L.RegistrationLimit}}</dt>
<dd>{{.RegistrationLimit}}</dd>
{{end}}
<dt>{{ get .L.Admins }}</dt>
{{$cfg := .Cfg}}{{ range $i, $username := .Admins }}
<dd><a href="/hypha/{{ $cfg.UserHypha }}/{{ $username }}">{{ $username }}</a></dd>
{{ end }}
{{ else }}
<dt>{{get .L.Auth}}</dt>
<dd>{{ get .L.AuthOff }}</dd>
{{ end }}
</dl>
</section>
</main>`
var aboutData = struct {
L map[string]L10nEntry
Version string
Cfg map[string]interface{}
Admins []string
UserCount uint64
RegistrationLimit uint64
}{
L: map[string]L10nEntry{
"Title": En("About %s").Ru("О %s"),
"Version": En("<a href=\"https://mycorrhiza.wiki\">Mycorrhiza Wiki</a> version").Ru("Версия <a href=\"https://mycorrhiza.wiki\">Микоризы</a>"),
"UserCount": En("User count").Ru("Число пользователей"),
"HomeHypha": En("Home hypha").Ru("Домашняя гифа"),
"RegistrationLimit": En("RegistrationLimit").Ru("Максимум пользователей"),
"Admins": En("Administrators").Ru("Администраторы"),
"Auth": En("Authentication").Ru("Аутентификация"),
"AuthOn": En("Authentication is on").Ru("Аутентификация включена"),
"AuthOff": En("Authentication is off").Ru("Аутентификация не включена"),
"TelegramOn": En("Telegram authentication is on").Ru("Вход через Телеграм включён"),
},
}
func AboutHTML(lc *l18n.Localizer) string {
get := func(e L10nEntry) string {
return e.Get(lc.Locale)
}
temp, err := template.New("about wiki").Funcs(template.FuncMap{"get": get}).Parse(aboutTemplateString)
if err != nil {
slog.Error("Failed to parse About template", "err", err)
os.Exit(1)
}
data := aboutData
data.Version = version.Short
data.Admins = user.ListUsersWithGroup("admin")
data.UserCount = user.Count()
data.RegistrationLimit = cfg.RegistrationLimit
data.Cfg = map[string]interface{}{
"UseAuth": cfg.UseAuth,
"WikiName": cfg.WikiName,
"HomeHypha": cfg.HomeHypha,
"TelegramEnabled": cfg.TelegramEnabled,
"RegistrationLimit": cfg.RegistrationLimit,
}
var out strings.Builder
err = temp.Execute(&out, data)
if err != nil {
slog.Error("Failed to execute About template", "err", err)
os.Exit(1)
}
return out.String()
}