cmark-sections-0.1.0.3: Represent cmark-parsed Markdown as a tree of sections

Safe HaskellNone
LanguageHaskell2010

CMark.Sections

Contents

Description

This library lets you parse Markdown into a hierarchical structure (delimited by headings). For instance, let's say your document looks like this:

This is the preface.

First chapter
========================================

This chapter doesn't have sections.

Second chapter
========================================

First section
--------------------

Here's some text.

Second section
--------------------

And more text.

It can be represented as a tree:

preface: "This is the preface."
sections:
    * heading: "First chapter"
      content: "This chapter doesn't have sections."
      sections: []

    * heading: "Second chapter"
      sections:
          * heading: "First section"
            content: "Here's some text."
            sections: []

          * heading: "Second section"
            content: "And more text."
            sections: []

That's what this library does. Moreover, it lets you access the Markdown source of every node of the tree.

In most cases the only thing you need to do is something like this:

nodesToDocument . commonmarkToAnnotatedNodes [optSafe, optNormalize]

You can preprocess parsed Markdown after doing commonmarkToAnnotatedNodes as long as you don't add or remove any top-level nodes.

Synopsis

Parse Markdown to trees

commonmarkToAnnotatedNodes :: [CMarkOption] -> Text -> Annotated [Node] Source #

commonmarkToAnnotatedNodes parses Markdown with the given options and extracts nodes from the initial DOCUMENT node.

nodesToDocument :: Annotated [Node] -> Document () () Source #

Turn a list of Markdown nodes into a tree.

data Annotated a Source #

A data type for annotating things with their source. In this library we only use Annotated [Node], which stands for “some Markdown nodes + source”.

Constructors

Ann 

Fields

Instances

Functor Annotated Source # 

Methods

fmap :: (a -> b) -> Annotated a -> Annotated b #

(<$) :: a -> Annotated b -> Annotated a #

Foldable Annotated Source # 

Methods

fold :: Monoid m => Annotated m -> m #

foldMap :: Monoid m => (a -> m) -> Annotated a -> m #

foldr :: (a -> b -> b) -> b -> Annotated a -> b #

foldr' :: (a -> b -> b) -> b -> Annotated a -> b #

foldl :: (b -> a -> b) -> b -> Annotated a -> b #

foldl' :: (b -> a -> b) -> b -> Annotated a -> b #

foldr1 :: (a -> a -> a) -> Annotated a -> a #

foldl1 :: (a -> a -> a) -> Annotated a -> a #

toList :: Annotated a -> [a] #

null :: Annotated a -> Bool #

length :: Annotated a -> Int #

elem :: Eq a => a -> Annotated a -> Bool #

maximum :: Ord a => Annotated a -> a #

minimum :: Ord a => Annotated a -> a #

sum :: Num a => Annotated a -> a #

product :: Num a => Annotated a -> a #

Traversable Annotated Source # 

Methods

traverse :: Applicative f => (a -> f b) -> Annotated a -> f (Annotated b) #

sequenceA :: Applicative f => Annotated (f a) -> f (Annotated a) #

mapM :: Monad m => (a -> m b) -> Annotated a -> m (Annotated b) #

sequence :: Monad m => Annotated (m a) -> m (Annotated a) #

Eq a => Eq (Annotated a) Source # 

Methods

(==) :: Annotated a -> Annotated a -> Bool #

(/=) :: Annotated a -> Annotated a -> Bool #

Show a => Show (Annotated a) Source # 
Monoid a => Monoid (Annotated a) Source # 

data Section a b Source #

A section in the Markdown tree. Does not contain subsections (the tree is built using Forest from Data.Tree).

Constructors

Section 

Fields

Instances

(Eq b, Eq a) => Eq (Section a b) Source # 

Methods

(==) :: Section a b -> Section a b -> Bool #

(/=) :: Section a b -> Section a b -> Bool #

(Show b, Show a) => Show (Section a b) Source # 

Methods

showsPrec :: Int -> Section a b -> ShowS #

show :: Section a b -> String #

showList :: [Section a b] -> ShowS #

data Document a b Source #

The whole parsed Markdown tree. The first parameter is the type of annotations for headings (i.e. sections), the second – chunks of text (which are all associated with sections except for the preface).

Constructors

Document 

Fields

Instances

(Eq a, Eq b) => Eq (Document a b) Source # 

Methods

(==) :: Document a b -> Document a b -> Bool #

(/=) :: Document a b -> Document a b -> Bool #

(Show a, Show b) => Show (Document a b) Source # 

Methods

showsPrec :: Int -> Document a b -> ShowS #

show :: Document a b -> String #

showList :: [Document a b] -> ShowS #

Work with parsed trees

Note that you can use (<>) to combine Annotated nodes together.