Prevent recursion into returned object
garethstockwell opened this issue · 1 comments
garethstockwell commented
Is there any way to prevent recursion into the object returned by a filter?
For example:
from pandocfilters import Link, Str
def my_filter(key, value, format, meta):
if key == 'Str':
if 'something' in value:
return Link(attr, [Str(value)], 'my_target')
The above will result in infinite recursion into the string. One possible solution would be to introduce pseudo-types NoFilterInline
and NoFilterBlock
, which cause pandocfilters.walk
to abort recursion down a path of the AST:
from pandocfilters import NoFilterInline, Link, Str
def my_filter(key, value, format, meta):
if key == 'Str':
if 'something' in value:
return Link(attr, [NoFilterInline([Str(value)])], 'my_target')
Is there a better solution?
jgm commented
This definitely seems like a flaw in the walking
algorithm; I don't think we should recurse into
newly introduced items like this. I'd rather just
make this the default; perhaps you want to submit a
PR?
By the way, with lua filters it works perfectly!
```lua
function Str(el)
if el.text:match("something") then
return pandoc.Link(pandoc.Str(el.text), "my_target", "", el.attributes)
end
end
```