{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, DeriveDataTypeable,
    GeneralizedNewtypeDeriving, CPP, StandaloneDeriving, DeriveGeneric,
    DeriveTraversable, OverloadedStrings, PatternGuards #-}

{-
Copyright (C) 2010-2023 John MacFarlane

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of John MacFarlane nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-}

{- |
   Module      : Text.Pandoc.Builder
   Copyright   : Copyright (C) 2010-2023 John MacFarlane
   License     : BSD3

   Maintainer  : John MacFarlane <jgm@berkeley.edu>
   Stability   : alpha
   Portability : portable

Convenience functions for building pandoc documents programmatically.

Example of use (with @OverloadedStrings@ pragma):

> import Text.Pandoc.Builder
>
> myDoc :: Pandoc
> myDoc = setTitle "My title" $ doc $
>   para "This is the first paragraph" <>
>   para ("And " <> emph "another" <> ".") <>
>   bulletList [ para "item one" <> para "continuation"
>              , plain ("item two and a " <>
>                  link "/url" "go to url" "link")
>              ]

Isn't that nicer than writing the following?

> import Text.Pandoc.Definition
> import Data.Map (fromList)
>
> myDoc :: Pandoc
> myDoc = Pandoc (Meta {unMeta = fromList [("title",
>           MetaInlines [Str "My",Space,Str "title"])]})
>         [Para [Str "This",Space,Str "is",Space,Str "the",Space,Str "first",
>          Space,Str "paragraph"],Para [Str "And",Space,Emph [Str "another"],
>          Str "."]
>         ,BulletList [
>           [Para [Str "item",Space,Str "one"]
>           ,Para [Str "continuation"]]
>          ,[Plain [Str "item",Space,Str "two",Space,Str "and",Space,
>                   Str "a",Space,Link nullAttr [Str "link"] ("/url","go to url")]]]]

And of course, you can use Haskell to define your own builders:

> import Text.Pandoc.Builder
> import Text.JSON
> import Control.Arrow ((***))
> import Data.Monoid (mempty)
>
> -- | Converts a JSON document into 'Blocks'.
> json :: String -> Blocks
> json x =
>   case decode x of
>        Ok y    -> jsValueToBlocks y
>        Error y -> error y
>    where jsValueToBlocks x =
>           case x of
>            JSNull         -> mempty
>            JSBool x       -> plain $ text $ show x
>            JSRational _ x -> plain $ text $ show x
>            JSString x     -> plain $ text $ fromJSString x
>            JSArray xs     -> bulletList $ map jsValueToBlocks xs
>            JSObject x     -> definitionList $
>                               map (text *** (:[]) . jsValueToBlocks) $
>                               fromJSObject x

-}

module Text.Pandoc.Builder ( module Text.Pandoc.Definition
                           , Many(..)
                           , Inlines
                           , Blocks
                           , (<>)
                           , singleton
                           , toList
                           , fromList
                           , isNull
                           -- * Document builders
                           , doc
                           , ToMetaValue(..)
                           , HasMeta(..)
                           , setTitle
                           , setAuthors
                           , setDate
                           -- * Inline list builders
                           , text
                           , str
                           , emph
                           , underline
                           , strong
                           , strikeout
                           , superscript
                           , subscript
                           , smallcaps
                           , singleQuoted
                           , doubleQuoted
                           , cite
                           , codeWith
                           , code
                           , space
                           , softbreak
                           , linebreak
                           , math
                           , displayMath
                           , rawInline
                           , link
                           , linkWith
                           , image
                           , imageWith
                           , note
                           , spanWith
                           , trimInlines
                           -- * Block list builders
                           , para
                           , plain
                           , lineBlock
                           , codeBlockWith
                           , codeBlock
                           , rawBlock
                           , blockQuote
                           , bulletList
                           , orderedListWith
                           , orderedList
                           , definitionList
                           , header
                           , headerWith
                           , horizontalRule
                           , cell
                           , simpleCell
                           , emptyCell
                           , cellWith
                           , table
                           , simpleTable
                           , tableWith
                           , figure
                           , figureWith
                           , caption
                           , simpleCaption
                           , emptyCaption
                           , simpleFigureWith
                           , simpleFigure
                           , divWith
                           -- * Table processing
                           , normalizeTableHead
                           , normalizeTableBody
                           , normalizeTableFoot
                           , placeRowSection
                           , clipRows
                           )
where
import Text.Pandoc.Definition
import Data.String
import qualified Data.Map as M
import Data.Text (Text)
import qualified Data.Text as T
import Data.Sequence (Seq, (|>), viewr, viewl, ViewR(..), ViewL(..))
import qualified Data.Sequence as Seq
import Data.Traversable (Traversable)
import Data.Foldable (Foldable)
import qualified Data.Foldable as F
import Data.Data
import Control.Arrow ((***))
import GHC.Generics (Generic)
import Data.Semigroup (Semigroup(..))

newtype Many a = Many { forall a. Many a -> Seq a
unMany :: Seq a }
                 deriving (Typeable (Many a)
Typeable (Many a) =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> Many a -> c (Many a))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Many a))
-> (Many a -> Constr)
-> (Many a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (Many a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Many a)))
-> ((forall b. Data b => b -> b) -> Many a -> Many a)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Many a -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Many a -> r)
-> (forall u. (forall d. Data d => d -> u) -> Many a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Many a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Many a -> m (Many a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Many a -> m (Many a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Many a -> m (Many a))
-> Data (Many a)
Many a -> Constr
Many a -> DataType
(forall b. Data b => b -> b) -> Many a -> Many a
forall a. Data a => Typeable (Many a)
forall a. Data a => Many a -> Constr
forall a. Data a => Many a -> DataType
forall a.
Data a =>
(forall b. Data b => b -> b) -> Many a -> Many a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Many a -> u
forall a u. Data a => (forall d. Data d => d -> u) -> Many a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Many a -> r
forall a r r'.
Data a =>
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Many a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Many a -> m (Many a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Many a -> m (Many a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Many a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Many a -> c (Many a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Many a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Many a))
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Many a -> u
forall u. (forall d. Data d => d -> u) -> Many a -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Many a -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Many a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Many a -> m (Many a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Many a -> m (Many a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Many a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Many a -> c (Many a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Many a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Many a))
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Many a -> c (Many a)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Many a -> c (Many a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Many a)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Many a)
$ctoConstr :: forall a. Data a => Many a -> Constr
toConstr :: Many a -> Constr
$cdataTypeOf :: forall a. Data a => Many a -> DataType
dataTypeOf :: Many a -> DataType
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Many a))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Many a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Many a))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Many a))
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> Many a -> Many a
gmapT :: (forall b. Data b => b -> b) -> Many a -> Many a
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Many a -> r
gmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Many a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Many a -> r
gmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Many a -> r
$cgmapQ :: forall a u. Data a => (forall d. Data d => d -> u) -> Many a -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> Many a -> [u]
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Many a -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Many a -> u
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Many a -> m (Many a)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Many a -> m (Many a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Many a -> m (Many a)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Many a -> m (Many a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Many a -> m (Many a)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Many a -> m (Many a)
Data, Eq (Many a)
Eq (Many a) =>
(Many a -> Many a -> Ordering)
-> (Many a -> Many a -> Bool)
-> (Many a -> Many a -> Bool)
-> (Many a -> Many a -> Bool)
-> (Many a -> Many a -> Bool)
-> (Many a -> Many a -> Many a)
-> (Many a -> Many a -> Many a)
-> Ord (Many a)
Many a -> Many a -> Bool
Many a -> Many a -> Ordering
Many a -> Many a -> Many a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (Many a)
forall a. Ord a => Many a -> Many a -> Bool
forall a. Ord a => Many a -> Many a -> Ordering
forall a. Ord a => Many a -> Many a -> Many a
$ccompare :: forall a. Ord a => Many a -> Many a -> Ordering
compare :: Many a -> Many a -> Ordering
$c< :: forall a. Ord a => Many a -> Many a -> Bool
< :: Many a -> Many a -> Bool
$c<= :: forall a. Ord a => Many a -> Many a -> Bool
<= :: Many a -> Many a -> Bool
$c> :: forall a. Ord a => Many a -> Many a -> Bool
> :: Many a -> Many a -> Bool
$c>= :: forall a. Ord a => Many a -> Many a -> Bool
>= :: Many a -> Many a -> Bool
$cmax :: forall a. Ord a => Many a -> Many a -> Many a
max :: Many a -> Many a -> Many a
$cmin :: forall a. Ord a => Many a -> Many a -> Many a
min :: Many a -> Many a -> Many a
Ord, Many a -> Many a -> Bool
(Many a -> Many a -> Bool)
-> (Many a -> Many a -> Bool) -> Eq (Many a)
forall a. Eq a => Many a -> Many a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Many a -> Many a -> Bool
== :: Many a -> Many a -> Bool
$c/= :: forall a. Eq a => Many a -> Many a -> Bool
/= :: Many a -> Many a -> Bool
Eq, Typeable, (forall m. Monoid m => Many m -> m)
-> (forall m a. Monoid m => (a -> m) -> Many a -> m)
-> (forall m a. Monoid m => (a -> m) -> Many a -> m)
-> (forall a b. (a -> b -> b) -> b -> Many a -> b)
-> (forall a b. (a -> b -> b) -> b -> Many a -> b)
-> (forall b a. (b -> a -> b) -> b -> Many a -> b)
-> (forall b a. (b -> a -> b) -> b -> Many a -> b)
-> (forall a. (a -> a -> a) -> Many a -> a)
-> (forall a. (a -> a -> a) -> Many a -> a)
-> (forall a. Many a -> [a])
-> (forall a. Many a -> Bool)
-> (forall a. Many a -> Int)
-> (forall a. Eq a => a -> Many a -> Bool)
-> (forall a. Ord a => Many a -> a)
-> (forall a. Ord a => Many a -> a)
-> (forall a. Num a => Many a -> a)
-> (forall a. Num a => Many a -> a)
-> Foldable Many
forall a. Eq a => a -> Many a -> Bool
forall a. Num a => Many a -> a
forall a. Ord a => Many a -> a
forall m. Monoid m => Many m -> m
forall a. Many a -> Bool
forall a. Many a -> Int
forall a. Many a -> [a]
forall a. (a -> a -> a) -> Many a -> a
forall m a. Monoid m => (a -> m) -> Many a -> m
forall b a. (b -> a -> b) -> b -> Many a -> b
forall a b. (a -> b -> b) -> b -> Many a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => Many m -> m
fold :: forall m. Monoid m => Many m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Many a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Many a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Many a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> Many a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> Many a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Many a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Many a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Many a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Many a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Many a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Many a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> Many a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> Many a -> a
foldr1 :: forall a. (a -> a -> a) -> Many a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Many a -> a
foldl1 :: forall a. (a -> a -> a) -> Many a -> a
$ctoList :: forall a. Many a -> [a]
toList :: forall a. Many a -> [a]
$cnull :: forall a. Many a -> Bool
null :: forall a. Many a -> Bool
$clength :: forall a. Many a -> Int
length :: forall a. Many a -> Int
$celem :: forall a. Eq a => a -> Many a -> Bool
elem :: forall a. Eq a => a -> Many a -> Bool
$cmaximum :: forall a. Ord a => Many a -> a
maximum :: forall a. Ord a => Many a -> a
$cminimum :: forall a. Ord a => Many a -> a
minimum :: forall a. Ord a => Many a -> a
$csum :: forall a. Num a => Many a -> a
sum :: forall a. Num a => Many a -> a
$cproduct :: forall a. Num a => Many a -> a
product :: forall a. Num a => Many a -> a
Foldable, Functor Many
Foldable Many
(Functor Many, Foldable Many) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> Many a -> f (Many b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Many (f a) -> f (Many a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Many a -> m (Many b))
-> (forall (m :: * -> *) a. Monad m => Many (m a) -> m (Many a))
-> Traversable Many
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Many (m a) -> m (Many a)
forall (f :: * -> *) a. Applicative f => Many (f a) -> f (Many a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Many a -> m (Many b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Many a -> f (Many b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Many a -> f (Many b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Many a -> f (Many b)
$csequenceA :: forall (f :: * -> *) a. Applicative f => Many (f a) -> f (Many a)
sequenceA :: forall (f :: * -> *) a. Applicative f => Many (f a) -> f (Many a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Many a -> m (Many b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Many a -> m (Many b)
$csequence :: forall (m :: * -> *) a. Monad m => Many (m a) -> m (Many a)
sequence :: forall (m :: * -> *) a. Monad m => Many (m a) -> m (Many a)
Traversable, (forall a b. (a -> b) -> Many a -> Many b)
-> (forall a b. a -> Many b -> Many a) -> Functor Many
forall a b. a -> Many b -> Many a
forall a b. (a -> b) -> Many a -> Many b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Many a -> Many b
fmap :: forall a b. (a -> b) -> Many a -> Many b
$c<$ :: forall a b. a -> Many b -> Many a
<$ :: forall a b. a -> Many b -> Many a
Functor, Int -> Many a -> ShowS
[Many a] -> ShowS
Many a -> String
(Int -> Many a -> ShowS)
-> (Many a -> String) -> ([Many a] -> ShowS) -> Show (Many a)
forall a. Show a => Int -> Many a -> ShowS
forall a. Show a => [Many a] -> ShowS
forall a. Show a => Many a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Many a -> ShowS
showsPrec :: Int -> Many a -> ShowS
$cshow :: forall a. Show a => Many a -> String
show :: Many a -> String
$cshowList :: forall a. Show a => [Many a] -> ShowS
showList :: [Many a] -> ShowS
Show, ReadPrec [Many a]
ReadPrec (Many a)
Int -> ReadS (Many a)
ReadS [Many a]
(Int -> ReadS (Many a))
-> ReadS [Many a]
-> ReadPrec (Many a)
-> ReadPrec [Many a]
-> Read (Many a)
forall a. Read a => ReadPrec [Many a]
forall a. Read a => ReadPrec (Many a)
forall a. Read a => Int -> ReadS (Many a)
forall a. Read a => ReadS [Many a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Read a => Int -> ReadS (Many a)
readsPrec :: Int -> ReadS (Many a)
$creadList :: forall a. Read a => ReadS [Many a]
readList :: ReadS [Many a]
$creadPrec :: forall a. Read a => ReadPrec (Many a)
readPrec :: ReadPrec (Many a)
$creadListPrec :: forall a. Read a => ReadPrec [Many a]
readListPrec :: ReadPrec [Many a]
Read)

deriving instance Generic (Many a)

toList :: Many a -> [a]
toList :: forall a. Many a -> [a]
toList = Many a -> [a]
forall a. Many a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList

singleton :: a -> Many a
singleton :: forall a. a -> Many a
singleton = Seq a -> Many a
forall a. Seq a -> Many a
Many (Seq a -> Many a) -> (a -> Seq a) -> a -> Many a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Seq a
forall a. a -> Seq a
Seq.singleton

fromList :: [a] -> Many a
fromList :: forall a. [a] -> Many a
fromList = Seq a -> Many a
forall a. Seq a -> Many a
Many (Seq a -> Many a) -> ([a] -> Seq a) -> [a] -> Many a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Seq a
forall a. [a] -> Seq a
Seq.fromList

{-# DEPRECATED isNull "Use null instead" #-}
isNull :: Many a -> Bool
isNull :: forall a. Many a -> Bool
isNull = Seq a -> Bool
forall a. Seq a -> Bool
Seq.null (Seq a -> Bool) -> (Many a -> Seq a) -> Many a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many a -> Seq a
forall a. Many a -> Seq a
unMany

type Inlines = Many Inline
type Blocks  = Many Block

deriving instance Semigroup Blocks
deriving instance Monoid Blocks

instance Semigroup Inlines where
  (Many Seq Inline
xs) <> :: Many Inline -> Many Inline -> Many Inline
<> (Many Seq Inline
ys) =
    case (Seq Inline -> ViewR Inline
forall a. Seq a -> ViewR a
viewr Seq Inline
xs, Seq Inline -> ViewL Inline
forall a. Seq a -> ViewL a
viewl Seq Inline
ys) of
      (ViewR Inline
EmptyR, ViewL Inline
_) -> Seq Inline -> Many Inline
forall a. Seq a -> Many a
Many Seq Inline
ys
      (ViewR Inline
_, ViewL Inline
EmptyL) -> Seq Inline -> Many Inline
forall a. Seq a -> Many a
Many Seq Inline
xs
      (Seq Inline
xs' :> Inline
x, Inline
y :< Seq Inline
ys') -> Seq Inline -> Many Inline
forall a. Seq a -> Many a
Many (Seq Inline
meld Seq Inline -> Seq Inline -> Seq Inline
forall a. Semigroup a => a -> a -> a
<> Seq Inline
ys')
        where meld :: Seq Inline
meld = case (Inline
x, Inline
y) of
                          (Inline
Space, Inline
Space)     -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> Inline
Space
                          (Inline
Space, Inline
SoftBreak) -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> Inline
SoftBreak
                          (Inline
SoftBreak, Inline
Space) -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> Inline
SoftBreak
                          (Str Text
t1, Str Text
t2)   -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> Text -> Inline
Str (Text
t1 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t2)
                          (Emph [Inline]
i1, Emph [Inline]
i2) -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> [Inline] -> Inline
Emph ([Inline]
i1 [Inline] -> [Inline] -> [Inline]
forall a. Semigroup a => a -> a -> a
<> [Inline]
i2)
                          (Underline [Inline]
i1, Underline [Inline]
i2) -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> [Inline] -> Inline
Underline ([Inline]
i1 [Inline] -> [Inline] -> [Inline]
forall a. Semigroup a => a -> a -> a
<> [Inline]
i2)
                          (Strong [Inline]
i1, Strong [Inline]
i2) -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> [Inline] -> Inline
Strong ([Inline]
i1 [Inline] -> [Inline] -> [Inline]
forall a. Semigroup a => a -> a -> a
<> [Inline]
i2)
                          (Subscript [Inline]
i1, Subscript [Inline]
i2) -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> [Inline] -> Inline
Subscript ([Inline]
i1 [Inline] -> [Inline] -> [Inline]
forall a. Semigroup a => a -> a -> a
<> [Inline]
i2)
                          (Superscript [Inline]
i1, Superscript [Inline]
i2) -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> [Inline] -> Inline
Superscript ([Inline]
i1 [Inline] -> [Inline] -> [Inline]
forall a. Semigroup a => a -> a -> a
<> [Inline]
i2)
                          (Strikeout [Inline]
i1, Strikeout [Inline]
i2) -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> [Inline] -> Inline
Strikeout ([Inline]
i1 [Inline] -> [Inline] -> [Inline]
forall a. Semigroup a => a -> a -> a
<> [Inline]
i2)
                          (Inline
Space, Inline
LineBreak) -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> Inline
LineBreak
                          (Inline
LineBreak, Inline
Space) -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> Inline
LineBreak
                          (Inline
SoftBreak, Inline
LineBreak) -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> Inline
LineBreak
                          (Inline
LineBreak, Inline
SoftBreak) -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> Inline
LineBreak
                          (Inline
SoftBreak, Inline
SoftBreak) -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> Inline
SoftBreak
                          (Inline, Inline)
_                  -> Seq Inline
xs' Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> Inline
x Seq Inline -> Inline -> Seq Inline
forall a. Seq a -> a -> Seq a
|> Inline
y
instance Monoid Inlines where
  mempty :: Many Inline
mempty = Seq Inline -> Many Inline
forall a. Seq a -> Many a
Many Seq Inline
forall a. Monoid a => a
mempty
  mappend :: Many Inline -> Many Inline -> Many Inline
mappend = Many Inline -> Many Inline -> Many Inline
forall a. Semigroup a => a -> a -> a
(<>)

instance IsString Inlines where
   fromString :: String -> Many Inline
fromString = Text -> Many Inline
text (Text -> Many Inline) -> (String -> Text) -> String -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

-- | Trim leading and trailing spaces and softbreaks from an Inlines.
trimInlines :: Inlines -> Inlines
#if MIN_VERSION_containers(0,4,0)
trimInlines :: Many Inline -> Many Inline
trimInlines (Many Seq Inline
ils) = Seq Inline -> Many Inline
forall a. Seq a -> Many a
Many (Seq Inline -> Many Inline) -> Seq Inline -> Many Inline
forall a b. (a -> b) -> a -> b
$ (Inline -> Bool) -> Seq Inline -> Seq Inline
forall a. (a -> Bool) -> Seq a -> Seq a
Seq.dropWhileL Inline -> Bool
isSp (Seq Inline -> Seq Inline) -> Seq Inline -> Seq Inline
forall a b. (a -> b) -> a -> b
$
                            (Inline -> Bool) -> Seq Inline -> Seq Inline
forall a. (a -> Bool) -> Seq a -> Seq a
Seq.dropWhileR Inline -> Bool
isSp (Seq Inline -> Seq Inline) -> Seq Inline -> Seq Inline
forall a b. (a -> b) -> a -> b
$ Seq Inline
ils
#else
-- for GHC 6.12, we need to workaround a bug in dropWhileR
-- see http://hackage.haskell.org/trac/ghc/ticket/4157
trimInlines (Many ils) = Many $ Seq.dropWhileL isSp $
                            Seq.reverse $ Seq.dropWhileL isSp $
                            Seq.reverse ils
#endif
  where isSp :: Inline -> Bool
isSp Inline
Space = Bool
True
        isSp Inline
SoftBreak = Bool
True
        isSp Inline
_ = Bool
False

-- Document builders

doc :: Blocks -> Pandoc
doc :: Many Block -> Pandoc
doc = Meta -> [Block] -> Pandoc
Pandoc Meta
nullMeta ([Block] -> Pandoc)
-> (Many Block -> [Block]) -> Many Block -> Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Block -> [Block]
forall a. Many a -> [a]
toList

class ToMetaValue a where
  toMetaValue :: a -> MetaValue

instance ToMetaValue MetaValue where
  toMetaValue :: MetaValue -> MetaValue
toMetaValue = MetaValue -> MetaValue
forall a. a -> a
id

instance ToMetaValue Blocks where
  toMetaValue :: Many Block -> MetaValue
toMetaValue = [Block] -> MetaValue
MetaBlocks ([Block] -> MetaValue)
-> (Many Block -> [Block]) -> Many Block -> MetaValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Block -> [Block]
forall a. Many a -> [a]
toList

instance ToMetaValue Inlines where
  toMetaValue :: Many Inline -> MetaValue
toMetaValue = [Inline] -> MetaValue
MetaInlines ([Inline] -> MetaValue)
-> (Many Inline -> [Inline]) -> Many Inline -> MetaValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> [Inline]
forall a. Many a -> [a]
toList

instance ToMetaValue Bool where
  toMetaValue :: Bool -> MetaValue
toMetaValue = Bool -> MetaValue
MetaBool

instance ToMetaValue Text where
  toMetaValue :: Text -> MetaValue
toMetaValue = Text -> MetaValue
MetaString

instance {-# OVERLAPPING #-} ToMetaValue String where
  toMetaValue :: String -> MetaValue
toMetaValue = Text -> MetaValue
MetaString (Text -> MetaValue) -> (String -> Text) -> String -> MetaValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

instance ToMetaValue a => ToMetaValue [a] where
  toMetaValue :: [a] -> MetaValue
toMetaValue = [MetaValue] -> MetaValue
MetaList ([MetaValue] -> MetaValue)
-> ([a] -> [MetaValue]) -> [a] -> MetaValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> MetaValue) -> [a] -> [MetaValue]
forall a b. (a -> b) -> [a] -> [b]
map a -> MetaValue
forall a. ToMetaValue a => a -> MetaValue
toMetaValue

instance ToMetaValue a => ToMetaValue (M.Map Text a) where
  toMetaValue :: Map Text a -> MetaValue
toMetaValue = Map Text MetaValue -> MetaValue
MetaMap (Map Text MetaValue -> MetaValue)
-> (Map Text a -> Map Text MetaValue) -> Map Text a -> MetaValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> MetaValue) -> Map Text a -> Map Text MetaValue
forall a b k. (a -> b) -> Map k a -> Map k b
M.map a -> MetaValue
forall a. ToMetaValue a => a -> MetaValue
toMetaValue

instance ToMetaValue a => ToMetaValue (M.Map String a) where
  toMetaValue :: Map String a -> MetaValue
toMetaValue = Map Text MetaValue -> MetaValue
MetaMap (Map Text MetaValue -> MetaValue)
-> (Map String a -> Map Text MetaValue)
-> Map String a
-> MetaValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> MetaValue) -> Map Text a -> Map Text MetaValue
forall a b k. (a -> b) -> Map k a -> Map k b
M.map a -> MetaValue
forall a. ToMetaValue a => a -> MetaValue
toMetaValue (Map Text a -> Map Text MetaValue)
-> (Map String a -> Map Text a)
-> Map String a
-> Map Text MetaValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Text) -> Map String a -> Map Text a
forall k2 k1 a. Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a
M.mapKeys String -> Text
T.pack

class HasMeta a where
  setMeta :: ToMetaValue b => Text -> b -> a -> a
  deleteMeta :: Text -> a -> a

instance HasMeta Meta where
  setMeta :: forall b. ToMetaValue b => Text -> b -> Meta -> Meta
setMeta Text
key b
val (Meta Map Text MetaValue
ms) = Map Text MetaValue -> Meta
Meta (Map Text MetaValue -> Meta) -> Map Text MetaValue -> Meta
forall a b. (a -> b) -> a -> b
$ Text -> MetaValue -> Map Text MetaValue -> Map Text MetaValue
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
key (b -> MetaValue
forall a. ToMetaValue a => a -> MetaValue
toMetaValue b
val) Map Text MetaValue
ms
  deleteMeta :: Text -> Meta -> Meta
deleteMeta Text
key (Meta Map Text MetaValue
ms) = Map Text MetaValue -> Meta
Meta (Map Text MetaValue -> Meta) -> Map Text MetaValue -> Meta
forall a b. (a -> b) -> a -> b
$ Text -> Map Text MetaValue -> Map Text MetaValue
forall k a. Ord k => k -> Map k a -> Map k a
M.delete Text
key Map Text MetaValue
ms

instance HasMeta Pandoc where
  setMeta :: forall b. ToMetaValue b => Text -> b -> Pandoc -> Pandoc
setMeta Text
key b
val (Pandoc (Meta Map Text MetaValue
ms) [Block]
bs) =
    Meta -> [Block] -> Pandoc
Pandoc (Map Text MetaValue -> Meta
Meta (Map Text MetaValue -> Meta) -> Map Text MetaValue -> Meta
forall a b. (a -> b) -> a -> b
$ Text -> MetaValue -> Map Text MetaValue -> Map Text MetaValue
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
key (b -> MetaValue
forall a. ToMetaValue a => a -> MetaValue
toMetaValue b
val) Map Text MetaValue
ms) [Block]
bs
  deleteMeta :: Text -> Pandoc -> Pandoc
deleteMeta Text
key (Pandoc (Meta Map Text MetaValue
ms) [Block]
bs) =
    Meta -> [Block] -> Pandoc
Pandoc (Map Text MetaValue -> Meta
Meta (Map Text MetaValue -> Meta) -> Map Text MetaValue -> Meta
forall a b. (a -> b) -> a -> b
$ Text -> Map Text MetaValue -> Map Text MetaValue
forall k a. Ord k => k -> Map k a -> Map k a
M.delete Text
key Map Text MetaValue
ms) [Block]
bs

setTitle :: Inlines -> Pandoc -> Pandoc
setTitle :: Many Inline -> Pandoc -> Pandoc
setTitle = Text -> Many Inline -> Pandoc -> Pandoc
forall a b. (HasMeta a, ToMetaValue b) => Text -> b -> a -> a
forall b. ToMetaValue b => Text -> b -> Pandoc -> Pandoc
setMeta Text
"title"

setAuthors :: [Inlines] -> Pandoc -> Pandoc
setAuthors :: [Many Inline] -> Pandoc -> Pandoc
setAuthors = Text -> [Many Inline] -> Pandoc -> Pandoc
forall a b. (HasMeta a, ToMetaValue b) => Text -> b -> a -> a
forall b. ToMetaValue b => Text -> b -> Pandoc -> Pandoc
setMeta Text
"author"

setDate :: Inlines -> Pandoc -> Pandoc
setDate :: Many Inline -> Pandoc -> Pandoc
setDate = Text -> Many Inline -> Pandoc -> Pandoc
forall a b. (HasMeta a, ToMetaValue b) => Text -> b -> a -> a
forall b. ToMetaValue b => Text -> b -> Pandoc -> Pandoc
setMeta Text
"date"

-- Inline list builders

-- | Convert a 'Text' to 'Inlines', treating interword spaces as 'Space's
-- or 'SoftBreak's.  If you want a 'Str' with literal spaces, use 'str'.
text :: Text -> Inlines
text :: Text -> Many Inline
text = [Inline] -> Many Inline
forall a. [a] -> Many a
fromList ([Inline] -> Many Inline)
-> (Text -> [Inline]) -> Text -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Inline) -> [Text] -> [Inline]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Inline
conv ([Text] -> [Inline]) -> (Text -> [Text]) -> Text -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
breakBySpaces
  where breakBySpaces :: Text -> [Text]
breakBySpaces = (Char -> Char -> Bool) -> Text -> [Text]
T.groupBy Char -> Char -> Bool
sameCategory
        sameCategory :: Char -> Char -> Bool
sameCategory Char
x Char
y = Char -> Bool
is_space Char
x Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Char -> Bool
is_space Char
y
        conv :: Text -> Inline
conv Text
xs | (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
is_space Text
xs =
           if (Char -> Bool) -> Text -> Bool
T.any Char -> Bool
is_newline Text
xs
              then Inline
SoftBreak
              else Inline
Space
        conv Text
xs = Text -> Inline
Str Text
xs
        is_space :: Char -> Bool
is_space Char
' '    = Bool
True
        is_space Char
'\r'   = Bool
True
        is_space Char
'\n'   = Bool
True
        is_space Char
'\t'   = Bool
True
        is_space Char
_      = Bool
False
        is_newline :: Char -> Bool
is_newline Char
'\r' = Bool
True
        is_newline Char
'\n' = Bool
True
        is_newline Char
_    = Bool
False

str :: Text -> Inlines
str :: Text -> Many Inline
str = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline) -> (Text -> Inline) -> Text -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Inline
Str

emph :: Inlines -> Inlines
emph :: Many Inline -> Many Inline
emph = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline)
-> (Many Inline -> Inline) -> Many Inline -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Inline
Emph ([Inline] -> Inline)
-> (Many Inline -> [Inline]) -> Many Inline -> Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> [Inline]
forall a. Many a -> [a]
toList

underline :: Inlines -> Inlines
underline :: Many Inline -> Many Inline
underline = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline)
-> (Many Inline -> Inline) -> Many Inline -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Inline
Underline ([Inline] -> Inline)
-> (Many Inline -> [Inline]) -> Many Inline -> Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> [Inline]
forall a. Many a -> [a]
toList

strong :: Inlines -> Inlines
strong :: Many Inline -> Many Inline
strong = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline)
-> (Many Inline -> Inline) -> Many Inline -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Inline
Strong ([Inline] -> Inline)
-> (Many Inline -> [Inline]) -> Many Inline -> Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> [Inline]
forall a. Many a -> [a]
toList

strikeout :: Inlines -> Inlines
strikeout :: Many Inline -> Many Inline
strikeout = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline)
-> (Many Inline -> Inline) -> Many Inline -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Inline
Strikeout ([Inline] -> Inline)
-> (Many Inline -> [Inline]) -> Many Inline -> Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> [Inline]
forall a. Many a -> [a]
toList

superscript :: Inlines -> Inlines
superscript :: Many Inline -> Many Inline
superscript = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline)
-> (Many Inline -> Inline) -> Many Inline -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Inline
Superscript ([Inline] -> Inline)
-> (Many Inline -> [Inline]) -> Many Inline -> Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> [Inline]
forall a. Many a -> [a]
toList

subscript :: Inlines -> Inlines
subscript :: Many Inline -> Many Inline
subscript = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline)
-> (Many Inline -> Inline) -> Many Inline -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Inline
Subscript ([Inline] -> Inline)
-> (Many Inline -> [Inline]) -> Many Inline -> Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> [Inline]
forall a. Many a -> [a]
toList

smallcaps :: Inlines -> Inlines
smallcaps :: Many Inline -> Many Inline
smallcaps = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline)
-> (Many Inline -> Inline) -> Many Inline -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Inline
SmallCaps ([Inline] -> Inline)
-> (Many Inline -> [Inline]) -> Many Inline -> Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> [Inline]
forall a. Many a -> [a]
toList

singleQuoted :: Inlines -> Inlines
singleQuoted :: Many Inline -> Many Inline
singleQuoted = QuoteType -> Many Inline -> Many Inline
quoted QuoteType
SingleQuote

doubleQuoted :: Inlines -> Inlines
doubleQuoted :: Many Inline -> Many Inline
doubleQuoted = QuoteType -> Many Inline -> Many Inline
quoted QuoteType
DoubleQuote

quoted :: QuoteType -> Inlines -> Inlines
quoted :: QuoteType -> Many Inline -> Many Inline
quoted QuoteType
qt = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline)
-> (Many Inline -> Inline) -> Many Inline -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QuoteType -> [Inline] -> Inline
Quoted QuoteType
qt ([Inline] -> Inline)
-> (Many Inline -> [Inline]) -> Many Inline -> Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> [Inline]
forall a. Many a -> [a]
toList

