diff --git a/controllers/brief.go b/controllers/brief.go index e3317bd..2dcb279 100644 --- a/controllers/brief.go +++ b/controllers/brief.go @@ -24,5 +24,5 @@ func GetLetter(c *fiber.Ctx) error { text := lib.Letters.Item(letter) tradition := lib.Traditions.Item(letter) - return c.Render("/brief/", map[string]any{"meta": meta, "text": text, "tradition": tradition, "next": np.Next, "prev": np.Prev}) + return c.Render("/brief/", fiber.Map{"meta": meta, "text": text, "tradition": tradition, "next": np.Next, "prev": np.Prev}) } diff --git a/controllers/index.go b/controllers/index.go index 3ec4996..bb98ce0 100644 --- a/controllers/index.go +++ b/controllers/index.go @@ -1,27 +1,12 @@ package controllers import ( - "strconv" - - "github.com/Theodor-Springmann-Stiftung/lenz-web/xmlmodels" "github.com/gofiber/fiber/v2" + "strconv" ) const DEFAULT_YEAR = 1765 func GetIndex(c *fiber.Ctx) error { - return c.Redirect(JAHRGAENGE_URL + "/" + strconv.Itoa(DEFAULT_YEAR)) -} - -func GetIndexYear(c *fiber.Ctx) error { - lib := xmlmodels.Get() - years, yearmap := lib.Years() - - y := c.Params(YEAR_PARAM) - year, err := strconv.Atoi(y) - if _, ok := yearmap[year]; (err != nil || !ok) && y != "all" { - return c.SendStatus(fiber.StatusNotFound) - } - - return c.Render(JAHRGAENGE_URL+"/", map[string]any{"years": years, "yearmap": yearmap, "year": year, "all": y == "all"}) + return c.Redirect(LETTERS_URL + "?year=" + strconv.Itoa(DEFAULT_YEAR)) } diff --git a/controllers/routes.go b/controllers/routes.go index 384c5e1..d0bf253 100644 --- a/controllers/routes.go +++ b/controllers/routes.go @@ -6,30 +6,37 @@ import ( "github.com/Theodor-Springmann-Stiftung/lenz-web/server" "github.com/Theodor-Springmann-Stiftung/lenz-web/views" + "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/compress" ) -const INDEX_URL = "/" -const ASSETS_URL = "/assets" -const DATENSCHUTZ_URL = "/datenschutz" -const JAHRGAENGE_URL = "/jahrgang" -const ZITATION_URL = "/ausgabe/zitation" -const EDITION_URL = "/ausgabe/edition" -const KONTAKT_URL = "/kontakt" - -var INDEX_YEAR_URL = JAHRGAENGE_URL + "/:" + YEAR_PARAM -var LETTER_URL = "/brief/:" + LETTER_PARAM - -const YEAR_PARAM = "year" -const LETTER_PARAM = "letter" +const ( + INDEX_URL = "/" + ASSETS_URL = "/assets" + DATENSCHUTZ_URL = "/datenschutz" + LETTERS_URL = "/briefe" + ZITATION_URL = "/ausgabe/zitation" + EDITION_URL = "/ausgabe/edition" + KONTAKT_URL = "/kontakt" + LETTER_URL = "/brief/:" + LETTER_PARAM + YEAR_PARAM = "year" + LETTER_PARAM = "letter" +) func Register(server server.Server, cfg config.Config) { server.Server.Use(ASSETS_URL, compress.New(compress.Config{ Level: compress.LevelBestSpeed, })) server.Server.Use(ASSETS_URL, middleware.StaticHandler(&views.StaticFS)) + + server.Server.Use(func(ctx *fiber.Ctx) error { + ctx.Locals("cfg", cfg) + ctx.Locals("path", ctx.Path()) + return ctx.Next() + }) + server.Server.Get(INDEX_URL, GetIndex) - server.Server.Get(INDEX_YEAR_URL, GetIndexYear) + server.Server.Get(LETTERS_URL, GetLetters) server.Server.Get(LETTER_URL, GetLetter) server.Server.Get(DATENSCHUTZ_URL, Static(DATENSCHUTZ_URL+"/")) diff --git a/controllers/static.go b/controllers/static.go index cf8de34..6ca0f02 100644 --- a/controllers/static.go +++ b/controllers/static.go @@ -11,6 +11,6 @@ func Static(url string) fiber.Handler { url += "/" } return func(c *fiber.Ctx) error { - return c.Render(url, map[string]any{}) + return c.Render(url, fiber.Map{"Was": "Static"}) } } diff --git a/controllers/uebersicht.go b/controllers/uebersicht.go new file mode 100644 index 0000000..449b2a7 --- /dev/null +++ b/controllers/uebersicht.go @@ -0,0 +1,23 @@ +package controllers + +import ( + "strconv" + + "github.com/Theodor-Springmann-Stiftung/lenz-web/xmlmodels" + "github.com/gofiber/fiber/v2" +) + +func GetLetters(c *fiber.Ctx) error { + lib := xmlmodels.Get() + years, yearmap := lib.Years() + y := c.Query(YEAR_PARAM, strconv.Itoa(DEFAULT_YEAR)) + // TODO: does not work ATM + c.Locals("path", c.Path()) + + year, err := strconv.Atoi(y) + if _, ok := yearmap[year]; (err != nil || !ok) && y != "all" { + return c.SendStatus(fiber.StatusNotFound) + } + + return c.Render(LETTERS_URL+"/", fiber.Map{"years": years, "yearmap": yearmap, "year": year, "all": y == "all"}) +} diff --git a/helpers/functions/string.go b/helpers/functions/string.go index 4f68ac9..fd189a7 100644 --- a/helpers/functions/string.go +++ b/helpers/functions/string.go @@ -1,6 +1,11 @@ package functions -import "html/template" +import ( + "html/template" + "strings" + + xmlparsing "github.com/Theodor-Springmann-Stiftung/lenz-web/xml" +) func FirstLetter(s string) string { if len(s) == 0 { @@ -15,3 +20,41 @@ func Safe(s string) template.HTML { } return template.HTML(s) } + +type LenzParseState struct{} + +func ParseGeneric(s string) string { + if len(s) == 0 { + return "" + } + + builder := strings.Builder{} + for elem, err := range xmlparsing.Iterate(s, LenzParseState{}) { + if err != nil { + return err.Error() + } + switch elem.Token.Type { + case xmlparsing.StartElement: + builder.WriteString("
") + } + + if elem.Token.Type == xmlparsing.CharData { + builder.WriteString(elem.Token.Data) + } + + if elem.Token.Type == xmlparsing.EndElement { + builder.WriteString("
") + } + } + + return builder.String() +} diff --git a/lenz.go b/lenz.go index 18199a0..113893b 100644 --- a/lenz.go +++ b/lenz.go @@ -9,6 +9,7 @@ import ( "github.com/Theodor-Springmann-Stiftung/lenz-web/config" "github.com/Theodor-Springmann-Stiftung/lenz-web/controllers" gitprovider "github.com/Theodor-Springmann-Stiftung/lenz-web/git" + "github.com/Theodor-Springmann-Stiftung/lenz-web/helpers/functions" "github.com/Theodor-Springmann-Stiftung/lenz-web/server" "github.com/Theodor-Springmann-Stiftung/lenz-web/templating" "github.com/Theodor-Springmann-Stiftung/lenz-web/views" @@ -52,7 +53,7 @@ func main() { engine := templating.New(&views.LayoutFS, &views.RoutesFS) engine.AddFuncs(lib.FuncMap()) - + engine.AddFunc("ParseGeneric", functions.ParseGeneric) storage := memory.New(memory.Config{ GCInterval: 24 * time.Hour, }) diff --git a/server/server.go b/server/server.go index 7a1c2d7..d216205 100644 --- a/server/server.go +++ b/server/server.go @@ -47,14 +47,6 @@ func New(engine *templating.Engine, storage fiber.Storage, debug bool) Server { PassLocalsToViews: true, }) - if debug { - server.Use(logger.New()) - } - - if !debug { - server.Use(recover.New()) - } - if debug { server.Use(cache.New(cache.Config{ Next: CacheFunc, @@ -62,13 +54,17 @@ func New(engine *templating.Engine, storage fiber.Storage, debug bool) Server { CacheControl: false, Storage: storage, })) - } else { + server.Use(logger.New()) + } + + if !debug { server.Use(cache.New(cache.Config{ Next: CacheFunc, Expiration: CACHE_TIME, CacheControl: true, Storage: storage, })) + server.Use(recover.New()) } return Server{ diff --git a/templating/engine.go b/templating/engine.go index 355f242..979c2ed 100644 --- a/templating/engine.go +++ b/templating/engine.go @@ -11,6 +11,7 @@ import ( "sync" "github.com/Theodor-Springmann-Stiftung/lenz-web/helpers/functions" + "github.com/gofiber/fiber/v2" "golang.org/x/net/websocket" ) @@ -188,18 +189,14 @@ func (e *Engine) AddFuncs(funcs template.FuncMap) { } func (e *Engine) Render(out io.Writer, path string, data any, layout ...string) error { - slog.Debug("Rendering template", "path", path, "layout", layout, "data", data) + slog.Debug("Rendering", "path", path, "layout", layout, "data", data) e.mu.RLock() - ld := data.(map[string]any) + ld := data.(fiber.Map) if e.GlobalData != nil { maps.Copy(ld, e.GlobalData) } e.mu.RUnlock() - if e.debug { - slog.Debug("Rendering template", "path", path, "layout", layout, "data", ld) - } - e.regmu.RLock() defer e.regmu.RUnlock() var l *template.Template diff --git a/views/layouts/components/_menu.gohtml b/views/layouts/components/_menu.gohtml index 72aa1c1..014f8c1 100644 --- a/views/layouts/components/_menu.gohtml +++ b/views/layouts/components/_menu.gohtml @@ -1,10 +1,8 @@ {{ $model := . }} - diff --git a/views/routes/brief/body.gohtml b/views/routes/brief/body.gohtml index 2556b99..265e18f 100644 --- a/views/routes/brief/body.gohtml +++ b/views/routes/brief/body.gohtml @@ -6,3 +6,5 @@ {{ if .next }} Nächster {{ end }} + +{{- Safe (ParseGeneric .text.Content) -}} diff --git a/views/routes/jahrgang/body.gohtml b/views/routes/briefe/body.gohtml similarity index 94% rename from views/routes/jahrgang/body.gohtml rename to views/routes/briefe/body.gohtml index 62a9cd7..c6b0699 100644 --- a/views/routes/jahrgang/body.gohtml +++ b/views/routes/briefe/body.gohtml @@ -5,14 +5,14 @@ {{- range $y := .years -}} {{ $y }} {{- end -}} Alle diff --git a/views/routes/jahrgang/head.gohtml b/views/routes/briefe/head.gohtml similarity index 100% rename from views/routes/jahrgang/head.gohtml rename to views/routes/briefe/head.gohtml