Safe Haskell | None |
---|---|
Language | Haskell2010 |
- data Node
- = Project [Node]
- | Unit [Node]
- | Empty
- | FunctionDeclaration { }
- | LetStatement { }
- | LexprStatement { }
- | LwindowStatement { }
- | UnletStatement { }
- | GenericStatement String
- | DocBlock [Node]
- | Paragraph [Node]
- | LinkTargets [String]
- | List [Node]
- | ListItem [Node]
- | Blockquote [Node]
- | Fenced [String]
- | Separator
- | Plaintext String
- | BreakTag
- | Link String
- | Code String
- | Whitespace
- | PluginAnnotation Name Description
- | FunctionsAnnotation
- | FunctionAnnotation Name
- | IndentAnnotation
- | DedentAnnotation
- | CommandsAnnotation
- | CommandAnnotation Name (Maybe Parameters)
- | FooterAnnotation
- | MappingsAnnotation
- | MappingAnnotation Name
- | OptionsAnnotation
- | OptionAnnotation Name Type (Maybe Default)
- | HeadingAnnotation String
- | SubheadingAnnotation String
- | TOC [String]
- data ArgumentList = ArgumentList [Argument]
- data Argument = Argument String
- type Default = String
- type Description = String
- type Name = String
- type Type = String
- type Parameters = String
- walk :: Monoid a => (Node -> a) -> a -> Node -> a
- sanitizeAnchor :: String -> String
- invalidNode :: forall t. t
Documentation
data ArgumentList Source #
type Description = String Source #
type Parameters = String Source #
walk :: Monoid a => (Node -> a) -> a -> Node -> a Source #
Walks an AST node calling the supplied visitor function.
This is an in-order traversal.
For example, to implement a visitor which counts all nodes:
import Data.Monoid count = getSum $ walk (\_ -> 1) (Sum 0) tree
For comparison, here is a (probably inefficient) alternative way using
Plated
instead of walk
:
import Control.Lens.Operators import Control.Lens.Plated import Data.Data.Lens count = length $ tree ^.. cosmosOf uniplate
Another example; accumulating SubheadingAnnotation
nodes into a list:
accumulator node@(SubheadingAnnotation _) = [node] accumulator _ = [] -- skip everything else nodes = walk accumulator [] tree
Again, for comparison, the same without walk
, this time using a list
comprehension:
import Control.Lens.Operators import Control.Lens.Plated import Data.Data.Lens [n | n@(SubheadingAnnotation _) <- tree ^.. cosmosOf uniplate]
sanitizeAnchor :: String -> String Source #
Sanitizes a link target similar to the way that GitHub does:
- Downcase.
- Filter, keeping only letter, number, space, hyphen.
- Change spaces to hyphens.
- Uniquify by appending "-1", "-2", "-3" etc (not yet implemented).
We use this both for generating GitHub friendly link targets, and for auto-generating new link targets for use inside Vim help files.
Source: https://gist.github.com/asabaylus/3071099#gistcomment-1593627
invalidNode :: forall t. t Source #