- Brand, DefaultBrand and CompileBrandCSS: thirteen colours, each checked as a six-digit hex colour, compiled into CSS for plain elements and the built-in layouts and partials - once the branding row exists its compiled CSS replaces mail.brandCss; mail.css still follows, and the file version sent after a failing override keeps the Branding - layouts get logoUrl, built per send from storage.uploads.public_url or app.url through attach.AbsolutePublicURL; a logo that cannot be shown is left out with a rate-limited warning - Editor previews and validates a Branding draft (rule colour) Plan 35-01, task 5.
129 lines
3.7 KiB
Go
129 lines
3.7 KiB
Go
package postcard
|
|
|
|
import (
|
|
"bytes"
|
|
_ "embed"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
texttemplate "text/template"
|
|
)
|
|
|
|
// Brand is the mail branding: the colours the brand CSS is compiled from
|
|
// and the logo layouts show. A colour is a six-digit hexadecimal colour such
|
|
// as #2da7c7; an empty colour is that field's default.
|
|
type Brand struct {
|
|
BodyBg string
|
|
ContentBg string
|
|
HeaderColor string
|
|
FooterColor string
|
|
HeadingColor string
|
|
TextColor string
|
|
LinkColor string
|
|
ButtonTextColor string
|
|
ButtonPrimaryBg string
|
|
ButtonPositiveBg string
|
|
ButtonNegativeBg string
|
|
PanelBg string
|
|
TableBorderColor string
|
|
// LogoURL is the absolute URL of the logo, given to layouts as
|
|
// {{ .logoUrl }}. It is not part of the CSS.
|
|
LogoURL string
|
|
}
|
|
|
|
// DefaultBrand returns WinterCMS's default mail colours.
|
|
func DefaultBrand() Brand {
|
|
return Brand{
|
|
BodyBg: "#f5f8fa",
|
|
ContentBg: "#ffffff",
|
|
HeaderColor: "#bbbfc3",
|
|
FooterColor: "#aeaeae",
|
|
HeadingColor: "#2f3133",
|
|
TextColor: "#74787e",
|
|
LinkColor: "#2da7c7",
|
|
ButtonTextColor: "#ffffff",
|
|
ButtonPrimaryBg: "#d66829",
|
|
ButtonPositiveBg: "#52a838",
|
|
ButtonNegativeBg: "#e01346",
|
|
PanelBg: "#edeff2",
|
|
TableBorderColor: "#edeff2",
|
|
}
|
|
}
|
|
|
|
// brandColours lists a brand's colours in the order they are checked, each
|
|
// under the name of its column in the branding table.
|
|
func brandColours(b *Brand) []brandColour {
|
|
return []brandColour{
|
|
{"body_bg", &b.BodyBg},
|
|
{"content_bg", &b.ContentBg},
|
|
{"header_color", &b.HeaderColor},
|
|
{"footer_color", &b.FooterColor},
|
|
{"heading_color", &b.HeadingColor},
|
|
{"text_color", &b.TextColor},
|
|
{"link_color", &b.LinkColor},
|
|
{"button_text_color", &b.ButtonTextColor},
|
|
{"button_primary_bg", &b.ButtonPrimaryBg},
|
|
{"button_positive_bg", &b.ButtonPositiveBg},
|
|
{"button_negative_bg", &b.ButtonNegativeBg},
|
|
{"panel_bg", &b.PanelBg},
|
|
{"table_border_color", &b.TableBorderColor},
|
|
}
|
|
}
|
|
|
|
type brandColour struct {
|
|
column string
|
|
value *string
|
|
}
|
|
|
|
// brandColourError is a brand colour that is not a six-digit hexadecimal
|
|
// colour. The value is left out: it may be anything.
|
|
type brandColourError struct {
|
|
column string
|
|
}
|
|
|
|
func (e *brandColourError) Error() string {
|
|
return "postcard: brand colour " + e.column + " must be a six-digit hexadecimal colour such as #2da7c7"
|
|
}
|
|
|
|
var hexColour = regexp.MustCompile(`^#[0-9a-f]{6}$`)
|
|
|
|
//go:embed assets/brand.css.tmpl
|
|
var brandCSSSource string
|
|
|
|
var (
|
|
brandCSSOnce sync.Once
|
|
brandCSSTemplate *texttemplate.Template
|
|
)
|
|
|
|
// CompileBrandCSS returns the brand CSS for b: styles for the page, links,
|
|
// headings, paragraphs and tables and for the classes of the built-in
|
|
// layouts and partials (button, panel, table, subcopy), so that the colours
|
|
// also show in a plugin's layout that carries no class of its own. Every
|
|
// colour must be a six-digit hexadecimal colour; upper-case digits are
|
|
// accepted and written lower-case, and an empty colour is that field's
|
|
// default. The error of an invalid colour names its column, such as
|
|
// button_primary_bg.
|
|
func CompileBrandCSS(b Brand) (string, error) {
|
|
def := DefaultBrand()
|
|
defaults := brandColours(&def)
|
|
for i, c := range brandColours(&b) {
|
|
colour := strings.ToLower(*c.value)
|
|
if colour == "" {
|
|
colour = *defaults[i].value
|
|
}
|
|
// Nothing but a checked colour reaches the style block.
|
|
if !hexColour.MatchString(colour) {
|
|
return "", &brandColourError{column: c.column}
|
|
}
|
|
*c.value = colour
|
|
}
|
|
brandCSSOnce.Do(func() {
|
|
brandCSSTemplate = texttemplate.Must(texttemplate.New("brand.css").Parse(brandCSSSource))
|
|
})
|
|
var buf bytes.Buffer
|
|
if err := brandCSSTemplate.Execute(&buf, b); err != nil {
|
|
return "", err
|
|
}
|
|
return buf.String(), nil
|
|
}
|