Currently, manipulating content is very involved. This is mainly caused by the content functions
- taking different amount of positional arguments
fields()
not distinguishing between positional and keyword arguments- not all content functions being accessible (e.g.
sequence
can as far as I know only obtained by hacks like[].func()
)
For example, I’d like to recursively update all strings in a content, which currently requires a lot of special casing and hard to know if you caught all edge cases. This leads to code like this
#let titlecase(data) = {
let update-dict(dict) = {
let new-dict = (:)
for (k, v) in dict.pairs() {
new-dict.insert(k, titlecase(v))
}
return new-dict
}
if type(data) == content {
if data.has("children") {
let new-fields = data.fields()
new-fields.remove("children")
let new-children = data.children.map(titlecase)
return data.func()(new-children, ..update-dict(new-fields))
}
if data.has("body") {
let new-body = titlecase(data.body)
let new-fields = data.fields()
new-fields.remove("body")
return data.func()(new-body, ..update-dict(new-fields))
}
if data.has("text") {
let new-text = titlecase(data.text)
let new-fields = data.fields()
new-fields.remove("text")
return data.func()(new-text, ..update-dict(new-fields))
}
if data.has("base") {
let new-base = titlecase(data.base)
let new-fields = data.fields()
new-fields.remove("base")
return data.func()(new-base, ..update-dict(new-fields))
}
// styled
if data.has("child") {
let new-child = titlecase(data.child)
let new-fields = data.fields()
new-fields.remove("child")
let _ = new-fields.remove("styles")
return data.func()(new-child, data.styles, ..update-dict(new-fields))
}
let new-fields = data.fields()
return data.func()(..update-dict(new-fields))
return data
}
if type(data) == str {
str(_plugin.titlecase(bytes(data)))
} else {
data
}
}
Am I overlooking something that would make this easier? Otherwise, it would be already much more straightforward if content
had an arguments()
function, that returns an arguments
object. That would at least allow an easier reconstruction of the content.