oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1package deck
2
3import (
4 "math"
5 "sort"
6 "strings"
7)
8
9// Reveal is one thing a press brings in: a whole box (Item -1), one item
10// of a list whose items arrive one per press (Item is the index of its
11// paragraph in the list's Text, blank paragraphs counted), or one state
12// of a scene block (Item is the state's index in its scene's States).
13type Reveal struct {
14 Block string `json:"block"`
15 Item int `json:"item"` // -1 is the whole box
16}
17
18// Press is everything one press reveals, in the slide's block order.
19type Press []Reveal
20
21// Presses works out a slide's presses from its boxes; they are never
22// stored. Element 0 is press 1, and press 0 is the slide as it arrives,
23// so a slide with no steps has none. scenes is the deck's (Deck.Scenes),
24// which only scene blocks need. fixtures/presses.mjs is the same
25// function for the browser, and fixtures/vectors/presses.json holds the
26// two to one answer:
27//
28// - A box's value is its own step, or its columns container's when
29// that is later (and so on up). A box with no step of its own arrives
30// with whatever holds it, the slide or its container.
31// - The distinct values, ascending, are the presses, compared exactly
32// as stored. Boxes sharing a value arrive together.
33// - An `each` list at value v takes one press per non-blank item from
34// v on; everything else at v comes with the first of them, lists tied
35// at v pair up by index, and the next value starts after the longest.
36// - A box that shows nothing is in no press, so a value with only such
37// boxes makes none. A columns box shows nothing until one of its
38// children does, so it arrives with the earliest of them.
39// - A scene block is an `each` list whose items are its scene's states
40// strictly after `from`, up to and including `to` (plan call R2). It
41// is there when the slide arrives, so with no step of its own (or a
42// container's) its value is 0 and its presses come first. With no
43// such states — from equal to to, or a scene or state the deck no
44// longer has — it makes none.
45// - A clip that plays on a press (play: press) is a box arriving at
46// its value, 0 with no step (plan call R3). Any other clip is a box
47// like any other.
48func Presses(s Slide, scenes map[string]Scene) []Press {
49 sl := newSlideBoxes(s)
50 type arriving struct {
51 b *Block
52 value float64
53 items []int // non-blank paragraph indices, for an each list
54 }
55 var all []arriving
56 var values []float64
57 seen := map[float64]bool{}
58 for i := range s.Blocks {
59 b := &s.Blocks[i]
60 var items []int
61 switch {
62 case b.Type == "scene":
63 if items = sceneItems(b, scenes); len(items) == 0 {
64 continue
65 }
66 case b.Type == "clip" && b.Props["play"] == "press":
67 case ownStep(b) == 0 || !sl.shows(b, map[string]bool{}):
68 continue
69 case b.Type == "list" && b.Items == "each":
70 items = filledItems(b.Text)
71 }
72 a := arriving{b: b, value: sl.arrival(b, map[string]bool{}), items: items}
73 all = append(all, a)
74 if !seen[a.value] {
75 seen[a.value] = true
76 values = append(values, a.value)
77 }
78 }
79 sort.Float64s(values)
80
81 var out []Press
82 for _, v := range values {
83 n := 1
84 for _, a := range all {
85 if a.value == v && len(a.items) > n {
86 n = len(a.items)
87 }
88 }
89 for j := 0; j < n; j++ {
90 var p Press
91 for _, a := range all {
92 switch {
93 case a.value != v:
94 case a.items != nil:
95 if j < len(a.items) {
96 p = append(p, Reveal{Block: a.b.ID, Item: a.items[j]})
97 }
98 case j == 0:
99 p = append(p, Reveal{Block: a.b.ID, Item: -1})
100 }
101 }
102 out = append(out, p)
103 }
104 }
105 return out
106}
107
108// ArrivalOf is the press number (from 1) of everything that arrives on a
109// press. What arrives with the slide is not in the map.
110func ArrivalOf(s Slide, scenes map[string]Scene) map[Reveal]int {
111 out := map[Reveal]int{}
112 for i, p := range Presses(s, scenes) {
113 for _, r := range p {
114 out[r] = i + 1
115 }
116 }
117 return out
118}
119
120// slideBoxes is a slide's blocks by id, and the columns box that holds
121// each one: the first in block order naming it in props.children, as
122// the renderer reads it (render.go's childIDs).
123type slideBoxes struct {
124 byID map[string]*Block
125 parent map[string]string
126}
127
128func newSlideBoxes(s Slide) slideBoxes {
129 sl := slideBoxes{byID: map[string]*Block{}, parent: map[string]string{}}
130 for i := range s.Blocks {
131 sl.byID[s.Blocks[i].ID] = &s.Blocks[i]
132 }
133 for _, b := range s.Blocks {
134 if b.Type != "columns" {
135 continue
136 }
137 for _, id := range children(b) {
138 _, named := sl.parent[id]
139 if _, ok := sl.byID[id]; ok && id != b.ID && !named {
140 sl.parent[id] = b.ID
141 }
142 }
143 }
144 return sl
145}
146
147// value is the latest of a box's own step and its containers', walking
148// up until a container repeats (columns naming each other).
149func (sl slideBoxes) value(b *Block) float64 {
150 v := ownStep(b)
151 seen := map[string]bool{b.ID: true}
152 for id, ok := sl.parent[b.ID]; ok && !seen[id]; id, ok = sl.parent[id] {
153 seen[id] = true
154 if s := ownStep(sl.byID[id]); s > v {
155 v = s
156 }
157 }
158 return v
159}
160
161// arrival is the value a box first shows at: its value, except that a
162// columns box waits for the earliest of its children that show, since
163// until then it is an empty frame. on is the columns boxes already being
164// looked into, so a cycle ends.
165func (sl slideBoxes) arrival(b *Block, on map[string]bool) float64 {
166 v := sl.value(b)
167 if b.Type != "columns" {
168 return v
169 }
170 on[b.ID] = true
171 first := math.Inf(1)
172 for _, id := range children(*b) {
173 c, ok := sl.byID[id]
174 if !ok || on[id] || !sl.shows(c, map[string]bool{}) {
175 continue
176 }
177 first = min(first, sl.arrival(c, on))
178 }
179 if first > v && !math.IsInf(first, 1) {
180 return first
181 }
182 return v
183}
184
185// shows is whether a box puts anything on the slide: a list with a
186// non-blank item, a heading, text or quote with non-blank text, a
187// columns box with a child that shows. Every other type does. on is the
188// columns boxes already being looked into, so a cycle ends.
189func (sl slideBoxes) shows(b *Block, on map[string]bool) bool {
190 switch b.Type {
191 case "list":
192 return len(filledItems(b.Text)) > 0
193 case "heading", "text", "quote":
194 return !blank(b.Text.Plain())
195 case "columns":
196 on[b.ID] = true
197 for _, id := range children(*b) {
198 if c, ok := sl.byID[id]; ok && !on[id] && sl.shows(c, on) {
199 return true
200 }
201 }
202 return false
203 }
204 return true
205}
206
207// sceneItems is a scene block's states as press items: the indices, in
208// its scene's States, of those strictly after props.from up to and
209// including props.to. None when the scene or either state is missing.
210func sceneItems(b *Block, scenes map[string]Scene) []int {
211 name, _ := b.Props["scene"].(string)
212 from, _ := b.Props["from"].(string)
213 to, _ := b.Props["to"].(string)
214 sc, ok := scenes[name]
215 if !ok {
216 return nil
217 }
218 f, t := sc.StateIndex(from), sc.StateIndex(to)
219 if f < 0 || t < 0 {
220 return nil
221 }
222 var out []int
223 for i := f + 1; i <= t; i++ {
224 out = append(out, i)
225 }
226 return out
227}
228
229// ownStep is a block's own step, 0 when it has none.
230func ownStep(b *Block) float64 {
231 if b.Step > 0 {
232 return b.Step
233 }
234 return 0
235}
236
237// filledItems is the indices of a text's non-blank paragraphs.
238func filledItems(t Text) []int {
239 out := []int{}
240 for i, para := range t {
241 if !blank(Text{para}.Plain()) {
242 out = append(out, i)
243 }
244 }
245 return out
246}
247
248// blank is text with nothing but white space in it, as Go's
249// unicode.IsSpace has it; presses.mjs spells the same set out.
250func blank(s string) bool {
251 return strings.TrimSpace(s) == ""
252}
253
254// children is a columns block's props.children, as they arrive from
255// JSON (the same reading as render.go's childIDs).
256func children(b Block) []string {
257 switch v := b.Props["children"].(type) {
258 case []string:
259 return v
260 case []any:
261 out := make([]string, 0, len(v))
262 for _, c := range v {
263 if s, ok := c.(string); ok {
264 out = append(out, s)
265 }
266 }
267 return out
268 }
269 return nil
270}
271
272// pressLine is the paragraph that ends one section of a slide's notes
273// and starts the next: exactly this, in plain text.
274const pressLine = "(Press.)"
275
276// NoteParts splits a slide's notes at its press lines, so each part is
277// what to say at one point of the slide: the first when the slide
278// arrives, the next after the first press, and so on. A paragraph is a
279// press line when its plain text, trimmed of surrounding white space, is
280// exactly "(Press.)"; marks inside it do not matter, case does. Press
281// lines are dropped. Notes with none are one part, and a leading or
282// trailing line leaves an empty part.
283func NoteParts(t Text) []Text {
284 parts := []Text{nil}
285 for _, para := range t {
286 if strings.TrimSpace(Text{para}.Plain()) == pressLine {
287 parts = append(parts, nil)
288 continue
289 }
290 parts[len(parts)-1] = append(parts[len(parts)-1], para)
291 }
292 return parts
293}
294