mirror of
https://github.com/Theodor-Springmann-Stiftung/lenz-web.git
synced 2025-10-29 17:25:32 +00:00
Init
This commit is contained in:
113
helpers/functions/date.go
Normal file
113
helpers/functions/date.go
Normal 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]
|
||||
}
|
||||
93
helpers/functions/embedding.go
Normal file
93
helpers/functions/embedding.go
Normal 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)
|
||||
}
|
||||
}
|
||||
18
helpers/functions/iterables.go
Normal file
18
helpers/functions/iterables.go
Normal 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
|
||||
}
|
||||
1
helpers/functions/slices.go
Normal file
1
helpers/functions/slices.go
Normal file
@@ -0,0 +1 @@
|
||||
package functions
|
||||
12
helpers/functions/sorting.go
Normal file
12
helpers/functions/sorting.go
Normal 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
|
||||
}
|
||||
17
helpers/functions/string.go
Normal file
17
helpers/functions/string.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user