{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE ScopedTypeVariables #-}

{- |
   Module      : Text.Pandoc.Readers.Typst
   Copyright   : Copyright (C) 2023 John MacFarlane
   License     : GNU GPL, version 2 or above

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

Reads and evaluates a Typst document as a Pandoc AST.
-}
module Text.Pandoc.Readers.Typst
  ( readTypst
  )
where

import Text.Pandoc.Class
import Text.Pandoc.Sources
import Text.Pandoc.Options
import Text.Pandoc.Definition
import Typst ( parseTypst, evaluateTypst )
import Text.Pandoc.Error (PandocError(..))
import Text.Pandoc.Shared (tshow, blocksToInlines)
import Control.Monad.Except (throwError)
import Control.Monad (MonadPlus (mplus), void, guard, foldM)
import qualified Data.Foldable as F
import qualified Data.Map as M
import Data.Maybe (catMaybes, fromMaybe)
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as T
import qualified Text.Pandoc.Builder as B
import Text.Pandoc.Walk
import Text.Parsec
import Text.TeXMath (writeTeX)
import Text.TeXMath.Shared (getSpaceChars)
import Text.Pandoc.Readers.Typst.Math (pMathMany)
import Text.Pandoc.Readers.Typst.Parsing (pTok, ignored, getField, P,
                                          PState(..), defaultPState)
import Typst.Methods (formatNumber, applyPureFunction)
import Typst.Types
import qualified Data.Vector as V

-- import Debug.Trace

-- | Read Typst from an input string and return a Pandoc document.
readTypst :: (PandocMonad m, ToSources a)
           => ReaderOptions -> a -> m Pandoc
readTypst :: forall (m :: * -> *) a.
(PandocMonad m, ToSources a) =>
ReaderOptions -> a -> m Pandoc
readTypst ReaderOptions
_opts a
inp = do
  let sources :: Sources
sources = a -> Sources
forall a. ToSources a => a -> Sources
toSources a
inp
  let inputName :: SourceName
inputName = case Sources
sources of
        Sources ((SourcePos
pos, Text
_):[(SourcePos, Text)]
_) -> SourcePos -> SourceName
sourceName SourcePos
pos
        Sources
_ -> SourceName
""
  case SourceName -> Text -> Either ParseError [Markup]
parseTypst SourceName
inputName (Sources -> Text
sourcesToText Sources
sources) of
    Left ParseError
e -> PandocError -> m Pandoc
forall a. PandocError -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (PandocError -> m Pandoc) -> PandocError -> m Pandoc
forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocParseError (Text -> PandocError) -> Text -> PandocError
forall a b. (a -> b) -> a -> b
$ SourceName -> Text
T.pack (SourceName -> Text) -> SourceName -> Text
forall a b. (a -> b) -> a -> b
$ ParseError -> SourceName
forall a. Show a => a -> SourceName
show ParseError
e
    Right [Markup]
parsed -> do
      let ops :: Operations m
ops = Operations {
                  loadBytes :: SourceName -> m ByteString
loadBytes = SourceName -> m ByteString
forall (m :: * -> *). PandocMonad m => SourceName -> m ByteString
readFileStrict,
                  currentUTCTime :: m UTCTime
currentUTCTime = m UTCTime
forall (m :: * -> *). PandocMonad m => m UTCTime
getCurrentTime,
                  lookupEnvVar :: SourceName -> m (Maybe SourceName)
lookupEnvVar = (Maybe Text -> Maybe SourceName)
-> m (Maybe Text) -> m (Maybe SourceName)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Text -> SourceName) -> Maybe Text -> Maybe SourceName
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> SourceName
T.unpack) (m (Maybe Text) -> m (Maybe SourceName))
-> (SourceName -> m (Maybe Text))
-> SourceName
-> m (Maybe SourceName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> m (Maybe Text)
forall (m :: * -> *). PandocMonad m => Text -> m (Maybe Text)
lookupEnv (Text -> m (Maybe Text))
-> (SourceName -> Text) -> SourceName -> m (Maybe Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceName -> Text
T.pack,
                  checkExistence :: SourceName -> m Bool
checkExistence = SourceName -> m Bool
forall (m :: * -> *). PandocMonad m => SourceName -> m Bool
fileExists }
      Either ParseError Content
res <- Operations m
-> SourceName -> [Markup] -> m (Either ParseError Content)
forall (m :: * -> *).
Monad m =>
Operations m
-> SourceName -> [Markup] -> m (Either ParseError Content)
evaluateTypst Operations m
ops SourceName
inputName [Markup]
parsed
      case Either ParseError Content
res of
        Left ParseError
e -> PandocError -> m Pandoc
forall a. PandocError -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (PandocError -> m Pandoc) -> PandocError -> m Pandoc
forall a b. (a -> b) -> a -> b
$ Text -> PandocError
PandocParseError (Text -> PandocError) -> Text -> PandocError
forall a b. (a -> b) -> a -> b
$ ParseError -> Text
forall a. Show a => a -> Text
tshow ParseError
e
        Right Content
content -> do
          let labs :: [Text]
labs = Seq Content -> [Text]
findLabels [Item (Seq Content)
Content
content]
          ParsecT [Content] PState m Pandoc
-> PState
-> SourceName
-> [Content]
-> m (Either ParseError Pandoc)
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> u -> SourceName -> s -> m (Either ParseError a)
runParserT ParsecT [Content] PState m Pandoc
forall (m :: * -> *). PandocMonad m => P m Pandoc
pPandoc PState
defaultPState{ sLabels = labs }
            SourceName
inputName [Item [Content]
Content
content] m (Either ParseError Pandoc)
-> (Either ParseError Pandoc -> m Pandoc) -> m Pandoc
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
              (ParseError -> m Pandoc)
-> (Pandoc -> m Pandoc) -> Either ParseError Pandoc -> m Pandoc
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (PandocError -> m Pandoc
forall a. PandocError -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (PandocError -> m Pandoc)
-> (ParseError -> PandocError) -> ParseError -> m Pandoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> PandocError
PandocParseError (Text -> PandocError)
-> (ParseError -> Text) -> ParseError -> PandocError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceName -> Text
T.pack (SourceName -> Text)
-> (ParseError -> SourceName) -> ParseError -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError -> SourceName
forall a. Show a => a -> SourceName
show) Pandoc -> m Pandoc
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

pBlockElt :: PandocMonad m => P m B.Blocks
pBlockElt :: forall (m :: * -> *). PandocMonad m => P m Blocks
pBlockElt = ParsecT [Content] PState m Blocks
-> ParsecT [Content] PState m Blocks
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT [Content] PState m Blocks
 -> ParsecT [Content] PState m Blocks)
-> ParsecT [Content] PState m Blocks
-> ParsecT [Content] PState m Blocks
forall a b. (a -> b) -> a -> b
$ do
  Content
res <- (Content -> Bool) -> P m Content
forall (m :: * -> *).
PandocMonad m =>
(Content -> Bool) -> P m Content
pTok (\Content
t -> Content -> Bool
isBlock Content
t Bool -> Bool -> Bool
|| Bool -> Bool
not (Content -> Bool
isInline Content
t))
  -- check for following label
  Maybe Text
mbident <- Maybe Text
-> ParsecT [Content] PState m (Maybe Text)
-> ParsecT [Content] PState m (Maybe Text)
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Maybe Text
forall a. Maybe a
Nothing (ParsecT [Content] PState m (Maybe Text)
 -> ParsecT [Content] PState m (Maybe Text))
-> ParsecT [Content] PState m (Maybe Text)
-> ParsecT [Content] PState m (Maybe Text)
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text)
-> ParsecT [Content] PState m Text
-> ParsecT [Content] PState m (Maybe Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Content] PState m Text
forall (m :: * -> *). PandocMonad m => P m Text
pLab
  case Content
res of
    Elt name :: Identifier
name@(Identifier Text
tname) Maybe SourcePos
pos Map Identifier Val
fields -> do
      case Identifier
-> Map
     Identifier
     (Maybe Text
      -> Map Identifier Val -> ParsecT [Content] PState m Blocks)
-> Maybe
     (Maybe Text
      -> Map Identifier Val -> ParsecT [Content] PState m Blocks)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Identifier
name Map
  Identifier
  (Maybe Text
   -> Map Identifier Val -> ParsecT [Content] PState m Blocks)
forall (m :: * -> *).
PandocMonad m =>
Map Identifier (Maybe Text -> Map Identifier Val -> P m Blocks)
blockHandlers of
        Maybe
  (Maybe Text
   -> Map Identifier Val -> ParsecT [Content] PState m Blocks)
Nothing -> do
          Text -> P m ()
forall (m :: * -> *). PandocMonad m => Text -> P m ()
ignored (Text
"unknown block element " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
tname Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
                   Text
" at " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Maybe SourcePos -> Text
forall a. Show a => a -> Text
tshow Maybe SourcePos
pos)
          Blocks -> ParsecT [Content] PState m Blocks
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Blocks
forall a. Monoid a => a
mempty
        Just Maybe Text
-> Map Identifier Val -> ParsecT [Content] PState m Blocks
handler -> Maybe Text
-> Map Identifier Val -> ParsecT [Content] PState m Blocks
handler Maybe Text
mbident Map Identifier Val
fields
    Content
_ -> Blocks -> ParsecT [Content] PState m Blocks
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Blocks
forall a. Monoid a => a
mempty

pInline :: PandocMonad m => P m B.Inlines
pInline :: forall (m :: * -> *). PandocMonad m => P m Inlines
pInline = ParsecT [Content] PState m Inlines
-> ParsecT [Content] PState m Inlines
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT [Content] PState m Inlines
 -> ParsecT [Content] PState m Inlines)
-> ParsecT [Content] PState m Inlines
-> ParsecT [Content] PState m Inlines
forall a b. (a -> b) -> a -> b
$ do
  Content
res <- (Content -> Bool) -> P m Content
forall (m :: * -> *).
PandocMonad m =>
(Content -> Bool) -> P m Content
pTok (\Content
t -> Content -> Bool
isInline Content
t Bool -> Bool -> Bool
|| Bool -> Bool
not (Content -> Bool
isBlock Content
t))
  case Content
res of
    Txt Text
t -> Inlines -> ParsecT [Content] PState m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Inlines -> ParsecT [Content] PState m Inlines)
-> Inlines -> ParsecT [Content] PState m Inlines
forall a b. (a -> b) -> a -> b
$ Text -> Inlines
B.text Text
t
    Lab Text
name -> Inlines -> ParsecT [Content] PState m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Inlines -> ParsecT [Content] PState m Inlines)
-> Inlines -> ParsecT [Content] PState m Inlines
forall a b. (a -> b) -> a -> b
$ Attr -> Inlines -> Inlines
B.spanWith (Text
name, [], []) Inlines
forall a. Monoid a => a
mempty
    Elt (Identifier Text
tname) Maybe SourcePos
_ Map Identifier Val
_
      | Text
"math." Text -> Text -> Bool
`T.isPrefixOf` Text
tname
      , Text
tname Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
"math.equation" ->
          Text -> Inlines
B.math (Text -> Inlines) -> ([Exp] -> Text) -> [Exp] -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Exp] -> Text
writeTeX ([Exp] -> Inlines)
-> ParsecT [Content] PState m [Exp]
-> ParsecT [Content] PState m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany (Content -> Seq Content
forall a. a -> Seq a
Seq.singleton Content
res)
    Elt name :: Identifier
name@(Identifier Text
tname) Maybe SourcePos
pos Map Identifier Val
fields -> do
      [Text]
labs <- PState -> [Text]
sLabels (PState -> [Text])
-> ParsecT [Content] PState m PState
-> ParsecT [Content] PState m [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Content] PState m PState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
      Bool
labelTarget <- (do Val
result <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Val
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"target" Map Identifier Val
fields
                         case Val
result of
                           VLabel Text
t | Text
t Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
labs -> Bool -> ParsecT [Content] PState m Bool
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
                           Val
_ -> Bool -> ParsecT [Content] PState m Bool
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False)
                  ParsecT [Content] PState m Bool
-> ParsecT [Content] PState m Bool
-> ParsecT [Content] PState m Bool
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> Bool -> ParsecT [Content] PState m Bool
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
      if Text
