refactor(glossy): shrink context{}, misc cleanup

This commit is contained in:
swaits committed 2024-10-23 06:20:36 -06:00
1 parent 54680f2ed3
commit 9f4ab25528
2 files changed
+22 -19

No files matched your search

+1 -1
View File
@@ -149,7 +149,7 @@ sections, groups, and entries are displayed.
- **section**: This is a main glossary section. It contains multiple glossary groups. - **section**: This is a main glossary section. It contains multiple glossary groups.
- **group**: A group within the main glossary section. Contains multiple - **group**: A group within the main glossary section. Contains multiple
entries. Can be an empty string `""` or `none`. entries. Can be `none`.
- **entry**: A single entry within a group. This is the actual term, and can - **entry**: A single entry within a group. This is the actual term, and can
include any of the fields in entry. Contains the following fields: include any of the fields in entry. Contains the following fields:
- `key`: The term key. (always present and unique) - `key`: The term key. (always present and unique)
+21 -18
View File
@@ -9,6 +9,7 @@
// given an array of dictionaries, make sure each has all the keys we'll // given an array of dictionaries, make sure each has all the keys we'll
// reference, using default values if needed // reference, using default values if needed
#let __normalize_entries(entry-list) = { #let __normalize_entries(entry-list) = {
// TODO: panic if key or short missing, all others optional
let new-list = () let new-list = ()
for entry in entry-list { for entry in entry-list {
let long = entry.at("long", default: none) let long = entry.at("long", default: none)
@@ -33,8 +34,7 @@
// update our state with a glossary entry // update our state with a glossary entry
#let __add_entry(entry) = { #let __add_entry(entry) = {
// make sure our final glossary state does not already have this key // make sure our final glossary state does not already have this key
let has_key = __gloss_entries.final().at(entry.key, default: false) if __gloss_entries.final().at(entry.key, default: false) == true {
if has_key == true {
panic("Glossary error. Duplicate key: " + entry.key) panic("Glossary error. Duplicate key: " + entry.key)
} }
@@ -82,7 +82,7 @@
} }
// update the usage count for a given key // update the usage count for a given key
#let __save_count(key, count) = { #let __save_term_usage(key, count) = {
__gloss_used.update(st => { __gloss_used.update(st => {
st.insert(key, count) st.insert(key, count)
return st; return st;
@@ -101,7 +101,7 @@
#let __gls( #let __gls(
key, key,
modifiers: array modifiers: array
) = context { ) = {
// Get the term // Get the term
let entry = __get_entry(key) let entry = __get_entry(key)
@@ -116,7 +116,7 @@
let first = key_index == 0 let first = key_index == 0
// Count the entry as used so we can link back from glossary // Count the entry as used so we can link back from glossary
__save_count(entry.key, key_index + 1) __save_term_usage(entry.key, key_index + 1)
// Helper: Apply pluralization if needed // Helper: Apply pluralization if needed
let pluralize_term = (singular, plural) => { let pluralize_term = (singular, plural) => {
@@ -166,20 +166,22 @@
let is_long = "long" in modifiers and not is_both let is_long = "long" in modifiers and not is_both
let is_short = "short" in modifiers and not is_both and not is_long let is_short = "short" in modifiers and not is_both and not is_long
// Final display logic context {
let display = if is_both or is_long or is_short { // Final display logic
// User requested specific behavior via modifiers let display = if is_both or is_long or is_short {
select_term(is_long, is_both) // User requested specific behavior via modifiers
} else { select_term(is_long, is_both)
// Default behavior: show "both" on first use, else "short" } else {
select_term(false, first) // Default behavior: show "both" on first use, else "short"
select_term(false, first)
}
// TODO: figure out how to link to __dict_label(key) if the glossary exists
// NOTE: this is a low priority, I don't think it's that important or useful.
// Emit with labels for this instance of the term usage
[#display#metadata(display)#__term_label(key, key_index)]
} }
// TODO: figure out how to link to __dict_label(key) if the glossary exists
// NOTE: this is a low priority, I don't think it's that important or useful.
// Emit with labels for this instance of the term usage
[#display#metadata(display)#__term_label(key, key_index)]
} }
// Create all the backlinks to term uses in a doc, in the form of page numbers // Create all the backlinks to term uses in a doc, in the form of page numbers
@@ -294,6 +296,7 @@
} }
} }
if cur.len() > 0 { if cur.len() > 0 {
if g == "" { g = none }
output.insert(g, cur.sorted(key: e => e.short)) output.insert(g, cur.sorted(key: e => e.short))
} }
} }