cite :: [Citation] -> Inlines -> Inlines
cite :: [Citation] -> Many Inline -> Many Inline
cite [Citation]
cts = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline)
-> (Many Inline -> Inline) -> Many Inline -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Citation] -> [Inline] -> Inline
Cite [Citation]
cts ([Inline] -> Inline)
-> (Many Inline -> [Inline]) -> Many Inline -> Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> [Inline]
forall a. Many a -> [a]
toList

-- | Inline code with attributes.
codeWith :: Attr -> Text -> Inlines
codeWith :: Attr -> Text -> Many Inline
codeWith Attr
attrs = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline) -> (Text -> Inline) -> Text -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attr -> Text -> Inline
Code Attr
attrs

-- | Plain inline code.
code :: Text -> Inlines
code :: Text -> Many Inline
code = Attr -> Text -> Many Inline
codeWith Attr
nullAttr

space :: Inlines
space :: Many Inline
space = Inline -> Many Inline
forall a. a -> Many a
singleton Inline
Space

softbreak :: Inlines
softbreak :: Many Inline
softbreak = Inline -> Many Inline
forall a. a -> Many a
singleton Inline
SoftBreak

linebreak :: Inlines
linebreak :: Many Inline
linebreak = Inline -> Many Inline
forall a. a -> Many a
singleton Inline
LineBreak

