From 9f4ab25528411dc4ad8eef14ff81ebc2883d0d26 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Wed, 23 Oct 2024 06:19:14 -0600 Subject: [PATCH] refactor(glossy): shrink context{}, misc cleanup --- glossy/0.1.0/README.md | 2 +- glossy/0.1.0/src/gloss.typ | 39 ++++++++++++++++++++------------------ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/glossy/0.1.0/README.md b/glossy/0.1.0/README.md index de8eae8..01fedeb 100644 --- a/glossy/0.1.0/README.md +++ b/glossy/0.1.0/README.md @@ -149,7 +149,7 @@ sections, groups, and entries are displayed. - **section**: This is a main glossary section. It contains multiple glossary groups. - **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 include any of the fields in entry. Contains the following fields: - `key`: The term key. (always present and unique) diff --git a/glossy/0.1.0/src/gloss.typ b/glossy/0.1.0/src/gloss.typ index 5c9a93d..28e1979 100644 --- a/glossy/0.1.0/src/gloss.typ +++ b/glossy/0.1.0/src/gloss.typ @@ -9,6 +9,7 @@ // given an array of dictionaries, make sure each has all the keys we'll // reference, using default values if needed #let __normalize_entries(entry-list) = { + // TODO: panic if key or short missing, all others optional let new-list = () for entry in entry-list { let long = entry.at("long", default: none) @@ -33,8 +34,7 @@ // update our state with a glossary entry #let __add_entry(entry) = { // make sure our final glossary state does not already have this key - let has_key = __gloss_entries.final().at(entry.key, default: false) - if has_key == true { + if __gloss_entries.final().at(entry.key, default: false) == true { panic("Glossary error. Duplicate key: " + entry.key) } @@ -82,7 +82,7 @@ } // update the usage count for a given key -#let __save_count(key, count) = { +#let __save_term_usage(key, count) = { __gloss_used.update(st => { st.insert(key, count) return st; @@ -101,7 +101,7 @@ #let __gls( key, modifiers: array -) = context { +) = { // Get the term let entry = __get_entry(key) @@ -116,7 +116,7 @@ let first = key_index == 0 // 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 let pluralize_term = (singular, plural) => { @@ -166,20 +166,22 @@ let is_long = "long" in modifiers and not is_both let is_short = "short" in modifiers and not is_both and not is_long - // Final display logic - let display = if is_both or is_long or is_short { - // User requested specific behavior via modifiers - select_term(is_long, is_both) - } else { - // Default behavior: show "both" on first use, else "short" - select_term(false, first) + context { + // Final display logic + let display = if is_both or is_long or is_short { + // User requested specific behavior via modifiers + select_term(is_long, is_both) + } else { + // 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 @@ -294,6 +296,7 @@ } } if cur.len() > 0 { + if g == "" { g = none } output.insert(g, cur.sorted(key: e => e.short)) } }