pandoc-types-1.20: Types for representing a structured document

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 (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 = walk modHeader

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

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

extractURLs :: Pandoc -> [String]
extractURLs = query extractURL
Synopsis

# Documentation

class Walkable a b where Source #

Minimal complete definition

Methods

walk :: (a -> a) -> b -> b Source #

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 Source #

A monadic version of walk.

query :: Monoid c => (a -> c) -> b -> c Source #

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

Instances

queryBlock :: (Walkable a Citation, Walkable a [Block], Walkable a [Inline], Monoid c) => (a -> c) -> Block -> c Source #

Perform a query on elements nested below a Block element by querying all directly nested lists of Inlines or Blocks.

queryCitation :: (Walkable a [Inline], Monoid c) => (a -> c) -> Citation -> c Source #

Perform a query on elements nested below a Citation element by querying the prefix and postfix Inline lists.

queryInline :: (Walkable a Citation, Walkable a [Block], Walkable a [Inline], Monoid c) => (a -> c) -> Inline -> c Source #

Perform a query on elements nested below an Inline element by querying nested lists of Inlines, Blocks, or Citations.

queryMetaValue :: (Walkable a MetaValue, Walkable a [Block], Walkable a [Inline], Monoid c) => (a -> c) -> MetaValue -> c Source #

Perform a query on elements nested below a MetaValue element by querying all directly nested lists of Inlines, list of Blocks, or lists or maps of MetaValues.

queryPandoc :: (Walkable a Meta, Walkable a [Block], Monoid c) => (a -> c) -> Pandoc -> c Source #

Query a pandoc element by recursing first into its Meta data and then append the result of recursing into the list of Blocks.

walkBlockM :: (Walkable a [Block], Walkable a [Inline], Monad m, Applicative m, Functor m) => (a -> m a) -> Block -> m Block Source #

Helper method to walk to elements nested below Block nodes.

When walking a block with this function, only the contents of the traversed block element may change. The element itself, i.e. its constructor, its Attr, and its raw text value, will remain unchanged.

walkCitationM :: (Walkable a [Inline], Monad m, Applicative m, Functor m) => (a -> m a) -> Citation -> m Citation Source #

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.

walkInlineM :: (Walkable a Citation, Walkable a [Block], Walkable a [Inline], Monad m, Applicative m, Functor m) => (a -> m a) -> Inline -> m Inline Source #

Helper method to walk to elements nested below Inline nodes.

When walking an inline with this function, only the contents of the traversed inline element may change. The element itself, i.e. its constructor, cannot be changed.

walkMetaValueM :: (Walkable a MetaValue, Walkable a [Block], Walkable a [Inline], Monad f, Applicative f, Functor f) => (a -> f a) -> MetaValue -> f MetaValue Source #

Helper method to walk to elements nested below MetaValue nodes.

When walking a meta value with this function, only the contents of the traversed meta value element may change. MetaBool and MetaString will always remain unchanged.

walkPandocM :: (Walkable a Meta, Walkable a [Block], Monad m, Applicative m, Functor m) => (a -> m a) -> Pandoc -> m Pandoc Source #

Helper method to walk the components of a Pandoc element.