|
119 | 119 | let titlenotenumbering(i) = { |
120 | 120 | if i < 6 { ("*", "#", "§", "¶", "‡").at(i - 1) } else { (i - 4) * "*" } |
121 | 121 | } |
| 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 | + } |
122 | 148 |
|
123 | 149 | /// Capitalize all characters in the text, e.g. "THIS IS AN ALLCAPS HEADING" |
124 | 150 | let allcaps = upper |
|
528 | 554 | show figure.caption: it => { |
529 | 555 | set par(first-line-indent: 0em) |
530 | 556 | 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] |
531 | 566 | align( |
532 | 567 | // 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), |
543 | 570 | ) |
544 | 571 | }) |
545 | 572 | } |
|
0 commit comments