Repository navigation
Expand file tree
/
Copy pathcode-callouts.typ
More file actions
69 lines (61 loc) · 1.77 KB
/
Copy pathcode-callouts.typ
File metadata and controls
69 lines (61 loc) · 1.77 KB
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
#import "./definitions.typ": *
#let default-callout(number /*: int */) /*: content*/ = {
text(
font: secondary-font,
number-type: "old-style",
size: 1em,
weight: "bold",
fill: luma(120),
"[" + str(number) + "]",
)
}
#let unicode-circle-callout(number /*: int */) /*: content*/ = {
str.from-unicode(
if number == 0 { 0x24EA }
else if number <= 20 { 0x245F + number }
else if number <= 35 { 0x3250 + (number - 20) }
else if number <= 50 { 0x32B0 + (number - 35) } else { number }
)
}
#let callout-pat = regex("<(\\d+)>(\\n|$)")
#let parse-callouts(
code-text /*: str */
) /*: (callouts: array(array(int)), text: str) */ = {
let callouts /*: array(array(int)) */ = ()
let new-text = ""
for text-line in code-text.split("\n") {
let match = text-line.match(callout-pat)
if match != none {
callouts.push((int(match.captures.at(0)),))
new-text += text-line.slice(0, match.start)
} else {
callouts.push(())
new-text += text-line
}
new-text += "\n"
}
(callouts: callouts, text: new-text)
}
#let processed-label = <TypstCodeCallout-was-processed>
#let code-with-callouts = (
it /*: content(raw) */,
callout-display: default-callout /*: function(str, content) */
) => {
if it.at("label", default: none) == processed-label {
it
} else {
let (callouts, text: new-text) = parse-callouts(it.text)
show raw.line: it => {
it
let callouts-of-line = callouts.at(it.number - 1, default: ())
for callout in callouts-of-line {
callout-display(callout)
}
}
let fields = it.fields()
let _ = fields.remove("text")
let _ = fields.remove("lines")
let _ = fields.remove("theme")
[#raw(..fields, new-text)#processed-label]
}
}