chore: remove preview/ directory

This commit is contained in:
swaits committed 2024-12-10 22:21:15 +00:00
1 parent 78c5b8a000
commit 274e9bad2f
169 files changed

No files matched your search

+466
View File
@@ -0,0 +1,466 @@
#import "./themes.typ": *
#import "./utils.typ": *
#let __gloss_entries = state("__gloss_entries", (:))
#let __gloss_used = state("__gloss_used", (:))
#let __gloss_label_prefix = "__gloss:"
// Normalizes a dictionary entry by ensuring all required and optional keys exist
// with appropriate default values.
//
// Parameters:
// entry (dictionary): Input dictionary containing at least 'short' and optionally
// 'long', 'plural', 'longplural', 'description', and 'group'
//
// Returns:
// dictionary: Normalized dictionary with all expected keys populated
//
// Throws:
// - If 'short' key is missing
// - If 'short' value is not a string
// - If 'short' value is an empty string
//
#let __normalize_entry(entry) = {
// Validate required fields
if not "short" in entry {
panic("Entry must contain a 'short' key")
}
if type(entry.short) != "string" {
panic("Entry 'short' must be a string")
}
if entry.short.trim() == "" {
panic("Entry 'short' cannot be empty")
}
// Extract values with defaults, using consistent none for missing optionals
let long = entry.at("long", default: none)
// Return the normalized entry
(
short: entry.short,
plural: entry.at("plural", default: __pluralize(entry.short)),
long: long,
longplural: entry.at("longplural", default: __pluralize(long)),
description: entry.at("description", default: none),
group: entry.at("group", default: "")
)
}
// Checks if a key exists in the glossary state.
//
// Parameters:
// key (string): The key to look up in the glossary entries
//
// Returns:
// boolean: True if the key exists in the glossary, false otherwise
//
#let __has_entry(key) = {
key in __gloss_entries.final().keys()
}
// Updates the glossary state by adding or updating an entry.
//
// Parameters:
// key (string): The key under which to store the entry
// entry (dictionary): The normalized glossary entry to store
//
// Returns:
// none: Updates state as a side effect
//
#let __add_entry(key, entry) = {
__gloss_entries.update(state => {
state.insert(key, entry)
state
})
}
// Retrieves a glossary entry from the state.
//
// Parameters:
// key (string): The key of the entry to retrieve
//
// Returns:
// dictionary: The glossary entry associated with the key
//
// Throws:
// - If the key does not exist in the glossary
//
#let __get_entry(key) = {
let entries = __gloss_entries.final()
if key not in entries.keys() {
panic("Glossary error: Missing key '" + key + "'")
}
entries.at(key)
}
// Creates a prefixed label string for dictionary entries and term usage.
//
// Parameters:
// key (string): The glossary entry key
//
// Returns:
// string: The prefixed label string for dictionary entries
//
#let __dict_label_str(key) = {
__gloss_label_prefix + key
}
// Creates a label object for dictionary entries.
//
// Parameters:
// key (string): The glossary entry key
//
// Returns:
// label: A Typst label object for the dictionary entry
//
#let __dict_label(key) = {
label(__dict_label_str(key))
}
// Creates a prefixed label string for term usage in documents.
//
// Parameters:
// key (string): The glossary entry key
// index (int): The occurrence index of the term in the document
//
// Returns:
// string: The prefixed label string for term usage
//
#let __term_label_str(key, index) = {
__gloss_label_prefix + key + "." + str(index)
}
// Creates a label object for term usage in documents.
//
// Parameters:
// key (string): The glossary entry key
// index (int): The occurrence index of the term in the document
//
// Returns:
// label: A Typst label object for the term usage
//
#let __term_label(key, index) = {
label(__term_label_str(key, index))
}
// Updates the term usage count in the glossary state.
//
// Parameters:
// key (string): The glossary entry key
// count (int): The number of times the term has been used
//
// Returns:
// none: Updates state as a side effect
//
#let __save_term_usage(key, count) = {
__gloss_used.update(state => {
state.insert(key, count)
state
})
}
// Renders a glossary term with various formatting options.
//
// This function handles the display of terms in the document, managing both first
// and subsequent uses, pluralization, capitalization, and different display forms
// (short, long, or combined forms).
//
// Parameters:
// key (string): The glossary entry key to render
// modifiers (array): Array of modifier strings that control term rendering:
// - "cap": Capitalize the first letter of the term
// - "pl": Use plural form of the term
// - "both": Show "Long form (short form)"
// - "short": Show only the short form
// - "long": Show only the long form
// - "def" or "desc": Show the term's description instead
//
// Returns:
// content: Formatted term with appropriate labels and metadata
//
// Behavior:
// - Without modifiers, first use shows "Long form (short form)", subsequent uses show short form
// - Explicit modifiers override the default first-use behavior
// - If long form is requested but unavailable, falls back to short form
// - Maintains usage counter for term references
//
#let __gls(key, modifiers: array) = {
// Fetch the entry and prepare label
let entry = __get_entry(key)
let entry_label = __dict_label_str(key)
// Handle description mode
if "def" in modifiers or "desc" in modifiers {
return [#entry.description]
}
// Track term usage
let entry_counter = counter(entry_label)
entry_counter.step()
let key_index = entry_counter.get().first()
let first = key_index == 0
__save_term_usage(key, key_index + 1)
// Helper: Apply pluralization if requested
let pluralize_term = (singular, plural) => {
if "pl" in modifiers and plural != none { plural } else { singular }
}
// Helper: Apply capitalization if requested
let capitalize_term = (term) => {
if "cap" in modifiers { upper(term.first()) + term.slice(1) } else { term }
}
// Helper: Apply both pluralization and capitalization
let format_term = (term, plural) => {
capitalize_term(pluralize_term(term, plural))
}
// Helper: Select and format the appropriate term form
let select_term = (is_long_mode, use_both) => {
let short_form = format_term(entry.short, entry.plural)
let long_form = if entry.long != none {
format_term(entry.long, entry.longplural)
} else {
none
}
if use_both and long_form != none {
[#long_form (#short_form)]
} else if is_long_mode and long_form != none {
[#long_form]
} else {
[#short_form]
}
}
// Determine display mode from modifiers
let is_both = "both" in modifiers
let is_long = "long" in modifiers and not is_both
let is_short = "short" in modifiers and not is_both and not is_long
context {
// Generate final display based on modifiers or default behavior
let display = if is_both or is_long or is_short {
select_term(is_long, is_both)
} else {
select_term(false, first)
}
// Emit term with metadata and usage label
[#display#metadata(display)#__term_label(key, key_index)]
}
}
// 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.
//
// Parameters:
// key (string): The glossary entry key
// count (int): Number of times the term appears in the document
//
// Returns:
// content: Comma-separated list of linked page numbers
//
// Example output: "1, 3, 5" where each number links to the term usage on that page
//
#let __create_backlinks(key, count) = {
// Convert term (key + count) to labels with their displayed page numbers
let page_labels = range(count).map(i => {
let label = __term_label(key, i)
let loc = locate(label)
(
label,
numbering(__default(loc.page-numbering(), "1"), ..counter(page).at(loc))
)
})
// Filter out repeated page numbers using a regular loop
let seen = (:)
let singulated = ()
for (label, page) in page_labels {
if not seen.keys().contains(page) {
seen.insert(page, true)
singulated.push((label, page))
}
}
// Convert resulting set to array of links
singulated.map(((label, page)) => link(label, page)).join(", ")
}
// Initializes the glossary system and sets up term reference handling.
//
// This function is typically used with `#show: init-glossary`. It performs
// three main tasks:
// 1. Validates and loads glossary entries into the state
// 2. Sets up reference handling to intercept and format term usage
// 3. Applies custom term formatting if provided
//
// Parameters:
// entries (dictionary): Dictionary of glossary entries where:
// - keys are term identifiers
// - values are entry dictionaries containing term details
// show-term ((content) => content): Optional function to customize term rendering
// Default: Returns term content unchanged
// body (content): Document content to process
//
// Returns:
// content: Processed document content with glossary functionality enabled
//
// Throws:
// - If entries parameter is not a dictionary
//
#let init-glossary(
entries,
show-term: (term-body) => term-body,
body
) = context {
// Validate entries parameter type
if type(entries) != dictionary {
panic("entries must be a dictionary, instead got: " + repr(entries))
}
// Process and store each glossary entry
for (key, entry) in entries {
__add_entry(key, __normalize_entry(entry))
}
// Set up reference handling for glossary terms
show ref: r => {
let (key, ..modifiers) = str(r.target).split(":")
if __has_entry(key) {
show-term(__gls(key, modifiers: modifiers))
} else {
r
}
}
body
}
// Renders a complete glossary with customizable formatting and grouping.
//
// The glossary displays all used terms, optionally grouped by category, with page
// references back to term usage. The appearance is controlled by a theme that
// defines how sections, groups, and entries are formatted.
//
// Parameters:
// title (string): Glossary section title (default: "Glossary")
// theme (dictionary): Controls glossary appearance with three functions:
// - section(title, body): Renders the main glossary section
// - title: The glossary title
// - body: Content containing all groups and entries
// - group(name, index, total, body): Renders a group of related terms
// - name: Group name
// - index: Zero-based group index
// - total: Total number of groups
// - body: Content containing the group's entries
// - entry(entry, index, total): Renders a single glossary entry
// - entry: Dictionary containing:
// - short: Short form of term
// - long: Long form of term (optional)
// - description: Term description (optional)
// - label: Term's dictionary label
// - pages: 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
// If empty, includes all groups
//
// Returns:
// content: Formatted glossary content
//
// Throws:
// - If a requested group doesn't exist
//
#let glossary(
title: "Glossary",
theme: theme-twocol,
ignore-case: false,
groups: (),
) = context {
// Collect and organize entries by group
let output = (:)
let all_entries = __gloss_entries.final()
let all_used = __gloss_used.final()
// Determine which groups to process
let all_groups = all_entries
.values()
.map(e => e.at("group"))
.dedup()
.sorted()
let target_groups = if groups.len() == 0 {
all_groups
} else {
// Validate requested groups exist
for g in groups {
if g not in all_groups {
panic("Requested group not found: " + g)
}
}
groups
}
// Process entries group by group
for group in target_groups {
let current_entries = ()
// Collect all used entries for this group
for (key, count) in all_used {
let entry = all_entries.at(key)
if entry.at("group") == group {
current_entries.push((
short: entry.at("short"),
long: entry.at("long"),
description: entry.at("description"),
label: [#metadata(key)#__dict_label(key)],
pages: __create_backlinks(key, count)
))
}
}
// Add non-empty groups to output
if current_entries.len() > 0 {
group = if group == none { "" } else { group }
// sort entries by case insensitivity if requested
let sorted_entries = current_entries
// 1. create array of tuples with (lower [if ignore-case], entry)
.map(e => { if ignore-case { (lower(e.short), e) } else { (e.short, e) } })
// 2. sort the tuples (by first element then second)
.sorted(key: t => t.first())
// 3. strip away the tuple's first element, leaving an array of entries
.map(t => t.last())
// add entries to this group's output map
output.insert(group, sorted_entries)
}
}
// Render the glossary using the theme
let group_index = 0
(theme.section)(
title,
for (group, entries) in output {
(theme.group)(
group,
group_index,
output.len(),
for (i, entry) in entries.enumerate() {
(theme.entry)(entry, i, entries.len())
}
)
group_index += 1
}
)
}
+6
View File
@@ -0,0 +1,6 @@
#import "themes/academic.typ": theme-academic
#import "themes/basic.typ": theme-basic
#import "themes/chicago-index.typ": theme-chicago-index
#import "themes/compact.typ": theme-compact
#import "themes/table.typ": theme-table
#import "themes/twocol.typ": theme-twocol
+50
View File
@@ -0,0 +1,50 @@
// Inspired by academic textbook glossaries
#let theme-academic = (
section: (title, body) => {
set text(font: "New Computer Modern")
heading(level: 1, title)
v(1em)
body
},
group: (name, index, total, body) => {
if name != "" and total > 1 {
v(1.5em)
align(left, text(weight: "bold", size: 1.2em, name))
v(0.75em)
line(length: 100%, stroke: 0.5pt)
v(0.75em)
}
body
},
entry: (entry, index, total) => {
let short-display = text(weight: "bold", entry.short)
let long-display = if entry.long == none {
[]
} else {
[. #entry.long]
}
let description = if entry.description == none {
[]
} else {
[. #entry.description]
}
block(
below: 1em,
text(
size: 0.95em,
{
grid(
columns: (1fr, auto),
gutter: 0.75em,
[#short-display#long-display#description],
text(fill: rgb("#666666"), entry.pages)
)
}
)
)
},
)
+70
View File
@@ -0,0 +1,70 @@
// Theme definition for a basic single-column glossary layout
//
// This theme provides a traditional glossary format with:
// 1. Bold terms
// 2. Indented descriptions
// 3. Simple page references
// 4. Optional group headings
//
#let theme-basic = (
// Renders the main glossary section as a single column
// Parameters:
// title: The glossary section title
// body: Content containing all groups and entries
section: (title, body) => {
heading(level: 1, title)
body
},
// Renders a group of related glossary terms
// Parameters:
// name: Group name (empty string for ungrouped terms)
// index: Zero-based group index
// total: Total number of groups
// body: Content containing the group's entries
group: (name, index, total, body) => {
if name != "" and total > 1 {
heading(level: 2, name)
}
body
},
// Renders a single glossary entry with term, definition, and page references
// Parameters:
// entry: Dictionary containing term data:
// - short: Short form of term
// - long: Long form of term (optional)
// - description: Term description (optional)
// - label: Term's dictionary label
// - pages: Linked page numbers where term appears
// index: Zero-based entry index within group
// total: Total entries in group
entry: (entry, index, total) => {
// Format the term parts
let term = text(weight: "bold", entry.short)
let long-form = if entry.long == none {
[]
} else {
[, #entry.long]
}
// Format the description with proper spacing
let description = if entry.description == none {
[]
} else {
[: #entry.description]
}
// Create the complete entry with hanging indent
block(
spacing: 0.5em,
pad(
left: 1em,
bottom: 0.5em,
block(
[#term#entry.label#long-form#description #h(1em) (pp. #entry.pages)]
)
)
)
},
)
+47
View File
@@ -0,0 +1,47 @@
// Inspired by the Chicago Manual of Style's index format
#let theme-chicago-index = (
section: (title, body) => {
set par(hanging-indent: 1em)
heading(level: 1, smallcaps(title))
body
},
group: (name, index, total, body) => {
if name != "" and total > 1 {
v(1em)
text(weight: "medium", style: "italic", name)
v(0.5em)
}
body
},
entry: (entry, index, total) => {
let short-display = text(weight: "regular", entry.short)
let long-display = if entry.long == none {
[]
} else {
[ — #entry.long] // Using em-dash for Chicago style
}
let description = if entry.description == none {
[]
} else {
text(style: "italic", [: #entry.description])
}
block(
below: 0.65em,
text(
size: 0.9em,
{
grid(
columns: (1fr, auto),
gutter: 1em,
[#short-display#entry.label#long-display#description],
[#entry.pages]
)
}
)
)
},
)
+91
View File
@@ -0,0 +1,91 @@
// Theme definition for a compact glossary layout optimized for space efficiency
//
// This theme provides a dense but readable format with:
// 1. Terms and definitions on same line
// 2. Smaller font size
// 3. Minimal vertical spacing
// 4. Left-aligned groups for quick scanning
// 5. Condensed page references
//
#let theme-compact = (
// Renders the main glossary section with minimal spacing
// Parameters:
// title: The glossary section title
// body: Content containing all groups and entries
section: (title, body) => {
set par(leading: 0.65em)
heading(level: 1, outlined: false, text(weight: "bold", size: 1.1em, title))
body
},
// Renders a group of related glossary terms
// Parameters:
// name: Group name (empty string for ungrouped terms)
// index: Zero-based group index
// total: Total number of groups
// body: Content containing the group's entries
group: (name, index, total, body) => {
if name != "" and total > 1 {
block(
spacing: 0.5em,
pad(
top: 0.5em,
text(weight: "bold", size: 0.9em, name)
)
)
}
body
},
// Renders a single glossary entry with term, definition, and page references
// Parameters:
// entry: Dictionary containing term data:
// - short: Short form of term
// - long: Long form of term (optional)
// - description: Term description (optional)
// - label: Term's dictionary label
// - pages: Linked page numbers where term appears
// index: Zero-based entry index within group
// total: Total entries in group
entry: (entry, index, total) => {
// Format term components with minimal spacing
let term = text(
size: 0.65em,
weight: "medium",
fill: gray.darken(60%),
entry.short
)
let long-form = if entry.long == none {
[]
} else {
text(size: 0.65em, fill: gray.darken(20%), [ (#entry.long)])
}
let description = if entry.description == none {
[]
} else {
text(size: 0.65em, [· #entry.description])
}
// Create the complete entry with tight spacing
block(
spacing: 0.4em,
grid(
columns: (auto, 1fr, auto),
align: left+bottom,
gutter: 0.5em,
// Term and description column
box[#term#entry.label#long-form #description],
// Dots....
repeat(h(0.25em) + text(fill: gray, ".") + h(0.25em)),
// Page references with smaller font
text(
size: 0.6em,
fill: gray.darken(20%),
entry.pages
)
)
)
},
)
+32
View File
@@ -0,0 +1,32 @@
// A tabular theme courtesy of [@Fevol](https://github.com/Fevol)
#let theme-table = (
section: (title, body) => {
heading(level: 1, title)
body
},
group: (name, index, total, body) => {
if name != "" and total > 1 {
heading(level: 2, name)
}
table(columns: 4,
stroke: none,
inset: (x, y) => {
if (x == 0) {
(left: 0pt, rest: 5pt)
} else if (x == 3) {
(right: 0pt, rest: 5pt)
} else {
5pt
}
},
table.header([*Abbreviation*], [*Full Name*], [*Description*], [*Pages*]),
..body
)
},
entry: (entry, index, total) => {
(entry.short, entry.long, entry.description, entry.pages)
}
)
+72
View File
@@ -0,0 +1,72 @@
// Theme definition for a two-column glossary layout with hierarchical sections
//
// This theme provides formatting for three levels of glossary content:
// 1. Section: The overall glossary container
// 2. Groups: Optional categorization of related terms
// 3. Entries: Individual glossary terms and their definitions
//
#let theme-twocol = (
// Renders the main glossary section as a two-column layout
// Parameters:
// title: The glossary section title
// body: Content containing all groups and entries
section: (title, body) => {
set par.line(numbering: none)
heading(level: 1, title)
columns(2, body)
},
// Renders a group of related glossary terms
// Parameters:
// name: Group name (empty string for ungrouped terms)
// index: Zero-based group index
// total: Total number of groups
// body: Content containing the group's entries
group: (name, index, total, body) => {
if name != "" and total > 1 {
heading(level: 2, name)
}
body
},
// Renders a single glossary entry with term, definition, and page references
// Parameters:
// entry: Dictionary containing term data:
// - short: Short form of term
// - long: Long form of term (optional)
// - description: Term description (optional)
// - label: Term's dictionary label
// - pages: Linked page numbers where term appears
// index: Zero-based entry index within group
// total: Total entries in group
entry: (entry, index, total) => {
// Format the short form and optional long form of the term
let short-display = text(weight: "regular", entry.short)
let long-display = if entry.long == none {
[]
} else {
[#h(0.25em) -- #entry.long]
}
// Format the optional description
let description = if entry.description == none {
[]
} else {
[: #entry.description]
}
// Render the complete entry with dotted leader line to page numbers
text(
size: 0.75em,
weight: "light",
grid(
columns: (auto,1fr,1em,auto),
align: (left, center, center, right),
[#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
)
)
},
)
+38
View File
@@ -0,0 +1,38 @@
// Returns the provided value if it is not none, otherwise returns the default value
// Parameters:
// val: The value to check
// default: The fallback value to use if val is none
// Returns: val if not none, otherwise default
#let __default(val, default) = if val == none { default } else { val }
// Converts an English word to its plural form following standard English pluralization rules
// Parameters:
// word: The singular word to pluralize
// Returns: The pluralized form of the word, or none if input is none
#let __pluralize(word) = {
// early exit with invalid input
if word == none {
return none
}
// Define helper functions for checking endings
let ends_with = (suffix) => lower(word).ends-with(suffix)
let ends_with_any = (suffixes) => suffixes.any((suffix) => ends_with(suffix))
// Handle special cases for "es" endings (s, x, z, sh, ch)
if ends_with_any(( "s", "x", "z", "sh", "ch" )) {
word + "es"
} else if ends_with("y") and not ends_with_any(( "a", "e", "i", "o", "u" )) {
// Convert y to ies when y follows a consonant
word.slice(0, -1) + "ies"
} else if ends_with("f") {
// Convert f to ves
word.slice(0, -1) + "ves"
} else if ends_with("fe") {
// Convert fe to ves
word.slice(0, -2) + "ves"
} else {
// Default case: just add s
word + "s"
}
}