-- | Inline math
math :: Text -> Inlines
math :: Text -> Many Inline
math = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline) -> (Text -> Inline) -> Text -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MathType -> Text -> Inline
Math MathType
InlineMath

-- | Display math
displayMath :: Text -> Inlines
displayMath :: Text -> Many Inline
displayMath = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline) -> (Text -> Inline) -> Text -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MathType -> Text -> Inline
Math MathType
DisplayMath

rawInline :: Text -> Text -> Inlines
rawInline :: Text -> Text -> Many Inline
rawInline Text
format = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline) -> (Text -> Inline) -> Text -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Format -> Text -> Inline
RawInline (Text -> Format
Format Text
format)

link :: Text  -- ^ URL
     -> Text  -- ^ Title
     -> Inlines -- ^ Label
     -> Inlines
link :: Text -> Text -> Many Inline -> Many Inline
link = Attr -> Text -> Text -> Many Inline -> Many Inline
linkWith Attr
nullAttr

linkWith :: Attr    -- ^ Attributes
         -> Text  -- ^ URL
         -> Text  -- ^ Title
         -> Inlines -- ^ Label
         -> Inlines
linkWith :: Attr -> Text -> Text -> Many Inline -> Many Inline
linkWith Attr
attr Text
url Text
title Many Inline
x = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline) -> Inline -> Many Inline
forall a b. (a -> b) -> a -> b
$ Attr -> [Inline] -> Target -> Inline
Link Attr
attr (Many Inline -> [Inline]
forall a. Many a -> [a]
toList Many Inline
x) (Text
url, Text
title)

