| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "html" |
| 6 | "html/template" |
| 7 | "net/http" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "time" |
| 11 | "unicode" |
| 12 | "unicode/utf8" |
| 13 | |
| 14 | "amadan.net/oficina/mapa/internal/mapa/locale" |
| 15 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 16 | ) |
| 17 | |
| 18 | // The History panel and a chosen version, as drawn (level-up §4.5). The |
| 19 | // panel is templates/history.html's mapa-history, beside the page it is |
| 20 | // the history of; the version view is templates/revision.html. |
| 21 | |
| 22 | // historyPanel is the panel's data. |
| 23 | type historyPanel struct { |
| 24 | // Close is the page itself: the panel's close and Back to current. |
| 25 | Close string |
| 26 | // State is "list" with no version chosen, "version" with one. |
| 27 | State string |
| 28 | Days []historyDay |
| 29 | Older string |
| 30 | AgedOut bool |
| 31 | // Restore is the chosen version's restore form, when it is not the |
| 32 | // current one. |
| 33 | Restore string |
| 34 | // Images is the Markdown screen's "Images on this page", where an |
| 35 | // image taken out of the text can still be removed (images §3.3). |
| 36 | Images string |
| 37 | } |
| 38 | |
| 39 | // historyDay is the versions of one day in the viewer's zone. |
| 40 | type historyDay struct { |
| 41 | Label string |
| 42 | Versions []versionRow |
| 43 | } |
| 44 | |
| 45 | // versionRow is one version as a row of the panel. |
| 46 | type versionRow struct { |
| 47 | Version |
| 48 | Href, Time string |
| 49 | Chosen bool |
| 50 | } |
| 51 | |
| 52 | // panelFor draws a screen of history for page, chosen marking the |
| 53 | // version on show (0 for none). now places Today and Yesterday; dates |
| 54 | // writes the days and times as the reader's locale does. |
| 55 | func panelFor(dates *locale.Locale, page PageView, list VersionList, |
| 56 | chosen int64, now time.Time) historyPanel { |
| 57 | base := "/p/" + page.Public + "/history" |
| 58 | panel := historyPanel{Close: page.Href, State: "list", |
| 59 | AgedOut: list.AgedOut, Images: page.Href + "/edit#page-images-title"} |
| 60 | if list.More { |
| 61 | panel.Older = base + "?older=" + |
| 62 | strconv.Itoa(list.Skip+len(list.Versions)) |
| 63 | } |
| 64 | if chosen != 0 { |
| 65 | panel.State = "version" |
| 66 | if chosen != page.Revision { |
| 67 | panel.Restore = base + "/" + strconv.FormatInt(chosen, 10) |
| 68 | } |
| 69 | } |
| 70 | for _, version := range list.Versions { |
| 71 | label := dayLabel(dates, version.At, now) |
| 72 | if len(panel.Days) == 0 || panel.Days[len(panel.Days)-1].Label != |
| 73 | label { |
| 74 | panel.Days = append(panel.Days, historyDay{Label: label}) |
| 75 | } |
| 76 | day := &panel.Days[len(panel.Days)-1] |
| 77 | day.Versions = append(day.Versions, versionRow{Version: version, |
| 78 | Href: base + "/" + strconv.FormatInt(version.Revision, 10), |
| 79 | Time: dates.Time(version.At), |
| 80 | Chosen: version.Revision == chosen}) |
| 81 | } |
| 82 | return panel |
| 83 | } |
| 84 | |
| 85 | // dayLabel is a day heading in the panel: Today, Yesterday, or the date. |
| 86 | func dayLabel(dates *locale.Locale, at, now time.Time) string { |
| 87 | now = now.In(at.Location()) |
| 88 | switch { |
| 89 | case sameDay(at, now): |
| 90 | return "Today" |
| 91 | case sameDay(at, now.AddDate(0, 0, -1)): |
| 92 | return "Yesterday" |
| 93 | case at.Year() == now.Year(): |
| 94 | return dates.Format("day", at) |
| 95 | } |
| 96 | return dates.Format("dayYear", at) |
| 97 | } |
| 98 | |
| 99 | // initial is the letter on a version's avatar. |
| 100 | func initial(who string) string { |
| 101 | first, _ := utf8.DecodeRuneInString(who) |
| 102 | if first == utf8.RuneError { |
| 103 | return "?" |
| 104 | } |
| 105 | return string(unicode.ToUpper(first)) |
| 106 | } |
| 107 | |
| 108 | // historyKey carries the panel from the history route to show, which |
| 109 | // draws it beside the page the route rendered. |
| 110 | type historyKey struct{} |
| 111 | |
| 112 | func withHistory(request *http.Request, panel *historyPanel) *http.Request { |
| 113 | return request.WithContext(context.WithValue(request.Context(), |
| 114 | historyKey{}, panel)) |
| 115 | } |
| 116 | |
| 117 | func historyIn(request *http.Request) *historyPanel { |
| 118 | panel, _ := request.Context().Value(historyKey{}).(*historyPanel) |
| 119 | return panel |
| 120 | } |
| 121 | |
| 122 | // wantsFragment is the panel as a fragment, under Docs' rule: only a |
| 123 | // fetch (Sec-Fetch-Dest: empty) asking ?panel=1 gets it. A document |
| 124 | // request, or one with no Sec-Fetch-* at all, gets the full page. |
| 125 | func wantsFragment(request *http.Request) bool { |
| 126 | return request.URL.Query().Get("panel") == "1" && |
| 127 | request.Header.Get("Sec-Fetch-Dest") == "empty" |
| 128 | } |
| 129 | |
| 130 | // versionView is templates/revision.html's data. |
| 131 | type versionView struct { |
| 132 | Page PageView |
| 133 | // Banner is "Bo's version, Friday 13 November at 16:15." and Note |
| 134 | // what the marks mean, or why there are none. |
| 135 | Banner, Note string |
| 136 | Title template.HTML |
| 137 | HTML template.HTML |
| 138 | // Look and Band are the version's own icon and banner (columns and |
| 139 | // icons spec §3.4), read from its record. |
| 140 | Look *Icon |
| 141 | Band *BannerView |
| 142 | } |
| 143 | |
| 144 | // versionOf draws a chosen version: the banner, its title, and its text |
| 145 | // with what changed from the version before marked. draw is the page's |
| 146 | // own renderer, in the viewer's scope. |
| 147 | func versionOf(dates *locale.Locale, page PageView, chosen Chosen, |
| 148 | draw drawRun) (versionView, error) { |
| 149 | at := chosen.Version.At |
| 150 | view := versionView{Page: page, Banner: possessive(chosen.Version.Who) + |
| 151 | " version, " + dates.Stamp(at) + ".", |
| 152 | Title: versionTitle(page, chosen), Look: versionLook(page, chosen)} |
| 153 | var err error |
| 154 | switch { |
| 155 | case chosen.Before != nil: |
| 156 | view.Note = "Marked: what changed from the version before." |
| 157 | view.HTML, err = diffHTML(markup.Parse(chosen.Before.Body), |
| 158 | markup.Parse(chosen.Record.Body), draw) |
| 159 | case chosen.Verb == VerbCreated || chosen.Verb == VerbRestored: |
| 160 | view.Note = "The first version." |
| 161 | view.HTML, err = drawPage(chosen.Record.Body, draw) |
| 162 | default: |
| 163 | view.Note = "The version before it has aged out, so nothing " + |
| 164 | "is marked." |
| 165 | view.HTML, err = drawPage(chosen.Record.Body, draw) |
| 166 | } |
| 167 | return view, err |
| 168 | } |
| 169 | |
| 170 | func possessive(who string) string { |
| 171 | if who == "You" { |
| 172 | return "Your" |
| 173 | } |
| 174 | return who + "'s" |
| 175 | } |
| 176 | |
| 177 | // versionTitle is the version's title, the old one struck through when |
| 178 | // this version renamed the page. Escaped here, as it is assembled here. |
| 179 | func versionTitle(page PageView, chosen Chosen) template.HTML { |
| 180 | title := chosen.Record.Title |
| 181 | if page.Kind != KindPage || title == "" { |
| 182 | return template.HTML(html.EscapeString(page.Label)) |
| 183 | } |
| 184 | escaped := html.EscapeString(title) |
| 185 | if chosen.Before == nil || chosen.Before.Title == "" || |
| 186 | chosen.Before.Title == title { |
| 187 | return template.HTML(escaped) |
| 188 | } |
| 189 | return template.HTML(strings.Join([]string{"<del>" + |
| 190 | html.EscapeString(chosen.Before.Title) + "</del>", |
| 191 | "<ins>" + escaped + "</ins>"}, " ")) |
| 192 | } |
| 193 | |
| 194 | // drawRun draws blocks for scope as the page does, with no empty |
| 195 | // paragraph after them: a version's view adds that once, at its end. |
| 196 | func (index *Index) drawRun(ctx context.Context, scope Scope, page int64, |
| 197 | blocks []markup.Block) (template.HTML, error) { |
| 198 | if err := index.Sync(ctx); err != nil { |
| 199 | return "", err |
| 200 | } |
| 201 | index.mutex.RLock() |
| 202 | defer index.mutex.RUnlock() |
| 203 | // Each image drawn in an old revision links to its Remove confirm |
| 204 | // page, so a photo taken out of the text can still be removed |
| 205 | // (images §3.3). |
| 206 | images := index.imageLookup(ctx) |
| 207 | images.remove = true |
| 208 | return markup.Render(blocks, resolver{index: index, scope: scope, |
| 209 | page: page, images: images}), nil |
| 210 | } |
| 211 | |
| 212 | // versionLook is the version's own icon: a team page's, from its |
| 213 | // record. |
| 214 | func versionLook(page PageView, chosen Chosen) *Icon { |
| 215 | if page.Kind != KindPage { |
| 216 | return nil |
| 217 | } |
| 218 | return chosen.Record.Icon |
| 219 | } |
| 220 | |