Skip to content

Commit

Permalink
Merge pull request #33 from St0wy/feat-plural-support
Browse files Browse the repository at this point in the history
Added proper plural support and changed order of abreviations
  • Loading branch information
slashformotion authored Apr 27, 2024
2 parents dfdde95 + 6302af2 commit bf1597a
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 27 deletions.
Binary file added examples/plural-example/main.pdf
Binary file not shown.
48 changes: 48 additions & 0 deletions examples/plural-example/main.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#import "../../glossarium.typ": make-glossary, print-glossary, gls, glspl
// Replace the local import with a import to the preview namespace.
// If you don't know what that mean, please go read typst documentation on how to import packages at https://typst.app/docs/packages/.

#show: make-glossary

#set page(numbering: "1", paper: "a5")

#show link: set text(fill: blue.darken(60%))

I like to eat #glspl("potato"). Yes I still like to eat #glspl("potato").

But I don't like to eat #glspl("dm").

...

I thought about it and now I like to eat #glspl("dm").

I love #glspl("cpu"). I ate a #gls("cpu") once.

Would you eat a #gls("buffer") ? I heard #glspl("buffer") are tasty.

= Glossary
#print-glossary(
((
key: "potato",
short: "potato",
// "plural" will be used when "short" should be pluralized
plural: "potatoes",
desc: [#lorem(10)],
), (
key: "dm",
short: "DM",
long: "diagonal matrix",
// "longplural" will be used when "long" should be pluralized
longplural: "diagonal matrices",
desc: "Probably some math stuff idk",
), (
key: "cpu",
short: "CPU",
long: "Central Processing Unit",
desc: [#lorem(10)],
), (key: "buffer", short: "buffer", desc: "A place to store stuff"),),
// show all term even if they are not referenced, default to true
show-all: true,
// disable the back ref at the end of the descriptions
disable-back-references: true,
)
102 changes: 75 additions & 27 deletions glossarium.typ
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ SOFTWARE.*/

#let __query_labels_with_key(loc, key, before: false) = {
if before {
query(selector(label(__glossary_label_prefix + key)).before(loc, inclusive: false), loc)
query(
selector(label(__glossary_label_prefix + key)).before(loc, inclusive: false),
loc,
)
} else {
query(selector(label(__glossary_label_prefix + key)), loc)
}
Expand All @@ -27,19 +30,19 @@ SOFTWARE.*/
let __glossary_entries = __glossary_entries.final(here())
if key in __glossary_entries {
let entry = __glossary_entries.at(key)

let gloss = __query_labels_with_key(here(), key, before: true)

let is_first = gloss == ()
let entlong = entry.at("long", default: "")
let textLink = if display != none {
[#display]
} else if (is_first or long == true) and entlong != [] and entlong != "" and long != false {
[#entry.short#suffix (#emph(entlong))]
[#entlong (#entry.short#suffix)]
} else {
[#entry.short#suffix]
}

[#link(label(entry.key), textLink)#label(__glossary_label_prefix + entry.key)]
} else {
text(fill: red, "Glossary entry not found: " + key)
Expand All @@ -48,7 +51,47 @@ SOFTWARE.*/
}

// reference to term with pluralisation
#let glspl(key) = gls(key, suffix: "s")
#let glspl(key, long: none) = {
context {
let __glossary_entries = __glossary_entries.final(here())
if key in __glossary_entries {
let entry = __glossary_entries.at(key)

let gloss = __query_labels_with_key(here(), key, before: true)

let suffix = "s";
let is_first = gloss == ()
let entlongplural = entry.at("longplural", default: "");
let entlong = if entlongplural == [] or entlongplural == "" {
let entlong = entry.at("long", default: "");
if entlong != [] and entlong != "" {
[#entlong#suffix]
} else {
entlong
}
} else {
[#entlongplural]
}

let entplural = entry.at("plural", default: "");
let short = if entplural == [] or entplural == "" {
[#entry.short#suffix]
} else {
[#entplural]
}

let textLink = if (is_first or long == true) and entlong != [] and entlong != "" and long != false {
[#entlong (#short)]
} else {
[#short]
}

[#link(label(entry.key), textLink)#label(__glossary_label_prefix + entry.key)]
} else {
text(fill: red, "Glossary entry not found: " + key)
}
}
}

// show rule to make the references for glossarium
#let make-glossary(body) = {
Expand All @@ -66,44 +109,49 @@ SOFTWARE.*/
#let __normalize-entry-list(entry_list) = {
let new-list = ()
for entry in entry_list {
new-list.push(
(
key: entry.key,
short: entry.short,
long: entry.at("long", default: ""),
desc: entry.at("desc", default: ""),
group: entry.at("group", default: ""),
),
)
}
return new-list
new-list.push((
key: entry.key,
short: entry.short,
plural: entry.at("plural", default: ""),
long: entry.at("long", default: ""),
longplural: entry.at("longplural", default: ""),
desc: entry.at("desc", default: ""),
group: entry.at("group", default: ""),
))
}
return new-list
}

#let print-glossary(entry_list, show-all: false, disable-back-references: false, enable-group-pagebreak: false) = {
#let print-glossary(
entry_list,
show-all: false,
disable-back-references: false,
enable-group-pagebreak: false,
) = {
let entries = __normalize-entry-list(entry_list)
__glossary_entries.update(x => {
for entry in entry_list {
x.insert(
entry.key,
entry,
)
x.insert(entry.key, entry)
}

x
})

let groups = entries.map(x => x.at("group", default: "")).dedup()
// move no group to the front
groups.insert(0, "")
groups.pop()

for group in groups.sorted() {
if group != "" [#heading(group, level: 2) ]
for entry in entries.sorted(key: x => x.key) {
if entry.group == group {
[
#show figure.where(kind: __glossarium_figure): it => it.caption
#par(hanging-indent: 1em, first-line-indent: 0em)[
#par(
hanging-indent: 1em,
first-line-indent: 0em,
)[
#figure(
supplement: "",
kind: __glossarium_figure,
Expand Down Expand Up @@ -146,6 +194,6 @@ SOFTWARE.*/
]
}
}
if enable-group-pagebreak {pagebreak(weak: true)}
if enable-group-pagebreak { pagebreak(weak: true) }
}
};

0 comments on commit bf1597a

Please sign in to comment.