tname Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"ref" Bool -> Bool -> Bool
&& Bool -> Bool
not Bool
labelTarget
         then do
           -- @foo is a citation unless it links to a lab in the doc:
           let targetToKey :: Identifier -> Identifier
targetToKey (Identifier Text
"target") = Text -> Identifier
Identifier Text
"key"
               targetToKey Identifier
k = Identifier
k
           case Identifier
-> Map
     Identifier
     (Maybe Text
      -> Map Identifier Val -> ParsecT [Content] PState m Inlines)
-> Maybe
     (Maybe Text
      -> Map Identifier Val -> ParsecT [Content] PState m Inlines)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Identifier
"cite" Map
  Identifier
  (Maybe Text
   -> Map Identifier Val -> ParsecT [Content] PState m Inlines)
forall (m :: * -> *).
PandocMonad m =>
Map Identifier (Maybe Text -> Map Identifier Val -> P m Inlines)
inlineHandlers of
             Maybe
  (Maybe Text
   -> Map Identifier Val -> ParsecT [Content] PState m Inlines)
Nothing -> do
               Text -> P m ()
forall (m :: * -> *). PandocMonad m => Text -> P m ()
ignored (Text
"unknown inline element " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
tname Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
                        Text
" at " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Maybe SourcePos -> Text
forall a. Show a => a -> Text
tshow Maybe SourcePos
pos)
               Inlines -> ParsecT [Content] PState m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Inlines
forall a. Monoid a => a
mempty
             Just Maybe Text
-> Map Identifier Val -> ParsecT [Content] PState m Inlines
handler -> Maybe Text
-> Map Identifier Val -> ParsecT [Content] PState m Inlines
handler Maybe Text
forall a. Maybe a
Nothing ((Identifier -> Identifier)
-> Map Identifier Val -> Map Identifier Val
forall k2 k1 a. Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a
M.mapKeys Identifier -> Identifier
targetToKey Map Identifier Val
fields)
         else do
          case Identifier
-> Map
     Identifier
     (Maybe Text
      -> Map Identifier Val -> ParsecT [Content] PState m Inlines)
-> Maybe
     (Maybe Text
      -> Map Identifier Val -> ParsecT [Content] PState m Inlines)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Identifier
name Map
  Identifier
  (Maybe Text
   -> Map Identifier Val -> ParsecT [Content] PState m Inlines)
forall (m :: * -> *).
PandocMonad m =>
Map Identifier (Maybe Text -> Map Identifier Val -> P m Inlines)
inlineHandlers of
            Maybe
  (Maybe Text
   -> Map Identifier Val -> ParsecT [Content] PState m Inlines)
Nothing -> do
              Text -> P m ()
forall (m :: * -> *). PandocMonad m => Text -> P m ()
ignored (Text
"unknown inline element " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
tname Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
                       Text
" at " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Maybe SourcePos -> Text
forall a. Show a => a -> Text
tshow Maybe SourcePos
pos)
              Inlines -> ParsecT [Content] PState m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Inlines
forall a. Monoid a => a
mempty
            Just Maybe Text
-> Map Identifier Val -> ParsecT [Content] PState m Inlines
handler -> Maybe Text
-> Map Identifier Val -> ParsecT [Content] PState m Inlines
handler Maybe Text
forall a. Maybe a
Nothing Map Identifier Val
fields

pPandoc :: PandocMonad m => P m B.Pandoc
pPandoc :: forall (m :: * -> *). PandocMonad m => P m Pandoc
pPandoc = do
  Elt Identifier
"document" Maybe SourcePos
_ Map Identifier Val
fields <- (Content -> Bool) -> P m Content
forall (m :: * -> *).
PandocMonad m =>
(Content -> Bool) -> P m Content
pTok Content -> Bool
isDocument
  Blocks
bs <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> ParsecT [Content] PState m Blocks)
-> ParsecT [Content] PState m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ParsecT [Content] PState m Blocks
-> Seq Content -> ParsecT [Content] PState m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents ParsecT [Content] PState m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks
  Pandoc -> P m Pandoc
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Pandoc -> P m Pandoc) -> Pandoc -> P m Pandoc
forall a b. (a -> b) -> a -> b
$ Blocks -> Pandoc
B.doc Blocks
bs
  -- The following alternative code would add metadata from the
  -- fields on the document element. It is commented out because
  -- the typst metadata doesn't print anything by default, in contrast
  -- to pandoc with its usual templates.  Hence, with this code,
  -- converting a typst document might yield a double title, author, etc.
  --
  -- title <- (getField "title" fields >>= pWithContents pInlines) <|>
  --             pure mempty
  -- authors <- (getField "author" fields >>=
  --                         mapM (pWithContents pInlines) . V.toList) <|>
  --            ((:[]) <$> (getField "author" fields >>=
  --                          (\x -> guard (not (null x)) *>
  --                            pWithContents pInlines x))) <|>
  --             pure []
  -- date <- (getField "date" fields >>= pWithContents pInlines) <|>
  --             pure mempty
  -- keywords <- (getField "keywords" fields >>=
  --                mapM (pWithContents pInlines) . V.toList)
  --               <|> pure []
  -- pure $
  --   (if title == mempty
  --       then id
  --       else B.setMeta "title" title) .
  --   (if null authors
  --       then id
  --       else B.setMeta "author" authors) .
  --   (if null date
  --       then id
  --       else B.setMeta "date" date) .
  --   (if null keywords
  --       then id
  --       else B.setMeta "keywords" keywords) $ B.doc bs

pBlocks :: PandocMonad m => P m B.Blocks
pBlocks :: forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks = [Blocks] -> Blocks
forall a. Monoid a => [a] -> a
mconcat ([Blocks] -> Blocks)
-> ParsecT [Content] PState m [Blocks]
-> ParsecT [Content] PState m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Content] PState m Blocks
-> ParsecT [Content] PState m [Blocks]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many ParsecT [Content] PState m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlock

pBlock :: PandocMonad m => P m B.Blocks
pBlock :: forall (m :: * -> *). PandocMonad m => P m Blocks
pBlock = P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pPara P m Blocks -> P m Blocks -> P m Blocks
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlockElt

pSpace :: PandocMonad m => P m Content
pSpace :: forall (m :: * -> *). PandocMonad m => P m Content
pSpace = (Content -> Bool) -> P m Content
forall (m :: * -> *).
PandocMonad m =>
(Content -> Bool) -> P m Content
pTok
      ( \case
          Txt Text
t | (Char -> Bool) -> Text -> Bool
T.all (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ') Text
t -> Bool
True
          Content
_ -> Bool
False )

pLab :: PandocMonad m => P m Text
pLab :: forall (m :: * -> *). PandocMonad m => P m Text
pLab = ParsecT [Content] PState m Text -> ParsecT [Content] PState m Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT [Content] PState m Text
 -> ParsecT [Content] PState m Text)
-> ParsecT [Content] PState m Text
-> ParsecT [Content] PState m Text
forall a b. (a -> b) -> a -> b
$ do
  ParsecT [Content] PState m Content -> ParsecT [Content] PState m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional ParsecT [Content] PState m Content
forall (m :: * -> *). PandocMonad m => P m Content
pSpace
  Lab Text
t <- (Content -> Bool) -> ParsecT [Content] PState m Content
forall (m :: * -> *).
PandocMonad m =>
(Content -> Bool) -> P m Content
pTok
       ( \case
           Lab Text
_ -> Bool
True
           Content
_ -> Bool
False
       )
  Text -> ParsecT [Content] PState m Text
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
t

isDocument :: Content -> Bool
isDocument :: Content -> Bool
isDocument (Elt Identifier
"document" Maybe SourcePos
_ Map Identifier Val
_) = Bool
True
isDocument Content
_ = Bool
False

isBlock :: Content -> Bool
isBlock :: Content -> Bool
isBlock (Elt Identifier
"raw" Maybe SourcePos
_ Map Identifier Val
fields) = Identifier -> Map Identifier Val -> Maybe Val
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Identifier
"block" Map Identifier Val
fields Maybe Val -> Maybe Val -> Bool
forall a. Eq a => a -> a -> Bool
== Val -> Maybe Val
forall a. a -> Maybe a
Just (Bool -> Val
VBoolean Bool
True)
isBlock (Elt Identifier
name Maybe SourcePos
_ Map Identifier Val
_) = Identifier
name Identifier -> Set Identifier -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set Identifier
blockKeys
isBlock Lab{} = Bool
True
isBlock Content
_ = Bool
False

isInline :: Content -> Bool
isInline :: Content -> Bool
isInline (Elt Identifier
"raw" Maybe SourcePos
_ Map Identifier Val
fields) = Identifier -> Map Identifier Val -> Maybe Val
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Identifier
"block" Map Identifier Val
fields Maybe Val -> Maybe Val -> Bool
forall a. Eq a => a -> a -> Bool
/= Val -> Maybe Val
forall a. a -> Maybe a
Just (Bool -> Val
VBoolean Bool
True)
isInline (Elt Identifier
name Maybe SourcePos
_ Map Identifier Val
_) = Identifier
name Identifier -> Set Identifier -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set Identifier
inlineKeys
isInline Lab{} = Bool
True
isInline Txt{} = Bool
True

blockKeys :: Set.Set Identifier
blockKeys :: Set Identifier
blockKeys = [Identifier] -> Set Identifier
forall a. Ord a => [a] -> Set a
Set.fromList ([Identifier] -> Set Identifier) -> [Identifier] -> Set Identifier
forall a b. (a -> b) -> a -> b
$ Map
  Identifier
  (Maybe Text -> Map Identifier Val -> P PandocPure Blocks)
-> [Identifier]
forall k a. Map k a -> [k]
M.keys
  (Map
  Identifier
  (Maybe Text -> Map Identifier Val -> P PandocPure Blocks)
forall (m :: * -> *).
PandocMonad m =>
Map Identifier (Maybe Text -> Map Identifier Val -> P m Blocks)
blockHandlers :: M.Map Identifier
     (Maybe Text -> M.Map Identifier Val -> P PandocPure B.Blocks))

inlineKeys :: Set.Set Identifier
inlineKeys :: Set Identifier
inlineKeys = [Identifier] -> Set Identifier
forall a. Ord a => [a] -> Set a
Set.fromList ([Identifier] -> Set Identifier) -> [Identifier] -> Set Identifier
forall a b. (a -> b) -> a -> b
$ Map
  Identifier
  (Maybe Text -> Map Identifier Val -> P PandocPure Inlines)
-> [Identifier]
forall k a. Map k a -> [k]
M.keys
  (Map
  Identifier
  (Maybe Text -> Map Identifier Val -> P PandocPure Inlines)
forall (m :: * -> *).
PandocMonad m =>
Map Identifier (Maybe Text -> Map Identifier Val -> P m Inlines)
inlineHandlers :: M.Map Identifier
     (Maybe Text -> M.Map Identifier Val -> P PandocPure B.Inlines))

blockHandlers :: PandocMonad m =>
                   M.Map Identifier
                   (Maybe Text -> M.Map Identifier Val -> P m B.Blocks)
