dorfylegends/backend/server/templates.go

114 lines
6.1 KiB
Go
Raw Permalink Normal View History

2022-04-26 10:24:16 +03:00
package server
import (
"fmt"
"html/template"
2022-04-26 18:39:24 +03:00
"net/url"
2022-04-26 10:24:16 +03:00
2022-04-27 22:44:39 +03:00
humanize "github.com/dustin/go-humanize"
2022-04-28 22:24:55 +03:00
"github.com/iancoleman/strcase"
2022-04-26 10:24:16 +03:00
"github.com/robertjanetzko/LegendsBrowser2/backend/model"
"github.com/robertjanetzko/LegendsBrowser2/backend/templates"
"github.com/robertjanetzko/LegendsBrowser2/backend/util"
)
2022-05-07 23:24:58 +03:00
var DebugJSON = false
2022-04-26 10:24:16 +03:00
func (srv *DfServer) LoadTemplates() {
functions := template.FuncMap{
2022-05-07 23:24:58 +03:00
"json": func(obj any) template.HTML {
if !DebugJSON {
return ""
} else {
return util.Json(obj)
}
},
2022-04-26 10:24:16 +03:00
"check": func(condition bool, v any) any {
if condition {
return v
}
return nil
},
2022-05-02 18:22:57 +03:00
"title": util.Title,
"kebab": func(s string) string { return strcase.ToKebab(s) },
2022-05-06 14:11:12 +03:00
"andList": model.AndList,
2022-05-09 18:20:31 +03:00
"suburi": func() string { return srv.context.config.SubUri },
2022-05-02 18:22:57 +03:00
"world": func() *model.DfWorld { return srv.context.world },
"context": func(r any) *model.Context { return model.NewContext(srv.context.world, r) },
2022-05-01 13:29:39 +03:00
"initMap": func() template.HTML {
2022-05-09 18:20:31 +03:00
return template.HTML(fmt.Sprintf(`<script>var worldWidth = %d, worldHeight = %d;</script><script src="./js/map.js"></script>`,
2022-05-01 13:29:39 +03:00
srv.context.world.Width, srv.context.world.Height))
},
2022-04-28 22:24:55 +03:00
"hf": func(id int) template.HTML { return model.LinkHf(srv.context.world, id) },
2022-05-05 23:18:31 +03:00
"hfShort": func(id int) template.HTML { return model.LinkHfShort(srv.context.world, id) },
2022-04-28 22:24:55 +03:00
"getHf": func(id int) *model.HistoricalFigure { return srv.context.world.HistoricalFigures[id] },
2022-05-05 17:55:57 +03:00
"hfList": func(ids []int) template.HTML { return model.LinkHfList(srv.context.world, ids) },
2022-05-07 22:17:05 +03:00
"identity": func(id int) template.HTML { return model.LinkIdentity(srv.context.world, id) },
2022-04-28 22:24:55 +03:00
"entity": func(id int) template.HTML { return model.LinkEntity(srv.context.world, id) },
"getEntity": func(id int) *model.Entity { return srv.context.world.Entities[id] },
"site": func(id int) template.HTML { return model.LinkSite(srv.context.world, id) },
"getSite": func(id int) *model.Site { return srv.context.world.Sites[id] },
2022-04-29 15:21:27 +03:00
"structure": func(siteId, id int) template.HTML { return model.LinkStructure(srv.context.world, siteId, id) },
2022-04-28 22:24:55 +03:00
"region": func(id int) template.HTML { return model.LinkRegion(srv.context.world, id) },
"getRegion": func(id int) *model.Region { return srv.context.world.Regions[id] },
2022-05-05 13:55:33 +03:00
"worldConstruction": func(id int) template.HTML { return model.LinkWorldConstruction(srv.context.world, id) },
"getWorldConstruction": func(id int) *model.WorldConstruction { return srv.context.world.WorldConstructions[id] },
2022-04-28 22:24:55 +03:00
"artifact": func(id int) template.HTML { return model.LinkArtifact(srv.context.world, id) },
"getArtifact": func(id int) *model.Artifact { return srv.context.world.Artifacts[id] },
"danceForm": func(id int) template.HTML { return model.LinkDanceForm(srv.context.world, id) },
"musicalForm": func(id int) template.HTML { return model.LinkMusicalForm(srv.context.world, id) },
"poeticForm": func(id int) template.HTML { return model.LinkPoeticForm(srv.context.world, id) },
2022-04-30 12:07:55 +03:00
"writtenContent": func(id int) template.HTML { return model.LinkWrittenContent(srv.context.world, id) },
2022-05-04 12:42:02 +03:00
"landmass": func(id int) template.HTML { return model.LinkLandmass(srv.context.world, id) },
"mountain": func(id int) template.HTML { return model.LinkMountain(srv.context.world, id) },
"river": func(id int) template.HTML { return model.LinkRiver(srv.context.world, id) },
2022-05-07 18:08:42 +03:00
"addLandmass": func(id int) template.HTML { return model.AddMapLandmass(srv.context.world, id) },
"addRegion": func(id int) template.HTML { return model.AddMapRegion(srv.context.world, id) },
2022-05-08 10:35:57 +03:00
"addSite": func(id int, color bool) template.HTML { return model.AddMapSite(srv.context.world, id, color) },
"addMountain": func(id int, color bool) template.HTML { return model.AddMapMountain(srv.context.world, id, color) },
2022-05-07 18:08:42 +03:00
"addWorldConstruction": func(id int) template.HTML { return model.AddMapWorldConstruction(srv.context.world, id) },
"addRiver": func(id int) template.HTML { return model.AddMapRiver(srv.context.world, id) },
2022-05-11 10:45:17 +03:00
"addCollection": func(id int) template.HTML { return model.AddMapCollection(srv.context.world, id) },
2022-05-07 18:08:42 +03:00
2022-04-26 18:39:24 +03:00
"events": func(obj any) *model.EventList {
return model.NewEventList(srv.context.world, obj)
},
2022-04-29 16:28:08 +03:00
"history": func(siteId int) []*model.HistoricalEvent {
return srv.context.world.SiteHistory(siteId)
},
2022-05-03 15:59:47 +03:00
"collection": func(id int) template.HTML { return model.LinkCollection(srv.context.world, id) },
"getCollection": func(id int) *model.HistoricalEventCollection { return srv.context.world.HistoricalEventCollections[id] },
"getOccasion": func(civId, occasionId int) *model.Occasion {
if civ, ok := srv.context.world.Entities[civId]; ok {
return civ.Occasion[occasionId]
}
return nil
},
"story": func(id int) template.HTML {
if e, ok := srv.context.world.HistoricalEvents[id]; ok {
return template.HTML(e.Details.Html(&model.Context{World: srv.context.world, Story: true}) + " in " + model.Time(e.Year, e.Seconds72))
}
return template.HTML("")
},
2022-05-03 22:39:00 +03:00
"description": func(d string) template.HTML { return model.LinkDescription(srv.context.world, d) },
2022-04-26 18:39:24 +03:00
"season": model.Season,
"time": model.Time,
"url": url.PathEscape,
"query": url.QueryEscape,
"isLegendsXml": isLegendsXml,
2022-04-26 10:24:16 +03:00
"html": func(value any) template.HTML {
return template.HTML(fmt.Sprint(value))
},
2022-05-07 23:19:30 +03:00
"bytes": func(s int64) string { return humanize.Bytes(uint64(s)) },
"first": util.FirstInMap,
"ifFirst": func(m any, k string, r string) string { return util.If(util.FirstInMap(m, k), r, "") },
"strip": util.Strip,
"string": util.String,
"capitalize": util.Capitalize,
"add": func(a, b int) int { return a + b },
"breakYearColumn": func(c, m int) bool { return (c % ((m + 2) / 4)) == 0 },
2022-04-26 10:24:16 +03:00
}
srv.templates = templates.New(functions)
}