From 03e8e62c705b037d10599b00fc11b68335855cb4 Mon Sep 17 00:00:00 2001 From: Thijs Paelman Date: Sun, 25 May 2025 18:56:39 +0200 Subject: [PATCH] feat(glossy): Return array of pages in theme Instead of returning an opaque context element with a fixed list of the pages, return an array of context elements with each a page (already unique!). This is conceptually cleaner. Now it's the themes responsability to join the array of unique pages however it wants. Most times, this will be a `.join(", ")`, but could be e.g. `.first()` if the theme only wants to show the page of the first occurance of this term. Also update the documentation. Breaking API change in the themes. No visual differences (tests remain the same), but there are binary differences. --- glossy/README.md | 4 ++-- glossy/src/gloss.typ | 16 ++++++++-------- glossy/src/themes/academic.typ | 2 +- glossy/src/themes/basic.typ | 4 ++-- glossy/src/themes/chicago-index.typ | 2 +- glossy/src/themes/compact.typ | 4 ++-- glossy/src/themes/table.typ | 2 +- glossy/src/themes/twocol.typ | 4 ++-- glossy/tests/abbreviated-syntax/test.typ | 2 +- glossy/tests/demo/test.typ | 2 +- 10 files changed, 21 insertions(+), 21 deletions(-) diff --git a/glossy/README.md b/glossy/README.md index 4c798aa..f743c41 100644 --- a/glossy/README.md +++ b/glossy/README.md @@ -304,7 +304,7 @@ Customize glossary appearance by defining a theme with three functions: columns: (auto, 1fr, auto), output, repeat([#h(0.25em) . #h(0.25em)]), - entry.pages, + entry.pages.join(", "), ) ) } @@ -317,7 +317,7 @@ Entry fields available to themes: - `long`: Long form (can be `none`) - `description`: Term description (can be `none`) - `label`: Term's dictionary label -- `pages`: Linked page numbers where term appears +- `pages`: Array of linked page numbers where term appears **NOTE:** If the theme does not emit `entry.label`, linking form terms to their glossary entry will not work. diff --git a/glossy/src/gloss.typ b/glossy/src/gloss.typ index ccc7781..586ba21 100644 --- a/glossy/src/gloss.typ +++ b/glossy/src/gloss.typ @@ -406,20 +406,21 @@ // Creates page number links back to each usage of a glossary term. // -// This function generates a comma-separated list of page numbers, where each -// number links back to a usage of the term in the document. Duplicate page -// numbers are removed to avoid redundancy. +// This function generates an array of page numbers, where each number links +// back to a usage of the term in the document. Duplicate page numers are +// removed to avoid redundancy. // // Parameters: // key (string): The glossary entry key // // Returns: -// content: Comma-separated list of linked page numbers +// array: Linked page numbers // -// Example output: "1, 3, 5" where each number links to the term usage on that page +// Example output after a typical .join(", "): +// "1, 3, 5" where each number links to the term usage on that page // #let __create_backlinks(key) = { - return context query(__term_label(key)) // find all reference + return query(__term_label(key)) // find all reference .map(meta => { // extract location and page number (or symbol) let loc = meta.location() let page = numbering(__default(loc.page-numbering(), "1"), ..counter(page).at(loc)) @@ -427,7 +428,6 @@ }) .dedup(key: ((loc, page)) => page) // deduplicate by page .map(((loc, page)) => link(loc, page)) // create links - .join(", ") } // Formats a term string based on the specified mode. @@ -575,7 +575,7 @@ // - long: Long form of term (optional) // - description: Term description (optional) // - label: Term's dictionary label -// - pages: Linked page numbers where term appears +// - pages: Array of linked page numbers where term appears // - index: Zero-based entry index within group // - total: Total entries in group // groups (array): Optional list of groups to include diff --git a/glossy/src/themes/academic.typ b/glossy/src/themes/academic.typ index 975c8ad..8da8513 100644 --- a/glossy/src/themes/academic.typ +++ b/glossy/src/themes/academic.typ @@ -40,7 +40,7 @@ columns: (1fr, auto), gutter: 0.75em, [#short-display#long-display#description#entry.label], - text(fill: rgb("#666666"), entry.pages) + text(fill: rgb("#666666"), entry.pages.join(", ")) ) } ) diff --git a/glossy/src/themes/basic.typ b/glossy/src/themes/basic.typ index b756c27..a36f04d 100644 --- a/glossy/src/themes/basic.typ +++ b/glossy/src/themes/basic.typ @@ -36,7 +36,7 @@ // - long: Long form of term (optional) // - description: Term description (optional) // - label: Term's dictionary label - // - pages: Linked page numbers where term appears + // - pages: Array of linked page numbers where term appears // index: Zero-based entry index within group // total: Total entries in group entry: (entry, index, total) => { @@ -62,7 +62,7 @@ left: 1em, bottom: 0.5em, block( - [#term#entry.label#long-form#description #h(1em) (pp. #entry.pages)] + [#term#entry.label#long-form#description #h(1em) (pp. #entry.pages.join(", "))] ) ) ) diff --git a/glossy/src/themes/chicago-index.typ b/glossy/src/themes/chicago-index.typ index 006c440..bdfb526 100644 --- a/glossy/src/themes/chicago-index.typ +++ b/glossy/src/themes/chicago-index.typ @@ -38,7 +38,7 @@ columns: (1fr, auto), gutter: 1em, [#short-display#entry.label#long-display#description#entry.label], - [#entry.pages] + [#entry.pages.join(", ")] ) } ) diff --git a/glossy/src/themes/compact.typ b/glossy/src/themes/compact.typ index 86a40e3..238fe94 100644 --- a/glossy/src/themes/compact.typ +++ b/glossy/src/themes/compact.typ @@ -44,7 +44,7 @@ // - long: Long form of term (optional) // - description: Term description (optional) // - label: Term's dictionary label - // - pages: Linked page numbers where term appears + // - pages: Array of linked page numbers where term appears // index: Zero-based entry index within group // total: Total entries in group entry: (entry, index, total) => { @@ -83,7 +83,7 @@ text( size: 0.6em, fill: gray.darken(20%), - entry.pages + entry.pages.join(", ") ) ) ) diff --git a/glossy/src/themes/table.typ b/glossy/src/themes/table.typ index 1dfe1f9..4a9400b 100644 --- a/glossy/src/themes/table.typ +++ b/glossy/src/themes/table.typ @@ -27,6 +27,6 @@ }, entry: (entry, index, total) => { - (entry.short + entry.label, entry.long, entry.description, entry.pages) + (entry.short + entry.label, entry.long, entry.description, entry.pages.join(", ")) } ) diff --git a/glossy/src/themes/twocol.typ b/glossy/src/themes/twocol.typ index 7183e6e..068446b 100644 --- a/glossy/src/themes/twocol.typ +++ b/glossy/src/themes/twocol.typ @@ -36,7 +36,7 @@ // - long: Long form of term (optional) // - description: Term description (optional) // - label: Term's dictionary label - // - pages: Linked page numbers where term appears + // - pages: Array of linked page numbers where term appears // index: Zero-based entry index within group // total: Total entries in group entry: (entry, index, total) => { @@ -65,7 +65,7 @@ [#short-display#entry.label#long-display#description], // Term with label [#repeat(h(0.25em) + "." + h(0.25em))], // Dotted leader line [ . ], // A 1em wide dot so we definitely get some break between term and pages - [#entry.pages] // Page references + [#entry.pages.join(", ")] // Page references ) ) }, diff --git a/glossy/tests/abbreviated-syntax/test.typ b/glossy/tests/abbreviated-syntax/test.typ index 369ec55..91e6019 100644 --- a/glossy/tests/abbreviated-syntax/test.typ +++ b/glossy/tests/abbreviated-syntax/test.typ @@ -79,7 +79,7 @@ which one to display. columns: (auto, 1fr, auto), output, repeat([#h(0.25em) . #h(0.25em)]), - entry.pages, + entry.pages.join(", "), ) ) } diff --git a/glossy/tests/demo/test.typ b/glossy/tests/demo/test.typ index 0a422bb..828f7f5 100644 --- a/glossy/tests/demo/test.typ +++ b/glossy/tests/demo/test.typ @@ -117,7 +117,7 @@ which one to display. columns: (auto, 1fr, auto), output, repeat([#h(0.25em) . #h(0.25em)]), - entry.pages, + entry.pages.join(", "), ) ) }