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

deriving instance Generic (Many a)

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

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

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

{-# DEPRECATED isNull "Use null instead" #-}
isNull :: Many a -> Bool
isNull :: forall a. Many a -> Bool
isNull = forall a. Seq a -> Bool
Seq.null forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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) <> :: Inlines -> Inlines -> Inlines
<> (Many Seq Inline
ys) =
    case (forall a. Seq a -> ViewR a
viewr Seq Inline
xs, forall a. Seq a -> ViewL a
viewl Seq Inline
ys) of
      (ViewR Inline
EmptyR, ViewL Inline
_) -> forall a. Seq a -> Many a
Many Seq Inline
ys
      (ViewR Inline
_, ViewL Inline
EmptyL) -> forall a. Seq a -> Many a
Many Seq Inline
xs
      (Seq Inline
xs' :> Inline
x, Inline
y :< Seq Inline
ys') -> forall a. Seq a -> Many a
Many (Seq Inline
meld 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' forall a. Seq a -> a -> Seq a
|> Inline
Space
                          (Inline
Space, Inline
SoftBreak) -> Seq Inline
xs' forall a. Seq a -> a -> Seq a
|> Inline
SoftBreak
                          (Inline
SoftBreak, Inline
Space) -> Seq Inline
xs' forall a. Seq a -> a -> Seq a
|> Inline
SoftBreak
                          (Str Text
t1, Str Text
t2)   -> Seq Inline
xs' forall a. Seq a -> a -> Seq a
|> Text -> Inline
Str (Text
t1 forall a. Semigroup a => a -> a -> a
<> Text
t2)
                          (Emph [Inline]
i1, Emph [Inline]
i2) -> Seq Inline
xs' forall a. Seq a -> a -> Seq a
|> [Inline] -> Inline
Emph ([Inline]
i1 forall a. Semigroup a => a -> a -> a
<> [Inline]
i2)
                          (Underline [Inline]
i1, Underline [Inline]
i2) -> Seq Inline
xs' forall a. Seq a -> a -> Seq a
|> [Inline] -> Inline
Underline ([Inline]
i1 forall a. Semigroup a => a -> a -> a
<> [Inline]
i2)
                          (Strong [Inline]
i1, Strong [Inline]
i2) -> Seq Inline
xs' forall a. Seq a -> a -> Seq a
|> [Inline] -> Inline
Strong ([Inline]
i1 forall a. Semigroup a => a -> a -> a
<> [Inline]
i2)
                          (Subscript [Inline]
i1, Subscript [Inline]
i2) -> Seq Inline
xs' forall a. Seq a -> a -> Seq a
|> [Inline] -> Inline
Subscript ([Inline]
i1 forall a. Semigroup a => a -> a -> a
<> [Inline]
i2)
                          (Superscript [Inline]
i1, Superscript [Inline]
i2) -> Seq Inline
xs' forall a. Seq a -> a -> Seq a
|> [Inline] -> Inline
Superscript ([Inline]
i1 forall a. Semigroup a => a -> a -> a
<> [Inline]
i2)
                          (Strikeout [Inline]
i1, Strikeout [Inline]
i2) -> Seq Inline
xs' forall a. Seq a -> a -> Seq a
|> [Inline] -> Inline
Strikeout ([Inline]
i1 forall a. Semigroup a => a -> a -> a
<> [Inline]
i2)
                          (Inline
Space, Inline
LineBreak) -> Seq Inline
xs' forall a. Seq a -> a -> Seq a
|> Inline
LineBreak
                          (Inline
LineBreak, Inline
Space) -> Seq Inline
xs' forall a. Seq a -> a -> Seq a
|> Inline
LineBreak
                          (Inline
SoftBreak, Inline
LineBreak) -> Seq Inline
xs' forall a. Seq a -> a -> Seq a
|> Inline
LineBreak
                          (Inline
LineBreak, Inline
SoftBreak) -> Seq Inline
xs' forall a. Seq a -> a -> Seq a
|> Inline
LineBreak
                          (Inline
SoftBreak, Inline
SoftBreak) -> Seq Inline
xs' forall a. Seq a -> a -> Seq a
|> Inline
SoftBreak
                          (Inline, Inline)
_                  -> Seq Inline
xs' forall a. Seq a -> a -> Seq a
|> Inline
x forall a. Seq a -> a -> Seq a
|> Inline
y
instance Monoid Inlines where
  mempty :: Inlines
mempty = forall a. Seq a -> Many a
Many forall a. Monoid a => a
mempty
  mappend :: Inlines -> Inlines -> Inlines
mappend = forall a. Semigroup a => a -> a -> a
(<>)

instance IsString Inlines where
   fromString :: String -> Inlines
fromString = Text -> Inlines
text 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 :: Inlines -> Inlines
trimInlines (Many Seq Inline
ils) = forall a. Seq a -> Many a
Many forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> Seq a -> Seq a
Seq.dropWhileL Inline -> Bool
isSp forall a b. (a -> b) -> a -> b
$
                            forall a. (a -> Bool) -> Seq a -> Seq a
Seq.dropWhileR Inline -> Bool
isSp 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 :: Blocks -> Pandoc
doc = Meta -> [Block] -> Pandoc
Pandoc Meta
nullMeta forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Many a -> [a]
toList

class ToMetaValue a where
  toMetaValue :: a -> MetaValue

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

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

instance ToMetaValue Inlines where
  toMetaValue :: Inlines -> MetaValue
toMetaValue = [Inline] -> MetaValue
MetaInlines forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b k. (a -> b) -> Map k a -> Map k b
M.map 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b k. (a -> b) -> Map k a -> Map k b
M.map forall a. ToMetaValue a => a -> MetaValue
toMetaValue forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
key (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 forall a b. (a -> b) -> a -> b
$ 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 forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Text
key (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 forall a b. (a -> b) -> a -> b
$ 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 :: Inlines -> Pandoc -> Pandoc
setTitle = forall a b. (HasMeta a, ToMetaValue b) => Text -> b -> a -> a
setMeta Text
"title"

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

setDate :: Inlines -> Pandoc -> Pandoc
setDate :: Inlines -> Pandoc -> Pandoc
setDate = forall a b. (HasMeta a, ToMetaValue b) => Text -> b -> a -> a
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 -> Inlines
text = forall a. [a] -> Many a
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Text -> Inline
conv 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 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 -> Inlines
str = forall a. a -> Many a
singleton forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Inline
Str

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

rawInline :: Text -> Text -> Inlines
rawInline :: Text -> Text -> Inlines
rawInline Text
format = forall a. a -> Many a
singleton 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 -> Inlines -> Inlines
link = Attr -> Text -> Text -> Inlines -> Inlines
linkWith Attr
nullAttr

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

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

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

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

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

-- Block list builders

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

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

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

-- | A code block with attributes.
codeBlockWith :: Attr -> Text -> Blocks
codeBlockWith :: Attr -> Text -> Blocks
codeBlockWith Attr
attrs = forall a. a -> Many a
singleton 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 -> Blocks
codeBlock = Attr -> Text -> Blocks
codeBlockWith Attr
nullAttr

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

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

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

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

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

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

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

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

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

cellWith :: Attr
         -> Alignment
         -> RowSpan
         -> ColSpan
         -> Blocks
         -> Cell
cellWith :: Attr -> Alignment -> RowSpan -> ColSpan -> Blocks -> 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Many a -> [a]
toList

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

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

-- | A 1×1 empty cell.
emptyCell :: Cell
emptyCell :: Cell
emptyCell = Blocks -> Cell
simpleCell 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 -> Blocks
table = Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Blocks
tableWith Attr
nullAttr

tableWith :: Attr
          -> Caption
          -> [ColSpec]
          -> TableHead
          -> [TableBody]
          -> TableFoot
          -> Blocks
tableWith :: Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Blocks
tableWith Attr
attr Caption
capt [ColSpec]
specs TableHead
th [TableBody]
tbs TableFoot
tf
  = forall a. a -> Many a
singleton 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 = 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' = 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 :: [Blocks] -> [[Blocks]] -> Blocks
simpleTable [Blocks]
headers [[Blocks]]
rows =
  Caption
-> [ColSpec] -> TableHead -> [TableBody] -> TableFoot -> Blocks
table Caption
emptyCaption (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  = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (forall a b. (a -> b) -> [a] -> [b]
map forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Blocks]
headersforall a. a -> [a] -> [a]
:[[Blocks]]
rows))
        toRow :: [Blocks] -> Row
toRow = Attr -> [Cell] -> Row
Row Attr
nullAttr forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Blocks -> Cell
simpleCell
        toHeaderRow :: t a -> [Row]
toHeaderRow t a
l
          | forall (t :: * -> *) a. Foldable t => t a -> Bool
null t a
l    = []
          | Bool
otherwise = [[Blocks] -> Row
toRow [Blocks]
headers]
        th :: TableHead
th = Attr -> [Row] -> TableHead
TableHead Attr
nullAttr (forall {t :: * -> *} {a}. Foldable t => t a -> [Row]
toHeaderRow [Blocks]
headers)
        tb :: TableBody
tb = Attr -> RowHeadColumns -> [Row] -> [Row] -> TableBody
TableBody Attr
nullAttr RowHeadColumns
0 [] forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map [Blocks] -> Row
toRow [[Blocks]]
rows
        tf :: TableFoot
tf = Attr -> [Row] -> TableFoot
TableFoot Attr
nullAttr []

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

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

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

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

emptyCaption :: Caption
emptyCaption :: Caption
emptyCaption = Blocks -> Caption
simpleCaption 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 -> Inlines -> Text -> Text -> Blocks
simpleFigureWith Attr
attr Inlines
figureCaption Text
url Text
title =
  Caption -> Blocks -> Blocks
figure (Blocks -> Caption
simpleCaption (Inlines -> Blocks
plain Inlines
figureCaption)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inlines -> Blocks
plain forall a b. (a -> b) -> a -> b
$
     Attr -> Text -> Text -> Inlines -> Inlines
imageWith Attr
attr Text
url Text
title forall a. Monoid a => a
mempty

simpleFigure :: Inlines -> Text -> Text -> Blocks
simpleFigure :: Inlines -> Text -> Text -> Blocks
simpleFigure = Attr -> Inlines -> Text -> Text -> Blocks
simpleFigureWith Attr
nullAttr

divWith :: Attr -> Blocks -> Blocks
divWith :: Attr -> Blocks -> Blocks
divWith Attr
attr = forall a. a -> Many a
singleton forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attr -> [Block] -> Block
Div Attr
attr forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 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' = forall a. Ord a => a -> a -> a
max RowHeadColumns
0 forall a b. (a -> b) -> a -> b
$ 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 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' (forall a. Int -> a -> [a]
replicate Int
twidth RowSpan
1) 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 forall a b. (a -> b) -> a -> b
$ [Cell]
cells forall a. Semigroup a => a -> a -> a
<> 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' 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' (forall a. Int -> a -> [a]
replicate Int
rhc RowSpan
1) (forall a. Int -> a -> [a]
replicate Int
rbc RowSpan
1) forall a b. (a -> b) -> a -> b
$ [Row] -> [Row]
clipRows [Row]
rows
  where
    rbc :: Int
rbc = Int
twidth 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 forall a b. (a -> b) -> a -> b
$ [Cell]
cells forall a. Semigroup a => a -> a -> a
<> 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 forall a. Semigroup a => a -> a -> a
<> [Cell]
rowBody) 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 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 forall a. Num a => a -> a -> a
- RowSpan
1 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' <- forall a. Ord a => a -> a -> a
max ColSpan
1 ColSpan
w
  , (Int
n, [RowSpan]
oldHang') <- forall a. (a -> Bool) -> Int -> [a] -> (Int, [a])
dropAtMostWhile (forall a. Eq a => a -> a -> Bool
== RowSpan
1) (ColSpan -> Int
getColSpan ColSpan
w') [RowSpan]
oldHang
  , Int
n forall a. Ord a => a -> a -> Bool
> Int
0
  = let w'' :: ColSpan
w'' = 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 (forall a. Int -> a -> [a]
replicate (ColSpan -> Int
getColSpan ColSpan
w'') RowSpan
h forall a. Semigroup a => a -> a -> a
<> [RowSpan]
newHang, Cell
c' 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 forall a. Ord a => a -> a -> Bool
< Int
n = Int -> [a] -> (Int, [a])
go (Int
accforall 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 forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Int
length [Row]
rows
    in forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith RowSpan -> Row -> Row
clipRowH [RowSpan
totalHeight, RowSpan
totalHeight 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 (forall a. Ord a => a -> a -> a
min RowSpan
high forall a b. (a -> b) -> a -> b
$ 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 forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (RowSpan -> RowSpan -> Cell -> Cell
clipH RowSpan
1 RowSpan
high) [Cell]
cells