When writing Lua filter it might be useful to check if code block have some class or not. At the moment the only way to achieve this is to iterate over all elements in block.classes list and see if there is desired class, which is inefficient to do. List approach has linear time complexity, while associative arrays would give a constant. Why not to store them as table?
block.classes = {
["class1"] = true,
["class2"] = true,
-- ...
["classN"] = true,
}
And deletion of the class would be simple:
block.classes["classToDelete"] = nil
The reason is simply, that Lua tables, when used as associative arrays, do not preserve element order. So the sequence class1 class2 class3 could come back as class3 class1 class2, or any other permutation. Positional information is important: e.g., the language of a code block is assumed to be the first class. Hence we need to preserve order.
We faced similar issues with attributes, which we worked around by using some Lua metatable "magic", making it possible to use the attributes object as if it was an associative table. It might make sense to do the same for classes, but I'm not sure.
I suppose you could make it a table where each class maps to an integer denoting its order in the table. This would make checking for the presence of a class fast, but it would slow down other operations (inserting or deleting classes, concatenating them in order). It would also be added complexity, which is always bad and must have a strong justification.
How much of a problem is this in practice? From the user perspective we offer a method already for checking for list membership without explicitly iterating. Of course, it iterates, which is not the fastest method, but iterating through a list with 3-4 members isn't going to be a performance bottleneck for anyone.
Yes, I agree with you, totally forgot about importance of order of classes, so you're right, this won't be a bottleneck.
Btw, what Lua version do pandoc use? It will be nice if it is mentioned in documentation.
Btw, what Lua version do pandoc use? It will be nice if it is mentioned in documentation.
See here: https://stackoverflow.com/a/56087697/2425163. TL;DR: It's Lua 5.3 since pandoc 2.3.1.
Btw, you can also compile pandoc to use system lua if you want.
It's a flag on the hslua package.
Most helpful comment
Btw, you can also compile pandoc to use system lua if you want.
It's a flag on the hslua package.