Skip to content

Commit 87aa7e0

Browse files
committed
Use clusters for word caps
1 parent c2d2dce commit 87aa7e0

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

jacow.typ

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,21 @@
125125

126126
/// Capitalize major words, e.g. "This is a Word-Caps Heading"
127127
/// Heuristic until we have https://github.com/typst/typst/issues/1707
128-
let wordcaps(body) = {
128+
let wordcaps(body, is-segment: false) = {
129129
if body.has("text") {
130130
let txt = body.text //lower(body.text)
131-
let words = txt.matches(regex("^()(\\w+)")) // first word
131+
let words = ()
132+
if (not is-segment){ words += txt.matches(regex("^()(\\w+)")) } // first word
132133
words += txt.matches(regex("([.:;?!]\s+)(\\w+)")) // words after punctuation
133134
words += txt.matches(regex("()(\\w{4,})")) // words with 4+ letters
134135
for m in words {
135136
let (pre, word) = m.captures
136-
word = upper(word.at(0)) + word.slice(1)
137-
txt = txt.slice(0, m.start) + pre + word + txt.slice(m.end)
137+
let (w, ord) = (word.clusters().at(0), word.clusters().slice(1).join())
138+
txt = txt.slice(0, m.start) + pre + upper(w) + ord + txt.slice(m.end)
138139
}
139140
txt
140141
} else if body.has("children") {
141-
body.children.map(it => wordcaps(it)).join()
142+
body.children.map(it => wordcaps(it, is-segment: true)).join()
142143
} else {
143144
body
144145
}

tests/headings/test.typ

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#import "/jacow.typ": *
2+
3+
#show: jacow.with(
4+
paper-size: "test",
5+
)
6+
7+
= Section heading
8+
#lorem(10)
9+
10+
== Subsection heading
11+
#lorem(10)
12+
13+
== анотація: a very long subsection heading spanning over two full lines
14+
15+
=== A third level heading
16+
#lorem(10)
17+
18+
=== a very very very long third level heading extending over two lines
19+
#lorem(10)
20+
21+
#colbreak()
22+
23+
= Long section heading spanning over two lines
24+
25+
== Second heading directly following
26+
#lorem(10)
27+
28+
== Another second level heading
29+
30+
=== and a third level heading too
31+
#lorem(10)
32+
33+
=== more third levels here
34+
#lorem(10)
35+
36+
== With math like $e^x$ or $pi^+$ and NaCl plus $attach("He", tl: 4, bl: 2)$

0 commit comments

Comments
 (0)