blockHandlers :: forall (m :: * -> *).
PandocMonad m =>
Map Identifier (Maybe Text -> Map Identifier Val -> P m Blocks)
blockHandlers = [(Identifier, Maybe Text -> Map Identifier Val -> P m Blocks)]
-> Map Identifier (Maybe Text -> Map Identifier Val -> P m Blocks)
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
  [(Identifier
"text", \Maybe Text
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      -- sometimes text elements include para breaks
      ParsecT [Content] PState m () -> ParsecT [Content] PState m ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy (ParsecT [Content] PState m () -> ParsecT [Content] PState m ())
-> ParsecT [Content] PState m () -> ParsecT [Content] PState m ()
forall a b. (a -> b) -> a -> b
$ ParsecT [Content] PState m Inlines -> ParsecT [Content] PState m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT [Content] PState m Inlines
 -> ParsecT [Content] PState m ())
-> ParsecT [Content] PState m Inlines
-> ParsecT [Content] PState m ()
forall a b. (a -> b) -> a -> b
$ ParsecT [Content] PState m Inlines
-> Seq Content -> ParsecT [Content] PState m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents ParsecT [Content] PState m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
body
      P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks Seq Content
body)
  ,(Identifier
"box", \Maybe Text
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      Attr -> Blocks -> Blocks
B.divWith (Text
"", [Text
Item [Text]
"box"], []) (Blocks -> Blocks) -> P m Blocks -> P m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks Seq Content
body)
  ,(Identifier
"heading", \Maybe Text
mbident Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      Int
lev <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Int
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"level" Map Identifier Val
fields ParsecT [Content] PState m Int
-> ParsecT [Content] PState m Int -> ParsecT [Content] PState m Int
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> Int -> ParsecT [Content] PState m Int
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
1
      Attr -> Int -> Inlines -> Blocks
B.headerWith (Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"" Maybe Text
mbident,[],[]) Int
lev
         (Inlines -> Blocks)
-> ParsecT [Content] PState m Inlines -> P m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Content] PState m Inlines
-> Seq Content -> ParsecT [Content] PState m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents ParsecT [Content] PState m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
body)
  ,(Identifier
"quote", \Maybe Text
_ Map Identifier Val
fields -> do
      Identifier -> Map Identifier Val -> ParsecT [Content] PState m Bool
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"block" Map Identifier Val
fields ParsecT [Content] PState m Bool
-> (Bool -> ParsecT [Content] PState m ())
-> ParsecT [Content] PState m ()
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> ParsecT [Content] PState m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard
      Blocks
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks
      Blocks
attribution <-
        ((\Inlines
x -> Inlines -> Blocks
B.para (Inlines
"\x2104\xa0" Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> Inlines
x)) (Inlines -> Blocks)
-> ParsecT [Content] PState m Inlines -> P m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
          (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"attribution" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> ParsecT [Content] PState m Inlines)
-> ParsecT [Content] PState m Inlines
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ParsecT [Content] PState m Inlines
-> Seq Content -> ParsecT [Content] PState m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents ParsecT [Content] PState m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines))
        P m Blocks -> P m Blocks -> P m Blocks
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> Blocks -> P m Blocks
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Blocks
forall a. Monoid a => a
mempty
      Blocks -> P m Blocks
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Blocks -> P m Blocks) -> Blocks -> P m Blocks
forall a b. (a -> b) -> a -> b
$ Blocks -> Blocks
B.blockQuote (Blocks -> Blocks) -> Blocks -> Blocks
forall a b. (a -> b) -> a -> b
$ Blocks
body Blocks -> Blocks -> Blocks
forall a. Semigroup a => a -> a -> a
<> Blocks
attribution)
  ,(Identifier
"list", \Maybe Text
_ Map Identifier Val
fields -> do
      [Seq Content]
children <- Vector (Seq Content) -> [Seq Content]
forall a. Vector a -> [a]
V.toList (Vector (Seq Content) -> [Seq Content])
-> ParsecT [Content] PState m (Vector (Seq Content))
-> ParsecT [Content] PState m [Seq Content]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Vector (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"children" Map Identifier Val
fields
      [Blocks] -> Blocks
B.bulletList ([Blocks] -> Blocks)
-> ParsecT [Content] PState m [Blocks] -> P m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Seq Content -> P m Blocks)
-> [Seq Content] -> ParsecT [Content] PState m [Blocks]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks) [Seq Content]
children)
  ,(Identifier
"list.item", \Maybe Text
_ Map Identifier Val
fields -> Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks)
  ,(Identifier
"enum", \Maybe Text
_ Map Identifier Val
fields -> do
      [Seq Content]
children <- Vector (Seq Content) -> [Seq Content]
forall a. Vector a -> [a]
V.toList (Vector (Seq Content) -> [Seq Content])
-> ParsecT [Content] PState m (Vector (Seq Content))
-> ParsecT [Content] PState m [Seq Content]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Vector (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"children" Map Identifier Val
fields
      Maybe Int
mbstart <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Maybe Int)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"start" Map Identifier Val
fields
      Int
start <- case Maybe Int
mbstart of
        Maybe Int
Nothing -> Int -> ParsecT [Content] PState m Int
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
1
        Just Int
x
          | Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 -> Int -> ParsecT [Content] PState m Int
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
x
          | Bool
otherwise -> SourceName -> ParsecT [Content] PState m Int
forall a. SourceName -> ParsecT [Content] PState m a
forall (m :: * -> *) a. MonadFail m => SourceName -> m a
fail SourceName
"number must be positive"
      (Text
numbering :: Text) <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Text
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"numbering" Map Identifier Val
fields ParsecT [Content] PState m Text
-> ParsecT [Content] PState m Text
-> ParsecT [Content] PState m Text
forall a.
ParsecT [Content] PState m a
-> ParsecT [Content] PState m a -> ParsecT [Content] PState m a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` Text -> ParsecT [Content] PState m Text
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
""
      let (ListNumberStyle
sty, ListNumberDelim
delim) =
            case Text
numbering of
              Text
"1." -> (ListNumberStyle
B.Decimal, ListNumberDelim
B.Period)
              Text
"1)" -> (ListNumberStyle
B.Decimal, ListNumberDelim
B.OneParen)
              Text
"(1)" -> (ListNumberStyle
B.Decimal, ListNumberDelim
B.TwoParens)
              Text
"a." -> (ListNumberStyle
B.LowerAlpha, ListNumberDelim
B.Period)
              Text
"a)" -> (ListNumberStyle
B.LowerAlpha, ListNumberDelim
B.OneParen)
              Text
"(a)" -> (ListNumberStyle
B.LowerAlpha, ListNumberDelim
B.TwoParens)
              Text
"A." -> (ListNumberStyle
B.UpperAlpha, ListNumberDelim
B.Period)
              Text
"A)" -> (ListNumberStyle
B.UpperAlpha, ListNumberDelim
B.OneParen)
              Text
"(A)" -> (ListNumberStyle
B.UpperAlpha, ListNumberDelim
B.TwoParens)
              Text
"i." -> (ListNumberStyle
B.LowerRoman, ListNumberDelim
B.Period)
              Text
"i)" -> (ListNumberStyle
B.LowerRoman, ListNumberDelim
B.OneParen)
              Text
"(i)" -> (ListNumberStyle
B.LowerRoman, ListNumberDelim
B.TwoParens)
              Text
"I." -> (ListNumberStyle
B.UpperRoman, ListNumberDelim
B.Period)
              Text
"I)" -> (ListNumberStyle
B.UpperRoman, ListNumberDelim
B.OneParen)
              Text
"(I)" -> (ListNumberStyle
B.UpperRoman, ListNumberDelim
B.TwoParens)
              Text
_ -> (ListNumberStyle
B.DefaultStyle, ListNumberDelim
B.DefaultDelim)
      let listAttr :: (Int, ListNumberStyle, ListNumberDelim)
listAttr = (Int
start, ListNumberStyle
sty, ListNumberDelim
delim)
      (Int, ListNumberStyle, ListNumberDelim) -> [Blocks] -> Blocks
B.orderedListWith (Int, ListNumberStyle, ListNumberDelim)
listAttr ([Blocks] -> Blocks)
-> ParsecT [Content] PState m [Blocks] -> P m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Seq Content -> P m Blocks)
-> [Seq Content] -> ParsecT [Content] PState m [Blocks]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks) [Seq Content]
children)
  ,(Identifier
"enum.item", \Maybe Text
_ Map Identifier Val
fields -> Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks)
  ,(Identifier
"terms", \Maybe Text
_ Map Identifier Val
fields -> do
      [Val]
children <- Vector Val -> [Val]
forall a. Vector a -> [a]
V.toList (Vector Val -> [Val])
-> ParsecT [Content] PState m (Vector Val)
-> ParsecT [Content] PState m [Val]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Vector Val)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"children" Map Identifier Val
fields
      [(Inlines, [Blocks])] -> Blocks
B.definitionList
        ([(Inlines, [Blocks])] -> Blocks)
-> ParsecT [Content] PState m [(Inlines, [Blocks])] -> P m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Val -> ParsecT [Content] PState m (Inlines, [Blocks]))
-> [Val] -> ParsecT [Content] PState m [(Inlines, [Blocks])]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM
          ( \case
              VTermItem Seq Content
t Seq Content
d -> do
                Inlines
t' <- ParsecT [Content] PState m Inlines
-> Seq Content -> ParsecT [Content] PState m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents ParsecT [Content] PState m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
t
                Blocks
d' <- P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks Seq Content
d
                (Inlines, [Blocks])
-> ParsecT [Content] PState m (Inlines, [Blocks])
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Inlines
t', [Item [Blocks]
Blocks
d'])
              Val
_ -> (Inlines, [Blocks])
-> ParsecT [Content] PState m (Inlines, [Blocks])
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Inlines
forall a. Monoid a => a
mempty, [])
          )
          [Val]
children)
  ,(Identifier
"terms.item", \Maybe Text
_ Map Identifier Val
fields -> Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks)
  ,(Identifier
"raw", \Maybe Text
mbident Map Identifier Val
fields -> do
      Text
txt <- (Char -> Bool) -> Text -> Text
T.filter (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\r') (Text -> Text)
-> ParsecT [Content] PState m Text
-> ParsecT [Content] PState m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identifier -> Map Identifier Val -> ParsecT [Content] PState m Text
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"text" Map Identifier Val
fields
      Maybe (Item [Text])
mblang <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Maybe (Item [Text]))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"lang" Map Identifier Val
fields
      let attr :: Attr
attr = (Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"" Maybe Text
mbident, [Text] -> (Item [Text] -> [Text]) -> Maybe (Item [Text]) -> [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\Item [Text]
l -> [Item [Text]
l]) Maybe (Item [Text])
mblang, [])
      Blocks -> P m Blocks
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Blocks -> P m Blocks) -> Blocks -> P m Blocks
forall a b. (a -> b) -> a -> b
$ Attr -> Text -> Blocks
B.codeBlockWith Attr
attr Text
txt)
  ,(Identifier
"parbreak", \Maybe Text
_ Map Identifier Val
_ -> Blocks -> P m Blocks
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Blocks
forall a. Monoid a => a
mempty)
  ,(Identifier
"block", \Maybe Text
mbident Map Identifier Val
fields ->
      Attr -> Blocks -> Blocks
B.divWith (Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"" Maybe Text
mbident, [], [])
        (Blocks -> Blocks) -> P m Blocks -> P m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks))
  ,(Identifier
"place", \Maybe Text
_ Map Identifier Val
fields -> do
      Text -> ParsecT [Content] PState m ()
forall (m :: * -> *). PandocMonad m => Text -> P m ()
ignored Text
"parameters of place"
      Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks)
  ,(Identifier
"columns", \Maybe Text
_ Map Identifier Val
fields -> do
      (Integer
cnt :: Integer) <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m Integer
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"count" Map Identifier Val
fields
      Attr -> Blocks -> Blocks
B.divWith (Text
"", [Text
Item [Text]
"columns-flow"], [(Text
"count", SourceName -> Text
T.pack (Integer -> SourceName
forall a. Show a => a -> SourceName
show Integer
cnt))])
        (Blocks -> Blocks) -> P m Blocks -> P m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks))
  ,(Identifier
"rect", \Maybe Text
_ Map Identifier Val
fields ->
      Attr -> Blocks -> Blocks
B.divWith (Text
"", [Text
Item [Text]
"rect"], []) (Blocks -> Blocks) -> P m Blocks -> P m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks))
  ,(Identifier
"circle", \Maybe Text
_ Map Identifier Val
fields ->
      Attr -> Blocks -> Blocks
B.divWith (Text
"", [Text
Item [Text]
"circle"], []) (Blocks -> Blocks) -> P m Blocks -> P m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks))
  ,(Identifier
"ellipse", \Maybe Text
_ Map Identifier Val
fields ->
      Attr -> Blocks -> Blocks
B.divWith (Text
"", [Text
Item [Text]
"ellipse"], []) (Blocks -> Blocks) -> P m Blocks -> P m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks))
  ,(Identifier
"polygon", \Maybe Text
_ Map Identifier Val
fields ->
      Attr -> Blocks -> Blocks
B.divWith (Text
"", [Text
Item [Text]
"polygon"], []) (Blocks -> Blocks) -> P m Blocks -> P m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks))
  ,(Identifier
"square", \Maybe Text
_ Map Identifier Val
fields ->
      Attr -> Blocks -> Blocks
B.divWith (Text
"", [Text
Item [Text]
"square"], []) (Blocks -> Blocks) -> P m Blocks -> P m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks))
  ,(Identifier
"align", \Maybe Text
_ Map Identifier Val
fields -> do
      Val
alignment <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Val
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"alignment" Map Identifier Val
fields
      Attr -> Blocks -> Blocks
B.divWith (Text
"", [], [(Text
"align", Val -> Text
repr Val
alignment)])
        (Blocks -> Blocks) -> P m Blocks -> P m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks))
  ,(Identifier
"stack", \Maybe Text
_ Map Identifier Val
fields -> do
      (Direction
dir :: Direction) <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m Direction
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"dir" Map Identifier Val
fields ParsecT [Content] PState m Direction
-> ParsecT [Content] PState m Direction
-> ParsecT [Content] PState m Direction
forall a.
ParsecT [Content] PState m a
-> ParsecT [Content] PState m a -> ParsecT [Content] PState m a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` Direction -> ParsecT [Content] PState m Direction
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Direction
Ltr
      Vector Val
rawchildren <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Vector Val)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"children" Map Identifier Val
fields
      [Blocks]