image :: Text  -- ^ URL
      -> Text  -- ^ Title
      -> Inlines -- ^ Alt text
      -> Inlines
image :: Text -> Text -> Many Inline -> Many Inline
image = Attr -> Text -> Text -> Many Inline -> Many Inline
imageWith Attr
nullAttr

imageWith :: Attr -- ^ Attributes
          -> Text  -- ^ URL
          -> Text  -- ^ Title
          -> Inlines -- ^ Alt text
          -> Inlines
imageWith :: Attr -> Text -> Text -> Many Inline -> Many Inline
imageWith Attr
attr Text
url Text
title Many Inline
x = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline) -> Inline -> Many Inline
forall a b. (a -> b) -> a -> b
$ Attr -> [Inline] -> Target -> Inline
Image Attr
attr (Many Inline -> [Inline]
forall a. Many a -> [a]
toList Many Inline
x) (Text
url, Text
title)

note :: Blocks -> Inlines
note :: Many Block -> Many Inline
note = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline)
-> (Many Block -> Inline) -> Many Block -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Block] -> Inline
Note ([Block] -> Inline)
-> (Many Block -> [Block]) -> Many Block -> Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Block -> [Block]
forall a. Many a -> [a]
toList

spanWith :: Attr -> Inlines -> Inlines
spanWith :: Attr -> Many Inline -> Many Inline
spanWith Attr
attr = Inline -> Many Inline
forall a. a -> Many a
singleton (Inline -> Many Inline)
-> (Many Inline -> Inline) -> Many Inline -> Many Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attr -> [Inline] -> Inline
Span Attr
attr ([Inline] -> Inline)
-> (Many Inline -> [Inline]) -> Many Inline -> Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> [Inline]
forall a. Many a -> [a]
toList

