Skip to content

Commit 4e9b53a

Browse files
committed
Add capitalized versions of labels
Key `ref` now generates `ref:pl`, `Ref`, and `Ref:pl`. This changes `__get_entry_with_key` to not panic but return `none`, so we can retry with the lowercase key. All call-sites have been updates but it's not very pretty.
1 parent c477f19 commit 4e9b53a

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

tests/capitalize/capitalize.typ

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#let glossary = (
55
(key: "foo", long: "foobar"),
66
(key: "bar", long: "barbaz"),
7+
(key: "ref", long: "reference"),
8+
(key: "test", long: "Test"),
79
)
810
#register-glossary(glossary)
911

@@ -15,6 +17,8 @@
1517
- #Gls("foo") is the second reference.
1618
- #Glspl("bar") is the first reference at the beginning of a sentence using `#Glspl`.
1719
- #Glspl("bar") is the second reference.
20+
- @Ref is the first reference at the beginning of a sentence using `@Ref`. @Ref is the second.
21+
- @Test:pl is the first reference at the beginning of a sentence using `@Test:pl`. @Test is the second.
1822

1923
= Glossary
2024

themes/default.typ

+48-8
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,7 @@
115115
// key (str): the key of the term
116116
//
117117
// # Returns
118-
// The entry of the term
119-
//
120-
// # Panics
121-
// If the key is not found, it will raise a `key_not_found` error
118+
// The entry of the term or `none`
122119
#let __get_entry_with_key(loc, key) = {
123120
let entries = if sys.version <= version(0, 11, 1) {
124121
__glossary_entries.final()
@@ -128,7 +125,7 @@
128125
if key in entries {
129126
return entries.at(key)
130127
} else {
131-
panic(__error_message(key, __key_not_found))
128+
return none
132129
}
133130
}
134131

@@ -264,6 +261,9 @@
264261
// The link and the entry label
265262
#let gls(key, suffix: none, long: none, display: none, update: true, capitalize: false) = context {
266263
let entry = __get_entry_with_key(here(), key)
264+
if entry == none {
265+
panic(__error_message(key, __key_not_found))
266+
}
267267

268268
// Attributes
269269
let ent-long = entry.at("long", default: "")
@@ -335,6 +335,9 @@
335335
// The link and the entry label
336336
#let agls(key, suffix: none, long: none, update: true) = context {
337337
let entry = __get_entry_with_key(here(), key)
338+
if entry == none {
339+
panic(__error_message(key, __key_not_found))
340+
}
338341

339342
// Attributes
340343
let ent-long = entry.at("long", default: "")
@@ -384,6 +387,9 @@
384387
#let glspl(key, long: none, update: true, capitalize: false) = context {
385388
let default-plural-suffix = "s"
386389
let entry = __get_entry_with_key(here(), key)
390+
if entry == none {
391+
panic(__error_message(key, __key_not_found))
392+
}
387393

388394
// Attributes
389395
let ent-short = entry.at("short", default: "")
@@ -456,6 +462,10 @@
456462
// The attribute of the term
457463
#let __gls_attribute(key, attr, link: false, update: false) = context {
458464
let entry = __get_entry_with_key(here(), key)
465+
if entry == none {
466+
panic(__error_message(key, __key_not_found))
467+
}
468+
459469
if link {
460470
return __link_and_label(entry.key, entry.at(attr), update: update)
461471
} else if attr in entry and entry.at(attr) != none {
@@ -584,17 +594,34 @@
584594
if (
585595
r.element != none and r.element.func() == figure and r.element.kind == __glossarium_figure
586596
) {
597+
let position = r.element.location()
587598
// call to the general citing function
588599
let key = str(r.target)
589600
if key.ends-with(":pl") {
590601
// Plural ref
591-
glspl(str(key).slice(0, -3), update: update)
602+
let singular_key = str(key).slice(0, -3)
603+
if __get_entry_with_key(position, singular_key) != none {
604+
return glspl(singular_key, update: update)
605+
}
606+
let lower_case_key = lower(singular_key.first()) + singular_key.slice(1)
607+
if __get_entry_with_key(position, lower_case_key) != none {
608+
return glspl(lower_case_key, update: update, capitalize: true)
609+
}
592610
} else {
593611
// Default ref
594-
gls(str(key), suffix: r.citation.supplement, update: update)
612+
let _key = str(key)
613+
if __get_entry_with_key(position, _key) != none {
614+
return gls(_key, suffix: r.citation.supplement, update: update)
615+
}
616+
let lower_case_key = lower(_key.first()) + _key.slice(1)
617+
if __get_entry_with_key(position, lower_case_key) != none {
618+
return gls(lower_case_key, suffix: r.citation.supplement, update: update, capitalize: true)
619+
}
595620
}
621+
622+
panic(__error_message(key, __key_not_found))
596623
} else {
597-
r
624+
return r
598625
}
599626
}
600627

@@ -852,6 +879,19 @@
852879
kind: __glossarium_figure,
853880
supplement: "",
854881
)[]#label(entry.key + ":pl")
882+
// Same as above, but for capitalized form, e.g., "@Term"
883+
#if upper(entry.key.first()) != entry.key.first() {
884+
[
885+
#figure(
886+
kind: __glossarium_figure,
887+
supplement: "",
888+
)[]#label(__capitalize(entry.key))
889+
#figure(
890+
kind: __glossarium_figure,
891+
supplement: "",
892+
)[]#label(__capitalize(entry.key) + ":pl")
893+
]
894+
}
855895
]
856896

857897
// default-group-break() -> content

0 commit comments

Comments
 (0)