Skip to content

Commit bcd0e5d

Browse files
committed
Sanitize captions
1 parent 8d1e5fe commit bcd0e5d

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

jacow.typ

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,32 @@
119119
let titlenotenumbering(i) = {
120120
if i < 6 { ("*", "#", "§", "", "").at(i - 1) } else { (i - 4) * "*" }
121121
}
122+
123+
/// Remove white space at begin and end
124+
let strip-white-space(content) = {
125+
if (content.has("children")){
126+
let clean = content.children
127+
for i in (0, -1){
128+
while (clean.len() > 0 and repr(clean.at(i).func()) in ("space", "parbreak")) {
129+
_ = clean.remove(i)
130+
}
131+
}
132+
clean.join()
133+
} else {
134+
content
135+
}
136+
}
137+
138+
/// Check if content ends with string
139+
let ends-with(content, end) = {
140+
if (content.has("children")){
141+
return ends-with(content.children.filter(c => c.has("text")).last(), end)
142+
} else if (content.has("text")) {
143+
return content.text.clusters().at(-1).ends-with(end)
144+
} else {
145+
return false
146+
}
147+
}
122148

123149
/// Capitalize all characters in the text, e.g. "THIS IS AN ALLCAPS HEADING"
124150
let allcaps = upper
@@ -528,18 +554,19 @@
528554
show figure.caption: it => {
529555
set par(first-line-indent: 0em)
530556
layout(size => context {
557+
let body = strip-white-space(it.body) // removes trailing whitespace
558+
if (it.kind == table){
559+
// table captions take the form of a heading (word caps)
560+
body = wordcaps(body)
561+
} else {
562+
// figure captions must end with a period
563+
if (not ends-with(body, ".")){ body += "."}
564+
}
565+
let caption = [#it.supplement #it.counter.display()#it.separator#body]
531566
align(
532567
// center for single-line, left for multi-line captions
533-
if measure(it).width < size.width { center } else { left },
534-
if sys.version >= version(0, 13) {
535-
// workaround for https://github.com/typst/typst/issues/5472#issuecomment-2730205275
536-
block(
537-
width: size.width,
538-
context [#it.supplement #it.counter.display()#it.separator#it.body],
539-
)
540-
} else {
541-
block(width: size.width, it) // use full width and justify
542-
},
568+
if measure(caption).width < size.width { center } else { left },
569+
block(width: size.width, caption),
543570
)
544571
})
545572
}

tests/captions/test.typ

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111

1212
#figure(
1313
box(fill: silver, width: 100%, height: 1cm),
14-
caption: [Short caption centered],
14+
caption: "Short caption centered.",
1515
)
1616

1717
#figure(
1818
box(fill: silver, width: 100%, height: 1cm),
19-
caption: [Medium caption with two lines, spanning the entire width and left aligned],
19+
caption: [
20+
21+
Medium caption with two lines, spanning the entire width and left aligned
22+
23+
],
2024
)
2125

2226
#figure(
@@ -33,7 +37,10 @@
3337

3438
#figure(
3539
table(columns: (1fr, 1fr, 1fr))[M],
36-
caption: [Medium caption with two lines, spanning the entire width and left aligned],
40+
caption: [
41+
Medium caption with two lines, spanning the entire width and left aligned
42+
43+
],
3744
)
3845

3946
#figure(
@@ -45,7 +52,7 @@
4552
box(fill: silver, width: 100%, height: 1cm),
4653
scope: "parent",
4754
placement: bottom,
48-
caption: [Short caption centered],
55+
caption: [Short caption centered with $pi$],
4956
)
5057

5158
#figure(

0 commit comments

Comments
 (0)