-- Block list builders

para :: Inlines -> Blocks
para :: Many Inline -> Many Block
para = Block -> Many Block
forall a. a -> Many a
singleton (Block -> Many Block)
-> (Many Inline -> Block) -> Many Inline -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Block
Para ([Inline] -> Block)
-> (Many Inline -> [Inline]) -> Many Inline -> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> [Inline]
forall a. Many a -> [a]
toList

plain :: Inlines -> Blocks
plain :: Many Inline -> Many Block
plain Many Inline
ils = if Many Inline -> Bool
forall a. Many a -> Bool
isNull Many Inline
ils
               then Many Block
forall a. Monoid a => a
mempty
               else Block -> Many Block
forall a. a -> Many a
singleton (Block -> Many Block)
-> (Many Inline -> Block) -> Many Inline -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Block
Plain ([Inline] -> Block)
-> (Many Inline -> [Inline]) -> Many Inline -> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> [Inline]
forall a. Many a -> [a]
toList (Many Inline -> Many Block) -> Many Inline -> Many Block
forall a b. (a -> b) -> a -> b
$ Many Inline
ils

lineBlock :: [Inlines] -> Blocks
lineBlock :: [Many Inline] -> Many Block
lineBlock = Block -> Many Block
forall a. a -> Many a
singleton (Block -> Many Block)
-> ([Many Inline] -> Block) -> [Many Inline] -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Inline]] -> Block
LineBlock ([[Inline]] -> Block)
-> ([Many Inline] -> [[Inline]]) -> [Many Inline] -> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Many Inline -> [Inline]) -> [Many Inline] -> [[Inline]]
forall a b. (a -> b) -> [a] -> [b]
map Many Inline -> [Inline]
forall a. Many a -> [a]
toList

-- | A code block with attributes.
codeBlockWith :: Attr -> Text -> Blocks
codeBlockWith :: Attr -> Text -> Many Block
codeBlockWith Attr
attrs = Block -> Many Block
forall a. a -> Many a
singleton (Block -> Many Block) -> (Text -> Block) -> Text -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attr -> Text -> Block
CodeBlock Attr
attrs

-- | A plain code block.
codeBlock :: Text -> Blocks
codeBlock :: Text -> Many Block
codeBlock = Attr -> Text -> Many Block
codeBlockWith Attr
nullAttr

rawBlock :: Text -> Text -> Blocks
rawBlock :: Text -> Text -> Many Block
rawBlock Text
format = Block -> Many Block
forall a. a -> Many a
singleton (Block -> Many Block) -> (Text -> Block) -> Text -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Format -> Text -> Block
RawBlock (Text -> Format
Format Text
format)

blockQuote :: Blocks -> Blocks
blockQuote :: Many Block -> Many Block
blockQuote = Block -> Many Block
forall a. a -> Many a
singleton (Block -> Many Block)
-> (Many Block -> Block) -> Many Block -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Block] -> Block
BlockQuote ([Block] -> Block)
-> (Many Block -> [Block]) -> Many Block -> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Block -> [Block]
forall a. Many a -> [a]
toList

-- | Ordered list with attributes.
orderedListWith :: ListAttributes -> [Blocks] -> Blocks
orderedListWith :: ListAttributes -> [Many Block] -> Many Block
orderedListWith ListAttributes
attrs = Block -> Many Block
forall a. a -> Many a
singleton (Block -> Many Block)
-> ([Many Block] -> Block) -> [Many Block] -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ListAttributes -> [[Block]] -> Block
OrderedList ListAttributes
attrs ([[Block]] -> Block)
-> ([Many Block] -> [[Block]]) -> [Many Block] -> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
.  (Many Block -> [Block]) -> [Many Block] -> [[Block]]
forall a b. (a -> b) -> [a] -> [b]
map Many Block -> [Block]
forall a. Many a -> [a]
toList

-- | Ordered list with default attributes.
orderedList :: [Blocks] -> Blocks
orderedList :: [Many Block] -> Many Block
orderedList = ListAttributes -> [Many Block] -> Many Block
orderedListWith (Int
1, ListNumberStyle
DefaultStyle, ListNumberDelim
DefaultDelim)

bulletList :: [Blocks] -> Blocks
bulletList :: [Many Block] -> Many Block
bulletList = Block -> Many Block
forall a. a -> Many a
singleton (Block -> Many Block)
-> ([Many Block] -> Block) -> [Many Block] -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Block]] -> Block
BulletList ([[Block]] -> Block)
-> ([Many Block] -> [[Block]]) -> [Many Block] -> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Many Block -> [Block]) -> [Many Block] -> [[Block]]
forall a b. (a -> b) -> [a] -> [b]
map Many Block -> [Block]
forall a. Many a -> [a]
toList

definitionList :: [(Inlines, [Blocks])] -> Blocks
definitionList :: [(Many Inline, [Many Block])] -> Many Block
definitionList = Block -> Many Block
forall a. a -> Many a
singleton (Block -> Many Block)
-> ([(Many Inline, [Many Block])] -> Block)
-> [(Many Inline, [Many Block])]
-> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [([Inline], [[Block]])] -> Block
DefinitionList ([([Inline], [[Block]])] -> Block)
-> ([(Many Inline, [Many Block])] -> [([Inline], [[Block]])])
-> [(Many Inline, [Many Block])]
-> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
.  ((Many Inline, [Many Block]) -> ([Inline], [[Block]]))
-> [(Many Inline, [Many Block])] -> [([Inline], [[Block]])]
forall a b. (a -> b) -> [a] -> [b]
map (Many Inline -> [Inline]
forall a. Many a -> [a]
toList (Many Inline -> [Inline])
-> ([Many Block] -> [[Block]])
-> (Many Inline, [Many Block])
-> ([Inline], [[Block]])
forall b c b' c'. (b -> c) -> (b' -> c') -> (b, b') -> (c, c')
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** (Many Block -> [Block]) -> [Many Block] -> [[Block]]
forall a b. (a -> b) -> [a] -> [b]
map Many Block -> [Block]
forall a. Many a -> [a]
toList)

header :: Int  -- ^ Level
       -> Inlines
       -> Blocks
