| 1 | package gallery |
| 2 | |
| 3 | import ( |
| 4 | "html/template" |
| 5 | |
| 6 | "amadan.net/rastrillo/fiesta/internal/tokens" |
| 7 | ) |
| 8 | |
| 9 | // The Tokens page: the theme's palette, then the structure every |
| 10 | // theme shares, each group under a heading the rail links. |
| 11 | const tokensBody = `{{define "ds-body-tokens"}}<p class="ds-lead">{{say "gallery.tokens.lead" "theme" .Theme}}</p> |
| 12 | <p class="ds-note">{{say "gallery.tokens.note"}}</p> |
| 13 | {{range .Groups}}<h2 id="{{.ID}}" data-ds-anchor>{{.Title}}</h2> |
| 14 | <ul class="ds-toks">{{$kind := .Kind}}{{range .Rows}}<li class="ds-tok">{{if eq $kind "type"}}<span class="ds-type" style="{{.Preview}}">Ag</span>{{else if eq $kind "space"}}<span class="ds-bar" style="{{.Preview}}"></span>{{else if eq $kind "radius"}}<span class="ds-chip ds-chip--fill" style="{{.Preview}}"></span>{{else if .Preview}}<span class="ds-chip" style="{{.Preview}}"></span>{{end}}<span class="ds-tok__text rst-mono"><span class="ds-tok__name">{{.Name}}</span><span class="ds-tok__value">{{.Value}}</span></span></li>{{end}}</ul> |
| 15 | {{end}}{{end}}` |
| 16 | |
| 17 | // tokenGroupView is one group with its anchor id and its title in the |
| 18 | // page's language. |
| 19 | type tokenGroupView struct { |
| 20 | tokens.Group |
| 21 | ID, Title string |
| 22 | } |
| 23 | |
| 24 | // tokenGroups is one theme's groups: its palette, then the structure. |
| 25 | // Read once per theme at setup, so a theme whose stylesheet does not |
| 26 | // parse fails the render before a page is written. |
| 27 | func (s *site) tokenGroups(theme string) []tokens.Group { return s.tokens[theme] } |
| 28 | |
| 29 | // readTokens parses every theme's palette and the shared structure. |
| 30 | func (s *site) readTokens() error { |
| 31 | structure, err := tokens.Structural() |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | s.tokens = map[string][]tokens.Group{} |
| 36 | for _, theme := range s.themes() { |
| 37 | palette, err := tokens.Palette(theme) |
| 38 | if err != nil { |
| 39 | return err |
| 40 | } |
| 41 | s.tokens[theme] = append(palette, structure...) |
| 42 | } |
| 43 | return nil |
| 44 | } |
| 45 | |
| 46 | // tokensBody renders the Tokens page's body for p. |
| 47 | func (s *site) tokensBody(p page) (template.HTML, error) { |
| 48 | var groups []tokenGroupView |
| 49 | for _, g := range s.tokenGroups(p.Theme) { |
| 50 | groups = append(groups, tokenGroupView{Group: g, ID: anchorID("tokens", g.Key), Title: s.say(p.Locale, "gallery.tokens.group."+g.Key)}) |
| 51 | } |
| 52 | return s.body(p.Locale, "ds-body-tokens", struct { |
| 53 | page |
| 54 | Groups []tokenGroupView |
| 55 | }{p, groups}) |
| 56 | } |
| 57 | |
| 58 | func init() { registerTemplate(tokensBody) } |
| 59 | |