children <-
        (Val -> P m Blocks) -> [Val] -> ParsecT [Content] PState m [Blocks]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM
          ( \case
              val :: Val
val@(VFraction {}) ->
                Blocks -> P m Blocks
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Blocks -> P m Blocks) -> Blocks -> P m Blocks
forall a b. (a -> b) -> a -> b
$ Attr -> Blocks -> Blocks
B.divWith (Text
"", [], [(Text
"space", Val -> Text
repr Val
val)]) Blocks
forall a. Monoid a => a
mempty
              Val
val -> Val -> ParsecT [Content] PState m (Seq Content)
forall a (m :: * -> *).
(FromVal a, MonadPlus m, MonadFail m) =>
Val -> m a
forall (m :: * -> *).
(MonadPlus m, MonadFail m) =>
Val -> m (Seq Content)
fromVal Val
val ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks
          )
          (Vector Val -> [Val]
forall a. Vector a -> [a]
V.toList Vector Val
rawchildren)
      Blocks -> P m Blocks
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Blocks -> P m Blocks) -> Blocks -> P m Blocks
forall a b. (a -> b) -> a -> b
$
        Attr -> Blocks -> Blocks
B.divWith (Text
"", [], [(Text
"stack", Val -> Text
repr (Direction -> Val
VDirection Direction
dir))]) (Blocks -> Blocks) -> Blocks -> Blocks
forall a b. (a -> b) -> a -> b
$
          [Blocks] -> Blocks
forall a. Monoid a => [a] -> a
mconcat ([Blocks] -> Blocks) -> [Blocks] -> Blocks
forall a b. (a -> b) -> a -> b
$
            (Blocks -> Blocks) -> [Blocks] -> [Blocks]
forall a b. (a -> b) -> [a] -> [b]
map (Attr -> Blocks -> Blocks
B.divWith (Text
"", [], [])) [Blocks]
children)
  ,(Identifier
"grid", \Maybe Text
mbident Map Identifier Val
fields -> Maybe Text -> Map Identifier Val -> P m Blocks
forall (m :: * -> *).
PandocMonad m =>
Maybe Text -> Map Identifier Val -> P m Blocks
parseTable Maybe Text
mbident Map Identifier Val
fields)
  ,(Identifier
"table", \Maybe Text
mbident Map Identifier Val
fields -> Maybe Text -> Map Identifier Val -> P m Blocks
forall (m :: * -> *).
PandocMonad m =>
Maybe Text -> Map Identifier Val -> P m Blocks
parseTable Maybe Text
mbident Map Identifier Val
fields)
  ,(Identifier
"figure", \Maybe Text
mbident Map Identifier Val
fields -> do
      Blocks
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks
      (Maybe (Seq Content)
mbCaption :: Maybe (Seq Content)) <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Maybe (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"caption" Map Identifier Val
fields
      (Blocks
caption :: B.Blocks) <- P m Blocks
-> (Seq Content -> P m Blocks) -> Maybe (Seq Content) -> P m Blocks
forall b a. b -> (a -> b) -> Maybe a -> b
maybe P m Blocks
forall a. Monoid a => a
mempty (P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks) Maybe (Seq Content)
mbCaption
      Blocks -> P m Blocks
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Blocks -> P m Blocks) -> Blocks -> P m Blocks
forall a b. (a -> b) -> a -> b
$ case Blocks -> [Block]
forall a. Many a -> [a]
B.toList Blocks
body of
        [B.Table Attr
attr Caption
_ [ColSpec]
colspecs TableHead
thead [TableBody]
tbodies TableFoot
tfoot] ->
          Block -> Blocks
forall a. a -> Many a
B.singleton
            (Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
B.Table Attr
attr (Maybe [Inline] -> [Block] -> Caption
B.Caption Maybe [Inline]
forall a. Maybe a
Nothing (Blocks -> [Block]
forall a. Many a -> [a]
B.toList Blocks
caption)) [ColSpec]
colspecs TableHead
thead [TableBody]
tbodies TableFoot
tfoot)
        [Block]
_ -> Attr -> Caption -> Blocks -> Blocks
B.figureWith (Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"" Maybe Text
mbident, [], [])
                          (Maybe [Inline] -> [Block] -> Caption
B.Caption Maybe [Inline]
forall a. Maybe a
Nothing (Blocks -> [Block]
forall a. Many a -> [a]
B.toList Blocks
caption)) Blocks
body)
  ,(Identifier
"line", \Maybe Text
_ Map Identifier Val
fields ->
      case ( Identifier -> Map Identifier Val -> Maybe Val
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Identifier
"start" Map Identifier Val
fields
              Maybe Val -> Maybe Val -> Maybe Val
forall a b. Maybe a -> Maybe b -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Identifier -> Map Identifier Val -> Maybe Val
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Identifier
"end" Map Identifier Val
fields
              Maybe Val -> Maybe Val -> Maybe Val
forall a b. Maybe a -> Maybe b -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Identifier -> Map Identifier Val -> Maybe Val
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Identifier
"angle" Map Identifier Val
fields ) of
        Maybe Val
Nothing -> Blocks -> P m Blocks
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Blocks
B.horizontalRule
        Maybe Val
_ -> Blocks -> P m Blocks
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Blocks
forall a. Monoid a => a
mempty)
  ,(Identifier
"numbering", \Maybe Text
_ Map Identifier Val
fields -> do
      Val
numStyle <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Val
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"numbering" Map Identifier Val
fields
      (Vector Integer
nums :: V.Vector Integer) <- Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Vector Integer)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"numbers" Map Identifier Val
fields
      let toText :: Val -> a
toText Val
v = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
"" (Maybe a -> a) -> Maybe a -> a
forall a b. (a -> b) -> a -> b
$ Val -> Maybe a
forall a (m :: * -> *).
(FromVal a, MonadPlus m, MonadFail m) =>
Val -> m a
forall (m :: * -> *). (MonadPlus m, MonadFail m) => Val -> m a
fromVal Val
v
      let toNum :: Integer -> Text
toNum Integer
n =
            case Val
numStyle of
              VString Text
t -> Text -> Int -> Text
formatNumber Text
t (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
n)
              VFunction Maybe Identifier
_ Map Identifier Val
_ Function
f ->
                case Function -> [Val] -> Attempt Val
applyPureFunction Function
f [Integer -> Val
VInteger Integer
n] of
                  Success Val
x -> Val -> Text
forall {a}. (IsString a, FromVal a) => Val -> a
toText Val
x
                  Failure SourceName
_ -> Text
"?"
              Val
_ -> Text
"?"
      Blocks -> P m Blocks
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Blocks -> P m Blocks) -> Blocks -> P m Blocks
forall a b. (a -> b) -> a -> b
$ Inlines -> Blocks
B.plain (Inlines -> Blocks)
-> ([Integer] -> Inlines) -> [Integer] -> Blocks
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Inlines
B.text (Text -> Inlines) -> ([Integer] -> Text) -> [Integer] -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat ([Text] -> Text) -> ([Integer] -> [Text]) -> [Integer] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Integer -> Text) -> [Integer] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> Text
toNum ([Integer] -> Blocks) -> [Integer] -> Blocks
forall a b. (a -> b) -> a -> b
$ Vector Integer -> [Integer]
forall a. Vector a -> [a]
V.toList Vector Integer
nums)
  ,(Identifier
"footnote.entry", \Maybe Text
_ Map Identifier Val
fields ->
      Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Blocks) -> P m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Blocks -> Seq Content -> P m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks)
  ]

inlineHandlers :: PandocMonad m =>
    M.Map Identifier (Maybe Text -> M.Map Identifier Val -> P m B.Inlines)
inlineHandlers :: forall (m :: * -> *).
PandocMonad m =>
Map Identifier (Maybe Text -> Map Identifier Val -> P m Inlines)
inlineHandlers = [(Identifier, Maybe Text -> Map Identifier Val -> P m Inlines)]
-> Map Identifier (Maybe Text -> Map Identifier Val -> P m Inlines)
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
  [(Identifier
"ref", \Maybe Text
_ Map Identifier Val
fields -> do
      VLabel Text
target <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Val
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"target" Map Identifier Val
fields
      Val
supplement' <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Val
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"supplement" Map Identifier Val
fields
      Inlines
supplement <- case Val
supplement' of
                      Val
VAuto -> -- TODO for now, until we can locate the element
                        Inlines -> P m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Inlines -> P m Inlines) -> Inlines -> P m Inlines
forall a b. (a -> b) -> a -> b
$ Text -> Inlines
B.text (Text
"[" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
target Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"]")
                      VContent Seq Content
cs -> P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
cs
                      VFunction Maybe Identifier
_ Map Identifier Val
_ Function
_f -> -- TODO for now, until we can locate the element
                           Inlines -> P m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Inlines -> P m Inlines) -> Inlines -> P m Inlines
forall a b. (a -> b) -> a -> b
$ Text -> Inlines
B.text (Text
"[" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
target Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"]")
                      Val
_ -> Inlines -> P m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Inlines
forall a. Monoid a => a
mempty
      Inlines -> P m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Inlines -> P m Inlines) -> Inlines -> P m Inlines
forall a b. (a -> b) -> a -> b
$ Attr -> Text -> Text -> Inlines -> Inlines
B.linkWith (Text
"", [Text
Item [Text]
"ref"], []) (Text
"#" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
target) Text
"" Inlines
supplement)
  ,(Identifier
"linebreak", \Maybe Text
_ Map Identifier Val
_ -> Inlines -> P m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Inlines
B.linebreak)
  ,(Identifier
"text", \Maybe Text
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      (Maybe Text
mbweight :: Maybe Text) <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Maybe Text)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"weight" Map Identifier Val
fields
      case Maybe Text
mbweight of
        Just Text
"bold" -> Inlines -> Inlines
B.strong (Inlines -> Inlines) -> P m Inlines -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
body
        Maybe Text
_ -> P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
body)
  ,(Identifier
"raw", \Maybe Text
_ Map Identifier Val
fields -> Text -> Inlines
B.code (Text -> Inlines) -> (Text -> Text) -> Text -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> Text -> Text
T.filter (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
'\r') (Text -> Inlines) -> ParsecT [Content] PState m Text -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identifier -> Map Identifier Val -> ParsecT [Content] PState m Text
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"text" Map Identifier Val
fields)
  ,(Identifier
"footnote", \Maybe Text
_ Map Identifier Val
fields ->
      Blocks -> Inlines
B.note (Blocks -> Inlines)
-> ParsecT [Content] PState m Blocks -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> ParsecT [Content] PState m Blocks)
-> ParsecT [Content] PState m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ParsecT [Content] PState m Blocks
-> Seq Content -> ParsecT [Content] PState m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents ParsecT [Content] PState m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks))
  ,(Identifier
"cite", \Maybe Text
_ Map Identifier Val
fields -> do
      VLabel Text
key <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Val
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"key" Map Identifier Val
fields
      (Text
form :: Text) <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Text
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"form" Map Identifier Val
fields ParsecT [Content] PState m Text
-> ParsecT [Content] PState m Text
-> ParsecT [Content] PState m Text
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> Text -> ParsecT [Content] PState m Text
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
"normal"
      let citation :: Citation
citation =
            B.Citation
              { citationId :: Text
B.citationId = Text
key,
                citationPrefix :: [Inline]
B.citationPrefix = [Inline]
forall a. Monoid a => a
mempty,
                citationSuffix :: [Inline]
B.citationSuffix = [Inline]
forall a. Monoid a => a
mempty,
                citationMode :: CitationMode
B.citationMode = case Text
form of
                                    Text
"year" -> CitationMode
B.SuppressAuthor
                                    Text
_ -> CitationMode
B.NormalCitation,
                citationNoteNum :: Int
B.citationNoteNum = Int
0,
                citationHash :: Int
B.citationHash = Int
0
              }
      Inlines -> P m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Inlines -> P m Inlines) -> Inlines -> P m Inlines