header :: Int -> Many Inline -> Many Block
header = Attr -> Int -> Many Inline -> Many Block
headerWith Attr
nullAttr

headerWith :: Attr -> Int -> Inlines -> Blocks
headerWith :: Attr -> Int -> Many Inline -> Many Block
headerWith Attr
attr Int
level = Block -> Many Block
forall a. a -> Many a
singleton (Block -> Many Block)
-> (Many Inline -> Block) -> Many Inline -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Attr -> [Inline] -> Block
Header Int
level Attr
attr ([Inline] -> Block)
-> (Many Inline -> [Inline]) -> Many Inline -> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> [Inline]
forall a. Many a -> [a]
toList

horizontalRule :: Blocks
horizontalRule :: Many Block
horizontalRule = Block -> Many Block
forall a. a -> Many a
singleton Block
HorizontalRule

cellWith :: Attr
         -> Alignment
         -> RowSpan
         -> ColSpan
         -> Blocks
         -> Cell
cellWith :: Attr -> Alignment -> RowSpan -> ColSpan -> Many Block -> Cell
cellWith Attr
at Alignment
a RowSpan
r ColSpan
c = Attr -> Alignment -> RowSpan -> ColSpan -> [Block] -> Cell
Cell Attr
at Alignment
a RowSpan
r ColSpan
c ([Block] -> Cell) -> (Many Block -> [Block]) -> Many Block -> Cell
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Block -> [Block]
forall a. Many a -> [a]
toList

cell :: Alignment
     -> RowSpan
     -> ColSpan
     -> Blocks
     -> Cell
cell :: Alignment -> RowSpan -> ColSpan -> Many Block -> Cell
cell = Attr -> Alignment -> RowSpan -> ColSpan -> Many Block -> Cell
cellWith Attr
nullAttr

-- | A 1×1 cell with default alignment.
simpleCell :: Blocks -> Cell
simpleCell :: Many Block -> Cell
simpleCell = Alignment -> RowSpan -> ColSpan -> Many Block -> Cell
cell Alignment
AlignDefault RowSpan
1 ColSpan
1

-- | A 1×1 empty cell.
emptyCell :: Cell
emptyCell :: Cell
emptyCell = Many Block -> Cell
simpleCell Many Block
forall a. Monoid a => a
mempty

-- | Table builder. Performs normalization with 'normalizeTableHead',
-- 'normalizeTableBody', and 'normalizeTableFoot'. The number of table
-- columns is given by the length of @['ColSpec']@.
table :: Caption
      -> [ColSpec]
      -> TableHead
      -> [TableBody]
      -> TableFoot
      -> Blocks
table :: Caption
-> [ColSpec] -> TableHead -> [TableBody] -> TableFoot -> Many Block
table = Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Many Block
tableWith Attr
nullAttr

tableWith :: Attr
          -> Caption
          -> [ColSpec]
          -> TableHead
          -> [TableBody]
          -> TableFoot
          -> Blocks
tableWith :: Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Many Block
tableWith Attr
attr Caption
capt [ColSpec]
specs TableHead
th [TableBody]
tbs TableFoot
tf
  = Block -> Many Block
forall a. a -> Many a
singleton (Block -> Many Block) -> Block -> Many Block
forall a b. (a -> b) -> a -> b
$ Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
Table Attr
attr Caption
capt [ColSpec]
specs TableHead
th' [TableBody]
tbs' TableFoot
tf'
  where
    twidth :: Int
twidth = [ColSpec] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ColSpec]
specs
    th' :: TableHead
th'  = Int -> TableHead -> TableHead
normalizeTableHead Int
twidth TableHead
th
    tbs' :: [TableBody]
tbs' = (TableBody -> TableBody) -> [TableBody] -> [TableBody]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> TableBody -> TableBody
normalizeTableBody Int
twidth) [TableBody]
tbs
    tf' :: TableFoot
tf'  = Int -> TableFoot -> TableFoot
normalizeTableFoot Int
twidth TableFoot
tf

-- | A simple table without a caption.
simpleTable :: [Blocks]   -- ^ Headers
            -> [[Blocks]] -- ^ Rows
            -> Blocks
simpleTable :: [Many Block] -> [[Many Block]] -> Many Block
simpleTable [Many Block]
headers [[Many Block]]
rows =
  Caption
-> [ColSpec] -> TableHead -> [TableBody] -> TableFoot -> Many Block
table Caption
emptyCaption (Int -> ColSpec -> [ColSpec]
forall a. Int -> a -> [a]
replicate Int
numcols ColSpec
defaults) TableHead
th [TableBody
tb] TableFoot
tf
  where defaults :: ColSpec
defaults = (Alignment
AlignDefault, ColWidth
ColWidthDefault)
        numcols :: Int
numcols  = [Int] -> Int
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (([Many Block] -> Int) -> [[Many Block]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map [Many Block] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Many Block]
headers[Many Block] -> [[Many Block]] -> [[Many Block]]
forall a. a -> [a] -> [a]
:[[Many Block]]
rows))
        toRow :: [Many Block] -> Row
toRow = Attr -> [Cell] -> Row
Row Attr
nullAttr ([Cell] -> Row) -> ([Many Block] -> [Cell]) -> [Many Block] -> Row
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Many Block -> Cell) -> [Many Block] -> [Cell]
forall a b. (a -> b) -> [a] -> [b]
map Many Block -> Cell
simpleCell
        toHeaderRow :: t a -> [Row]
toHeaderRow t a
l
          | t a -> Bool
forall a. t a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null t a
l    = []
          | Bool
otherwise = [[Many Block] -> Row
toRow [Many Block]
headers]
        th :: TableHead
th = Attr -> [Row] -> TableHead
TableHead Attr
nullAttr ([Many Block] -> [Row]
forall {t :: * -> *} {a}. Foldable t => t a -> [Row]
toHeaderRow [Many Block]
headers)
        tb :: TableBody
tb = Attr -> RowHeadColumns -> [Row] -> [Row] -> TableBody
TableBody Attr
nullAttr RowHeadColumns
0 [] ([Row] -> TableBody) -> [Row] -> TableBody
forall a b. (a -> b) -> a -> b
$ ([Many Block] -> Row) -> [[Many Block]] -> [Row]
forall a b. (a -> b) -> [a] -> [b]
map [Many Block] -> Row
toRow [[Many Block]]
rows
        tf :: TableFoot
tf = Attr -> [Row] -> TableFoot
TableFoot Attr
nullAttr []

figure :: Caption -> Blocks -> Blocks
figure :: Caption -> Many Block -> Many Block
figure = Attr -> Caption -> Many Block -> Many Block
figureWith Attr
nullAttr

figureWith :: Attr -> Caption -> Blocks -> Blocks
figureWith :: Attr -> Caption -> Many Block -> Many Block
figureWith Attr
attr Caption
capt = Block -> Many Block
forall a. a -> Many a
singleton (Block -> Many Block)
-> (Many Block -> Block) -> Many Block -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attr -> Caption -> [Block] -> Block
Figure Attr
attr Caption
capt ([Block] -> Block)
-> (Many Block -> [Block]) -> Many Block -> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Block -> [Block]
forall a. Many a -> [a]
toList

caption :: Maybe ShortCaption -> Blocks -> Caption
caption :: Maybe [Inline] -> Many Block -> Caption
caption Maybe [Inline]
x = Maybe [Inline] -> [Block] -> Caption
Caption Maybe [Inline]
x ([Block] -> Caption)
-> (Many Block -> [Block]) -> Many Block -> Caption
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Block -> [Block]
forall a. Many a -> [a]
toList

simpleCaption :: Blocks -> Caption
simpleCaption :: Many Block -> Caption
simpleCaption = Maybe [Inline] -> Many Block -> Caption
caption Maybe [Inline]
forall a. Maybe a
Nothing

emptyCaption :: Caption
emptyCaption :: Caption
emptyCaption = Many Block -> Caption
simpleCaption Many Block
forall a. Monoid a => a
mempty

-- | Creates a simple figure from attributes, a figure caption, an image
-- path and image title. The attributes are used as the image
-- attributes.
simpleFigureWith :: Attr -> Inlines -> Text -> Text -> Blocks
simpleFigureWith :: Attr -> Many Inline -> Text -> Text -> Many Block
simpleFigureWith Attr
attr Many Inline
figureCaption Text
url Text
title =
  Caption -> Many Block -> Many Block
figure (Many Block -> Caption
simpleCaption (Many Inline -> Many Block
plain Many Inline
figureCaption)) (Many Block -> Many Block)
-> (Many Inline -> Many Block) -> Many Inline -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Inline -> Many Block
plain (Many Inline -> Many Block) -> Many Inline -> Many Block
forall a b. (a -> b) -> a -> b
$
     Attr -> Text -> Text -> Many Inline -> Many Inline
imageWith Attr
attr Text
url Text
title Many Inline
forall a. Monoid a => a
mempty

