commit ec08fb3bbf2ae197e13f5558111ed198a55f69f1 Author: Arthur Grisel-Davy Date: Sat Jul 1 11:32:58 2023 -0400 initial commit diff --git a/acrobat.typ b/acrobat.typ new file mode 100644 index 0000000..9ed8497 --- /dev/null +++ b/acrobat.typ @@ -0,0 +1,125 @@ + + + +#import "acronyms.typ": acronyms + +#let reset-acronym(term) = { + // Reset a specific acronym. It will be expanded on next use. + if term in acronyms{ + state("acronym-state-" + term, false).update(false) + } +} + +#let reset-all-acronyms() = { + // Reset all acronyms. They will all be expanded on the next use. + for acr in acronyms.keys() { + state("acronym-state-" + acr, false).update(false) + } +} + +#let get-def(plural: false, acr) = { + // Return the definition of an acronyms + // Args: + // - plural: If true, return the plural form of the definition. Default to false. + + if acr not in acronyms.keys(){ + panic("Acronym "+acr+" is not defined.") + } + let value = acronyms.at(acr) + let definition = "" + + if plural{ + if type(value) == "string"{ + definition = value + "s" + }else if type(value) == "array"{ + if value.len() == 1{ + definition = value.at(0) + "s" + }else if value.len() > 1{ + definition = value.at(1) + }else{ + panic("Definition for acronym "+acr+" is an empty array.") + } + } + }else{ + if type(value) == "string"{ + definition = value + }else if type(value) == "array" and value.len() > 0{ + definition = value.at(0) + }else{ + panic("Definition of "+acr+" is not properly defined. It should be a string or an array of strings.") + } + } + definition +} + +#let acr(acr) = { + // Display an acronym in the singular form. Expand if used for the first time. + if acr in acronyms{ + // Grab definition of the term + let definition = get-def(acr) + // Generate the key associated with this term + let state-key = "acronym-state-" + acr + // Create a state to keep track of the expansion of this acronym + state(state-key,false).display(seen => {if seen{acr}else{[#definition (#acr)]}}) + // Update state to true as it has just been defined + state(state-key, false).update(true) + }else{ + panic("Acronym "+acr+" is not defined.") + } +} + +#let acrpl(acr) = { + // Display an acronym in the plural form. Expand if used for the first time. + if acr in acronyms{ + // Grab definition of the term + let definition = get-def(plural: true, acr) + // Generate the key associated with this term + let state-key = "acronym-state-" + acr + // Create a state to keep track of the expansion of this acronym + state(state-key,false).display(seen => {if seen{acr+"s"}else{[#definition (#acr\s)]}}) + // Update state to true as it has just been defined + state(state-key, false).update(true) + }else{ + panic("Acronym "+acr+" is not defined.") + } +} + +#let print-index(level: 1, outlined: false, all: true) = [ + //Print an index of all the acronyms and their definitions. + // Args: + // level: level of the heading. Default to 1. + // outlined: make the index section outlined. Default to false + // all: print all acronyms even if not used in the text. Default to true. + + + #heading(level: level, outlined: outlined)[Acronym index:] + #if all { + for acr in acronyms.keys(){ + let definition = "" + if type(acronyms.at(acr)) == "string"{ + definition = acronyms.at(acr) + }else{ + definition = acronyms.at(acr).at(0) + } + [*#acr:* #definition\ ] + } + }else{ + + for acr in acronyms.keys(){ + let state-key = "acronym-state-" + acr + let s = state(state-key,false) + let definition = "" + if type(acronyms.at(acr)) == "string"{ + definition = acronyms.at(acr) + }else{ + definition = acronyms.at(acr).at(0) + } + locate(loc => { + if s.final(loc){ + [*#acr:* #definition\ ] + }else{ + [] + }}) + } + } +]