-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.go
71 lines (68 loc) · 1.76 KB
/
render.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package oni
import (
"html/template"
"strings"
vocab "github.com/go-ap/activitypub"
json "github.com/go-ap/jsonld"
"github.com/mariusor/render"
)
var (
ren = render.New(render.Options{
Directory: "templates",
Layout: "main",
Extensions: []string{".html"},
FileSystem: TemplateFS,
IsDevelopment: false,
DisableHTTPErrorRendering: true,
Funcs: []template.FuncMap{{
"HTMLAttr": func(n vocab.NaturalLanguageValues) template.HTMLAttr {
return template.HTMLAttr(n.First().Value)
},
"HTML": func(n vocab.NaturalLanguageValues) template.HTML {
return template.HTML(n.First().Value)
},
"oniType": func(i any) template.HTML {
switch it := i.(type) {
case vocab.Item:
t := it.GetType()
switch {
case vocab.ActorTypes.Contains(t):
return "actor"
case vocab.ActivityTypes.Contains(t), vocab.IntransitiveActivityTypes.Contains(t):
return "activity"
case vocab.CollectionTypes.Contains(t):
return "collection"
case t == "":
return "tag"
default:
return template.HTML(strings.ToLower(string(t)))
}
case vocab.IRI:
return "iri"
case vocab.NaturalLanguageValues:
return "natural-language-values"
case vocab.LangRefValue:
return "natural-language-value"
case vocab.LangRef:
return "value"
case error:
default:
return "error"
}
return "error"
},
"JSON": func(it any) template.HTMLAttr {
var res []byte
switch o := it.(type) {
case vocab.Item:
res, _ = vocab.MarshalJSON(o)
case vocab.NaturalLanguageValues:
res, _ = o.MarshalJSON()
default:
res, _ = json.Marshal(o)
}
return template.HTMLAttr(res)
},
}},
})
)