simpleFigure :: Inlines -> Text -> Text -> Blocks
simpleFigure :: Many Inline -> Text -> Text -> Many Block
simpleFigure = Attr -> Many Inline -> Text -> Text -> Many Block
simpleFigureWith Attr
nullAttr

divWith :: Attr -> Blocks -> Blocks
divWith :: Attr -> Many Block -> Many Block
divWith Attr
attr = Block -> Many Block
forall a. a -> Many a
singleton (Block -> Many Block)
-> (Many Block -> Block) -> Many Block -> Many Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attr -> [Block] -> Block
Div Attr
attr ([Block] -> Block)
-> (Many Block -> [Block]) -> Many Block -> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Many Block -> [Block]
forall a. Many a -> [a]
toList

-- | Normalize the 'TableHead' with 'clipRows' and 'placeRowSection'
-- so that when placed on a grid with the given width and a height
-- equal to the number of rows in the initial 'TableHead', there will
-- be no empty spaces or overlapping cells, and the cells will not
-- protrude beyond the grid.
normalizeTableHead :: Int -> TableHead -> TableHead
normalizeTableHead :: Int -> TableHead -> TableHead
normalizeTableHead Int
twidth (TableHead Attr
attr [Row]
rows)
  = Attr -> [Row] -> TableHead
TableHead Attr
attr ([Row] -> TableHead) -> [Row] -> TableHead
forall a b. (a -> b) -> a -> b
$ Int -> [Row] -> [Row]
normalizeHeaderSection Int
twidth [Row]
rows

-- | Normalize the intermediate head and body section of a
-- 'TableBody', as in 'normalizeTableHead', but additionally ensure
-- that row head cells do not go beyond the row head inside the
-- intermediate body.
normalizeTableBody :: Int -> TableBody -> TableBody
normalizeTableBody :: Int -> TableBody -> TableBody
normalizeTableBody Int
twidth (TableBody Attr
attr RowHeadColumns
rhc [Row]
th [Row]
tb)
  = Attr -> RowHeadColumns -> [Row] -> [Row] -> TableBody
TableBody Attr
attr
              RowHeadColumns
rhc'
              (Int -> [Row] -> [Row]
normalizeHeaderSection Int
twidth [Row]
th)
              (Int -> RowHeadColumns -> [Row] -> [Row]
normalizeBodySection Int
twidth RowHeadColumns
rhc' [Row]
tb)
  where
    rhc' :: RowHeadColumns
rhc' = RowHeadColumns -> RowHeadColumns -> RowHeadColumns
forall a. Ord a => a -> a -> a
max RowHeadColumns
0 (RowHeadColumns -> RowHeadColumns)
-> RowHeadColumns -> RowHeadColumns
forall a b. (a -> b) -> a -> b
$ RowHeadColumns -> RowHeadColumns -> RowHeadColumns
forall a. Ord a => a -> a -> a
min (Int -> RowHeadColumns
RowHeadColumns Int
twidth) RowHeadColumns
rhc

-- | Normalize the 'TableFoot', as in 'normalizeTableHead'.
normalizeTableFoot :: Int -> TableFoot -> TableFoot
normalizeTableFoot :: Int -> TableFoot -> TableFoot
normalizeTableFoot Int
twidth (TableFoot Attr
attr [Row]
rows)
  = Attr -> [Row] -> TableFoot
TableFoot Attr
attr ([Row] -> TableFoot) -> [Row] -> TableFoot
forall a b. (a -> b) -> a -> b
$ Int -> [Row] -> [Row]
normalizeHeaderSection Int
twidth [Row]
rows

normalizeHeaderSection :: Int -- ^ The desired width of the table
                       -> [Row]
                       -> [Row]
normalizeHeaderSection :: Int -> [Row] -> [Row]
normalizeHeaderSection Int
twidth [Row]
rows
  = [RowSpan] -> [Row] -> [Row]
normalizeRows' (Int -> RowSpan -> [RowSpan]
forall a. Int -> a -> [a]
replicate Int
twidth RowSpan
1) ([Row] -> [Row]) -> [Row] -> [Row]
forall a b. (a -> b) -> a -> b
$ [Row] -> [Row]
clipRows [Row]
rows
  where
    normalizeRows' :: [RowSpan] -> [Row] -> [Row]
normalizeRows' [RowSpan]
oldHang (Row Attr
attr [Cell]
cells:[Row]
rs)
      = let ([RowSpan]
newHang, [Cell]
cells', [Cell]
_) = [RowSpan] -> [Cell] -> ([RowSpan], [Cell], [Cell])
placeRowSection [RowSpan]
oldHang ([Cell] -> ([RowSpan], [Cell], [Cell]))
-> [Cell] -> ([RowSpan], [Cell], [Cell])
forall a b. (a -> b) -> a -> b
$ [Cell]
cells [Cell] -> [Cell] -> [Cell]
forall a. Semigroup a => a -> a -> a
<> Cell -> [Cell]
forall a. a -> [a]
repeat Cell
emptyCell
            rs' :: [Row]
rs' = [RowSpan] -> [Row] -> [Row]
normalizeRows' [RowSpan]
newHang [Row]
rs
        in Attr -> [Cell] -> Row
Row Attr
attr [Cell]
cells' Row -> [Row] -> [Row]
forall a. a -> [a] -> [a]
: [Row]
rs'
    normalizeRows' [RowSpan]
_ [] = []

normalizeBodySection :: Int -- ^ The desired width of the table
                     -> RowHeadColumns -- ^ The width of the row head,
                                       -- between 0 and the table
                                       -- width
                     -> [Row]
                     -> [Row]
normalizeBodySection :: Int -> RowHeadColumns -> [Row] -> [Row]
normalizeBodySection Int
twidth (RowHeadColumns Int
rhc) [Row]
rows
  = [RowSpan] -> [RowSpan] -> [Row] -> [Row]
normalizeRows' (Int -> RowSpan -> [RowSpan]
forall a. Int -> a -> [a]
replicate Int
rhc RowSpan
1) (Int -> RowSpan -> [RowSpan]
forall a. Int -> a -> [a]
replicate Int
rbc RowSpan
1) ([Row] -> [Row]) -> [Row] -> [Row]
forall a b. (a -> b) -> a -> b
$ [Row] -> [Row]
clipRows [Row]
rows
  where
    rbc :: Int
rbc = Int
twidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
rhc

    normalizeRows' :: [RowSpan] -> [RowSpan] -> [Row] -> [Row]