forall a b. (a -> b) -> a -> b
$ [Citation] -> Inlines -> Inlines
B.cite [Item [Citation]
Citation
citation] (Text -> Inlines
B.text (Text -> Inlines) -> Text -> Inlines
forall a b. (a -> b) -> a -> b
$ Text
"[" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
key Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"]"))
  ,(Identifier
"lower", \Maybe Text
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"text" Map Identifier Val
fields
      (Inline -> Inline) -> Inlines -> Inlines
forall a b. Walkable a b => (a -> a) -> b -> b
walk ((Text -> Text) -> Inline -> Inline
modString Text -> Text
T.toLower) (Inlines -> Inlines) -> P m Inlines -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
body)
  ,(Identifier
"upper", \Maybe Text
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"text" Map Identifier Val
fields
      (Inline -> Inline) -> Inlines -> Inlines
forall a b. Walkable a b => (a -> a) -> b -> b
walk ((Text -> Text) -> Inline -> Inline
modString Text -> Text
T.toUpper) (Inlines -> Inlines) -> P m Inlines -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
body)
  ,(Identifier
"emph", \Maybe Text
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      Inlines -> Inlines
B.emph (Inlines -> Inlines) -> P m Inlines -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
body)
  ,(Identifier
"strong", \Maybe Text
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      Inlines -> Inlines
B.strong (Inlines -> Inlines) -> P m Inlines -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
body)
  ,(Identifier
"sub", \Maybe Text
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      Inlines -> Inlines
B.subscript (Inlines -> Inlines) -> P m Inlines -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
body)
  ,(Identifier
"super", \Maybe Text
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      Inlines -> Inlines
B.superscript (Inlines -> Inlines) -> P m Inlines -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
body)
  ,(Identifier
"strike", \Maybe Text
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      Inlines -> Inlines
B.strikeout (Inlines -> Inlines) -> P m Inlines -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
body)
  ,(Identifier
"smallcaps", \Maybe Text
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      Inlines -> Inlines
B.smallcaps (Inlines -> Inlines) -> P m Inlines -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
body)
  ,(Identifier
"underline", \Maybe Text
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      Inlines -> Inlines
B.underline (Inlines -> Inlines) -> P m Inlines -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
body)
  ,(Identifier
"quote", \Maybe Text
_ Map Identifier Val
fields -> do
      (Identifier -> Map Identifier Val -> ParsecT [Content] PState m Bool
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"block" Map Identifier Val
fields ParsecT [Content] PState m Bool
-> ParsecT [Content] PState m Bool
-> ParsecT [Content] PState m Bool
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> Bool -> ParsecT [Content] PState m Bool
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False) ParsecT [Content] PState m Bool
-> (Bool -> ParsecT [Content] PState m ())
-> ParsecT [Content] PState m ()
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> ParsecT [Content] PState m ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> ParsecT [Content] PState m ())
-> (Bool -> Bool) -> Bool -> ParsecT [Content] PState m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Bool
not
      Inlines
body <- Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *).
PandocMonad m =>
Map Identifier Val -> P m (Seq Content)
getInlineBody Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Inlines) -> P m Inlines
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines
      Inlines -> P m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Inlines -> P m Inlines) -> Inlines -> P m Inlines
forall a b. (a -> b) -> a -> b
$ Inlines -> Inlines
B.doubleQuoted (Inlines -> Inlines) -> Inlines -> Inlines
forall a b. (a -> b) -> a -> b
$ Inlines -> Inlines
B.trimInlines Inlines
body)
  ,(Identifier
"link", \Maybe Text
_ Map Identifier Val
fields -> do
      Val
dest <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Val
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"dest" Map Identifier Val
fields
      Text
src <- case Val
dest of
        VString Text
t -> Text -> ParsecT [Content] PState m Text
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
t
        VLabel Text
t -> Text -> ParsecT [Content] PState m Text
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> ParsecT [Content] PState m Text)
-> Text -> ParsecT [Content] PState m Text
forall a b. (a -> b) -> a -> b
$ Text
"#" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t
        VDict OMap Identifier Val
_ -> do
          Text -> ParsecT [Content] PState m ()
forall (m :: * -> *). PandocMonad m => Text -> P m ()
ignored Text
"link to location, linking to #"
          Text -> ParsecT [Content] PState m Text
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
"#"
        Val
_ -> SourceName -> ParsecT [Content] PState m Text
forall a. SourceName -> ParsecT [Content] PState m a
forall (m :: * -> *) a. MonadFail m => SourceName -> m a
fail (SourceName -> ParsecT [Content] PState m Text)
-> SourceName -> ParsecT [Content] PState m Text
forall a b. (a -> b) -> a -> b
$ SourceName
"Expected string or label for dest"
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      Inlines
description <-
        if Seq Content -> Bool
forall a. Seq a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Seq Content
body
          then
            Inlines -> P m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Inlines -> P m Inlines) -> Inlines -> P m Inlines
forall a b. (a -> b) -> a -> b
$
              Text -> Inlines
B.text (Text -> Inlines) -> Text -> Inlines
forall a b. (a -> b) -> a -> b
$
                if Text
"mailto:" Text -> Text -> Bool
`T.isPrefixOf` Text
src
                  then Int -> Text -> Text
T.drop Int
7 Text
src
                  else
                    if Text
"tel:" Text -> Text -> Bool
`T.isPrefixOf` Text
src
                      then Int -> Text -> Text
T.drop Int
4 Text
src
                      else Text
src
          else P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
body P m Inlines -> P m Inlines -> P m Inlines
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|>
               P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents
                ([Inline] -> Inlines
forall a. [a] -> Many a
B.fromList ([Inline] -> Inlines) -> (Blocks -> [Inline]) -> Blocks -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Block] -> [Inline]
blocksToInlines ([Block] -> [Inline]) -> (Blocks -> [Block]) -> Blocks -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Blocks -> [Block]
forall a. Many a -> [a]
B.toList (Blocks -> Inlines)
-> ParsecT [Content] PState m Blocks -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Content] PState m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks) Seq Content
body
      Inlines -> P m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Inlines -> P m Inlines) -> Inlines -> P m Inlines
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Inlines -> Inlines
B.link Text
src Text
"" Inlines
description)
  ,(Identifier
"image", \Maybe Text
_ Map Identifier Val
fields -> do
      Text
path <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Text
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"path" Map Identifier Val
fields
      Inlines
alt <- (Text -> Inlines
B.text (Text -> Inlines) -> ParsecT [Content] PState m Text -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identifier -> Map Identifier Val -> ParsecT [Content] PState m Text
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"alt" Map Identifier Val
fields) P m Inlines -> P m Inlines -> P m Inlines
forall a.
ParsecT [Content] PState m a
-> ParsecT [Content] PState m a -> ParsecT [Content] PState m a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` Inlines -> P m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Inlines
forall a. Monoid a => a
mempty
      (Maybe Text
mbwidth :: Maybe Text) <-
        (Length -> Text) -> Maybe Length -> Maybe Text
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Bool -> Length -> Text
renderLength Bool
False) (Maybe Length -> Maybe Text)
-> ParsecT [Content] PState m (Maybe Length)
-> ParsecT [Content] PState m (Maybe Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Maybe Length)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"width" Map Identifier Val
fields
      (Maybe Text
mbheight :: Maybe Text) <-
        (Length -> Text) -> Maybe Length -> Maybe Text
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Bool -> Length -> Text
renderLength Bool
False) (Maybe Length -> Maybe Text)
-> ParsecT [Content] PState m (Maybe Length)
-> ParsecT [Content] PState m (Maybe Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Maybe Length)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"height" Map Identifier Val
fields
      let attr :: Attr
attr =
            ( Text
"",
              [],
              [(Text, Text)]
-> (Text -> [(Text, Text)]) -> Maybe Text -> [(Text, Text)]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\Text
x -> [(Text
"width", Text
x)]) Maybe Text
mbwidth
                [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
forall a. [a] -> [a] -> [a]
++ [(Text, Text)]
-> (Text -> [(Text, Text)]) -> Maybe Text -> [(Text, Text)]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\Text
x -> [(Text
"height", Text
x)]) Maybe Text
mbheight
            )
      Inlines -> P m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Inlines -> P m Inlines) -> Inlines -> P m Inlines
forall a b. (a -> b) -> a -> b
$ Attr -> Text -> Text -> Inlines -> Inlines
B.imageWith Attr
attr Text
path Text
"" Inlines
alt)
  ,(Identifier
"box", \Maybe Text
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      Attr -> Inlines -> Inlines
B.spanWith (Text
"", [Text
Item [Text]
"box"], []) (Inlines -> Inlines) -> P m Inlines -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines Seq Content
body)
  ,(Identifier
"h", \Maybe Text
_ Map Identifier Val
fields -> do
      Length
amount <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m Length
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"amount" Map Identifier Val
fields ParsecT [Content] PState m Length
-> ParsecT [Content] PState m Length
-> ParsecT [Content] PState m Length
forall a.
ParsecT [Content] PState m a
-> ParsecT [Content] PState m a -> ParsecT [Content] PState m a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` Length -> ParsecT [Content] PState m Length
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Double -> LUnit -> Length
LExact Double
1 LUnit
LEm)
      let em :: Rational
em = case Length
amount of
            LExact Double
x LUnit
LEm -> Double -> Rational
forall a. Real a => a -> Rational
toRational Double
x
            Length
_ -> case Length
amount Length -> Length -> Length
forall a. Semigroup a => a -> a -> a
<> Double -> LUnit -> Length
LExact Double
0 LUnit
LPt of -- force to Pt
              LExact Double
x LUnit
LPt -> Double -> Rational
forall a. Real a => a -> Rational
toRational Double
x Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Rational
12
              Length
_ -> Rational
1 Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/ Rational
3 -- guess!
      Inlines -> P m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Inlines -> P m Inlines) -> Inlines -> P m Inlines
forall a b. (a -> b) -> a -> b
$ Text -> Inlines
B.text (Text -> Inlines) -> Text -> Inlines
forall a b. (a -> b) -> a -> b
$ Rational -> Text
getSpaceChars Rational
em)
  ,(Identifier
"place", \Maybe Text
_ Map Identifier Val
fields -> do
      Text -> ParsecT [Content] PState m ()
forall (m :: * -> *). PandocMonad m => Text -> P m ()
ignored Text
"parameters of place"
      Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Inlines) -> P m Inlines
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines)
  ,(Identifier
"align", \Maybe Text
_ Map Identifier Val
fields -> do
      Val
alignment <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Val
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"alignment" Map Identifier Val
fields
      Attr -> Inlines -> Inlines
B.spanWith (Text
"", [], [(Text
"align", Val -> Text
repr Val
alignment)])
        (Inlines -> Inlines) -> P m Inlines -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> P m Inlines) -> P m Inlines
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= P m Inlines -> Seq Content -> P m Inlines
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines))
  ,(Identifier
"sys.version", \Maybe Text
_ Map Identifier Val
_ -> Inlines -> P m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Inlines -> P m Inlines) -> Inlines -> P m Inlines
forall a b. (a -> b) -> a -> b
$ Text -> Inlines
B.text Text
"typst-hs")
  ,(Identifier
"math.equation", \Maybe Text
_ Map Identifier Val
fields -> do
      Seq Content
body <- Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields
      Bool
display <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Bool
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"block" Map Identifier Val
fields
      (if Bool
display then Text -> Inlines
B.displayMath else Text -> Inlines
B.math) (Text -> Inlines) -> ([Exp] -> Text) -> [Exp] -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Exp] -> Text
writeTeX ([Exp] -> Inlines)
-> ParsecT [Content] PState m [Exp] -> P m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq Content -> ParsecT [Content] PState m [Exp]
forall (m :: * -> *). PandocMonad m => Seq Content -> P m [Exp]
pMathMany Seq Content
body)
  ]

