    {-# LANGUAGE FlexibleContexts  #-}
    {-# LANGUAGE TupleSections     #-}
    {-# LANGUAGE OverloadedStrings #-}
    {- |
       Module      : Text.Pandoc.Readers.Org.Meta
       Copyright   : Copyright (C) 2014-2020 Albert Krewinkel
       License     : GNU GPL, version 2 or above

       Maintainer  : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>

    Parsers for Org-mode meta declarations.
    -}
    module Text.Pandoc.Readers.Org.Meta
      ( metaExport
      , metaKey
      , metaLine
      ) where

    import Text.Pandoc.Readers.Org.BlockStarts
    import Text.Pandoc.Readers.Org.ExportSettings (exportSettings)
    import Text.Pandoc.Readers.Org.Inlines
    import Text.Pandoc.Readers.Org.ParserState
    import Text.Pandoc.Readers.Org.Parsing

    import Text.Pandoc.Builder (Blocks, Inlines)
    import qualified Text.Pandoc.Builder as B
    import Text.Pandoc.Class.PandocMonad (PandocMonad)
    import Text.Pandoc.Definition
    import Text.Pandoc.Shared (safeRead)

    import Control.Monad (mzero, void, when)
    import Data.List (intersperse)
    import Data.Maybe (fromMaybe)
    import qualified Data.Map as M
    import qualified Data.Set as Set
    import Data.Text (Text)
    import qualified Data.Text as T
    import Network.HTTP (urlEncode)

    -- | Returns the current meta, respecting export options.
    metaExport :: Monad m => OrgParser m (F Meta)
    metaExport :: OrgParser m (F Meta)
    metaExport = do
      OrgParserState
    st <- ParsecT
      Text OrgParserState (ReaderT OrgParserLocal m) OrgParserState
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
      let settings :: ExportSettings
    settings = OrgParserState -> ExportSettings
    orgStateExportSettings OrgParserState
    st
      F Meta -> OrgParser m (F Meta)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (F Meta -> OrgParser m (F Meta)) -> F Meta -> OrgParser m (F Meta)
    forall a b. (a -> b) -> a -> b
    $ (if ExportSettings -> Bool
    exportWithAuthor  ExportSettings
    settings then Meta -> Meta
    forall a. a -> a
    id else Text -> Meta -> Meta
    removeMeta "author")
             (Meta -> Meta) -> (Meta -> Meta) -> Meta -> Meta
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (if ExportSettings -> Bool
    exportWithCreator ExportSettings
    settings then Meta -> Meta
    forall a. a -> a
    id else Text -> Meta -> Meta
    removeMeta "creator")
             (Meta -> Meta) -> (Meta -> Meta) -> Meta -> Meta
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (if ExportSettings -> Bool
    exportWithEmail   ExportSettings
    settings then Meta -> Meta
    forall a. a -> a
    id else Text -> Meta -> Meta
    removeMeta "email")
            (Meta -> Meta) -> F Meta -> F Meta
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> OrgParserState -> F Meta
    orgStateMeta OrgParserState
    st

    removeMeta :: Text -> Meta -> Meta
    removeMeta :: Text -> Meta -> Meta
    removeMeta key :: Text
    key meta' :: Meta
    meta' =
      let metaMap :: Map Text MetaValue
    metaMap = Meta -> Map Text MetaValue
    unMeta Meta
    meta'
      in Map Text MetaValue -> Meta
    Meta (Map Text MetaValue -> Meta) -> Map Text MetaValue -> Meta
    forall a b. (a -> b) -> a -> b
    $ Text -> Map Text MetaValue -> Map Text MetaValue
    forall k a. Ord k => k -> Map k a -> Map k a
    M.delete Text
    key Map Text MetaValue
    metaMap

    -- | Parse and handle a single line containing meta information
    -- The order, in which blocks are tried, makes sure that we're not looking at
    -- the beginning of a block, so we don't need to check for it
    metaLine :: PandocMonad m => OrgParser m Blocks
    metaLine :: OrgParser m Blocks
    metaLine = Blocks
    forall a. Monoid a => a
    mempty Blocks
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    -> OrgParser m Blocks
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    forall (m :: * -> *). Monad m => OrgParser m ()
    metaLineStart OrgParser m Blocks
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    -> OrgParser m Blocks
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* (ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    forall (m :: * -> *). PandocMonad m => OrgParser m ()
    optionLine ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    forall s u (m :: * -> *) a.
    ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
    <|> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    forall (m :: * -> *). PandocMonad m => OrgParser m ()
    declarationLine)

    declarationLine :: PandocMonad m => OrgParser m ()
    declarationLine :: OrgParser m ()
    declarationLine = OrgParser m () -> OrgParser m ()
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (OrgParser m () -> OrgParser m ())
    -> OrgParser m () -> OrgParser m ()
    forall a b. (a -> b) -> a -> b
    $ do
      Text
    key   <- Text -> Text
    T.toLower (Text -> Text)
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall (m :: * -> *). Monad m => OrgParser m Text
    metaKey
      (key' :: Text
    key', value :: F MetaValue
    value) <- Text -> OrgParser m (Text, F MetaValue)
    forall (m :: * -> *).
    PandocMonad m =>
    Text -> OrgParser m (Text, F MetaValue)
    metaValue Text
    key
      let addMetaValue :: OrgParserState -> OrgParserState
    addMetaValue st :: OrgParserState
    st =
            OrgParserState
    st { orgStateMeta :: F Meta
    orgStateMeta = Text -> MetaValue -> Meta -> Meta
    forall a b. (HasMeta a, ToMetaValue b) => Text -> b -> a -> a
    B.setMeta Text
    key' (MetaValue -> Meta -> Meta)
    -> F MetaValue -> Future OrgParserState (Meta -> Meta)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> F MetaValue
    value Future OrgParserState (Meta -> Meta) -> F Meta -> F Meta
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> OrgParserState -> F Meta
    orgStateMeta OrgParserState
    st }
      Bool -> OrgParser m () -> OrgParser m ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    when (Text
    key' Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    /= "results") (OrgParser m () -> OrgParser m ())
    -> OrgParser m () -> OrgParser m ()
    forall a b. (a -> b) -> a -> b
    $ (OrgParserState -> OrgParserState) -> OrgParser m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState OrgParserState -> OrgParserState
    addMetaValue

    metaKey :: Monad m => OrgParser m Text
    metaKey :: OrgParser m Text
    metaKey = Text -> Text
    T.toLower (Text -> Text) -> OrgParser m Text -> OrgParser m Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParserT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> OrgParser m Text
    forall s (m :: * -> *) t st.
    Stream s m t =>
    ParserT s st m Char -> ParserT s st m Text
    many1Char ([Char]
    -> ParserT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall s (m :: * -> *) u.
    Stream s m Char =>
    [Char] -> ParsecT s u m Char
    noneOf ": \n\r")
                        OrgParser m Text
    -> ParserT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> OrgParser m Text
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <*  Char -> ParserT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall s (m :: * -> *) u.
    Stream s m Char =>
    Char -> ParsecT s u m Char
    char ':'
                        OrgParser m Text
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    -> OrgParser m Text
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <*  ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    forall s (m :: * -> *) st. Stream s m Char => ParserT s st m ()
    skipSpaces

    metaValue :: PandocMonad m => Text -> OrgParser m (Text, F MetaValue)
    metaValue :: Text -> OrgParser m (Text, F MetaValue)
    metaValue key :: Text
    key =
      let inclKey :: Text
    inclKey = "header-includes"
      in case Text
    key of
        "author"          -> (Text
    key,) (F MetaValue -> (Text, F MetaValue))
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> OrgParser m (Text, F MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT
      Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *). PandocMonad m => OrgParser m (F MetaValue)
    metaInlinesCommaSeparated
        "keywords"        -> (Text
    key,) (F MetaValue -> (Text, F MetaValue))
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> OrgParser m (Text, F MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT
      Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *). PandocMonad m => OrgParser m (F MetaValue)
    metaInlinesCommaSeparated
        "title"           -> (Text
    key,) (F MetaValue -> (Text, F MetaValue))
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> OrgParser m (Text, F MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT
      Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *). PandocMonad m => OrgParser m (F MetaValue)
    metaInlines
        "subtitle"        -> (Text
    key,) (F MetaValue -> (Text, F MetaValue))
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> OrgParser m (Text, F MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT
      Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *). PandocMonad m => OrgParser m (F MetaValue)
    metaInlines
        "date"            -> (Text
    key,) (F MetaValue -> (Text, F MetaValue))
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> OrgParser m (Text, F MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT
      Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *). PandocMonad m => OrgParser m (F MetaValue)
    metaInlines
        "nocite"          -> (Text
    key,) (F MetaValue -> (Text, F MetaValue))
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> OrgParser m (Text, F MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *).
    Monad m =>
    Text -> OrgParser m (F MetaValue) -> OrgParser m (F MetaValue)
    accumulatingList Text
    key ParsecT
      Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *). PandocMonad m => OrgParser m (F MetaValue)
    metaInlines
        "header-includes" -> (Text
    key,) (F MetaValue -> (Text, F MetaValue))
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> OrgParser m (Text, F MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *).
    Monad m =>
    Text -> OrgParser m (F MetaValue) -> OrgParser m (F MetaValue)
    accumulatingList Text
    key ParsecT
      Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *). PandocMonad m => OrgParser m (F MetaValue)
    metaInlines
        "latex_header"    -> (Text
    inclKey,) (F MetaValue -> (Text, F MetaValue))
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> OrgParser m (Text, F MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
                             Text
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *).
    Monad m =>
    Text -> OrgParser m (F MetaValue) -> OrgParser m (F MetaValue)
    accumulatingList Text
    inclKey (Text
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *). Monad m => Text -> OrgParser m (F MetaValue)
    metaExportSnippet "latex")
        "latex_class"     -> ("documentclass",) (F MetaValue -> (Text, F MetaValue))
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> OrgParser m (Text, F MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT
      Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *). Monad m => OrgParser m (F MetaValue)
    metaString
        -- Org-mode expects class options to contain the surrounding brackets,
        -- pandoc does not.
        "latex_class_options" -> ("classoption",) (F MetaValue -> (Text, F MetaValue))
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> OrgParser m (Text, F MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
                                 (Text -> Text)
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *).
    Monad m =>
    (Text -> Text) -> OrgParser m (F MetaValue)
    metaModifiedString ((Char -> Bool) -> Text -> Text
    T.filter (Char -> [Char] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `notElem` ("[]" :: String)))
        "html_head"       -> (Text
    inclKey,) (F MetaValue -> (Text, F MetaValue))
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> OrgParser m (Text, F MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
                             Text
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *).
    Monad m =>
    Text -> OrgParser m (F MetaValue) -> OrgParser m (F MetaValue)
    accumulatingList Text
    inclKey (Text
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *). Monad m => Text -> OrgParser m (F MetaValue)
    metaExportSnippet "html")
        _                 -> (Text
    key,) (F MetaValue -> (Text, F MetaValue))
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    -> OrgParser m (Text, F MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT
      Text OrgParserState (ReaderT OrgParserLocal m) (F MetaValue)
    forall (m :: * -> *). Monad m => OrgParser m (F MetaValue)
    metaString

    metaInlines :: PandocMonad m => OrgParser m (F MetaValue)
    metaInlines :: OrgParser m (F MetaValue)
    metaInlines = (Many Inline -> MetaValue)
    -> Future OrgParserState (Many Inline) -> F MetaValue
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap ([Inline] -> MetaValue
    MetaInlines ([Inline] -> MetaValue)
    -> (Many Inline -> [Inline]) -> Many Inline -> MetaValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Many Inline -> [Inline]
    forall a. Many a -> [a]
    B.toList) (Future OrgParserState (Many Inline) -> F MetaValue)
    -> ParsecT
         Text
         OrgParserState
         (ReaderT OrgParserLocal m)
         (Future OrgParserState (Many Inline))
    -> OrgParser m (F MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT
      Text
      OrgParserState
      (ReaderT OrgParserLocal m)
      (Future OrgParserState (Many Inline))
    forall (m :: * -> *).
    PandocMonad m =>
    OrgParser m (Future OrgParserState (Many Inline))
    inlinesTillNewline

    metaInlinesCommaSeparated :: PandocMonad m => OrgParser m (F MetaValue)
    metaInlinesCommaSeparated :: OrgParser m (F MetaValue)
    metaInlinesCommaSeparated = do
      [Text]
    itemStrs <- ParserT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> ParserT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall s (m :: * -> *) t st.
    Stream s m t =>
    ParserT s st m Char -> ParserT s st m Text
    many1Char ([Char]
    -> ParserT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall s (m :: * -> *) u.
    Stream s m Char =>
    [Char] -> ParsecT s u m Char
    noneOf ",\n") ParserT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> ParserT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) [Text]
    forall s (m :: * -> *) t u a sep.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
    `sepBy1` Char -> ParserT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall s (m :: * -> *) u.
    Stream s m Char =>
    Char -> ParsecT s u m Char
    char ','
      ParserT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall (m :: * -> *). Monad m => OrgParser m Char
    newline
      [Future OrgParserState (Many Inline)]
    items <- (Text
     -> ParsecT
          Text
          OrgParserState
          (ReaderT OrgParserLocal m)
          (Future OrgParserState (Many Inline)))
    -> [Text]
    -> ParsecT
         Text
         OrgParserState
         (ReaderT OrgParserLocal m)
         [Future OrgParserState (Many Inline)]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM (ParsecT
      Text
      OrgParserState
      (ReaderT OrgParserLocal m)
      (Future OrgParserState (Many Inline))
    -> Text
    -> ParsecT
         Text
         OrgParserState
         (ReaderT OrgParserLocal m)
         (Future OrgParserState (Many Inline))
    forall (m :: * -> *) a.
    Monad m =>
    OrgParser m a -> Text -> OrgParser m a
    parseFromString ParsecT
      Text
      OrgParserState
      (ReaderT OrgParserLocal m)
      (Future OrgParserState (Many Inline))
    forall (m :: * -> *).
    PandocMonad m =>
    OrgParser m (Future OrgParserState (Many Inline))
    inlinesTillNewline (Text
     -> ParsecT
          Text
          OrgParserState
          (ReaderT OrgParserLocal m)
          (Future OrgParserState (Many Inline)))
    -> (Text -> Text)
    -> Text
    -> ParsecT
         Text
         OrgParserState
         (ReaderT OrgParserLocal m)
         (Future OrgParserState (Many Inline))
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> "\n")) [Text]
    itemStrs
      let toMetaInlines :: Many Inline -> MetaValue
    toMetaInlines = [Inline] -> MetaValue
    MetaInlines ([Inline] -> MetaValue)
    -> (Many Inline -> [Inline]) -> Many Inline -> MetaValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Many Inline -> [Inline]
    forall a. Many a -> [a]
    B.toList
      F MetaValue -> OrgParser m (F MetaValue)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (F MetaValue -> OrgParser m (F MetaValue))
    -> F MetaValue -> OrgParser m (F MetaValue)
    forall a b. (a -> b) -> a -> b
    $ [MetaValue] -> MetaValue
    MetaList ([MetaValue] -> MetaValue)
    -> ([Many Inline] -> [MetaValue]) -> [Many Inline] -> MetaValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Many Inline -> MetaValue) -> [Many Inline] -> [MetaValue]
    forall a b. (a -> b) -> [a] -> [b]
    map Many Inline -> MetaValue
    toMetaInlines ([Many Inline] -> MetaValue)
    -> Future OrgParserState [Many Inline] -> F MetaValue
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Future OrgParserState (Many Inline)]
    -> Future OrgParserState [Many Inline]
    forall (t :: * -> *) (m :: * -> *) a.
    (Traversable t, Monad m) =>
    t (m a) -> m (t a)
    sequence [Future OrgParserState (Many Inline)]
    items

    metaString :: Monad m => OrgParser m (F MetaValue)
    metaString :: OrgParser m (F MetaValue)
    metaString = (Text -> Text) -> OrgParser m (F MetaValue)
    forall (m :: * -> *).
    Monad m =>
    (Text -> Text) -> OrgParser m (F MetaValue)
    metaModifiedString Text -> Text
    forall a. a -> a
    id

    metaModifiedString :: Monad m => (Text -> Text) -> OrgParser m (F MetaValue)
    metaModifiedString :: (Text -> Text) -> OrgParser m (F MetaValue)
    metaModifiedString f :: Text -> Text
    f = MetaValue -> F MetaValue
    forall (m :: * -> *) a. Monad m => a -> m a
    return (MetaValue -> F MetaValue)
    -> (Text -> MetaValue) -> Text -> F MetaValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> MetaValue
    MetaString (Text -> MetaValue) -> (Text -> Text) -> Text -> MetaValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Text
    f (Text -> F MetaValue)
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> OrgParser m (F MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall (m :: * -> *). Monad m => OrgParser m Text
    anyLine

    -- | Read an format specific meta definition
    metaExportSnippet :: Monad m => Text -> OrgParser m (F MetaValue)
    metaExportSnippet :: Text -> OrgParser m (F MetaValue)
    metaExportSnippet format :: Text
    format =
      MetaValue -> F MetaValue
    forall (m :: * -> *) a. Monad m => a -> m a
    return (MetaValue -> F MetaValue)
    -> (Text -> MetaValue) -> Text -> F MetaValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Inline] -> MetaValue
    MetaInlines ([Inline] -> MetaValue) -> (Text -> [Inline]) -> Text -> MetaValue
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Many Inline -> [Inline]
    forall a. Many a -> [a]
    B.toList (Many Inline -> [Inline])
    -> (Text -> Many Inline) -> Text -> [Inline]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Text -> Many Inline
    B.rawInline Text
    format (Text -> F MetaValue)
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> OrgParser m (F MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall (m :: * -> *). Monad m => OrgParser m Text
    anyLine

    -- | Accumulate the result of the @parser@ in a list under @key@.
    accumulatingList :: Monad m => Text
                     -> OrgParser m (F MetaValue)
                     -> OrgParser m (F MetaValue)
    accumulatingList :: Text -> OrgParser m (F MetaValue) -> OrgParser m (F MetaValue)
    accumulatingList key :: Text
    key p :: OrgParser m (F MetaValue)
    p = do
      F MetaValue
    value <- OrgParser m (F MetaValue)
    p
      F Meta
    meta' <- OrgParserState -> F Meta
    orgStateMeta (OrgParserState -> F Meta)
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) OrgParserState
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) (F Meta)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT
      Text OrgParserState (ReaderT OrgParserLocal m) OrgParserState
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
      F MetaValue -> OrgParser m (F MetaValue)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (F MetaValue -> OrgParser m (F MetaValue))
    -> F MetaValue -> OrgParser m (F MetaValue)
    forall a b. (a -> b) -> a -> b
    $ (\m :: Meta
    m v :: MetaValue
    v -> [MetaValue] -> MetaValue
    MetaList (Meta -> [MetaValue]
    curList Meta
    m [MetaValue] -> [MetaValue] -> [MetaValue]
    forall a. [a] -> [a] -> [a]
    ++ [MetaValue
    v])) (Meta -> MetaValue -> MetaValue)
    -> F Meta -> Future OrgParserState (MetaValue -> MetaValue)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> F Meta
    meta' Future OrgParserState (MetaValue -> MetaValue)
    -> F MetaValue -> F MetaValue
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> F MetaValue
    value
     where curList :: Meta -> [MetaValue]
    curList m :: Meta
    m = case Text -> Meta -> Maybe MetaValue
    lookupMeta Text
    key Meta
    m of
                         Just (MetaList ms :: [MetaValue]
    ms) -> [MetaValue]
    ms
                         Just x :: MetaValue
    x             -> [MetaValue
    x]
                         _                  -> []

    --
    -- export options
    --
    optionLine :: PandocMonad m => OrgParser m ()
    optionLine :: OrgParser m ()
    optionLine = OrgParser m () -> OrgParser m ()
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (OrgParser m () -> OrgParser m ())
    -> OrgParser m () -> OrgParser m ()
    forall a b. (a -> b) -> a -> b
    $ do
      Text
    key <- OrgParser m Text
    forall (m :: * -> *). Monad m => OrgParser m Text
    metaKey
      case Text
    key of
        "link"     -> OrgParser m (Text, Text -> Text)
    forall (m :: * -> *). Monad m => OrgParser m (Text, Text -> Text)
    parseLinkFormat OrgParser m (Text, Text -> Text)
    -> ((Text, Text -> Text) -> OrgParser m ()) -> OrgParser m ()
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= (Text -> (Text -> Text) -> OrgParser m ())
    -> (Text, Text -> Text) -> OrgParser m ()
    forall a b c. (a -> b -> c) -> (a, b) -> c
    uncurry Text -> (Text -> Text) -> OrgParser m ()
    forall (m :: * -> *).
    Monad m =>
    Text -> (Text -> Text) -> OrgParser m ()
    addLinkFormat
        "options"  -> OrgParser m ()
    forall (m :: * -> *). PandocMonad m => OrgParser m ()
    exportSettings
        "todo"     -> OrgParser m TodoSequence
    forall (m :: * -> *). Monad m => OrgParser m TodoSequence
    todoSequence OrgParser m TodoSequence
    -> (TodoSequence -> OrgParser m ()) -> OrgParser m ()
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= (OrgParserState -> OrgParserState) -> OrgParser m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((OrgParserState -> OrgParserState) -> OrgParser m ())
    -> (TodoSequence -> OrgParserState -> OrgParserState)
    -> TodoSequence
    -> OrgParser m ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . TodoSequence -> OrgParserState -> OrgParserState
    registerTodoSequence
        "seq_todo" -> OrgParser m TodoSequence
    forall (m :: * -> *). Monad m => OrgParser m TodoSequence
    todoSequence OrgParser m TodoSequence
    -> (TodoSequence -> OrgParser m ()) -> OrgParser m ()
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= (OrgParserState -> OrgParserState) -> OrgParser m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((OrgParserState -> OrgParserState) -> OrgParser m ())
    -> (TodoSequence -> OrgParserState -> OrgParserState)
    -> TodoSequence
    -> OrgParser m ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . TodoSequence -> OrgParserState -> OrgParserState
    registerTodoSequence
        "typ_todo" -> OrgParser m TodoSequence
    forall (m :: * -> *). Monad m => OrgParser m TodoSequence
    todoSequence OrgParser m TodoSequence
    -> (TodoSequence -> OrgParser m ()) -> OrgParser m ()
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= (OrgParserState -> OrgParserState) -> OrgParser m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((OrgParserState -> OrgParserState) -> OrgParser m ())
    -> (TodoSequence -> OrgParserState -> OrgParserState)
    -> TodoSequence
    -> OrgParser m ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . TodoSequence -> OrgParserState -> OrgParserState
    registerTodoSequence
        "macro"    -> OrgParser m (Text, [Text] -> Text)
    forall (m :: * -> *). Monad m => OrgParser m (Text, [Text] -> Text)
    macroDefinition OrgParser m (Text, [Text] -> Text)
    -> ((Text, [Text] -> Text) -> OrgParser m ()) -> OrgParser m ()
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= (OrgParserState -> OrgParserState) -> OrgParser m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((OrgParserState -> OrgParserState) -> OrgParser m ())
    -> ((Text, [Text] -> Text) -> OrgParserState -> OrgParserState)
    -> (Text, [Text] -> Text)
    -> OrgParser m ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Text, [Text] -> Text) -> OrgParserState -> OrgParserState
    registerMacro
        "exclude_tags" -> OrgParser m [Tag]
    forall (m :: * -> *). Monad m => OrgParser m [Tag]
    tagList OrgParser m [Tag] -> ([Tag] -> OrgParser m ()) -> OrgParser m ()
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= (OrgParserState -> OrgParserState) -> OrgParser m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((OrgParserState -> OrgParserState) -> OrgParser m ())
    -> ([Tag] -> OrgParserState -> OrgParserState)
    -> [Tag]
    -> OrgParser m ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Tag] -> OrgParserState -> OrgParserState
    setExcludedTags
        "select_tags" -> OrgParser m [Tag]
    forall (m :: * -> *). Monad m => OrgParser m [Tag]
    tagList OrgParser m [Tag] -> ([Tag] -> OrgParser m ()) -> OrgParser m ()
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= (OrgParserState -> OrgParserState) -> OrgParser m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((OrgParserState -> OrgParserState) -> OrgParser m ())
    -> ([Tag] -> OrgParserState -> OrgParserState)
    -> [Tag]
    -> OrgParser m ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Tag] -> OrgParserState -> OrgParserState
    setSelectedTags
        "pandoc-emphasis-pre" -> OrgParser m (Maybe [Char])
    forall (m :: * -> *). Monad m => OrgParser m (Maybe [Char])
    emphChars OrgParser m (Maybe [Char])
    -> (Maybe [Char] -> OrgParser m ()) -> OrgParser m ()
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= (OrgParserState -> OrgParserState) -> OrgParser m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((OrgParserState -> OrgParserState) -> OrgParser m ())
    -> (Maybe [Char] -> OrgParserState -> OrgParserState)
    -> Maybe [Char]
    -> OrgParser m ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Maybe [Char] -> OrgParserState -> OrgParserState
    setEmphasisPreChar
        "pandoc-emphasis-post" -> OrgParser m (Maybe [Char])
    forall (m :: * -> *). Monad m => OrgParser m (Maybe [Char])
    emphChars OrgParser m (Maybe [Char])
    -> (Maybe [Char] -> OrgParser m ()) -> OrgParser m ()
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= (OrgParserState -> OrgParserState) -> OrgParser m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((OrgParserState -> OrgParserState) -> OrgParser m ())
    -> (Maybe [Char] -> OrgParserState -> OrgParserState)
    -> Maybe [Char]
    -> OrgParser m ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Maybe [Char] -> OrgParserState -> OrgParserState
    setEmphasisPostChar
        _          -> OrgParser m ()
    forall (m :: * -> *) a. MonadPlus m => m a
    mzero

    addLinkFormat :: Monad m => Text
                  -> (Text -> Text)
                  -> OrgParser m ()
    addLinkFormat :: Text -> (Text -> Text) -> OrgParser m ()
    addLinkFormat key :: Text
    key formatter :: Text -> Text
    formatter = (OrgParserState -> OrgParserState) -> OrgParser m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((OrgParserState -> OrgParserState) -> OrgParser m ())
    -> (OrgParserState -> OrgParserState) -> OrgParser m ()
    forall a b. (a -> b) -> a -> b
    $ \s :: OrgParserState
    s ->
      let fs :: OrgLinkFormatters
    fs = OrgParserState -> OrgLinkFormatters
    orgStateLinkFormatters OrgParserState
    s
      in OrgParserState
    s{ orgStateLinkFormatters :: OrgLinkFormatters
    orgStateLinkFormatters = Text -> (Text -> Text) -> OrgLinkFormatters -> OrgLinkFormatters
    forall k a. Ord k => k -> a -> Map k a -> Map k a
    M.insert Text
    key Text -> Text
    formatter OrgLinkFormatters
    fs }

    parseLinkFormat :: Monad m => OrgParser m (Text, Text -> Text)
    parseLinkFormat :: OrgParser m (Text, Text -> Text)
    parseLinkFormat = OrgParser m (Text, Text -> Text)
    -> OrgParser m (Text, Text -> Text)
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (OrgParser m (Text, Text -> Text)
     -> OrgParser m (Text, Text -> Text))
    -> OrgParser m (Text, Text -> Text)
    -> OrgParser m (Text, Text -> Text)
    forall a b. (a -> b) -> a -> b
    $ do
      Text
    linkType <- Char -> Text -> Text
    T.cons (Char -> Text -> Text)
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (Text -> Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
    letter ParsecT
      Text OrgParserState (ReaderT OrgParserLocal m) (Text -> Text)
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall s (m :: * -> *) t st.
    Stream s m t =>
    ParserT s st m Char -> ParserT s st m Text
    manyChar (ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
    alphaNum ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall s u (m :: * -> *) a.
    ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
    <|> [Char]
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall s (m :: * -> *) u.
    Stream s m Char =>
    [Char] -> ParsecT s u m Char
    oneOf "-_") ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    forall s (m :: * -> *) st. Stream s m Char => ParserT s st m ()
    skipSpaces
      Text -> Text
    linkSubst <- ParsecT
      Text OrgParserState (ReaderT OrgParserLocal m) (Text -> Text)
    forall (m :: * -> *). Monad m => OrgParser m (Text -> Text)
    parseFormat
      (Text, Text -> Text) -> OrgParser m (Text, Text -> Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text
    linkType, Text -> Text
    linkSubst)

    -- | An ad-hoc, single-argument-only implementation of a printf-style format
    -- parser.
    parseFormat :: Monad m => OrgParser m (Text -> Text)
    parseFormat :: OrgParser m (Text -> Text)
    parseFormat = OrgParser m (Text -> Text) -> OrgParser m (Text -> Text)
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (OrgParser m (Text -> Text) -> OrgParser m (Text -> Text))
    -> OrgParser m (Text -> Text) -> OrgParser m (Text -> Text)
    forall a b. (a -> b) -> a -> b
    $ OrgParser m (Text -> Text)
    forall u. ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    replacePlain OrgParser m (Text -> Text)
    -> OrgParser m (Text -> Text) -> OrgParser m (Text -> Text)
    forall s u (m :: * -> *) a.
    ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
    <|> OrgParser m (Text -> Text)
    forall u. ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    replaceUrl OrgParser m (Text -> Text)
    -> OrgParser m (Text -> Text) -> OrgParser m (Text -> Text)
    forall s u (m :: * -> *) a.
    ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
    <|> OrgParser m (Text -> Text)
    forall u. ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    justAppend
     where
       -- inefficient, but who cares
       replacePlain :: ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    replacePlain = ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    -> ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
     -> ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text))
    -> ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    -> ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    forall a b. (a -> b) -> a -> b
    $ (\x :: [Text]
    x -> [Text] -> Text
    T.concat ([Text] -> Text) -> (Text -> [Text]) -> Text -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Text -> [Text] -> [Text]) -> [Text] -> Text -> [Text]
    forall a b c. (a -> b -> c) -> b -> a -> c
    flip Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    intersperse [Text]
    x)
                         ([Text] -> Text -> Text)
    -> ParsecT Text u (ReaderT OrgParserLocal m) [Text]
    -> ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [ParsecT Text u (ReaderT OrgParserLocal m) Text]
    -> ParsecT Text u (ReaderT OrgParserLocal m) [Text]
    forall (t :: * -> *) (m :: * -> *) a.
    (Traversable t, Monad m) =>
    t (m a) -> m (t a)
    sequence [Char -> ParsecT Text u (ReaderT OrgParserLocal m) Text
    forall s (m :: * -> *) st.
    Stream s m Char =>
    Char -> ParserT s st m Text
    tillSpecifier 's', ParsecT Text u (ReaderT OrgParserLocal m) Text
    forall st. ParserT Text st (ReaderT OrgParserLocal m) Text
    rest]
       replaceUrl :: ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    replaceUrl   = ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    -> ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
     -> ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text))
    -> ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    -> ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    forall a b. (a -> b) -> a -> b
    $ (\x :: [Text]
    x -> [Text] -> Text
    T.concat ([Text] -> Text) -> (Text -> [Text]) -> Text -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Text -> [Text] -> [Text]) -> [Text] -> Text -> [Text]
    forall a b c. (a -> b -> c) -> b -> a -> c
    flip Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    intersperse [Text]
    x (Text -> [Text]) -> (Text -> Text) -> Text -> [Text]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Char] -> Text
    T.pack ([Char] -> Text) -> (Text -> [Char]) -> Text -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Char] -> [Char]
    urlEncode ([Char] -> [Char]) -> (Text -> [Char]) -> Text -> [Char]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> [Char]
    T.unpack)
                         ([Text] -> Text -> Text)
    -> ParsecT Text u (ReaderT OrgParserLocal m) [Text]
    -> ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [ParsecT Text u (ReaderT OrgParserLocal m) Text]
    -> ParsecT Text u (ReaderT OrgParserLocal m) [Text]
    forall (t :: * -> *) (m :: * -> *) a.
    (Traversable t, Monad m) =>
    t (m a) -> m (t a)
    sequence [Char -> ParsecT Text u (ReaderT OrgParserLocal m) Text
    forall s (m :: * -> *) st.
    Stream s m Char =>
    Char -> ParserT s st m Text
    tillSpecifier 'h', ParsecT Text u (ReaderT OrgParserLocal m) Text
    forall st. ParserT Text st (ReaderT OrgParserLocal m) Text
    rest]
       justAppend :: ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    justAppend   = ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    -> ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
     -> ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text))
    -> ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    -> ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    (<>) (Text -> Text -> Text)
    -> ParsecT Text u (ReaderT OrgParserLocal m) Text
    -> ParsecT Text u (ReaderT OrgParserLocal m) (Text -> Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT Text u (ReaderT OrgParserLocal m) Text
    forall st. ParserT Text st (ReaderT OrgParserLocal m) Text
    rest

       rest :: ParserT Text st (ReaderT OrgParserLocal m) Text
    rest            = ParserT Text st (ReaderT OrgParserLocal m) Char
    -> ParserT Text st (ReaderT OrgParserLocal m) ()
    -> ParserT Text st (ReaderT OrgParserLocal m) Text
    forall s (m :: * -> *) t st a.
    Stream s m t =>
    ParserT s st m Char -> ParserT s st m a -> ParserT s st m Text
    manyTillChar ParserT Text st (ReaderT OrgParserLocal m) Char
    forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
    anyChar         (ParserT Text st (ReaderT OrgParserLocal m) ()
    forall s (m :: * -> *) t u.
    (Stream s m t, Show t) =>
    ParsecT s u m ()
    eof ParserT Text st (ReaderT OrgParserLocal m) ()
    -> ParserT Text st (ReaderT OrgParserLocal m) ()
    -> ParserT Text st (ReaderT OrgParserLocal m) ()
    forall s u (m :: * -> *) a.
    ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
    <|> () ()
    -> ParserT Text st (ReaderT OrgParserLocal m) Char
    -> ParserT Text st (ReaderT OrgParserLocal m) ()
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ [Char] -> ParserT Text st (ReaderT OrgParserLocal m) Char
    forall s (m :: * -> *) u.
    Stream s m Char =>
    [Char] -> ParsecT s u m Char
    oneOf "\n\r")
       tillSpecifier :: Char -> ParserT s st m Text
    tillSpecifier c :: Char
    c = ParserT s st m Char -> ParserT s st m [Char] -> ParserT s st m Text
    forall s (m :: * -> *) t st a.
    Stream s m t =>
    ParserT s st m Char -> ParserT s st m a -> ParserT s st m Text
    manyTillChar ([Char] -> ParserT s st m Char
    forall s (m :: * -> *) u.
    Stream s m Char =>
    [Char] -> ParsecT s u m Char
    noneOf "\n\r") (ParserT s st m [Char] -> ParserT s st m [Char]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParserT s st m [Char] -> ParserT s st m [Char])
    -> ParserT s st m [Char] -> ParserT s st m [Char]
    forall a b. (a -> b) -> a -> b
    $ [Char] -> ParserT s st m [Char]
    forall s (m :: * -> *) u.
    Stream s m Char =>
    [Char] -> ParsecT s u m [Char]
    string ('%'Char -> [Char] -> [Char]
    forall a. a -> [a] -> [a]
    :Char
    cChar -> [Char] -> [Char]
    forall a. a -> [a] -> [a]
    :""))

    tagList :: Monad m => OrgParser m [Tag]
    tagList :: OrgParser m [Tag]
    tagList = do
      ParserT Text OrgParserState (ReaderT OrgParserLocal m) ()
    forall s (m :: * -> *) st. Stream s m Char => ParserT s st m ()
    skipSpaces
      (Text -> Tag) -> [Text] -> [Tag]
    forall a b. (a -> b) -> [a] -> [b]
    map Text -> Tag
    Tag ([Text] -> [Tag])
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) [Text]
    -> OrgParser m [Tag]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) [Text]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
    many (ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall (m :: * -> *). Monad m => OrgParser m Text
    orgTagWord ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> ParserT Text OrgParserState (ReaderT OrgParserLocal m) ()
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* ParserT Text OrgParserState (ReaderT OrgParserLocal m) ()
    forall s (m :: * -> *) st. Stream s m Char => ParserT s st m ()
    skipSpaces) OrgParser m [Tag]
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> OrgParser m [Tag]
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall (m :: * -> *). Monad m => OrgParser m Char
    newline

    setExcludedTags :: [Tag] -> OrgParserState -> OrgParserState
    setExcludedTags :: [Tag] -> OrgParserState -> OrgParserState
    setExcludedTags tags :: [Tag]
    tags st :: OrgParserState
    st =
      let finalSet :: Set Tag
    finalSet = if OrgParserState -> Bool
    orgStateExcludeTagsChanged OrgParserState
    st
                       then (Tag -> Set Tag -> Set Tag) -> Set Tag -> [Tag] -> Set Tag
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr Tag -> Set Tag -> Set Tag
    forall a. Ord a => a -> Set a -> Set a
    Set.insert (OrgParserState -> Set Tag
    orgStateExcludeTags OrgParserState
    st) [Tag]
    tags
                       else [Tag] -> Set Tag
    forall a. Ord a => [a] -> Set a
    Set.fromList [Tag]
    tags
      in OrgParserState
    st { orgStateExcludeTags :: Set Tag
    orgStateExcludeTags = Set Tag
    finalSet, orgStateExcludeTagsChanged :: Bool
    orgStateExcludeTagsChanged = Bool
    True }

    setSelectedTags :: [Tag] -> OrgParserState -> OrgParserState
    setSelectedTags :: [Tag] -> OrgParserState -> OrgParserState
    setSelectedTags tags :: [Tag]
    tags st :: OrgParserState
    st =
      let finalSet :: Set Tag
    finalSet = if OrgParserState -> Bool
    orgStateSelectTagsChanged OrgParserState
    st
                       then (Tag -> Set Tag -> Set Tag) -> Set Tag -> [Tag] -> Set Tag
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr Tag -> Set Tag -> Set Tag
    forall a. Ord a => a -> Set a -> Set a
    Set.insert (OrgParserState -> Set Tag
    orgStateSelectTags OrgParserState
    st) [Tag]
    tags
                       else [Tag] -> Set Tag
    forall a. Ord a => [a] -> Set a
    Set.fromList [Tag]
    tags
      in OrgParserState
    st { orgStateSelectTags :: Set Tag
    orgStateSelectTags = Set Tag
    finalSet, orgStateSelectTagsChanged :: Bool
    orgStateSelectTagsChanged = Bool
    True }

    setEmphasisPreChar :: Maybe [Char] -> OrgParserState -> OrgParserState
    setEmphasisPreChar :: Maybe [Char] -> OrgParserState -> OrgParserState
    setEmphasisPreChar csMb :: Maybe [Char]
    csMb st :: OrgParserState
    st =
      let preChars :: [Char]
    preChars = [Char] -> Maybe [Char] -> [Char]
    forall a. a -> Maybe a -> a
    fromMaybe (OrgParserState -> [Char]
    orgStateEmphasisPostChars OrgParserState
    defaultOrgParserState) Maybe [Char]
    csMb
      in OrgParserState
    st { orgStateEmphasisPreChars :: [Char]
    orgStateEmphasisPreChars = [Char]
    preChars }

    setEmphasisPostChar :: Maybe [Char] -> OrgParserState -> OrgParserState
    setEmphasisPostChar :: Maybe [Char] -> OrgParserState -> OrgParserState
    setEmphasisPostChar csMb :: Maybe [Char]
    csMb st :: OrgParserState
    st =
      let postChars :: [Char]
    postChars = [Char] -> Maybe [Char] -> [Char]
    forall a. a -> Maybe a -> a
    fromMaybe (OrgParserState -> [Char]
    orgStateEmphasisPostChars OrgParserState
    defaultOrgParserState) Maybe [Char]
    csMb
      in OrgParserState
    st { orgStateEmphasisPostChars :: [Char]
    orgStateEmphasisPostChars = [Char]
    postChars }

    emphChars :: Monad m => OrgParser m (Maybe [Char])
    emphChars :: OrgParser m (Maybe [Char])
    emphChars = do
      ParserT Text OrgParserState (ReaderT OrgParserLocal m) ()
    forall s (m :: * -> *) st. Stream s m Char => ParserT s st m ()
    skipSpaces
      Text -> Maybe [Char]
    forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
    safeRead (Text -> Maybe [Char])
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> OrgParser m (Maybe [Char])
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall (m :: * -> *). Monad m => OrgParser m Text
    anyLine

    inlinesTillNewline :: PandocMonad m => OrgParser m (F Inlines)
    inlinesTillNewline :: OrgParser m (Future OrgParserState (Many Inline))
    inlinesTillNewline = do
      OrgParser m ()
    forall (m :: * -> *). Monad m => OrgParser m ()
    updateLastPreCharPos
      Future OrgParserState (Many Inline)
    -> Future OrgParserState (Many Inline)
    forall s. Future s (Many Inline) -> Future s (Many Inline)
    trimInlinesF (Future OrgParserState (Many Inline)
     -> Future OrgParserState (Many Inline))
    -> ([Future OrgParserState (Many Inline)]
        -> Future OrgParserState (Many Inline))
    -> [Future OrgParserState (Many Inline)]
    -> Future OrgParserState (Many Inline)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Future OrgParserState (Many Inline)]
    -> Future OrgParserState (Many Inline)
    forall a. Monoid a => [a] -> a
    mconcat ([Future OrgParserState (Many Inline)]
     -> Future OrgParserState (Many Inline))
    -> ParsecT
         Text
         OrgParserState
         (ReaderT OrgParserLocal m)
         [Future OrgParserState (Many Inline)]
    -> OrgParser m (Future OrgParserState (Many Inline))
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> OrgParser m (Future OrgParserState (Many Inline))
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> ParsecT
         Text
         OrgParserState
         (ReaderT OrgParserLocal m)
         [Future OrgParserState (Many Inline)]
    forall s (m :: * -> *) t u a sep.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
    manyTill OrgParser m (Future OrgParserState (Many Inline))
    forall (m :: * -> *).
    PandocMonad m =>
    OrgParser m (Future OrgParserState (Many Inline))
    inline ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall (m :: * -> *). Monad m => OrgParser m Char
    newline

    --
    -- ToDo Sequences and Keywords
    --
    todoSequence :: Monad m => OrgParser m TodoSequence
    todoSequence :: OrgParser m TodoSequence
    todoSequence = OrgParser m TodoSequence -> OrgParser m TodoSequence
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (OrgParser m TodoSequence -> OrgParser m TodoSequence)
    -> OrgParser m TodoSequence -> OrgParser m TodoSequence
    forall a b. (a -> b) -> a -> b
    $ do
      [Text]
    todoKws <- OrgParser m [Text]
    forall (m :: * -> *). Monad m => OrgParser m [Text]
    todoKeywords
      Maybe [Text]
    doneKws <- OrgParser m [Text]
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (Maybe [Text])
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m (Maybe a)
    optionMaybe (OrgParser m [Text]
     -> ParsecT
          Text OrgParserState (ReaderT OrgParserLocal m) (Maybe [Text]))
    -> OrgParser m [Text]
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (Maybe [Text])
    forall a b. (a -> b) -> a -> b
    $ OrgParser m ()
    forall (m :: * -> *). Monad m => OrgParser m ()
    todoDoneSep OrgParser m () -> OrgParser m [Text] -> OrgParser m [Text]
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> OrgParser m [Text]
    forall (m :: * -> *). Monad m => OrgParser m [Text]
    todoKeywords
      OrgParser m Char
    forall (m :: * -> *). Monad m => OrgParser m Char
    newline
      -- There must be at least one DONE keyword. The last TODO keyword is taken if
      -- necessary.
      case Maybe [Text]
    doneKws of
        Just done :: [Text]
    done  -> TodoSequence -> OrgParser m TodoSequence
    forall (m :: * -> *) a. Monad m => a -> m a
    return (TodoSequence -> OrgParser m TodoSequence)
    -> TodoSequence -> OrgParser m TodoSequence
    forall a b. (a -> b) -> a -> b
    $ [Text] -> [Text] -> TodoSequence
    keywordsToSequence [Text]
    todoKws [Text]
    done
        Nothing    -> case [Text] -> [Text]
    forall a. [a] -> [a]
    reverse [Text]
    todoKws of
                        []     -> OrgParser m TodoSequence
    forall (m :: * -> *) a. MonadPlus m => m a
    mzero  -- no keywords present
                        (x :: Text
    x:xs :: [Text]
    xs) -> TodoSequence -> OrgParser m TodoSequence
    forall (m :: * -> *) a. Monad m => a -> m a
    return (TodoSequence -> OrgParser m TodoSequence)
    -> TodoSequence -> OrgParser m TodoSequence
    forall a b. (a -> b) -> a -> b
    $ [Text] -> [Text] -> TodoSequence
    keywordsToSequence ([Text] -> [Text]
    forall a. [a] -> [a]
    reverse [Text]
    xs) [Text
    x]

     where
       todoKeywords :: Monad m => OrgParser m [Text]
       todoKeywords :: OrgParser m [Text]
    todoKeywords = OrgParser m [Text] -> OrgParser m [Text]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (OrgParser m [Text] -> OrgParser m [Text])
    -> OrgParser m [Text] -> OrgParser m [Text]
    forall a b. (a -> b) -> a -> b
    $
         let keyword :: ParsecT Text st (ReaderT OrgParserLocal m) Text
    keyword = ParserT Text st (ReaderT OrgParserLocal m) Char
    -> ParsecT Text st (ReaderT OrgParserLocal m) Text
    forall s (m :: * -> *) t st.
    Stream s m t =>
    ParserT s st m Char -> ParserT s st m Text
    many1Char ParserT Text st (ReaderT OrgParserLocal m) Char
    forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
    nonspaceChar ParsecT Text st (ReaderT OrgParserLocal m) Text
    -> ParsecT Text st (ReaderT OrgParserLocal m) ()
    -> ParsecT Text st (ReaderT OrgParserLocal m) Text
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* ParsecT Text st (ReaderT OrgParserLocal m) ()
    forall s (m :: * -> *) st. Stream s m Char => ParserT s st m ()
    skipSpaces
             endOfKeywords :: ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    endOfKeywords = ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    forall (m :: * -> *). Monad m => OrgParser m ()
    todoDoneSep ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    forall s u (m :: * -> *) a.
    ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
    <|> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    forall (f :: * -> *) a. Functor f => f a -> f ()
    void ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall (m :: * -> *). Monad m => OrgParser m Char
    newline
         in ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    -> OrgParser m [Text]
    forall s (m :: * -> *) t u a sep.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
    manyTill ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall st. ParsecT Text st (ReaderT OrgParserLocal m) Text
    keyword (ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    forall s (m :: * -> *) t u a.
    Stream s m t =>
    ParsecT s u m a -> ParsecT s u m a
    lookAhead ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    endOfKeywords)

       todoDoneSep :: Monad m => OrgParser m ()
       todoDoneSep :: OrgParser m ()
    todoDoneSep = ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> OrgParser m ()
    forall (f :: * -> *) a. Functor f => f a -> f ()
    void (ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
     -> OrgParser m ())
    -> (ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
        -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char)
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> OrgParser m ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
     -> OrgParser m ())
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> OrgParser m ()
    forall a b. (a -> b) -> a -> b
    $ OrgParser m ()
    forall s (m :: * -> *) st. Stream s m Char => ParserT s st m ()
    skipSpaces OrgParser m ()
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> Char -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall s (m :: * -> *) u.
    Stream s m Char =>
    Char -> ParsecT s u m Char
    char '|' ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> OrgParser m ()
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* OrgParser m ()
    forall (m :: * -> *). Monad m => OrgParser m ()
    skipSpaces1

       keywordsToSequence :: [Text] -> [Text] -> TodoSequence
       keywordsToSequence :: [Text] -> [Text] -> TodoSequence
    keywordsToSequence todo :: [Text]
    todo done :: [Text]
    done =
         let todoMarkers :: TodoSequence
    todoMarkers = (Text -> TodoMarker) -> [Text] -> TodoSequence
    forall a b. (a -> b) -> [a] -> [b]
    map (TodoState -> Text -> TodoMarker
    TodoMarker TodoState
    Todo) [Text]
    todo
             doneMarkers :: TodoSequence
    doneMarkers = (Text -> TodoMarker) -> [Text] -> TodoSequence
    forall a b. (a -> b) -> [a] -> [b]
    map (TodoState -> Text -> TodoMarker
    TodoMarker TodoState
    Done) [Text]
    done
         in TodoSequence
    todoMarkers TodoSequence -> TodoSequence -> TodoSequence
    forall a. [a] -> [a] -> [a]
    ++ TodoSequence
    doneMarkers

    macroDefinition :: Monad m => OrgParser m (Text, [Text] -> Text)
    macroDefinition :: OrgParser m (Text, [Text] -> Text)
    macroDefinition = OrgParser m (Text, [Text] -> Text)
    -> OrgParser m (Text, [Text] -> Text)
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (OrgParser m (Text, [Text] -> Text)
     -> OrgParser m (Text, [Text] -> Text))
    -> OrgParser m (Text, [Text] -> Text)
    -> OrgParser m (Text, [Text] -> Text)
    forall a b. (a -> b) -> a -> b
    $ do
      Text
    macroName <- ParserT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> ParserT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall s (m :: * -> *) t st.
    Stream s m t =>
    ParserT s st m Char -> ParserT s st m Text
    many1Char ParserT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
    nonspaceChar ParserT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    -> ParserT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
    <* ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    forall s (m :: * -> *) st. Stream s m Char => ParserT s st m ()
    skipSpaces
      Text
    firstPart <- ParserT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall (m :: * -> *). Monad m => OrgParser m Text
    expansionPart
      (elemOrder :: [Int]
    elemOrder, parts :: [Text]
    parts) <- [(Int, Text)] -> ([Int], [Text])
    forall a b. [(a, b)] -> ([a], [b])
    unzip ([(Int, Text)] -> ([Int], [Text]))
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) [(Int, Text)]
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) ([Int], [Text])
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) (Int, Text)
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) [(Int, Text)]
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
    many ((,) (Int -> Text -> (Int, Text))
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Int
    -> ParsecT
         Text
         OrgParserState
         (ReaderT OrgParserLocal m)
         (Text -> (Int, Text))
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Int
    forall (m :: * -> *). Monad m => OrgParser m Int
    placeholder ParsecT
      Text
      OrgParserState
      (ReaderT OrgParserLocal m)
      (Text -> (Int, Text))
    -> ParserT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> ParsecT
         Text OrgParserState (ReaderT OrgParserLocal m) (Int, Text)
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> ParserT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall (m :: * -> *). Monad m => OrgParser m Text
    expansionPart)
      let expander :: [Text] -> Text
    expander = [Text] -> Text
    forall a. Monoid a => [a] -> a
    mconcat ([Text] -> Text) -> ([Text] -> [Text]) -> [Text] -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    alternate (Text
    firstPartText -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    :[Text]
    parts) ([Text] -> [Text]) -> ([Text] -> [Text]) -> [Text] -> [Text]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Int] -> [Text] -> [Text]
    reorder [Int]
    elemOrder
      (Text, [Text] -> Text) -> OrgParser m (Text, [Text] -> Text)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text
    macroName, [Text] -> Text
    expander)
     where
      placeholder :: Monad m => OrgParser m Int
      placeholder :: OrgParser m Int
    placeholder = OrgParser m Int -> OrgParser m Int
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (OrgParser m Int -> OrgParser m Int)
    -> (ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
        -> OrgParser m Int)
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> OrgParser m Int
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Text -> Int)
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> OrgParser m Int
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap (Int -> Maybe Int -> Int
    forall a. a -> Maybe a -> a
    fromMaybe 1 (Maybe Int -> Int) -> (Text -> Maybe Int) -> Text -> Int
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Maybe Int
    forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
    safeRead) (ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
     -> OrgParser m Int)
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> OrgParser m Int
    forall a b. (a -> b) -> a -> b
    $ Char -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall s (m :: * -> *) u.
    Stream s m Char =>
    Char -> ParsecT s u m Char
    char '$' ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Text
    forall s (m :: * -> *) t st.
    Stream s m t =>
    ParserT s st m Char -> ParserT s st m Text
    many1Char ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
    digit

      expansionPart :: Monad m => OrgParser m Text
      expansionPart :: OrgParser m Text
    expansionPart = OrgParser m Text -> OrgParser m Text
    forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
    try (OrgParser m Text -> OrgParser m Text)
    -> OrgParser m Text -> OrgParser m Text
    forall a b. (a -> b) -> a -> b
    $ ParserT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> OrgParser m Text
    forall s (m :: * -> *) t st.
    Stream s m t =>
    ParserT s st m Char -> ParserT s st m Text
    manyChar (ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Int
    -> ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    forall s (m :: * -> *) t a u.
    (Stream s m t, Show a) =>
    ParsecT s u m a -> ParsecT s u m ()
    notFollowedBy ParsecT Text OrgParserState (ReaderT OrgParserLocal m) Int
    forall (m :: * -> *). Monad m => OrgParser m Int
    placeholder ParsecT Text OrgParserState (ReaderT OrgParserLocal m) ()
    -> ParserT Text OrgParserState (ReaderT OrgParserLocal m) Char
    -> ParserT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
    *> [Char]
    -> ParserT Text OrgParserState (ReaderT OrgParserLocal m) Char
    forall s (m :: * -> *) u.
    Stream s m Char =>
    [Char] -> ParsecT s u m Char
    noneOf "\n\r")

      alternate :: [a] -> [a] -> [a]
      alternate :: [a] -> [a] -> [a]
    alternate []     ys :: [a]
    ys     = [a]
    ys
      alternate xs :: [a]
    xs     []     = [a]
    xs
      alternate (x :: a
    x:xs :: [a]
    xs) (y :: a
    y:ys :: [a]
    ys) = a
    x a -> [a] -> [a]
    forall a. a -> [a] -> [a]
    : a
    y a -> [a] -> [a]
    forall a. a -> [a] -> [a]
    : [a] -> [a] -> [a]
    forall a. [a] -> [a] -> [a]
    alternate [a]
    xs [a]
    ys

      reorder :: [Int] -> [Text] -> [Text]
      reorder :: [Int] -> [Text] -> [Text]
    reorder perm :: [Int]
    perm xs :: [Text]
    xs =
        let element :: Int -> [Text]
    element n :: Int
    n = Int -> [Text] -> [Text]
    forall a. Int -> [a] -> [a]
    take 1 ([Text] -> [Text]) -> [Text] -> [Text]
    forall a b. (a -> b) -> a -> b
    $ Int -> [Text] -> [Text]
    forall a. Int -> [a] -> [a]
    drop (Int
    n Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - 1) [Text]
    xs
        in (Int -> [Text]) -> [Int] -> [Text]
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap Int -> [Text]
    element [Int]
    perm