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 "sort"
5
6 "amadan.net/oficina/mapa/internal/mapa/markup"
7)
8
9// What is around a page (level-up spec §4.3): the Links panel's links
10// out and its map. Every read goes through scoped, the one accessor the
11// level-up's read paths share, so a page the viewer may not open is
12// never a node, an edge, a chip or a count (base §3.3, gate 1, L6).
13
14// scoped is the index as one scope sees it. The caller holds the read
15// lock for as long as it uses one.
16type scoped struct {
17 index *Index
18 scope Scope
19}
20
21func (index *Index) in(scope Scope) scoped {
22 return scoped{index: index, scope: scope}
23}
24
25// page is a page the scope may open, or nil.
26func (view scoped) page(pageID int64) *entry {
27 page := view.index.pages[pageID]
28 if page == nil || !view.scope.sees(page) {
29 return nil
30 }
31 return page
32}
33
34// target is where a [[link]]'s text leads for this scope: a team page,
35// or the scope's own note for a date; nil for a pending link.
36func (view scoped) target(text string) *entry {
37 if markup.IsDateShaped(text) {
38 if view.scope.Member == 0 {
39 return nil
40 }
41 return view.page(view.index.days[view.scope.Member][text])
42 }
43 return view.page(view.index.titles[Fold(text)])
44}
45
46// wroteOn reports whether the scope has its own note for a date.
47func (view scoped) wroteOn(date string) bool {
48 if view.scope.Member == 0 {
49 return false
50 }
51 return view.page(view.index.days[view.scope.Member][date]) != nil
52}
53
54// Chip is one link out, as the panel lists it.
55type Chip struct {
56 Label, Href string
57 // Kind is "team", "pending" or "date".
58 Kind string
59 // Look is a team page's icon (columns and icons spec §2.4).
60 Look *Icon
61}
62
63// Around is the panel's own data: links out and the map. Backlinks and
64// mentions are the PageView's, already scoped.
65type Around struct {
66 Out []Chip
67 Map MapView
68}
69
70// linksOut is each distinct link a page types, in body order: team
71// pages, pending titles and dates. A date that is no date is left out.
72func (view scoped) linksOut(page *entry) []Chip {
73 seen := map[string]bool{}
74 var out []Chip
75 for _, link := range page.links {
76 fold := Fold(link.Target)
77 if seen[fold] {
78 continue
79 }
80 seen[fold] = true
81 if chip, ok := view.chip(link.Target); ok {
82 out = append(out, chip)
83 }
84 }
85 return out
86}
87
88func (view scoped) chip(text string) (Chip, bool) {
89 if markup.IsDateShaped(text) {
90 if !markup.IsDate(text) {
91 return Chip{}, false
92 }
93 return Chip{Label: view.scope.Locale.Day(text, thisYear()),
94 Href: "/d/" + text,
95 Kind: "date"}, true
96 }
97 if target := view.target(text); target != nil {
98 return Chip{Label: target.label(view.scope.Locale), Href: target.href(),
99 Kind: "team", Look: target.look()}, true
100 }
101 return Chip{Label: text, Href: pendingHref(text), Kind: "pending"},
102 true
103}
104
105// neighbour is one page on the map before it is placed.
106type neighbour struct {
107 page *entry // nil for a pending title
108 label string
109 href string
110 // kind is "linked", "pending" or "mentioned".
111 kind string
112 sort string
113}
114
115// mapNeighbours is the map's ring (§4.3): linked pages first (both
116// ways, by public id), then titles not written yet, then pages that
117// mention this one, at most mapCap of them.
118func (view scoped) mapNeighbours(page *entry, backlinks,
119 mentions []Linked) []neighbour {
120 have := map[int64]bool{page.id: true}
121 var linked, pending, mentioned []neighbour
122 addPage := func(found *entry, kind string, into *[]neighbour) {
123 if found == nil || have[found.id] {
124 return
125 }
126 have[found.id] = true
127 *into = append(*into, neighbour{page: found,
128 label: found.label(view.scope.Locale),
129 href: found.href(), kind: kind, sort: publicID(found.nonce)})
130 }
131 for _, link := range backlinks {
132 addPage(view.page(link.ID), "linked", &linked)
133 }
134 pendingSeen := map[string]bool{}
135 for _, link := range page.links {
136 if markup.IsDateShaped(link.Target) {
137 continue
138 }
139 if found := view.target(link.Target); found != nil {
140 addPage(found, "linked", &linked)
141 } else if fold := Fold(link.Target); !pendingSeen[fold] {
142 pendingSeen[fold] = true
143 pending = append(pending, neighbour{label: link.Target,
144 href: pendingHref(link.Target), kind: "pending",
145 sort: fold})
146 }
147 }
148 for _, link := range mentions {
149 addPage(view.page(link.ID), "mentioned", &mentioned)
150 }
151 return capRing(linked, pending, mentioned)
152}
153
154// mapCap is how many neighbours the map draws. Past it there is no node
155// and no count: the lists below the map are complete (§4.3).
156const mapCap = 12
157
158func capRing(groups ...[]neighbour) []neighbour {
159 var ring []neighbour
160 for _, group := range groups {
161 sort.SliceStable(group, func(left, right int) bool {
162 return group[left].sort < group[right].sort
163 })
164 ring = append(ring, group...)
165 }
166 if len(ring) > mapCap {
167 ring = ring[:mapCap]
168 }
169 return ring
170}
171
172// links reports whether one page on the ring links to another, by the
173// text it typed. Both are in scope, or they would not be on the ring.
174func (view scoped) links(from, to *entry) bool {
175 if from == nil || to == nil {
176 return false
177 }
178 targets := to.targetFolds()
179 for _, link := range from.links {
180 if targets[Fold(link.Target)] && view.target(link.Target) == to {
181 return true
182 }
183 }
184 return false
185}
186
187// around is the panel's data for a page the scope sees.
188func (view scoped) around(page *entry, backlinks,
189 mentions []Linked) *Around {
190 ring := view.mapNeighbours(page, backlinks, mentions)
191 return &Around{Out: view.linksOut(page), Map: view.mapOf(page, ring)}
192}
193
194// LinkState is one chip's state for the editor (level-up §6.8): its
195// kind (team, pending, date, block, "block gone" or broken), where it
196// leads, and the current title of the team page it names, or the day
197// in words.
198type LinkState struct {
199 Kind string `json:"kind"`
200 Href string `json:"href"`
201 Title string `json:"title"`
202 // Alias is a link through an old title: the chip shows the words
203 // typed and says what the page is called now (§4.2).
204 Alias bool `json:"alias,omitempty"`
205 // Icon is the team page's icon (columns and icons spec §2.4).
206 Icon *markup.ChipIcon `json:"icon,omitempty"`
207}
208
209// linkState is where a [[link]] leads for this scope, as a chip draws
210// it. Like target, it reads team titles and the scope's own days only.
211func (view scoped) linkState(target, anchor string) LinkState {
212 if markup.IsDateShaped(target) {
213 if !markup.IsDate(target) {
214 return LinkState{Kind: "broken"}
215 }
216 return LinkState{Kind: "date", Href: "/d/" + target,
217 Title: view.scope.Locale.Day(target, thisYear())}
218 }
219 page := view.target(target)
220 if page == nil {
221 return LinkState{Kind: "pending", Href: pendingHref(target)}
222 }
223 state := LinkState{Kind: "team", Href: page.href(),
224 Title: page.record.Title,
225 Alias: Fold(target) != Fold(page.record.Title)}
226 if icon := page.chipIcon(); icon != (markup.ChipIcon{}) && anchor == "" {
227 state.Icon = &icon
228 }
229 switch {
230 case anchor == "":
231 case page.hasMarker(anchor):
232 state.Kind, state.Href = "block", state.Href+"#b-"+anchor
233 default:
234 state.Kind = "block gone"
235 }
236 return state
237}
238