getInlineBody :: PandocMonad m => M.Map Identifier Val -> P m (Seq Content)
getInlineBody :: forall (m :: * -> *).
PandocMonad m =>
Map Identifier Val -> P m (Seq Content)
getInlineBody Map Identifier Val
fields =
  Seq Content -> Seq Content
parbreaksToLinebreaks (Seq Content -> Seq Content)
-> ParsecT [Content] PState m (Seq Content)
-> ParsecT [Content] PState m (Seq Content)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fields

parbreaksToLinebreaks :: Seq Content -> Seq Content
parbreaksToLinebreaks :: Seq Content -> Seq Content
parbreaksToLinebreaks =
  (Content -> Content) -> Seq Content -> Seq Content
forall a b. (a -> b) -> Seq a -> Seq b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Content -> Content
go (Seq Content -> Seq Content)
-> (Seq Content -> Seq Content) -> Seq Content -> Seq Content
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Content -> Bool) -> Seq Content -> Seq Content
forall a. (a -> Bool) -> Seq a -> Seq a
Seq.dropWhileL Content -> Bool
isParbreak (Seq Content -> Seq Content)
-> (Seq Content -> Seq Content) -> Seq Content -> Seq Content
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Content -> Bool) -> Seq Content -> Seq Content
forall a. (a -> Bool) -> Seq a -> Seq a
Seq.dropWhileR Content -> Bool
isParbreak
 where
   go :: Content -> Content
go (Elt Identifier
"parbreak" Maybe SourcePos
pos Map Identifier Val
_) = Identifier -> Maybe SourcePos -> Map Identifier Val -> Content
Elt Identifier
"linebreak" Maybe SourcePos
pos Map Identifier Val
forall a. Monoid a => a
mempty
   go Content
x = Content
x
   isParbreak :: Content -> Bool
isParbreak (Elt Identifier
"parbreak" Maybe SourcePos
_ Map Identifier Val
_) = Bool
True
   isParbreak Content
_ = Bool
False

pPara :: PandocMonad m => P m B.Blocks
pPara :: forall (m :: * -> *). PandocMonad m => P m Blocks
pPara =
  Inlines -> Blocks
B.para (Inlines -> Blocks)
-> ([Inlines] -> Inlines) -> [Inlines] -> Blocks
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inlines -> Inlines
B.trimInlines (Inlines -> Inlines)
-> ([Inlines] -> Inlines) -> [Inlines] -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inlines -> Inlines
collapseAdjacentCites (Inlines -> Inlines)
-> ([Inlines] -> Inlines) -> [Inlines] -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inlines] -> Inlines
forall a. Monoid a => [a] -> a
mconcat
    ([Inlines] -> Blocks)
-> ParsecT [Content] PState m [Inlines]
-> ParsecT [Content] PState m Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ParsecT [Content] PState m Inlines
-> ParsecT [Content] PState m [Inlines]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT [Content] PState m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInline ParsecT [Content] PState m [Inlines]
-> ParsecT [Content] PState m ()
-> ParsecT [Content] PState m [Inlines]
forall a b.
ParsecT [Content] PState m a
-> ParsecT [Content] PState m b -> ParsecT [Content] PState m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT [Content] PState m () -> ParsecT [Content] PState m ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional ParsecT [Content] PState m ()
forall (m :: * -> *). PandocMonad m => P m ()
pParBreak)

pParBreak :: PandocMonad m => P m ()
pParBreak :: forall (m :: * -> *). PandocMonad m => P m ()
pParBreak =
  ParsecT [Content] PState m Content -> ParsecT [Content] PState m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT [Content] PState m Content
 -> ParsecT [Content] PState m ())
-> ParsecT [Content] PState m Content
-> ParsecT [Content] PState m ()
forall a b. (a -> b) -> a -> b
$
    (Content -> Bool) -> ParsecT [Content] PState m Content
forall (m :: * -> *).
PandocMonad m =>
(Content -> Bool) -> P m Content
pTok
      ( \case
          Elt Identifier
"parbreak" Maybe SourcePos
_ Map Identifier Val
_ -> Bool
True
          Content
_ -> Bool
False
      )

pWithContents :: PandocMonad m => P m a -> Seq Content -> P m a
pWithContents :: forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents P m a
pa Seq Content
cs = P m a -> P m a
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (P m a -> P m a) -> P m a -> P m a
forall a b. (a -> b) -> a -> b
$ do
  [Content]
inp <- ParsecT [Content] PState m [Content]
forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput
  [Content] -> ParsecT [Content] PState m ()
forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput ([Content] -> ParsecT [Content] PState m ())
-> [Content] -> ParsecT [Content] PState m ()
forall a b. (a -> b) -> a -> b
$ Seq Content -> [Content]
forall a. Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList Seq Content
cs
  a
res <- P m a
pa
  ParsecT [Content] PState m ()
forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m ()
eof
  [Content] -> ParsecT [Content] PState m ()
forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput [Content]
inp
  a -> P m a
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
res

pInlines :: PandocMonad m => P m B.Inlines
pInlines :: forall (m :: * -> *). PandocMonad m => P m Inlines
pInlines =
  Inlines -> Inlines -> Inlines
forall a. Monoid a => a -> a -> a
mappend (Inlines -> Inlines -> Inlines)
-> ParsecT [Content] PState m Inlines
-> ParsecT [Content] PState m (Inlines -> Inlines)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Inlines -> Inlines
collapseAdjacentCites (Inlines -> Inlines)
-> ([Inlines] -> Inlines) -> [Inlines] -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inlines] -> Inlines
forall a. Monoid a => [a] -> a
mconcat ([Inlines] -> Inlines)
-> ParsecT [Content] PState m [Inlines]
-> ParsecT [Content] PState m Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Content] PState m Inlines
-> ParsecT [Content] PState m [Inlines]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many ParsecT [Content] PState m Inlines
forall (m :: * -> *). PandocMonad m => P m Inlines
pInline)
          ParsecT [Content] PState m (Inlines -> Inlines)
-> ParsecT [Content] PState m Inlines
-> ParsecT [Content] PState m Inlines
forall a b.
ParsecT [Content] PState m (a -> b)
-> ParsecT [Content] PState m a -> ParsecT [Content] PState m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((Inlines
B.softbreak Inlines
-> ParsecT [Content] PState m ()
-> ParsecT [Content] PState m Inlines
forall a b.
a -> ParsecT [Content] PState m b -> ParsecT [Content] PState m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT [Content] PState m ()
forall (m :: * -> *). PandocMonad m => P m ()
pParBreak) ParsecT [Content] PState m Inlines
-> ParsecT [Content] PState m Inlines
-> ParsecT [Content] PState m Inlines
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> Inlines -> ParsecT [Content] PState m Inlines
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Inlines
forall a. Monoid a => a
mempty)

collapseAdjacentCites :: B.Inlines -> B.Inlines
collapseAdjacentCites :: Inlines -> Inlines
collapseAdjacentCites = [Inline] -> Inlines
forall a. [a] -> Many a
B.fromList ([Inline] -> Inlines)
-> (Inlines -> [Inline]) -> Inlines -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Inline -> [Inline] -> [Inline])
-> [Inline] -> [Inline] -> [Inline]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Inline -> [Inline] -> [Inline]
go [] ([Inline] -> [Inline])
-> (Inlines -> [Inline]) -> Inlines -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inlines -> [Inline]
forall a. Many a -> [a]
B.toList
 where
   go :: Inline -> [Inline] -> [Inline]
go (Cite [Citation]
cs1 [Inline]
ils1) (Cite [Citation]
cs2 [Inline]
ils2 : [Inline]
xs) =
     [Citation] -> [Inline] -> Inline
Cite ([Citation]
cs1 [Citation] -> [Citation] -> [Citation]
forall a. [a] -> [a] -> [a]
++ [Citation]
cs2) ([Inline]
ils1 [Inline] -> [Inline] -> [Inline]
forall a. Semigroup a => a -> a -> a
<> [Inline]
ils2) Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline]
xs
   go (Cite [Citation]
cs1 [Inline]
ils1) (Inline
Space : Cite [Citation]
cs2 [Inline]
ils2 : [Inline]
xs) =
     [Citation] -> [Inline] -> Inline
Cite ([Citation]
cs1 [Citation] -> [Citation] -> [Citation]
forall a. [a] -> [a] -> [a]
++ [Citation]
cs2) ([Inline]
ils1 [Inline] -> [Inline] -> [Inline]
forall a. Semigroup a => a -> a -> a
<> [Inline]
ils2) Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline]
xs
   go Inline
x [Inline]
xs = Inline
xInline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:[Inline]
xs

modString :: (Text -> Text) -> B.Inline -> B.Inline
modString :: (Text -> Text) -> Inline -> Inline
modString Text -> Text
f (B.Str Text
t) = Text -> Inline
B.Str (Text -> Text
f Text
t)
modString Text -> Text
_ Inline
x = Inline
x

findLabels :: Seq.Seq Content -> [Text]
findLabels :: Seq Content -> [Text]
findLabels = (Content -> [Text] -> [Text]) -> [Text] -> Seq Content -> [Text]
forall a b. (a -> b -> b) -> b -> Seq a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Content -> [Text] -> [Text]
go []
 where
   go :: Content -> [Text] -> [Text]
go (Txt{}) = [Text] -> [Text]
forall a. a -> a
id
   go (Lab Text
t) = (Text
t Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
:)
   go (Elt{ eltFields :: Content -> Map Identifier Val
eltFields = Map Identifier Val
fs }) = \[Text]
ts -> (Val -> [Text] -> [Text]) -> [Text] -> Map Identifier Val -> [Text]
forall a b. (a -> b -> b) -> b -> Map Identifier a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Val -> [Text] -> [Text]
go' [Text]
ts Map Identifier Val
fs
   go' :: Val -> [Text] -> [Text]
