-
Notifications
You must be signed in to change notification settings - Fork 137
/
assets.go
81 lines (73 loc) · 2.63 KB
/
assets.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
72
73
74
75
76
77
78
79
80
81
package middlewares
import (
"bytes"
"fmt"
"html/template"
"github.com/cozy/cozy-stack/model/instance"
build "github.com/cozy/cozy-stack/pkg/config"
"github.com/labstack/echo/v4"
)
// FuncsMap is a the helper functions used in templates.
// It is filled in web/statik but declared here to avoid circular imports.
var FuncsMap template.FuncMap
var fontsTemplate *template.Template
var themeTemplate *template.Template
var faviconTemplate *template.Template
// BuildTemplates ensure that the cozy-ui can be injected in templates
func BuildTemplates() {
fontsTemplate = template.Must(template.New("fonts").Funcs(FuncsMap).Parse(`` +
`<link rel="stylesheet" type="text/css" href="{{asset .Domain "/fonts/fonts.css" .ContextName}}">`,
))
themeTemplate = template.Must(template.New("theme").Funcs(FuncsMap).Parse(`` +
`<link rel="stylesheet" type="text/css" href="{{asset .Domain "/styles/theme.css" .ContextName}}">`,
))
faviconTemplate = template.Must(template.New("favicon").Funcs(FuncsMap).Parse(`
<link rel="icon" href="{{asset .Domain "/favicon.ico" .ContextName}}">
{{if .Dev}}
<link rel="icon" href="{{asset .Domain "/images/cozy-dev.svg"}}" type="image/svg+xml" sizes="any">
{{else}}
<link rel="icon" href="{{asset .Domain "/icon.svg"}}" type="image/svg+xml" sizes="any">
{{end}}
<link rel="apple-touch-icon" sizes="180x180" href="{{asset .Domain "/apple-touch-icon.png" .ContextName}}"/>
<link rel="manifest" href="{{asset .Domain "/manifest.webmanifest"}}">
`))
}
// CozyFonts returns an HTML template for inserting the HTML tag for the loading
// the CSS file for web fonts (lato and lato-bold).
func CozyFonts(i *instance.Instance) template.HTML {
buf := new(bytes.Buffer)
err := fontsTemplate.Execute(buf, echo.Map{
"Domain": i.ContextualDomain(),
"ContextName": i.ContextName,
})
if err != nil {
panic(err)
}
return template.HTML(buf.String())
}
// ThemeCSS returns an HTML template for inserting the HTML tag for the custom
// CSS theme
func ThemeCSS(i *instance.Instance) template.HTML {
buf := new(bytes.Buffer)
err := themeTemplate.Execute(buf, echo.Map{
"Domain": i.ContextualDomain(),
"ContextName": i.ContextName,
})
if err != nil {
panic(err)
}
return template.HTML(buf.String())
}
// Favicon returns a helper to insert the favicons in an HTML template.
func Favicon(i *instance.Instance) template.HTML {
buf := new(bytes.Buffer)
err := faviconTemplate.Execute(buf, echo.Map{
"Domain": i.ContextualDomain(),
"ContextName": i.ContextName,
"Dev": build.IsDevRelease(),
})
if err != nil {
panic(fmt.Sprintf("failed to generate the favicon: %s", err))
}
return template.HTML(buf.String())
}