|
| 1 | + |
| 2 | +#import "bibstrings.typ": default-bibstring |
| 3 | + |
| 4 | +#let matches-completely(s, re) = { |
| 5 | + let result = s.match(re) |
| 6 | + |
| 7 | + if result == none { |
| 8 | + return false |
| 9 | + } else { |
| 10 | + // [#result] |
| 11 | + result.start == 0 and result.end == s.len() |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +// Checks whether a string can be converted into an int |
| 17 | +#let is-integer(s) = { |
| 18 | + // s = s.trim() |
| 19 | + |
| 20 | + // TODO: allow negative numbers at some point |
| 21 | + matches-completely(s.trim(), regex("\d+")) |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +/// Concatenates an array of names. If there is only one name, it is returned |
| 26 | +/// unmodified. If there are two names, they are concatenated with the |
| 27 | +/// value `options.list-end-delim-two` ("and"). If there are three names, they |
| 28 | +/// are concatenated with the value `options.list-end-delim-two` (", "), except |
| 29 | +/// the last name is joined with `options.list-end-delim-many` (", and"). |
| 30 | +/// |
| 31 | +/// -> str | content |
| 32 | +#let concatenate-names( |
| 33 | + /// An array of names. Each name can a string or content. If the names are strings, |
| 34 | + /// the function will return a string; if at least one name is content, the |
| 35 | + /// function will return content. |
| 36 | + /// |
| 37 | + /// -> array |
| 38 | + names, |
| 39 | + |
| 40 | + /// Options that control the concatenation. `concatenate-names` defines reasonable |
| 41 | + /// default options for `list-end-delim-two`, |
| 42 | + /// `list-end-delim-two`, `list-end-delim-many`, and `bibstring`. |
| 43 | + /// You can override these options by passing them in a dictionary here. |
| 44 | + /// |
| 45 | + /// -> dictionary |
| 46 | + options: (:), |
| 47 | + |
| 48 | + /// Maximum number of names that is displayed before the name list is truncated |
| 49 | + /// with "et al." See the `maxnames` parameter in @format-reference for details. |
| 50 | + maxnames: 2, |
| 51 | + |
| 52 | + /// Minimum number of names that is guaranteed to be displayed. See the `minnames` |
| 53 | + /// parameter in @format-reference for details. |
| 54 | + minnames: 1 |
| 55 | + ) = { |
| 56 | + let etal = names.len() > maxnames and names.len() > minnames // print "et al.", at least one name dropped |
| 57 | + let num-names = if etal { calc.min(minnames, names.len()) } else { names.len() } // #names that will be printed |
| 58 | + let options = (list-end-delim-two: " and ", list-middle-delim: ", ", list-end-delim-many: ", and ", bibstring: default-bibstring) + options |
| 59 | + |
| 60 | + if etal { |
| 61 | + let nn = names.slice(0, num-names).join(options.list-middle-delim) |
| 62 | + nn + " " + options.bibstring.andothers |
| 63 | + } else { |
| 64 | + if names.len() == 1 { |
| 65 | + names.at(0) |
| 66 | + } else if names.len() == 2 { |
| 67 | + names.at(0) + options.list-end-delim-two + names.at(1) |
| 68 | + } else { |
| 69 | + names.join(options.list-middle-delim, last: options.list-end-delim-many) |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +// Map "modern" Biblatex field names to legacy field names as they |
| 76 | +// might appear in the bib file. Should be complete, as per biblatex.def |
| 77 | +#let field-aliases = ( |
| 78 | + "journaltitle": "journal", |
| 79 | + "langid": "hyphenation", |
| 80 | + "location": "address", |
| 81 | + "institution": "school", |
| 82 | + "annotation": "annote", |
| 83 | + "eprinttype": "archiveprefix", |
| 84 | + "eprintclass": "primaryclass", |
| 85 | + "sortkey": "key", |
| 86 | + "file": "pdf" |
| 87 | +) |
| 88 | + |
| 89 | + |
| 90 | +// Map legacy Bibtex entry types to their "modern" Biblatex names. |
| 91 | +#let type-aliases = ( |
| 92 | + "conference": reference => { reference.insert("entry_type", "inproceedings"); return reference }, |
| 93 | + "electronic": reference => { reference.insert("entry_type", "online"); return reference }, |
| 94 | + "www": reference => { reference.insert("entry_type", "online"); return reference }, |
| 95 | + "mastersthesis": reference => { |
| 96 | + reference.insert("entry_type", "thesis") |
| 97 | + if not "type" in reference.fields { |
| 98 | + reference.fields.insert("type", "mathesis") |
| 99 | + } |
| 100 | + return reference |
| 101 | + }, |
| 102 | + "phdthesis": reference => { |
| 103 | + reference.insert("entry_type", "thesis") |
| 104 | + if not "type" in reference.fields { |
| 105 | + reference.fields.insert("type", "phdthesis") |
| 106 | + } |
| 107 | + return reference |
| 108 | + }, |
| 109 | + "techreport": reference => { |
| 110 | + reference.insert("entry_type", "report") |
| 111 | + reference.fields.insert("type", "techreport") |
| 112 | + return reference |
| 113 | + }, |
| 114 | +) |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +#let fd(reference, field, options, format: x => x) = { |
| 119 | + let legacy-field = field-aliases.at(field, default: "dummy-field-name") |
| 120 | + |
| 121 | + if field in options.at("suppressed-fields", default: ()) { |
| 122 | + return none |
| 123 | + } else if field in reference.fields { |
| 124 | + return format(reference.fields.at(field)) |
| 125 | + } else if legacy-field in reference.fields { |
| 126 | + return format(reference.fields.at(legacy-field)) |
| 127 | + } else { |
| 128 | + return none |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +#let ifdef(reference, field, options, fn) = { |
| 134 | + let value = fd(reference, field, options) |
| 135 | + |
| 136 | + if value == none { none } else { fn(value) } |
| 137 | +} |
| 138 | + |
| 139 | +// Convert an array of (key, value) pairs into a "multimap": |
| 140 | +// a dictionary in which each key is assigned to an array of all |
| 141 | +// the values with which it appeared. |
| 142 | +// |
| 143 | +// Example: (("a", 1), ("a", 2), ("b", 3)) -> (a: (1, 2), b: (3,)) |
| 144 | +#let collect-deduplicate(pairs) = { |
| 145 | + let ret = (:) |
| 146 | + |
| 147 | + for (key, value) in pairs { |
| 148 | + if key in ret { |
| 149 | + ret.at(key).push(value) |
| 150 | + } else { |
| 151 | + ret.insert(key, (value,)) |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return ret |
| 156 | +} |
| 157 | + |
| 158 | + |
| 159 | +/// Wraps a function in `none`-handling code. |
| 160 | +/// `nn(func)` is a function that |
| 161 | +/// behaves like `func` on arguments that are not `none`, |
| 162 | +/// but if the argument is `none`, it simply returns `none`. |
| 163 | +/// Only works for functions `func` that have a single argument. |
| 164 | +/// -> function |
| 165 | +#let nn(func) = { |
| 166 | + it => if it == none { none } else { func(it) } |
| 167 | +} |
| 168 | + |
| 169 | + |
| 170 | + |
0 commit comments