-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathacronym-lib.typ
126 lines (106 loc) · 3.09 KB
/
acronym-lib.typ
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#import "locale.typ": ACRONYMS
#import "shared-lib.typ": display, display-link, is-in-dict
#let prefix = "acronym-state-"
#let acros = state("acronyms", none)
#let init-acronyms(acronyms) = {
acros.update(acronyms)
}
#let acrs(acr, plural: false, link: true) = {
if plural {
display("acronyms", acros, acr, acr + "s", link: link)
} else {
display("acronyms", acros, acr, acr, link: link)
}
}
#let acrspl(acr, link: true) = {
acrs(acr, plural: true, link: link)
}
#let acrl(acr, plural: false, link: true) = {
context {
let acronyms = acros.get()
if is-in-dict("acronyms", acros, acr) {
let defs = acronyms.at(acr)
if type(defs) == "string" {
if plural {
display("acronyms", acros, acr, defs + "s", link: link)
} else {
display("acronyms", acros, acr, defs, link: link)
}
} else if type(defs) == "array" {
if defs.len() == 0 {
panic("No definitions found for acronym " + acr + ". Make sure it is defined in the dictionary passed to #init-acronyms(dict)")
}
if plural {
if defs.len() == 1 {
display("acronyms", acros, acr, defs.at(0) + "s", link: link)
} else if defs.len() == 2 {
display("acronyms", acros, acr, defs.at(1), link: link)
} else {
panic("Definitions should be arrays of one or two strings. Definition of " + acr + " is: " + type(defs))
}
} else {
display("acronyms", acros, acr, defs.at(0), link: link)
}
} else {
panic("Definitions should be arrays of one or two strings. Definition of " + acr + " is: " + type(defs))
}
}
}
}
#let acrlpl(acr, link: true) = {
acrl(acr, plural: true, link: link)
}
#let acrf(acr, plural: false, link: true) = {
if plural {
display("acronyms", acros, acr, [#acrlpl(acr) (#acr\s)], link: link)
} else {
display("acronyms", acros, acr, [#acrl(acr) (#acr)], link: link)
}
state(prefix + acr, false).update(true)
}
#let acrfpl(acr, link: true) = {
acrf(acr, plural: true, link: link)
}
#let acr(acr, plural: false, link: true) = {
context {
let seen = state(prefix + acr, false).get()
if seen {
if plural {
acrspl(acr, link: link)
} else {
acrs(acr, link: link)
}
} else {
if plural {
acrfpl(acr, link: link)
} else {
acrf(acr, link: link)
}
}
}
}
#let acrpl(acronym, link: true) = {
acr(acronym, plural: true, link: link)
}
#let print-acronyms(language, acronym-spacing) = {
heading(level: 1, outlined: false, numbering: none)[#ACRONYMS.at(language)]
context {
let acronyms = acros.get()
let acronym-keys = acronyms.keys()
let max-width = 0pt
for acr in acronym-keys {
let result = measure(acr).width
if (result > max-width) {
max-width = result
}
}
let acr-list = acronym-keys.sorted()
for acr in acr-list {
grid(
columns: (max-width + 0.5em, auto),
gutter: acronym-spacing,
[*#acr#label("acronyms-" + acr)*], [#acrl(acr, link: false)],
)
}
}
}