mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2025-10-29 09:15:33 +00:00
Statische Seiten
This commit is contained in:
67
helpers/functions/datetime.go
Normal file
67
helpers/functions/datetime.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package functions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Weekday struct {
|
||||
Name string
|
||||
ShortName string
|
||||
Number int
|
||||
}
|
||||
|
||||
type Month struct {
|
||||
Name string
|
||||
ShortName string
|
||||
Number int
|
||||
}
|
||||
|
||||
var Months = []Month{
|
||||
{"Januar", "Jan", 1},
|
||||
{"Februar", "Feb", 2},
|
||||
{"März", "Mär", 3},
|
||||
{"April", "Apr", 4},
|
||||
{"Mai", "Mai", 5},
|
||||
{"Juni", "Jun", 6},
|
||||
{"Juli", "Jul", 7},
|
||||
{"August", "Aug", 8},
|
||||
{"September", "Sep", 9},
|
||||
{"Oktober", "Okt", 10},
|
||||
{"November", "Nov", 11},
|
||||
{"Dezember", "Dez", 12},
|
||||
{"N/A", "N/A", 0},
|
||||
}
|
||||
|
||||
var Weekdays = []Weekday{
|
||||
{"Sonntag", "So", 0},
|
||||
{"Montag", "Mo", 1},
|
||||
{"Dienstag", "Di", 2},
|
||||
{"Mittwoch", "Mi", 3},
|
||||
{"Donnerstag", "Do", 4},
|
||||
{"Freitag", "Fr", 5},
|
||||
{"Samstag", "Sa", 6},
|
||||
{"N/A", "N/A", 7},
|
||||
}
|
||||
|
||||
func Today() time.Time {
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
func GetMonth(month any) Month {
|
||||
if val, ok := month.(int); ok {
|
||||
val -= 1
|
||||
if val < 0 || val > 11 {
|
||||
val = 12
|
||||
}
|
||||
return Months[val]
|
||||
}
|
||||
|
||||
if val, ok := month.(time.Time); ok {
|
||||
m := val.Month() - 1
|
||||
return Months[m]
|
||||
}
|
||||
|
||||
fmt.Println("Invalid month value", month)
|
||||
return Months[12]
|
||||
}
|
||||
11
helpers/functions/int.go
Normal file
11
helpers/functions/int.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package functions
|
||||
|
||||
func Add(a, b any) int {
|
||||
val1, ok1 := a.(int)
|
||||
val2, ok2 := b.(int)
|
||||
if !ok1 || !ok2 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return val1 + val2
|
||||
}
|
||||
@@ -6,8 +6,8 @@ func Arr(els ...any) []any {
|
||||
return els
|
||||
}
|
||||
|
||||
// Must have even number of args: key, value, key, value, ...
|
||||
func Dict(values ...interface{}) (map[string]interface{}, error) {
|
||||
// Must have even number of args: key, value, key, value, ...
|
||||
if len(values)%2 != 0 {
|
||||
return nil, fmt.Errorf("invalid dict call: must have even number of args")
|
||||
}
|
||||
|
||||
48
helpers/functions/toc.go
Normal file
48
helpers/functions/toc.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package functions
|
||||
|
||||
import (
|
||||
"golang.org/x/net/html"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type TOCEntry struct {
|
||||
Level int
|
||||
Title string
|
||||
}
|
||||
|
||||
func getText(n *html.Node) string {
|
||||
if n.Type == html.TextNode {
|
||||
return n.Data
|
||||
}
|
||||
var sb strings.Builder
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
sb.WriteString(getText(c))
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func TOCFromHTML(htmlStr string) ([]TOCEntry, error) {
|
||||
doc, err := html.Parse(strings.NewReader(htmlStr))
|
||||
toc := []TOCEntry{}
|
||||
if err != nil {
|
||||
return toc, err
|
||||
}
|
||||
var f func(*html.Node)
|
||||
f = func(n *html.Node) {
|
||||
if n.Type == html.ElementNode {
|
||||
if len(n.Data) == 2 && n.Data[0] == 'h' && n.Data[1] >= '1' && n.Data[1] <= '6' {
|
||||
level, err := strconv.Atoi(n.Data[1:])
|
||||
if err == nil {
|
||||
title := strings.TrimSpace(getText(n))
|
||||
toc = append(toc, TOCEntry{Level: level, Title: title})
|
||||
}
|
||||
}
|
||||
}
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
f(c)
|
||||
}
|
||||
}
|
||||
f(doc)
|
||||
return toc, nil
|
||||
}
|
||||
Reference in New Issue
Block a user