HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulepandoc-types-1.23.1Haskell2010

Text.Pandoc.Walk

Functions for manipulating Pandoc documents or extracting information from them by walking the Pandoc structure (or intermediate structures like '[Block]' or '[Inline]'. These are faster (by a factor of four or five) than the generic functions defined in Text.Pandoc.Generic.

Here's a simple example, defining a function that replaces all the level 3+ headers in a document with regular paragraphs in ALL CAPS:

import Text.Pandoc.Definition
import Text.Pandoc.Walk
import Data.Char (toUpper)

modHeader :: Block -> Block
modHeader (Header n _ xs) | n >= 3 = Para $ walk allCaps xs
modHeader x = x

allCaps :: Inline -> Inline
allCaps (Str xs) = Str $ map toUpper xs
allCaps x = x

changeHeaders :: Pandoc -> Pandoc
changeHeaders = walk modHeader

query can be used, for example, to compile a list of URLs linked to in a document:

extractURL :: Inline -> [Text]
extractURL (Link _ _ (u,_)) = [u]
extractURL (Image _ _ (u,_)) = [u]
extractURL _ = []

extractURLs :: Pandoc -> [Text]
extractURLs = query extractURL
  • 1 class
  • 24 values
classclass Walkable a b where
#

Methods

  • walk :: (a -> a) -> b -> b

    walk f x walks the structure x (bottom up) and replaces every occurrence of an a with the result of applying f to it.

  • walkM :: (Monad m, Applicative m, Functor m) => (a -> m a) -> b -> m b

    A monadic version of walk.

  • query :: Monoid c => (a -> c) -> b -> c

    query f x walks the structure x (bottom up) and applies f to every a, appending the results.

Instances58Walkable, …
valuewalkCitationM
  1. :: (Walkable a [Inline], Monad m, Applicative m, Functor m)
  2. => a -> m a
  3. -> Citation
  4. -> m Citation
#

Helper method to walk to elements nested below Citation nodes.

The non-inline contents of a citation will remain unchanged during traversal. Only the inline contents, viz. the citation's prefix and postfix, will be traversed further and can thus be changed during this operation.