oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "amadan.net/oficina/mapa/internal/mapa/locale"
5 "amadan.net/oficina/mapa/internal/mapa/markup"
6
7 "html/template"
8 "strings"
9 "time"
10
11 "amadan.net/oficina/estilo.git/savestatus"
12)
13
14// templateFuncs is what Mapa's own templates call, beside rastrillo's
15// and the locale's (localeFuncs).
16var templateFuncs = template.FuncMap{
17 "asset": assets.Path,
18 "editor": editorBundle,
19 "saveStatus": saveStatus,
20 // importFailure is a run or page failure code in words
21 // (import_failures.go).
22 "importFailure": importFailure,
23 "hasSuffix": strings.HasSuffix,
24 // grammar is the block grammar a form's indices are counted by
25 // (grammar.go): every form that posts one carries it.
26 "grammar": func() int { return markup.Grammar },
27 // pageIcon is a page's icon in a glyph cell (looks_render.go).
28 "pageIcon": pageIcon,
29 // emojiHref is the icon picker's list (emoji_route.go).
30 "emojiHref": emojiHref,
31}
32
33// relative is a past moment in words, for a <time> whose title carries
34// the absolute date (AGENTS.md: dates relative, absolute in a title).
35// The words are English; past two weeks it is the date, as the reader's
36// locale writes it.
37func relative(dates *locale.Locale, now, when time.Time) string {
38 elapsed := now.Sub(when)
39 switch {
40 case elapsed < time.Minute:
41 return "just now"
42 case elapsed < time.Hour:
43 return plural(dates, int(elapsed.Minutes()), "minute") + " ago"
44 case elapsed < 24*time.Hour:
45 return plural(dates, int(elapsed.Hours()), "hour") + " ago"
46 case elapsed < 14*24*time.Hour:
47 return plural(dates, int(elapsed.Hours()/24), "day") + " ago"
48 }
49 return dates.Format("dateYear", when)
50}
51
52func plural(dates *locale.Locale, count int, unit string) string {
53 if count == 1 {
54 return "1 " + unit
55 }
56 return dates.Number(count) + " " + unit + "s"
57}
58
59// saveStatus is estilo's save status in the lead slot, idle until the
60// editor reports (§3.1).
61func saveStatus() savestatus.Data {
62 return savestatus.Data{State: "idle", Saved: "Saved",
63 Saving: "Saving…", Retrying: "Retrying…", Failed: "Not saved",
64 Label: "Save status"}
65}
66
67// editedWhen is the kicker's time: "at 11:40" today, "yesterday", or
68// "on 12 November", the time and the day as the reader's locale writes
69// them.
70func editedWhen(dates *locale.Locale, when time.Time) string {
71 now := time.Now().In(when.Location())
72 switch {
73 case sameDay(when, now):
74 return "at " + dates.Time(when)
75 case sameDay(when, now.AddDate(0, 0, -1)):
76 return "yesterday"
77 case when.Year() == now.Year():
78 return "on " + dates.Format("date", when)
79 }
80 return "on " + dates.Format("dateYear", when)
81}
82
83func sameDay(one, other time.Time) bool {
84 return one.Year() == other.Year() && one.YearDay() == other.YearDay()
85}
86