go' (VContent Seq Content
cs) = (Seq Content -> [Text]
findLabels Seq Content
cs [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++)
   go' Val
_ = [Text] -> [Text]
forall a. a -> a
id

parseTable :: PandocMonad m
           => Maybe Text -> M.Map Identifier Val -> P m B.Blocks
parseTable :: forall (m :: * -> *).
PandocMonad m =>
Maybe Text -> Map Identifier Val -> P m Blocks
parseTable Maybe Text
mbident Map Identifier Val
fields = do
  [Seq Content]
children <- Vector (Seq Content) -> [Seq Content]
forall a. Vector a -> [a]
V.toList (Vector (Seq Content) -> [Seq Content])
-> ParsecT [Content] PState m (Vector (Seq Content))
-> ParsecT [Content] PState m [Seq Content]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Vector (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"children" Map Identifier Val
fields
  (Val
columns :: Val) <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Val
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"columns" Map Identifier Val
fields
  let toWidth :: Val -> Maybe a
toWidth (VFraction Double
f) = a -> Maybe a
forall a. a -> Maybe a
Just (Double -> a
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (Double -> a) -> Double -> a
forall a b. (a -> b) -> a -> b
$ Double
1000 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
f)
      toWidth Val
_ = Maybe a
forall a. Maybe a
Nothing
  let normalizeWidths :: [Maybe Int] -> [ColWidth]
normalizeWidths [Maybe Int]
xs =
        let givenwidths :: [Int]
givenwidths = [Maybe Int] -> [Int]
forall a. [Maybe a] -> [a]
catMaybes [Maybe Int]
xs
            (Int
totgivenwidth :: Int) = [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Int]
givenwidths
            avgwidth :: Int
avgwidth = Int
totgivenwidth Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` [Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
givenwidths
            totwidth :: Int
totwidth = Int
avgwidth Int -> Int -> Int
forall a. Num a => a -> a -> a
* [Maybe Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Maybe Int]
xs
         in if [Int] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Int]
givenwidths
              then Int -> ColWidth -> [ColWidth]
forall a. Int -> a -> [a]
replicate ([Maybe Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Maybe Int]
xs) ColWidth
B.ColWidthDefault
              else
                (Maybe Int -> ColWidth) -> [Maybe Int] -> [ColWidth]
forall a b. (a -> b) -> [a] -> [b]
map
                  ( \case
                      Just Int
x ->
                        Double -> ColWidth
B.ColWidth (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
totwidth)
                      Maybe Int
Nothing ->
                        Double -> ColWidth
B.ColWidth
                        (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
avgwidth Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
totwidth)
                  )
                  [Maybe Int]
xs
  [ColWidth]
widths <- case Val
columns of
    VInteger Integer
x -> [ColWidth] -> ParsecT [Content] PState m [ColWidth]
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([ColWidth] -> ParsecT [Content] PState m [ColWidth])
-> [ColWidth] -> ParsecT [Content] PState m [ColWidth]
forall a b. (a -> b) -> a -> b
$ Int -> ColWidth -> [ColWidth]
forall a. Int -> a -> [a]
replicate (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
x) ColWidth
B.ColWidthDefault
    VArray Vector Val
x -> [ColWidth] -> ParsecT [Content] PState m [ColWidth]
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([ColWidth] -> ParsecT [Content] PState m [ColWidth])
-> [ColWidth] -> ParsecT [Content] PState m [ColWidth]
forall a b. (a -> b) -> a -> b
$ [Maybe Int] -> [ColWidth]
normalizeWidths ([Maybe Int] -> [ColWidth]) -> [Maybe Int] -> [ColWidth]
forall a b. (a -> b) -> a -> b
$ (Val -> Maybe Int) -> [Val] -> [Maybe Int]
forall a b. (a -> b) -> [a] -> [b]
map Val -> Maybe Int
forall {a}. Integral a => Val -> Maybe a
toWidth (Vector Val -> [Val]
forall a. Vector a -> [a]
V.toList Vector Val
x)
    Val
VNone -> [ColWidth] -> ParsecT [Content] PState m [ColWidth]
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Item [ColWidth]
ColWidth
B.ColWidthDefault]
    Val
_ -> SourceName -> ParsecT [Content] PState m [ColWidth]
forall a. SourceName -> ParsecT [Content] PState m a
forall (m :: * -> *) a. MonadFail m => SourceName -> m a
fail (SourceName -> ParsecT [Content] PState m [ColWidth])
-> SourceName -> ParsecT [Content] PState m [ColWidth]
forall a b. (a -> b) -> a -> b
$ SourceName
"Could not determine number of columns: " SourceName -> SourceName -> SourceName
forall a. Semigroup a => a -> a -> a
<> Val -> SourceName
forall a. Show a => a -> SourceName
show Val
columns
  let numcols :: Int
numcols = [ColWidth] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ColWidth]
widths
  Val
align <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Val
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"align" Map Identifier Val
fields
  let toAlign :: Val -> Alignment
toAlign (VAlignment (Just Horiz
horiz) Maybe Vert
_) =
        case Horiz
horiz of
          Horiz
HorizStart -> Alignment
B.AlignLeft
          Horiz
HorizLeft -> Alignment
B.AlignLeft
          Horiz
HorizEnd -> Alignment
B.AlignRight
          Horiz
HorizRight -> Alignment
B.AlignRight
          Horiz
HorizCenter -> Alignment
B.AlignCenter
      toAlign Val
_ = Alignment
B.AlignDefault
  [Alignment]
aligns <-
    case Val
align of
      VAlignment {} -> [Alignment] -> ParsecT [Content] PState m [Alignment]
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Alignment] -> ParsecT [Content] PState m [Alignment])
-> [Alignment] -> ParsecT [Content] PState m [Alignment]
forall a b. (a -> b) -> a -> b
$ Int -> Alignment -> [Alignment]
forall a. Int -> a -> [a]
replicate Int
numcols (Val -> Alignment
toAlign Val
align)
      VArray Vector Val
v -> [Alignment] -> ParsecT [Content] PState m [Alignment]
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Alignment] -> ParsecT [Content] PState m [Alignment])
-> [Alignment] -> ParsecT [Content] PState m [Alignment]
forall a b. (a -> b) -> a -> b
$ (Val -> Alignment) -> [Val] -> [Alignment]
forall a b. (a -> b) -> [a] -> [b]
map Val -> Alignment
toAlign (Vector Val -> [Val]
forall a. Vector a -> [a]
V.toList Vector Val
v)
      VFunction Maybe Identifier
_ Map Identifier Val
_ Function
f -> do
        (Integer -> ParsecT [Content] PState m Alignment)
-> [Integer] -> ParsecT [Content] PState m [Alignment]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM
          ( \Integer
colnum -> case Function -> [Val] -> Attempt Val
applyPureFunction
              Function
f
              [Integer -> Val
VInteger Integer
colnum, Integer -> Val
VInteger Integer
0] of
              Success Val
x -> Alignment -> ParsecT [Content] PState m Alignment
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Alignment -> ParsecT [Content] PState m Alignment)
-> Alignment -> ParsecT [Content] PState m Alignment
forall a b. (a -> b) -> a -> b
$ Val -> Alignment
toAlign Val
x
              Failure SourceName
e -> SourceName -> ParsecT [Content] PState m Alignment
forall a. SourceName -> ParsecT [Content] PState m a
forall (m :: * -> *) a. MonadFail m => SourceName -> m a
fail SourceName
e
          )
          [Integer
Item [Integer]
0 .. (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
numcols Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1)]
      Val
_ -> [Alignment] -> ParsecT [Content] PState m [Alignment]
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Alignment] -> ParsecT [Content] PState m [Alignment])
-> [Alignment] -> ParsecT [Content] PState m [Alignment]
forall a b. (a -> b) -> a -> b
$ Int -> Alignment -> [Alignment]
forall a. Int -> a -> [a]
replicate Int
numcols Alignment
B.AlignDefault
  let colspecs :: [ColSpec]
colspecs = [Alignment] -> [ColWidth] -> [ColSpec]
forall a b. [a] -> [b] -> [(a, b)]
zip ([Alignment]
aligns [Alignment] -> [Alignment] -> [Alignment]
forall a. [a] -> [a] -> [a]
++ Alignment -> [Alignment]
forall a. a -> [a]
repeat Alignment
B.AlignDefault) [ColWidth]
widths
  let addCell' :: Cell -> Maybe ([Int], [[Cell]]) -> Maybe ([Int], [[Cell]])
addCell' Cell
cell Maybe ([Int], [[Cell]])
Nothing = Cell -> Maybe ([Int], [[Cell]]) -> Maybe ([Int], [[Cell]])
addCell' Cell
cell (([Int], [[Cell]]) -> Maybe ([Int], [[Cell]])
forall a. a -> Maybe a
Just ([], []))
      addCell' cell :: Cell
cell@(B.Cell Attr
_ Alignment
_ (B.RowSpan Int
rowspan) (B.ColSpan Int
colspan) [Block]
_)
       (Just ([Int]
freecols, [[Cell]]
revrows))  =
        let freecols' :: [Int]
freecols' =
              case (Int
rowspan Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
- [Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
freecols of
                Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 -> [Int]
freecols
                  | Bool
otherwise -> [Int]
freecols [Int] -> [Int] -> [Int]
forall a. [a] -> [a] -> [a]
++ Int -> Int -> [Int]
forall a. Int -> a -> [a]
replicate Int
n Int
numcols
        in case [Int]
freecols' of
             [] -> -- should not happen
                   SourceName -> Maybe ([Int], [[Cell]])
forall a. HasCallStack => SourceName -> a
error SourceName
"empty freecols'"
             Int
x:[Int]
xs
               | Int
colspan Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
x -- there is room on current row
                 -> let ([Int]
as, [Int]
bs) = Int -> [Int] -> ([Int], [Int])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
rowspan (Int
xInt -> [Int] -> [Int]
forall a. a -> [a] -> [a]
:[Int]
xs)
                    in  ([Int], [[Cell]]) -> Maybe ([Int], [[Cell]])
forall a. a -> Maybe a
Just
                        ((Int -> Int) -> [Int] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (\Int
z -> Int
z Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
colspan) [Int]
as [Int] -> [Int] -> [Int]
forall a. [a] -> [a] -> [a]
++ [Int]
bs,
                           case [[Cell]]
revrows of
                             [] -> [[Item [Cell]
Cell
cell]]
                             [Cell]
r:[[Cell]]
rs -> (Cell
cellCell -> [Cell] -> [Cell]
forall a. a -> [a] -> [a]
:[Cell]
r)[Cell] -> [[Cell]] -> [[Cell]]
forall a. a -> [a] -> [a]
:[[Cell]]
rs)
               | Bool
otherwise ->
                    let ([Int]
as, [Int]
bs) = Int -> [Int] -> ([Int], [Int])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
rowspan [Int]
xs
                    in  ([Int], [[Cell]]) -> Maybe ([Int], [[Cell]])
forall a. a -> Maybe a
Just ((Int -> Int) -> [Int] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (\Int
z -> Int
z Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
colspan) [Int]
as [Int] -> [Int] -> [Int]
forall a. [a] -> [a] -> [a]
++ [Int]
bs, [Item [Cell]
Cell
cell][Cell] -> [[Cell]] -> [[Cell]]
forall a. a -> [a] -> [a]
:[[Cell]]
revrows)
  let addCell :: TableSection -> Cell -> TableData -> TableData
addCell TableSection
tableSection Cell
cell (TableData Map TableSection ([Int], [[Cell]])
tdata) =
        Map TableSection ([Int], [[Cell]]) -> TableData
TableData ((Maybe ([Int], [[Cell]]) -> Maybe ([Int], [[Cell]]))
-> TableSection
-> Map TableSection ([Int], [[Cell]])
-> Map TableSection ([Int], [[Cell]])
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
M.alter (Cell -> Maybe ([Int], [[Cell]]) -> Maybe ([Int], [[Cell]])
addCell' Cell
cell) TableSection
tableSection Map TableSection ([Int], [[Cell]])
tdata)
  let toCell :: TableSection
-> TableData -> Seq Content -> ParsecT [Content] PState m TableData
toCell TableSection
tableSection TableData
tableData Seq Content
contents = do
        case Seq Content
contents of
          [Elt (Identifier Text
"grid.cell") Maybe SourcePos
_pos Map Identifier Val
fs] -> do
            [Block]
bs <- Blocks -> [Block]
forall a. Many a -> [a]
B.toList (Blocks -> [Block])
-> ParsecT [Content] PState m Blocks
-> ParsecT [Content] PState m [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Identifier
-> Map Identifier Val -> ParsecT [Content] PState m (Seq Content)
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"body" Map Identifier Val
fs ParsecT [Content] PState m (Seq Content)
-> (Seq Content -> ParsecT [Content] PState m Blocks)
-> ParsecT [Content] PState m Blocks
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ParsecT [Content] PState m Blocks
-> Seq Content -> ParsecT [Content] PState m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents ParsecT [Content] PState m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks)
            Int
rowspan <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Int
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"rowspan" Map Identifier Val
fs ParsecT [Content] PState m Int
-> ParsecT [Content] PState m Int -> ParsecT [Content] PState m Int
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> Int -> ParsecT [Content] PState m Int
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
1
            Int
colspan <- Identifier -> Map Identifier Val -> ParsecT [Content] PState m Int
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"colspan" Map Identifier Val
fs ParsecT [Content] PState m Int
-> ParsecT [Content] PState m Int -> ParsecT [Content] PState m Int
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> Int -> ParsecT [Content] PState m Int
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
1
            Alignment
align' <- (Val -> Alignment
toAlign (Val -> Alignment)
-> ParsecT [Content] PState m Val
-> ParsecT [Content] PState m Alignment
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identifier -> Map Identifier Val -> ParsecT [Content] PState m Val
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"align" Map Identifier Val
fs) ParsecT [Content] PState m Alignment
-> ParsecT [Content] PState m Alignment
-> ParsecT [Content] PState m Alignment
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> Alignment -> ParsecT [Content] PState m Alignment
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Alignment
B.AlignDefault
            TableData -> ParsecT [Content] PState m TableData
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TableData -> ParsecT [Content] PState m TableData)
-> TableData -> ParsecT [Content] PState m TableData
forall a b. (a -> b) -> a -> b
$ TableSection -> Cell -> TableData -> TableData
addCell TableSection
tableSection
              (Attr -> Alignment -> RowSpan -> ColSpan -> [Block] -> Cell
B.Cell Attr
B.nullAttr Alignment
align' (Int -> RowSpan
B.RowSpan Int
rowspan)
                (Int -> ColSpan
B.ColSpan Int
colspan) [Block]
bs) TableData
tableData
          [Elt (Identifier Text
"table.cell") Maybe SourcePos
pos Map Identifier Val
fs] ->
            TableSection
-> TableData -> Seq Content -> ParsecT [Content] PState m TableData
toCell TableSection
tableSection TableData
tableData [Identifier -> Maybe SourcePos -> Map Identifier Val -> Content
Elt (Text -> Identifier
Identifier Text
"grid.cell") Maybe SourcePos
pos Map Identifier Val
fs]
          [Elt (Identifier Text
"table.vline") Maybe SourcePos
_pos Map Identifier Val
_fs] -> TableData -> ParsecT [Content] PState m TableData
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TableData
tableData
          [Elt (Identifier Text
"table.hline") Maybe SourcePos
_pos Map Identifier Val
_fs] -> TableData -> ParsecT [Content] PState m TableData
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TableData
tableData
          [Elt (Identifier Text
"grid.vline") Maybe SourcePos
_pos Map Identifier Val
_fs] -> TableData -> ParsecT [Content] PState m TableData
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TableData
tableData
          [Elt (Identifier Text
"grid.hline") Maybe SourcePos
_pos Map Identifier Val
_fs] -> TableData -> ParsecT [Content] PState m TableData
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TableData
tableData
          [Elt (Identifier Text
"table.header") Maybe SourcePos
_pos Map Identifier Val
fs] ->
            Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Vector (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"children" Map Identifier Val
fs ParsecT [Content] PState m (Vector (Seq Content))
-> (Vector (Seq Content) -> ParsecT [Content] PState m TableData)
-> ParsecT [Content] PState m TableData
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
              (TableData -> Seq Content -> ParsecT [Content] PState m TableData)
-> TableData
-> [Seq Content]
-> ParsecT [Content] PState m TableData
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM (TableSection
-> TableData -> Seq Content -> ParsecT [Content] PState m TableData
toCell TableSection
THeader) TableData
tableData ([Seq Content] -> ParsecT [Content] PState m TableData)
-> (Vector (Seq Content) -> [Seq Content])
-> Vector (Seq Content)
-> ParsecT [Content] PState m TableData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector (Seq Content) -> [Seq Content]
forall a. Vector a -> [a]
V.toList
          [Elt (Identifier Text
"table.footer") Maybe SourcePos
_pos Map Identifier Val
fs] ->
            Identifier
-> Map Identifier Val
-> ParsecT [Content] PState m (Vector (Seq Content))
forall (m :: * -> *) a.
(MonadFail m, MonadPlus m, FromVal a) =>
Identifier -> Map Identifier Val -> m a
getField Identifier
"children" Map Identifier Val
fs ParsecT [Content] PState m (Vector (Seq Content))
-> (Vector (Seq Content) -> ParsecT [Content] PState m TableData)
-> ParsecT [Content] PState m TableData
forall a b.
ParsecT [Content] PState m a
-> (a -> ParsecT [Content] PState m b)
-> ParsecT [Content] PState m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
              (TableData -> Seq Content -> ParsecT [Content] PState m TableData)
-> TableData
-> [Seq Content]
-> ParsecT [Content] PState m TableData
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM (TableSection
-> TableData -> Seq Content -> ParsecT [Content] PState m TableData
toCell TableSection
TFooter) TableData
tableData ([Seq Content] -> ParsecT [Content] PState m TableData)
-> (Vector (Seq Content) -> [Seq Content])
-> Vector (Seq Content)
-> ParsecT [Content] PState m TableData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector (Seq Content) -> [Seq Content]
forall a. Vector a -> [a]
V.toList
          Seq Content
_ -> do
            [Block]
bs <- Blocks -> [Block]
forall a. Many a -> [a]
B.toList (Blocks -> [Block])
-> ParsecT [Content] PState m Blocks
-> ParsecT [Content] PState m [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT [Content] PState m Blocks
-> Seq Content -> ParsecT [Content] PState m Blocks
forall (m :: * -> *) a.
PandocMonad m =>
P m a -> Seq Content -> P m a
pWithContents ParsecT [Content] PState m Blocks
forall (m :: * -> *). PandocMonad m => P m Blocks
pBlocks Seq Content
contents
            TableData -> ParsecT [Content] PState m TableData
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TableData -> ParsecT [Content] PState m TableData)
-> TableData -> ParsecT [Content] PState m TableData
forall a b. (a -> b) -> a -> b
$ TableSection -> Cell -> TableData -> TableData
addCell TableSection
tableSection
              (Attr -> Alignment -> RowSpan -> ColSpan -> [Block] -> Cell
B.Cell Attr
B.nullAttr Alignment
B.AlignDefault (Int -> RowSpan
B.RowSpan Int
1) (Int -> ColSpan
B.ColSpan Int
1) [Block]
bs)
              TableData
tableData
  TableData
tableData <- (TableData -> Seq Content -> ParsecT [Content] PState m TableData)
-> TableData
-> [Seq Content]
-> ParsecT [Content] PState m TableData
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM (TableSection
-> TableData -> Seq Content -> ParsecT [Content] PState m TableData
forall {m :: * -> *}.
PandocMonad m =>
TableSection
-> TableData -> Seq Content -> ParsecT [Content] PState m TableData
toCell TableSection
TBody) (Map TableSection ([Int], [[Cell]]) -> TableData
TableData Map TableSection ([Int], [[Cell]])
forall a. Monoid a => a
mempty) [Seq Content]
children
  let getRows :: TableSection -> TableData -> [Row]
getRows TableSection
tablePart = ([Cell] -> Row) -> [[Cell]] -> [Row]
forall a b. (a -> b) -> [a] -> [b]
map (Attr -> [Cell] -> Row
B.Row Attr
B.nullAttr ([Cell] -> Row) -> ([Cell] -> [Cell]) -> [Cell] -> Row
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Cell] -> [Cell]
forall a. [a] -> [a]
reverse)
                          ([[Cell]] -> [Row])
-> (TableData -> [[Cell]]) -> TableData -> [Row]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Cell]]
-> (([Int], [[Cell]]) -> [[Cell]])
-> Maybe ([Int], [[Cell]])
-> [[Cell]]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] ([[Cell]] -> [[Cell]]
forall a. [a] -> [a]
reverse ([[Cell]] -> [[Cell]])
-> (([Int], [[Cell]]) -> [[Cell]]) -> ([Int], [[Cell]]) -> [[Cell]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Int], [[Cell]]) -> [[Cell]]
forall a b. (a, b) -> b
snd)
                          (Maybe ([Int], [[Cell]]) -> [[Cell]])
-> (TableData -> Maybe ([Int], [[Cell]])) -> TableData -> [[Cell]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TableSection
-> Map TableSection ([Int], [[Cell]]) -> Maybe ([Int], [[Cell]])
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup TableSection
tablePart (Map TableSection ([Int], [[Cell]]) -> Maybe ([Int], [[Cell]]))
-> (TableData -> Map TableSection ([Int], [[Cell]]))
-> TableData
-> Maybe ([Int], [[Cell]])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TableData -> Map TableSection ([Int], [[Cell]])
unTableData
  let headRows :: [Row]
headRows = TableSection -> TableData -> [Row]
getRows TableSection
THeader TableData
tableData
  let bodyRows :: [Row]
bodyRows = TableSection -> TableData -> [Row]
getRows TableSection
TBody TableData
tableData
  let footRows :: [Row]
footRows = TableSection -> TableData -> [Row]
getRows TableSection
TFooter TableData
tableData
  Blocks -> P m Blocks
forall a. a -> ParsecT [Content] PState m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Blocks -> P m Blocks) -> Blocks -> P m Blocks
forall a b. (a -> b) -> a -> b
$
    Attr
-> Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> Blocks
B.tableWith
      (Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"" Maybe Text
mbident, [], [])
      (Maybe [Inline] -> [Block] -> Caption
B.Caption Maybe [Inline]
forall a. Monoid a => a
mempty [Block]
forall a. Monoid a => a
mempty)
      [ColSpec]
colspecs
      (Attr -> [Row] -> TableHead
B.TableHead Attr
B.nullAttr [Row]
headRows)
      [Attr -> RowHeadColumns -> [Row] -> [Row] -> TableBody
B.TableBody Attr
B.nullAttr RowHeadColumns
0 [] [Row]
bodyRows]
      (Attr -> [Row] -> TableFoot
B.TableFoot Attr
B.nullAttr [Row]
footRows)

data TableSection = THeader | TBody | TFooter
  deriving (Int -> TableSection -> SourceName -> SourceName
[TableSection] -> SourceName -> SourceName
TableSection -> SourceName
(Int -> TableSection -> SourceName -> SourceName)
-> (TableSection -> SourceName)
-> ([TableSection] -> SourceName -> SourceName)
-> Show TableSection
forall a.
(Int -> a -> SourceName -> SourceName)
-> (a -> SourceName) -> ([a] -> SourceName -> SourceName) -> Show a
$cshowsPrec :: Int -> TableSection -> SourceName -> SourceName
showsPrec :: Int -> TableSection -> SourceName -> SourceName
$cshow :: TableSection -> SourceName
show :: TableSection -> SourceName
$cshowList :: [TableSection] -> SourceName -> SourceName
showList :: [TableSection] -> SourceName -> SourceName
Show, Eq TableSection
Eq TableSection =>
(TableSection -> TableSection -> Ordering)
-> (TableSection -> TableSection -> Bool)
-> (TableSection -> TableSection -> Bool)
-> (TableSection -> TableSection -> Bool)
-> (TableSection -> TableSection -> Bool)
-> (TableSection -> TableSection -> TableSection)
-> (TableSection -> TableSection -> TableSection)
-> Ord TableSection
TableSection -> TableSection -> Bool
TableSection -> TableSection -> Ordering
TableSection -> TableSection -> TableSection
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
$ccompare :: TableSection -> TableSection -> Ordering
compare :: TableSection -> TableSection -> Ordering
$c< :: TableSection -> TableSection -> Bool
< :: TableSection -> TableSection -> Bool
$c<= :: TableSection -> TableSection -> Bool
<= :: TableSection -> TableSection -> Bool
$c> :: TableSection -> TableSection -> Bool
> :: TableSection -> TableSection -> Bool
$c>= :: TableSection -> TableSection -> Bool
>= :: TableSection -> TableSection -> Bool
$cmax :: TableSection -> TableSection -> TableSection
max :: TableSection -> TableSection -> TableSection
$cmin :: TableSection -> TableSection -> TableSection
min :: TableSection -> TableSection -> TableSection
Ord, TableSection -> TableSection -> Bool
(TableSection -> TableSection -> Bool)
-> (TableSection -> TableSection -> Bool) -> Eq TableSection
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TableSection -> TableSection -> Bool
== :: TableSection -> TableSection -> Bool
$c/= :: TableSection -> TableSection -> Bool
/= :: TableSection -> TableSection -> Bool
Eq)

newtype TableData =
  TableData { TableData -> Map TableSection ([Int], [[Cell]])
unTableData :: M.Map TableSection ([Int], [[Cell]]) }
  deriving (Int -> TableData -> SourceName -> SourceName
[TableData] -> SourceName -> SourceName
TableData -> SourceName
(Int -> TableData -> SourceName -> SourceName)
-> (TableData -> SourceName)
-> ([TableData] -> SourceName -> SourceName)
-> Show TableData
forall a.
(Int -> a -> SourceName -> SourceName)
-> (a -> SourceName) -> ([a] -> SourceName -> SourceName) -> Show a
$cshowsPrec :: Int -> TableData -> SourceName -> SourceName
showsPrec :: Int -> TableData -> SourceName -> SourceName
$cshow :: TableData -> SourceName
show :: TableData -> SourceName
$cshowList :: [TableData] -> SourceName -> SourceName
showList :: [TableData] -> SourceName -> SourceName
Show)
  -- for each table section, we have a pair
  -- the first element indicates the number of column spaces left
  -- in [currentLine, nextLine, lineAfter, etc.]
  -- the second element is a list of rows, in reverse order,
  -- each of which is a list of cells, in reverse order