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
3// History's words for a styled revision (columns and icons spec §3.4):
4// what changed in the look, against the revision before it.
5
6// styled summarises rows[index]: "Changed the icon", "Added a banner",
7// or two joined. With the revision before aged out, there is nothing to
8// compare.
9func (history *pageHistory) styled(index int) (string, error) {
10 if index+1 >= len(history.rows) {
11 return "Changed how the page looks", nil
12 }
13 after, err := history.record(index)
14 if err != nil {
15 return "", err
16 }
17 before, err := history.record(index + 1)
18 if err != nil {
19 return "", err
20 }
21 return lookSummary(before, after), nil
22}
23
24// lookSummary is what changed between two looks, in words.
25func lookSummary(before, after Record) string {
26 icon := lookWord(before.Icon != nil, after.Icon != nil,
27 samePointer(before.Icon, after.Icon), "icon")
28 banner := lookWord(before.Banner != nil, after.Banner != nil,
29 sameBannerImage(before.Banner, after.Banner), "banner")
30 if !samePointer(before.Banner, after.Banner) && banner == "" {
31 banner = "Moved the banner"
32 }
33 switch {
34 case icon != "" && banner != "":
35 return joinLook(icon, banner)
36 case icon != "":
37 return icon
38 case banner != "":
39 return banner
40 }
41 return "Changed how the page looks"
42}
43
44// lookWord is one part's change: added, removed or changed, or "" when
45// the part is the same.
46func lookWord(had, has, same bool, thing string) string {
47 switch {
48 case !had && has:
49 return "Added " + article(thing) + thing
50 case had && !has:
51 return "Removed the " + thing
52 case had && !same:
53 return "Changed the " + thing
54 }
55 return ""
56}
57
58func article(thing string) string {
59 if thing == "icon" {
60 return "an "
61 }
62 return "a "
63}
64
65func sameBannerImage(one, other *Banner) bool {
66 if one == nil || other == nil {
67 return one == other
68 }
69 return one.Image == other.Image
70}
71
72// joinLook joins two parts: "Changed the icon and the banner" when the
73// verbs agree, otherwise both parts ("Added an icon and added a
74// banner").
75func joinLook(icon, banner string) string {
76 for _, verb := range []string{"Changed the ", "Removed the "} {
77 if len(icon) > len(verb) && icon[:len(verb)] == verb &&
78 len(banner) > len(verb) && banner[:len(verb)] == verb {
79 return icon + " and the " + banner[len(verb):]
80 }
81 }
82 return icon + " and " + lowerFirst(banner)
83}
84
85func lowerFirst(text string) string {
86 if text == "" {
87 return text
88 }
89 return string(text[0]+'a'-'A') + text[1:]
90}
91