normalizeRows' [RowSpan]
headHang [RowSpan]
bodyHang (Row Attr
attr [Cell]
cells:[Row]
rs)
      = let ([RowSpan]
headHang', [Cell]
rowHead, [Cell]
cells') = [RowSpan] -> [Cell] -> ([RowSpan], [Cell], [Cell])
placeRowSection [RowSpan]
headHang ([Cell] -> ([RowSpan], [Cell], [Cell]))
-> [Cell] -> ([RowSpan], [Cell], [Cell])
forall a b. (a -> b) -> a -> b
$ [Cell]
cells [Cell] -> [Cell] -> [Cell]
forall a. Semigroup a => a -> a -> a
<> Cell -> [Cell]
forall a. a -> [a]
repeat Cell
emptyCell
            ([RowSpan]
bodyHang', [Cell]
rowBody, [Cell]
_)      = [RowSpan] -> [Cell] -> ([RowSpan], [Cell], [Cell])
placeRowSection [RowSpan]
bodyHang [Cell]
cells'
            rs' :: [Row]
rs' = [RowSpan] -> [RowSpan] -> [Row] -> [Row]
normalizeRows' [RowSpan]
headHang' [RowSpan]
bodyHang' [Row]
rs
        in Attr -> [Cell] -> Row
Row Attr
attr ([Cell]
rowHead [Cell] -> [Cell] -> [Cell]
forall a. Semigroup a => a -> a -> a
<> [Cell]
rowBody) Row -> [Row] -> [Row]
forall a. a -> [a] -> [a]
: [Row]
rs'
    normalizeRows' [RowSpan]
_ [RowSpan]
_ [] = []

-- | Normalize the given list of cells so that they fit on a single
-- grid row. The 'RowSpan' values of the cells are assumed to be valid
-- (clamped to lie between 1 and the remaining grid height). The cells
-- in the list are also assumed to be able to fill the entire grid
-- row. These conditions can be met by appending @repeat 'emptyCell'@
-- to the @['Cell']@ list and using 'clipRows' on the entire table
-- section beforehand.
--
-- Normalization follows the principle that cells are placed on a grid
-- row in order, each at the first available grid position from the
-- left, having their 'ColSpan' reduced if they would overlap with a
-- previous cell, stopping once the row is filled. Only the dimensions
-- of cells are changed, and only of those cells that fit on the row.
--
-- Possible overlap is detected using the given @['RowSpan']@, which
-- is the "overhang" of the previous grid row, a list of the heights
-- of cells that descend through the previous row, reckoned
-- /only from the previous row/.
-- Its length should be the width (number of columns) of the current
-- grid row.
--
-- For example, the numbers in the following headerless grid table
-- represent the overhang at each grid position for that table:
--
-- @
--     1   1   1   1
--   +---+---+---+---+
--   | 1 | 2   2 | 3 |
--   +---+       +   +
--   | 1 | 1   1 | 2 |
--   +---+---+---+   +
--   | 1   1 | 1 | 1 |
--   +---+---+---+---+
-- @
--
-- In any table, the row before the first has an overhang of
-- @replicate tableWidth 1@, since there are no cells to descend into
-- the table from there.  The overhang of the first row in the example
-- is @[1, 2, 2, 3]@.
--
-- So if after 'clipRows' the unnormalized second row of that example
-- table were
--
-- > r = [("a", 1, 2),("b", 2, 3)] -- the cells displayed as (label, RowSpan, ColSpan) only
--
-- a correct invocation of 'placeRowSection' to normalize it would be
--
-- >>> placeRowSection [1, 2, 2, 3] $ r ++ repeat emptyCell
-- ([1, 1, 1, 2], [("a", 1, 1)], [("b", 2, 3)] ++ repeat emptyCell) -- wouldn't stop printing, of course
--
-- and if the third row were only @[("c", 1, 2)]@, then the expression
-- would be
--
-- >>> placeRowSection [1, 1, 1, 2] $ [("c", 1, 2)] ++ repeat emptyCell
-- ([1, 1, 1, 1], [("c", 1, 2), emptyCell], repeat emptyCell)
placeRowSection :: [RowSpan] -- ^ The overhang of the previous grid
                             -- row
                -> [Cell]    -- ^ The cells to lay on the grid row
                -> ([RowSpan], [Cell], [Cell]) -- ^ The overhang of
                                               -- the current grid
                                               -- row, the normalized
                                               -- cells that fit on
                                               -- the current row, and
                                               -- the remaining
                                               -- unmodified cells
placeRowSection :: [RowSpan] -> [Cell] -> ([RowSpan], [Cell], [Cell])
placeRowSection [RowSpan]
oldHang [Cell]
cellStream
  -- If the grid has overhang at our position, try to re-lay in
  -- the next position.
  | RowSpan
o:[RowSpan]
os <- [RowSpan]
oldHang
  , RowSpan
o RowSpan -> RowSpan -> Bool
forall a. Ord a => a -> a -> Bool
> RowSpan
1 = let ([RowSpan]
newHang, [Cell]
newCell, [Cell]
cellStream') = [RowSpan] -> [Cell] -> ([RowSpan], [Cell], [Cell])
placeRowSection [RowSpan]
os [Cell]
cellStream
            in (RowSpan
o RowSpan -> RowSpan -> RowSpan
forall a. Num a => a -> a -> a
- RowSpan
1 RowSpan -> [RowSpan] -> [RowSpan]
forall a. a -> [a] -> [a]
: [RowSpan]
newHang, [Cell]
newCell, [Cell]
cellStream')
  -- Otherwise if there is any available width, place the cell and
  -- continue.
  | Cell
c:[Cell]
cellStream' <- [Cell]
cellStream
  , (RowSpan
h, ColSpan
w) <- Cell -> (RowSpan, ColSpan)
getDim Cell
c
  , ColSpan
w' <- ColSpan -> ColSpan -> ColSpan
forall a. Ord a => a -> a -> a
max ColSpan
1 ColSpan
w
  , (Int
n, [RowSpan]
oldHang') <- (RowSpan -> Bool) -> Int -> [RowSpan] -> (Int, [RowSpan])
forall a. (a -> Bool) -> Int -> [a] -> (Int, [a])
dropAtMostWhile (RowSpan -> RowSpan -> Bool
forall a. Eq a => a -> a -> Bool
== RowSpan
1) (ColSpan -> Int
getColSpan ColSpan
w') [RowSpan]
oldHang
  , Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0
  = let w'' :: ColSpan
w'' = ColSpan -> ColSpan -> ColSpan
forall a. Ord a => a -> a -> a
min (Int -> ColSpan
ColSpan Int
n) ColSpan
w'
        c' :: Cell
c' = ColSpan -> Cell -> Cell
setW ColSpan
w'' Cell
c
        ([RowSpan]
newHang, [Cell]
newCell, [Cell]
remainCell) = [RowSpan] -> [Cell] -> ([RowSpan], [Cell], [Cell])
placeRowSection [RowSpan]
oldHang' [Cell]
cellStream'
    in (Int -> RowSpan -> [RowSpan]
forall a. Int -> a -> [a]
replicate (ColSpan -> Int
getColSpan ColSpan
w'') RowSpan
h [RowSpan] -> [RowSpan] -> [RowSpan]
forall a. Semigroup a => a -> a -> a
<> [RowSpan]
newHang, Cell
c' Cell -> [Cell] -> [Cell]
forall a. a -> [a] -> [a]
: [Cell]
newCell, [Cell]
remainCell)
  -- Otherwise there is no room in the section, or not enough cells
  -- were given.
  | Bool
otherwise = ([], [], [Cell]
cellStream)
  where
    getColSpan :: ColSpan -> Int
getColSpan (ColSpan Int
w) = Int
w
    getDim :: Cell -> (RowSpan, ColSpan)
getDim (Cell Attr
_ Alignment
_ RowSpan
h ColSpan
w [Block]
_) = (RowSpan
h, ColSpan
w)
    setW :: ColSpan -> Cell -> Cell
setW ColSpan
w (Cell Attr
a Alignment
ma RowSpan
h ColSpan
_ [Block]
b) = Attr -> Alignment -> RowSpan -> ColSpan -> [Block] -> Cell
Cell Attr
a Alignment
ma RowSpan
h ColSpan
w [Block]
b

    dropAtMostWhile :: (a -> Bool) -> Int -> [a] -> (Int, [a])
    dropAtMostWhile :: forall a. (a -> Bool) -> Int -> [a] -> (Int, [a])
dropAtMostWhile a -> Bool
p Int
n = Int -> [a] -> (Int, [a])
go Int
0
      where
        go :: Int -> [a] -> (Int, [a])
go Int
acc (a
l:[a]
ls) | a -> Bool
p a
l Bool -> Bool -> Bool
&& Int
acc Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n = Int -> [a] -> (Int, [a])
go (Int
accInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) [a]
ls
        go Int
acc [a]
l = (Int
acc, [a]
l)

-- | Ensure that the height of each cell in a table section lies
-- between 1 and the distance from its row to the end of the
-- section. So if there were four rows in the input list, the cells in
-- the second row would have their height clamped between 1 and 3.
clipRows :: [Row] -> [Row]
clipRows :: [Row] -> [Row]
clipRows [Row]
rows
  = let totalHeight :: RowSpan
totalHeight = Int -> RowSpan
RowSpan (Int -> RowSpan) -> Int -> RowSpan
forall a b. (a -> b) -> a -> b
$ [Row] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Row]
rows
    in (RowSpan -> Row -> Row) -> [RowSpan] -> [Row] -> [Row]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith RowSpan -> Row -> Row
clipRowH [RowSpan
totalHeight, RowSpan
totalHeight RowSpan -> RowSpan -> RowSpan
forall a. Num a => a -> a -> a
- RowSpan
1..RowSpan
1] [Row]
rows
  where
    getH :: Cell -> RowSpan
getH (Cell Attr
_ Alignment
_ RowSpan
h ColSpan
_ [Block]
_) = RowSpan
h
    setH :: RowSpan -> Cell -> Cell
setH RowSpan
h (Cell Attr
a Alignment
ma RowSpan
_ ColSpan
w [Block]
body) = Attr -> Alignment -> RowSpan -> ColSpan -> [Block] -> Cell
Cell Attr
a Alignment
ma RowSpan
h ColSpan
w [Block]
body
    clipH :: RowSpan -> RowSpan -> Cell -> Cell
clipH RowSpan
low RowSpan
high Cell
c = let h :: RowSpan
h = Cell -> RowSpan
getH Cell
c in RowSpan -> Cell -> Cell
setH (RowSpan -> RowSpan -> RowSpan
forall a. Ord a => a -> a -> a
min RowSpan
high (RowSpan -> RowSpan) -> RowSpan -> RowSpan
forall a b. (a -> b) -> a -> b
$ RowSpan -> RowSpan -> RowSpan
forall a. Ord a => a -> a -> a
max RowSpan
low RowSpan
h) Cell
c
    clipRowH :: RowSpan -> Row -> Row
clipRowH RowSpan
high (Row Attr
attr [Cell]
cells) = Attr -> [Cell] -> Row
Row Attr
attr ([Cell] -> Row) -> [Cell] -> Row
forall a b. (a -> b) -> a -> b
$ (Cell -> Cell) -> [Cell] -> [Cell]
forall a b. (a -> b) -> [a] -> [b]
map (RowSpan -> RowSpan -> Cell -> Cell
clipH RowSpan
1 RowSpan
high) [Cell]
cells