This commit is contained in:
Simon Martens
2025-03-05 16:41:39 +01:00
commit e19fd47c17
88 changed files with 9765 additions and 0 deletions

113
helpers/functions/date.go Normal file
View File

@@ -0,0 +1,113 @@
package functions
import (
"strconv"
"github.com/Theodor-Springmann-Stiftung/lenz-web/helpers/xsdtime"
)
type Month struct {
Full string
Short string
Number string
No int
}
type Weekday struct {
Full string
Short string
No int
}
func (m Month) String() string {
return m.Full
}
func (w Weekday) String() string {
return w.Full
}
var TRANSLM = []Month{
{"NotAvailable", "NA", "0", 0},
{"Januar", "Jan", "1", 1},
{"Februar", "Feb", "2", 2},
{"März", "Mär", "3", 3},
{"April", "Apr", "4", 4},
{"Mai", "Mai", "5", 5},
{"Juni", "Jun", "6", 6},
{"Juli", "Jul", "7", 7},
{"August", "Aug", "8", 8},
{"September", "Sep", "9", 9},
{"Oktober", "Okt", "10", 10},
{"November", "Nov", "11", 11},
{"Dezember", "Dez", "12", 12},
}
var TRANSLD = []Weekday{
{"NotAvailable", "NA", 0},
{"Montag", "Mo", 1},
{"Dienstag", "Di", 2},
{"Mittwoch", "Mi", 3},
{"Donnerstag", "Do", 4},
{"Freitag", "Fr", 5},
{"Samstag", "Sa", 6},
{"Sonntag", "So", 7},
}
func HRDateShort(date string) string {
xsdt, err := xsdtime.New(date)
if err != nil {
return ""
}
t := xsdt.Type()
if t == xsdtime.GYear {
return strconv.Itoa(xsdt.Year)
}
if t == xsdtime.GYearMonth {
return strconv.Itoa(xsdt.Month) + "." + strconv.Itoa(xsdt.Year)
}
if t == xsdtime.Date {
return strconv.Itoa(xsdt.Day) + "." + strconv.Itoa(xsdt.Month) + "." + strconv.Itoa(xsdt.Year)
}
return ""
}
func HRDateYear(date string) string {
xsdt, err := xsdtime.New(date)
if err != nil {
return ""
}
t := xsdt.Type()
if t == xsdtime.GYear {
return strconv.Itoa(xsdt.Year)
}
if t == xsdtime.GYearMonth {
return strconv.Itoa(xsdt.Year)
}
if t == xsdtime.Date {
return strconv.Itoa(xsdt.Year)
}
return ""
}
func MonthName(i int) Month {
if i > 12 || i < 1 {
return TRANSLM[0]
}
return TRANSLM[i]
}
func WeekdayName(i int) Weekday {
if i > 7 || i < 1 {
return TRANSLD[0]
}
return TRANSLD[i]
}

View File

@@ -0,0 +1,93 @@
package functions
import (
"html/template"
"io"
"io/fs"
"path/filepath"
"strings"
"sync"
)
type Embedder struct {
embed_cache sync.Map
fs fs.FS
}
func NewEmbedder(fs fs.FS) *Embedder {
return &Embedder{
fs: fs,
embed_cache: sync.Map{},
}
}
// INFO: We initialize the cache in both functions, which is only valid if both of these get
// called in the same context, eg. when creating a template engine.
func (e *Embedder) EmbedSafe() func(string) template.HTML {
return func(path string) template.HTML {
path = strings.TrimSpace(path)
path = filepath.Clean(path)
val, err := e.getFileData(path)
if err != nil {
return template.HTML("")
}
return template.HTML(val)
}
}
func (e *Embedder) Embed() func(string) string {
return func(path string) string {
path = strings.TrimSpace(path)
path = filepath.Clean(path)
val, err := e.getFileData(path)
if err != nil {
return ""
}
return string(val)
}
}
func (e *Embedder) getFileData(path string) ([]byte, error) {
if val, ok := e.embed_cache.Load(path); ok {
return val.([]byte), nil
}
f, err := e.fs.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
data, err := io.ReadAll(f)
if err != nil {
return nil, err
}
e.embed_cache.Store(path, data)
return data, nil
}
func (e *Embedder) EmbedXSLT() func(string) template.HTML {
return func(path string) template.HTML {
path = strings.TrimSpace(path)
path = filepath.Clean(path)
fn := filepath.Base(path)
ext := filepath.Ext(fn)
fn = fn[:len(fn)-len(ext)]
if (ext != ".xsl" && ext != ".xslt") || ext == "" || fn == "" {
return template.HTML("[ERROR: " + "file is not an XSLT file" + "]")
}
val, err := e.getFileData(path)
if err != nil {
return template.HTML("[ERROR: " + err.Error() + "]")
}
src := "<script id=\"" + fn + "\" type=\"application/xml\">\n" + string(val) + "\n</script>"
return template.HTML(src)
}
}

View File

@@ -0,0 +1,18 @@
package functions
func MapArrayInsert[K comparable, V any](m map[K][]V, k K, v V) {
l, ok := m[k]
if !ok {
m[k] = []V{v}
} else {
m[k] = append(l, v)
}
}
func Keys[K comparable, V any](m map[K]V) []K {
keys := make([]K, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}

View File

@@ -0,0 +1 @@
package functions

View File

@@ -0,0 +1,12 @@
package functions
import (
"golang.org/x/text/collate"
"golang.org/x/text/language"
)
func Sort(s []string) []string {
c := collate.New(language.German, collate.IgnoreCase)
c.SortStrings(s)
return s
}

View File

@@ -0,0 +1,17 @@
package functions
import "html/template"
func FirstLetter(s string) string {
if len(s) == 0 {
return ""
}
return string(s[:1])
}
func Safe(s string) template.HTML {
if len(s) == 0 {
return ""
}
return template.HTML(s)
}