|
| 1 | + |
| 2 | + |
| 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 | +// Concatenate an array of strings ("A", "B", "C") into "A, B, and C". |
| 26 | +#let concatenate-list(names, options) = { |
| 27 | + let ret = names.at(0) |
| 28 | + |
| 29 | + for i in range(1, names.len()) { |
| 30 | + if type(names.at(i)) != dictionary { // no idea how it would be a dictionary |
| 31 | + if names.len() == 2 { |
| 32 | + ret = ret + options.list-end-delim-two + names.at(i) |
| 33 | + } else if i == names.len()-1 { |
| 34 | + ret = ret + options.list-end-delim-many + names.at(i) |
| 35 | + } else { |
| 36 | + ret = ret + options.list-middle-delim + names.at(i) |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + ret |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +// Map "modern" Biblatex field names to legacy field names as they |
| 46 | +// might appear in the bib file. Should be complete, as per biblatex.def |
| 47 | +#let field-aliases = ( |
| 48 | + "journaltitle": "journal", |
| 49 | + "langid": "hyphenation", |
| 50 | + "location": "address", |
| 51 | + "institution": "school", |
| 52 | + "annotation": "annote", |
| 53 | + "eprinttype": "archiveprefix", |
| 54 | + "eprintclass": "primaryclass", |
| 55 | + "sortkey": "key", |
| 56 | + "file": "pdf" |
| 57 | +) |
| 58 | + |
| 59 | + |
| 60 | +// Map legacy Bibtex entry types to their "modern" Biblatex names. |
| 61 | +#let type-aliases = ( |
| 62 | + "conference": reference => { reference.insert("entry_type", "inproceedings"); return reference }, |
| 63 | + "electronic": reference => { reference.insert("entry_type", "online"); return reference }, |
| 64 | + "www": reference => { reference.insert("entry_type", "online"); return reference }, |
| 65 | + "mastersthesis": reference => { |
| 66 | + reference.insert("entry_type", "thesis") |
| 67 | + if not "type" in reference.fields { |
| 68 | + reference.fields.insert("type", "mathesis") |
| 69 | + } |
| 70 | + return reference |
| 71 | + }, |
| 72 | + "phdthesis": reference => { |
| 73 | + reference.insert("entry_type", "thesis") |
| 74 | + if not "type" in reference.fields { |
| 75 | + reference.fields.insert("type", "phdthesis") |
| 76 | + } |
| 77 | + return reference |
| 78 | + }, |
| 79 | + "techreport": reference => { |
| 80 | + reference.insert("entry_type", "report") |
| 81 | + reference.fields.insert("type", "techreport") |
| 82 | + return reference |
| 83 | + }, |
| 84 | +) |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +#let fd(reference, field, options, format: x => x) = { |
| 89 | + let legacy-field = field-aliases.at(field, default: "dummy-field-name") |
| 90 | + |
| 91 | + if field in options.at("suppressed-fields", default: ()) { |
| 92 | + return none |
| 93 | + } else if field in reference.fields { |
| 94 | + return format(reference.fields.at(field)) |
| 95 | + } else if legacy-field in reference.fields { |
| 96 | + return format(reference.fields.at(legacy-field)) |
| 97 | + } else { |
| 98 | + return none |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +#let ifdef(reference, field, options, fn) = { |
| 104 | + let value = fd(reference, field, options) |
| 105 | + |
| 106 | + if value == none { none } else { fn(value) } |
| 107 | +} |
| 108 | + |
| 109 | +// Convert an array of (key, value) pairs into a "multimap": |
| 110 | +// a dictionary in which each key is assigned to an array of all |
| 111 | +// the values with which it appeared. |
| 112 | +// |
| 113 | +// Example: (("a", 1), ("a", 2), ("b", 3)) -> (a: (1, 2), b: (3,)) |
| 114 | +#let collect-deduplicate(pairs) = { |
| 115 | + let ret = (:) |
| 116 | + |
| 117 | + for (key, value) in pairs { |
| 118 | + if key in ret { |
| 119 | + ret.at(key).push(value) |
| 120 | + } else { |
| 121 | + ret.insert(key, (value,)) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return ret |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | +/// Wraps a function in `none`-handling code. |
| 130 | +/// `nn(func)` is a function that |
| 131 | +/// behaves like `func` on arguments that are not `none`, |
| 132 | +/// but if the argument is `none`, it simply returns `none`. |
| 133 | +/// Only works for functions `func` that have a single argument. |
| 134 | +/// -> function |
| 135 | +#let nn(func) = { |
| 136 | + it => if it == none { none } else { func(it) } |
| 137 | +} |
| 138 | + |
| 139 | + |
| 140 | + |
0 commit comments