-
Notifications
You must be signed in to change notification settings - Fork 10
/
slide.typ
679 lines (672 loc) · 21.9 KB
/
slide.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
#import "utils/utils.typ"
#import "utils/states.typ"
#import "utils/pdfpc.typ"
// touying pause mark
#let pause = metadata((kind: "touying-pause"))
// touying meanwhile mark
#let meanwhile = metadata((kind: "touying-meanwhile"))
// touying slides-end mark
#let slides-end = metadata((kind: "touying-slides-end"))
// touying equation mark
#let touying-equation(block: true, numbering: none, supplement: auto, scope: (:), body) = {
metadata((
kind: "touying-equation",
block: block,
numbering: numbering,
supplement: supplement,
scope: scope,
body: {
if type(body) == function {
body
} else if type(body) == str {
body
} else if type(body) == content and body.has("text") {
body.text
} else {
panic("Unsupported type: " + str(type(body)))
}
},
))
}
// touying reducer mark
#let touying-reducer(reduce: arr => arr.sum(), cover: arr => none, ..args) = {
metadata((
kind: "touying-reducer",
reduce: reduce,
cover: cover,
kwargs: args.named(),
args: args.pos(),
))
}
// parse touying equation, and get the repetitions
#let _parse-touying-equation(self: none, need-cover: true, base: 1, index: 1, eqt) = {
let result-arr = ()
// repetitions
let repetitions = base
let max-repetitions = repetitions
// get cover function from self
let cover = self.methods.cover.with(self: self)
// get eqt body
let it = eqt.body
// if it is a function, then call it with self
if type(it) == function {
// subslide index
self.subslide = index
it = it(self)
}
assert(type(it) == str, message: "Unsupported type: " + str(type(it)))
// parse the content
let result = ()
let cover-arr = ()
let children = it.split(regex("(#meanwhile;?)|(meanwhile)")).intersperse("touying-meanwhile")
.map(s => s.split(regex("(#pause;?)|(pause)")).intersperse("touying-pause")).flatten()
.map(s => s.split(regex("(\\\\\\s)|(\\\\\\n)")).intersperse("\\\n")).flatten()
.map(s => s.split(regex("&")).intersperse("&")).flatten()
for child in children {
if child == "touying-pause" {
repetitions += 1
} else if child == "touying-meanwhile" {
// clear the cover-arr when encounter #meanwhile
if cover-arr.len() != 0 {
result.push("cover(" + cover-arr.sum() + ")")
cover-arr = ()
}
// then reset the repetitions
max-repetitions = calc.max(max-repetitions, repetitions)
repetitions = 1
} else if child == "\\\n" or child == "&" {
// clear the cover-arr when encounter linebreak or parbreak
if cover-arr.len() != 0 {
result.push("cover(" + cover-arr.sum() + ")")
cover-arr = ()
}
result.push(child)
} else {
if repetitions <= index or not need-cover {
result.push(child)
} else {
cover-arr.push(child)
}
}
}
// clear the cover-arr when end
if cover-arr.len() != 0 {
result.push("cover(" + cover-arr.sum() + ")")
cover-arr = ()
}
result-arr.push(
math.equation(
block: eqt.block,
numbering: eqt.numbering,
supplement: eqt.supplement,
eval("$" + result.sum(default: "") + "$", scope: eqt.scope + (cover: (..args) => {
let cover = eqt.scope.at("cover", default: cover)
if args.pos().len() != 0 {
cover(args.pos().first())
}
})),
)
)
max-repetitions = calc.max(max-repetitions, repetitions)
return (result-arr, max-repetitions)
}
// parse touying reducer, and get the repetitions
#let _parse-touying-reducer(self: none, base: 1, index: 1, reducer) = {
let result-arr = ()
// repetitions
let repetitions = base
let max-repetitions = repetitions
// get cover function from self
let cover = reducer.cover
// parse the content
let result = ()
let cover-arr = ()
for child in reducer.args.flatten() {
if type(child) == content and child.func() == metadata and type(child.value) == dictionary {
let kind = child.value.at("kind", default: none)
if kind == "touying-pause" {
repetitions += 1
} else if kind == "touying-meanwhile" {
// clear the cover-arr when encounter #meanwhile
if cover-arr.len() != 0 {
result.push(cover(cover-arr.sum()))
cover-arr = ()
}
// then reset the repetitions
max-repetitions = calc.max(max-repetitions, repetitions)
repetitions = 1
} else {
if repetitions <= index {
result.push(child)
} else {
cover-arr.push(child)
}
}
} else {
if repetitions <= index {
result.push(child)
} else {
cover-arr.push(child)
}
}
}
// clear the cover-arr when end
if cover-arr.len() != 0 {
let r = cover(cover-arr)
if type(r) == array {
result += r
} else {
result.push(r)
}
cover-arr = ()
}
result-arr.push(
(reducer.reduce)(
..reducer.kwargs,
result,
)
)
max-repetitions = calc.max(max-repetitions, repetitions)
return (result-arr, max-repetitions)
}
// parse a sequence into content, and get the repetitions
#let _parse-content(self: none, need-cover: true, base: 1, index: 1, ..bodies) = {
let bodies = bodies.pos()
let result-arr = ()
// repetitions
let repetitions = base
let max-repetitions = repetitions
// get cover function from self
let cover = self.methods.cover.with(self: self)
for it in bodies {
// if it is a function, then call it with self
if type(it) == function {
// subslide index
self.subslide = index
it = it(self)
}
// parse the content
let result = ()
let cover-arr = ()
let children = if utils.is-sequence(it) { it.children } else { (it,) }
for child in children {
if type(child) == content and child.func() == metadata and type(child.value) == dictionary {
let kind = child.value.at("kind", default: none)
if kind == "touying-pause" {
repetitions += 1
} else if kind == "touying-meanwhile" {
// clear the cover-arr when encounter #meanwhile
if cover-arr.len() != 0 {
result.push(cover(cover-arr.sum()))
cover-arr = ()
}
// then reset the repetitions
max-repetitions = calc.max(max-repetitions, repetitions)
repetitions = 1
} else if kind == "touying-equation" {
// handle touying-equation
let (conts, nextrepetitions) = _parse-touying-equation(
self: self, need-cover: repetitions <= index, base: repetitions, index: index, child.value
)
let cont = conts.first()
if repetitions <= index or not need-cover {
result.push(cont)
} else {
cover-arr.push(cont)
}
repetitions = nextrepetitions
} else if kind == "touying-reducer" {
// handle touying-reducer
let (conts, nextrepetitions) = _parse-touying-reducer(
self: self, base: repetitions, index: index, child.value
)
let cont = conts.first()
if repetitions <= index or not need-cover {
result.push(cont)
} else {
cover-arr.push(cont)
}
repetitions = nextrepetitions
} else {
if repetitions <= index or not need-cover {
result.push(child)
} else {
cover-arr.push(child)
}
}
} else if child == linebreak() or child == parbreak() {
// clear the cover-arr when encounter linebreak or parbreak
if cover-arr.len() != 0 {
result.push(cover(cover-arr.sum()))
cover-arr = ()
}
result.push(child)
} else if utils.is-sequence(child) {
// handle the sequence
let (conts, nextrepetitions) = _parse-content(
self: self, need-cover: repetitions <= index, base: repetitions, index: index, child
)
let cont = conts.first()
if repetitions <= index or not need-cover {
result.push(cont)
} else {
cover-arr.push(cont)
}
repetitions = nextrepetitions
} else if utils.is-styled(child) {
// handle styled
let (conts, nextrepetitions) = _parse-content(
self: self, need-cover: repetitions <= index, base: repetitions, index: index, child.child
)
let cont = conts.first()
if repetitions <= index or not need-cover {
result.push(utils.typst-builtin-styled(cont, child.styles))
} else {
cover-arr.push(utils.typst-builtin-styled(cont, child.styles))
}
repetitions = nextrepetitions
} else if type(child) == content and child.func() in (list.item, enum.item, align) {
// handle the list item
let (conts, nextrepetitions) = _parse-content(
self: self, need-cover: repetitions <= index, base: repetitions, index: index, child.body
)
let cont = conts.first()
if repetitions <= index or not need-cover {
result.push(utils.reconstruct(child, cont))
} else {
cover-arr.push(utils.reconstruct(child, cont))
}
repetitions = nextrepetitions
} else if type(child) == content and child.func() in (pad,) {
// handle the pad
let (conts, nextrepetitions) = _parse-content(
self: self, need-cover: repetitions <= index, base: repetitions, index: index, child.body
)
let cont = conts.first()
if repetitions <= index or not need-cover {
result.push(utils.reconstruct(named: true, child, cont))
} else {
cover-arr.push(utils.reconstruct(named: true, child, cont))
}
repetitions = nextrepetitions
} else if type(child) == content and child.func() == terms.item {
// handle the terms item
let (conts, nextrepetitions) = _parse-content(
self: self, need-cover: repetitions <= index, base: repetitions, index: index, child.description
)
let cont = conts.first()
if repetitions <= index or not need-cover {
result.push(terms.item(child.term, cont))
} else {
cover-arr.push(terms.item(child.term, cont))
}
repetitions = nextrepetitions
} else {
if repetitions <= index or not need-cover {
result.push(child)
} else {
cover-arr.push(child)
}
}
}
// clear the cover-arr when end
if cover-arr.len() != 0 {
result.push(cover(cover-arr.sum()))
cover-arr = ()
}
result-arr.push(result.sum(default: []))
}
max-repetitions = calc.max(max-repetitions, repetitions)
return (result-arr, max-repetitions)
}
// touying-slide
#let touying-slide(
self: none,
repeat: auto,
setting: body => body,
composer: auto,
section: none,
subsection: none,
title: none,
..bodies,
) = {
assert(bodies.named().len() == 0, message: "unexpected named arguments:" + repr(bodies.named().keys()))
let setting-with-pad(body) = {
pad(..self.padding, setting(body))
}
let composer-with-side-by-side(..args) = {
if type(composer) == function {
composer(..args)
} else {
utils.side-by-side(columns: composer, ..args)
}
}
let bodies = bodies.pos()
let page-preamble(curr-subslide) = locate(loc => {
if loc.page() == self.first-slide-number {
// preamble
utils.call-or-display(self, self.preamble)
// pdfpc slide markers
if self.pdfpc-file {
pdfpc.pdfpc-file(loc)
}
}
[
#metadata((t: "NewSlide")) <pdfpc>
#metadata((t: "Idx", v: loc.page() - 1)) <pdfpc>
#metadata((t: "Overlay", v: curr-subslide - 1)) <pdfpc>
#metadata((t: "LogicalSlide", v: states.slide-counter.at(loc).first())) <pdfpc>
]
})
// update states
let _update-states(repetitions) = {
states.slide-counter.step()
if not self.appendix or self.appendix-in-outline {
// if section is not none, then create a new section
let section = utils.unify-section(section)
if section != none {
states._new-section(short-title: section.short-title, section.title)
}
// if subsection is not none, then create a new subsection
let subsection = utils.unify-section(subsection)
if subsection != none {
states._new-subsection(short-title: subsection.short-title, subsection.title)
}
}
// if appendix is false, then update the last-slide-counter and sections step
if not self.appendix {
states.last-slide-counter.step()
states._sections-step(repetitions)
}
}
// page header and footer
let header = utils.call-or-display(self, self.page-args.at("header", default: none))
let footer = utils.call-or-display(self, self.page-args.at("footer", default: none))
// for speed up, do not parse the content if repeat is none
if repeat == none {
return {
header = _update-states(1) + header
page(..(self.page-args + (header: header, footer: footer)), setting-with-pad(
page-preamble(1) + composer-with-side-by-side(..bodies)
))
}
}
// for single page slide, get the repetitions
if repeat == auto {
let (_, repetitions) = _parse-content(
self: self,
base: 1,
index: 1,
..bodies,
)
repeat = repetitions
}
if self.handout {
let (conts, _) = _parse-content(self: self, index: repeat, ..bodies)
header = _update-states(1) + header
page(..(self.page-args + (header: header, footer: footer)), setting-with-pad(
page-preamble(1) + composer-with-side-by-side(..conts)
))
} else {
// render all the subslides
let result = ()
let current = 1
for i in range(1, repeat + 1) {
let new-header = header
let (conts, _) = _parse-content(self: self, index: i, ..bodies)
// update the counter in the first subslide
if i == 1 {
new-header = _update-states(repeat) + new-header
}
result.push(page(
..(self.page-args + (header: new-header, footer: footer)),
setting-with-pad(page-preamble(i) + composer-with-side-by-side(..conts)),
))
}
// return the result
result.sum()
}
}
// touying-slides
#let touying-slides(self: none, slide-level: 1, body) = {
// make sure 0 <= slide-level <= 2
assert(type(slide-level) == int and 0 <= slide-level and slide-level <= 2, message: "slide-level should be 0, 1 or 2")
// init
let (section, subsection, title, slide) = (none, none, none, ())
let last-title = none
let children = if utils.is-sequence(body) { body.children } else { (body,) }
// flatten children
children = children.map(it => {
if utils.is-sequence(it) { it.children } else { it }
}).flatten()
// trim space of children
children = utils.trim(children)
if children.len() == 0 { return none }
// begin
let i = 0
let is-end = false
for child in children {
i += 1
if type(child) == content and child.func() == metadata and type(child.value) == dictionary and child.value.at("kind", default: none) == "touying-slides-end" {
is-end = true
break
} else if type(child) == content and child.func() == metadata and type(child.value) == dictionary and child.value.at("kind", default: none) == "touying-slide-wrapper" {
slide = utils.trim(slide)
if slide != () {
(self.methods.slide)(self: self, section: section, subsection: subsection, ..(if last-title != none { (title: last-title) }), slide.sum())
(section, subsection, title, slide) = (none, none, none, ())
}
if child.value.at("name") in self.slides {
(child.value.at("fn"))(section: section, subsection: subsection, ..(if last-title != none { (title: last-title) }), ..child.value.at("args"))
} else {
(child.value.at("fn"))(..child.value.at("args"))
}
(section, subsection, title, slide) = (none, none, none, ())
} else if type(child) == content and child.func() == heading and utils.heading-depth(child) <= slide-level + 1 {
slide = utils.trim(slide)
if (utils.heading-depth(child) == 1 and section != none) or (utils.heading-depth(child) == 2 and subsection != none) or (utils.heading-depth(child) > slide-level and title != none) or slide != () {
(self.methods.slide)(self: self, section: section, subsection: subsection, ..(if last-title != none { (title: last-title) }), slide.sum(default: []))
(section, subsection, title, slide) = (none, none, none, ())
if utils.heading-depth(child) <= slide-level {
last-title = none
}
}
let child-body = if child.body != [] { child.body } else { none }
if utils.heading-depth(child) == 1 {
if slide-level >= 1 {
if type(self.methods.at("touying-new-section-slide", default: none)) == function {
(self.methods.touying-new-section-slide)(self: self, child-body)
} else {
section = child-body
}
last-title = none
} else {
title = child.body
last-title = child-body
}
} else if utils.heading-depth(child) == 2 {
if slide-level >= 2 {
if type(self.methods.at("touying-new-subsection-slide", default: none)) == function {
(self.methods.touying-new-subsection-slide)(self: self, child-body)
} else {
subsection = child-body
}
last-title = none
} else {
title = child.body
last-title = child-body
}
} else {
title = child.body
last-title = child-body
}
} else {
slide.push(child)
}
}
slide = utils.trim(slide)
if section != none or subsection != none or title != none or slide != () {
(self.methods.slide)(self: self, section: section, subsection: subsection, ..(if last-title != none { (title: last-title) }), slide.sum(default: []))
}
if is-end {
children.slice(i).sum(default: none)
}
}
// build the touying singleton
#let s = (
// info interface
info: (
title: none,
short-title: auto,
subtitle: none,
short-subtitle: auto,
author: none,
date: none,
institution: none,
logo: none,
),
// colors interface
colors: (
neutral: rgb("#303030"),
neutral-light: rgb("#a0a0a0"),
neutral-lighter: rgb("#d0d0d0"),
neutral-lightest: rgb("#ffffff"),
neutral-dark: rgb("#202020"),
neutral-darker: rgb("#101010"),
neutral-darkest: rgb("#000000"),
primary: rgb("#303030"),
primary-light: rgb("#a0a0a0"),
primary-lighter: rgb("#d0d0d0"),
primary-lightest: rgb("#ffffff"),
primary-dark: rgb("#202020"),
primary-darker: rgb("#101010"),
primary-darkest: rgb("#000000"),
secondary: rgb("#303030"),
secondary-light: rgb("#a0a0a0"),
secondary-lighter: rgb("#d0d0d0"),
secondary-lightest: rgb("#ffffff"),
secondary-dark: rgb("#202020"),
secondary-darker: rgb("#101010"),
secondary-darkest: rgb("#000000"),
tertiary: rgb("#303030"),
tertiary-light: rgb("#a0a0a0"),
tertiary-lighter: rgb("#d0d0d0"),
tertiary-lightest: rgb("#ffffff"),
tertiary-dark: rgb("#202020"),
tertiary-darker: rgb("#101010"),
tertiary-darkest: rgb("#000000"),
),
// slides mode
slides: ("slide",),
// handle mode
handout: false,
// appendix mode
appendix: false,
appendix-in-outline: true,
// enable pdfpc-file
pdfpc-file: true,
// first-slide page number, which will affect preamble,
// default is 1
first-slide-number: 1,
// global preamble
preamble: [],
// page args
page-args: (
paper: "presentation-16-9",
header: none,
footer: none,
fill: rgb("#ffffff"),
),
padding: (x: 0em, y: 0em),
// datetime format
datetime-format: auto,
// register the methods
methods: (
// info
info: (self: none, ..args) => {
self.info += args.named()
self
},
// colors
colors: (self: none, ..args) => {
self.colors += args.named()
self
},
// cover method
cover: utils.wrap-method(hide),
update-cover: (self: none, is-method: false, cover-fn) => {
if is-method {
self.methods.cover = cover-fn
} else {
self.methods.cover = utils.wrap-method(cover-fn)
}
self
},
enable-transparent-cover: (
self: none, constructor: rgb, alpha: 85%) => {
// it is based on the default cover method
self.methods.cover = (self: none, body) => {
utils.cover-with-rect(fill: utils.update-alpha(
constructor: constructor, self.page-args.fill, alpha), body)
}
self
},
// dynamic control
uncover: utils.uncover,
only: utils.only,
alternatives-match: utils.alternatives-match,
alternatives: utils.alternatives,
alternatives-fn: utils.alternatives-fn,
alternatives-cases: utils.alternatives-cases,
// alert interface
alert: utils.wrap-method(text.with(weight: "bold")),
// handout mode
enable-handout-mode: (self: none) => {
self.handout = true
self
},
// disable pdfpc-file mode
disable-pdfpc-file: (self: none) => {
self.pdfpc-file = false
self
},
// default slide
touying-slide: touying-slide,
slide: touying-slide,
empty-slide: (self: none, ..args) => {
self = utils.empty-page(self)
(self.methods.slide)(self: self, ..args)
},
touying-slides: touying-slides,
slides: touying-slides,
// append the preamble
append-preamble: (self: none, preamble) => {
self.preamble += preamble
self
},
// datetime format
datetime-format: (self: none, format) => {
self.datetime-format = format
self
},
// default init
init: (self: none, body) => {
// default text size
set text(size: 20pt)
show heading.where(level: 2): set block(below: 1em)
body
},
// default outline
touying-outline: (self: none, ..args) => {
states.touying-outline(..args)
},
appendix: (self: none) => {
self.appendix = true
self
},
appendix-in-outline: (self: none, value) => {
self.appendix-in-outline = value
self
}
),
)