Copyright | Copyright (C) 2013-2023 John MacFarlane |
---|---|
License | BSD3 |
Maintainer | John MacFarlane <[email protected]> |
Stability | alpha |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Text.Pandoc.Walk
Description
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
Synopsis
- class Walkable a b where
- walk :: (a -> a) -> b -> b
- walkM :: (Monad m, Applicative m,