forked from andreasKroepelin/polylux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.typ
188 lines (169 loc) · 5.44 KB
/
logic.typ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#let subslide = counter("subslide")
#let logical-slide = counter("logical-slide")
#let repetitions = counter("repetitions")
#let handout-mode = state("handout-mode", false)
#let _slides-cover(mode, body) = {
if mode == "invisible" {
hide(body)
} else if mode == "transparent" {
text(gray.lighten(50%), body)
} else {
panic("Illegal cover mode: " + mode)
}
}
#let _parse-subslide-indices(s) = {
let parts = s.split(",").map(p => p.trim())
let parse-part(part) = {
let match-until = part.match(regex("^-([[:digit:]]+)$"))
let match-beginning = part.match(regex("^([[:digit:]]+)-$"))
let match-range = part.match(regex("^([[:digit:]]+)-([[:digit:]]+)$"))
let match-single = part.match(regex("^([[:digit:]]+)$"))
if match-until != none {
let parsed = int(match-until.captures.first())
// assert(parsed > 0, "parsed idx is non-positive")
( until: parsed )
} else if match-beginning != none {
let parsed = int(match-beginning.captures.first())
// assert(parsed > 0, "parsed idx is non-positive")
( beginning: parsed )
} else if match-range != none {
let parsed-first = int(match-range.captures.first())
let parsed-last = int(match-range.captures.last())
// assert(parsed-first > 0, "parsed idx is non-positive")
// assert(parsed-last > 0, "parsed idx is non-positive")
( beginning: parsed-first, until: parsed-last )
} else if match-single != none {
let parsed = int(match-single.captures.first())
// assert(parsed > 0, "parsed idx is non-positive")
parsed
} else {
panic("failed to parse visible slide idx:" + part)
}
}
parts.map(parse-part)
}
#let _check-visible(idx, visible-subslides) = {
if type(visible-subslides) == "integer" {
idx == visible-subslides
} else if type(visible-subslides) == "array" {
visible-subslides.any(s => _check-visible(idx, s))
} else if type(visible-subslides) == "string" {
let parts = _parse-subslide-indices(visible-subslides)
_check-visible(idx, parts)
} else if type(visible-subslides) == "dictionary" {
let lower-okay = if "beginning" in visible-subslides {
visible-subslides.beginning <= idx
} else {
true
}
let upper-okay = if "until" in visible-subslides {
visible-subslides.until >= idx
} else {
true
}
lower-okay and upper-okay
} else {
panic("you may only provide a single integer, an array of integers, or a string")
}
}
#let _last-required-subslide(visible-subslides) = {
if type(visible-subslides) == "integer" {
visible-subslides
} else if type(visible-subslides) == "array" {
calc.max(..visible-subslides.map(s => _last-required-subslide(s)))
} else if type(visible-subslides) == "string" {
let parts = _parse-subslide-indices(visible-subslides)
_last-required-subslide(parts)
} else if type(visible-subslides) == "dictionary" {
let last = 0
if "beginning" in visible-subslides {
last = calc.max(last, visible-subslides.beginning)
}
if "until" in visible-subslides {
last = calc.max(last, visible-subslides.until)
}
last
} else {
panic("you may only provide a single integer, an array of integers, or a string")
}
}
#let _conditional-display(visible-subslides, reserve-space, mode, body) = {
locate( loc => {
let vs = if reserve-space and handout-mode.at(loc) {
(:)
} else {
visible-subslides
}
repetitions.update(rep => calc.max(rep, _last-required-subslide(vs)))
if _check-visible(subslide.at(loc).first(), vs) {
body
} else if reserve-space {
_slides-cover(mode, body)
}
})
}
#let uncover(visible-subslides, mode: "invisible", body) = {
_conditional-display(visible-subslides, true, mode, body)
}
#let only(visible-subslides, body) = {
_conditional-display(visible-subslides, false, "doesn't even matter", body)
}
#let one-by-one(start: 1, mode: "invisible", ..children) = {
for (idx, child) in children.pos().enumerate() {
uncover((beginning: start + idx), mode: mode, child)
}
}
#let alternatives(start: 1, position: bottom + left, ..children) = {
style(styles => {
let sizes = children.pos().map(c => measure(c, styles))
let max-width = calc.max(..sizes.map(sz => sz.width))
let max-height = calc.max(..sizes.map(sz => sz.height))
for (idx, child) in children.pos().enumerate() {
only(start + idx, box(
width: max-width,
height: max-height,
align(position, child)
))
}
})
}
#let line-by-line(start: 1, mode: "invisible", body) = {
let items = if repr(body.func()) == "sequence" {
body.children
} else {
( body, )
}
let idx = start
for item in items {
if repr(item.func()) != "space" {
uncover((beginning: idx), mode: mode, item)
idx += 1
} else {
item
}
}
}
#let pause(beginning, mode: "invisible") = body => {
uncover((beginning: beginning), mode: mode, body)
}
#let polylux-slide(max-repetitions: 10, body) = {
locate( loc => {
if counter(page).at(loc).first() > 1 {
pagebreak(weak: true)
}
})
logical-slide.step()
subslide.update(1)
repetitions.update(1)
for _ in range(max-repetitions) {
locate( loc => {
let curr-subslide = subslide.at(loc).first()
if curr-subslide <= repetitions.at(loc).first() {
if curr-subslide > 1 { pagebreak(weak: true) }
set heading(outlined: false) if curr-subslide > 1
body
}
})
subslide.step()
}
}