jgm/pandoc-types

Work with a StateT transformator

Closed this issue · 4 comments

It would be nice if we can also use a StateT monad transformer when walking through the AST and thus keep track of the section, and update the state based on content that we see when "walking" over the tree.

jgm commented

walkM in Text.Pandoc.Walk works for any monad. So yes, you can use it with a state monad or something else with StateT. Am I missing something in your question?

After rereading, I realized it was missing the most important part: the toJSONFilter which does not seem to allow effectively walk over this.

The implementation looks like

instance (Walkable [a] Pandoc) => ToJSONFilter (a -> IO [a]) where
toJSONFilter f = BL.getContents >>=
(walkM (fmap concat . mapM f) :: Pandoc -> IO Pandoc) .
either error id . eitherDecode' >>=
BL.putStr . encode

so probably generalizing it to any monad transformer with IO as the "outer" one is possible. I will take a look into it.

jgm commented

Yes, maybe we could generalize these to any MonadIO instance.
An alternative approach that should work already would be to use the IO Monad and an IORef for state.

jgm commented

Closed by #105.