{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
{- |
   Module      : Text.Pandoc.Readers.Txt2Tags
   Copyright   : Copyright (C) 2014 Matthew Pickering
   License     : GNU GPL, version 2 or above

   Maintainer  : Matthew Pickering <matthewtpickering@gmail.com>

Conversion of txt2tags formatted plain text to 'Pandoc' document.
-}
module Text.Pandoc.Readers.Txt2Tags ( readTxt2Tags
                                    , getT2TMeta
                                    , T2TMeta (..)
                                    )
                                    where

import Control.Monad (guard, void, when)
import Control.Monad.Except (catchError, throwError)
import Control.Monad.Reader (Reader, asks, runReader)
import Data.Default
import Data.List (intercalate, transpose)
import Data.List.NonEmpty (nonEmpty)
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time.Format (formatTime)
import Text.Pandoc.Builder (Blocks, Inlines, trimInlines)
import qualified Text.Pandoc.Builder as B
import Text.Pandoc.Class.PandocMonad (PandocMonad)
import qualified Text.Pandoc.Class.PandocMonad as P
import Data.Time (defaultTimeLocale)
import Text.Pandoc.Definition
import Text.Pandoc.Options
import Text.Pandoc.Parsing hiding (space, spaces, uri)
import Text.Pandoc.Shared (compactify, compactifyDL)
import Text.Pandoc.URI (escapeURI)

type T2T = ParsecT Sources ParserState (Reader T2TMeta)

-- | An object for the T2T macros meta information
-- the contents of each field is simply substituted verbatim into the file
data  T2TMeta = T2TMeta {
                 T2TMeta -> Text
date    :: Text -- ^ Current date
               , T2TMeta -> Text
mtime   :: Text -- ^ Last modification time of infile
               , T2TMeta -> [Char]
infile  :: FilePath -- ^ Input file
               , T2TMeta -> [Char]
outfile :: FilePath -- ^ Output file
               } deriving Column -> T2TMeta -> ShowS
[T2TMeta] -> ShowS
T2TMeta -> [Char]
(Column -> T2TMeta -> ShowS)
-> (T2TMeta -> [Char]) -> ([T2TMeta] -> ShowS) -> Show T2TMeta
forall a.
(Column -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Column -> T2TMeta -> ShowS
showsPrec :: Column -> T2TMeta -> ShowS
$cshow :: T2TMeta -> [Char]
show :: T2TMeta -> [Char]
$cshowList :: [T2TMeta] -> ShowS
showList :: [T2TMeta] -> ShowS
Show

instance Default T2TMeta where
    def :: T2TMeta
def = Text -> Text -> [Char] -> [Char] -> T2TMeta
T2TMeta Text
"" Text
"" [Char]
"" [Char]
""

-- | Get the meta information required by Txt2Tags macros
getT2TMeta :: PandocMonad m => m T2TMeta
getT2TMeta :: forall (m :: * -> *). PandocMonad m => m T2TMeta
getT2TMeta = do
    [[Char]]
inps <- m [[Char]]
forall (m :: * -> *). PandocMonad m => m [[Char]]
P.getInputFiles
    [Char]
outp <- [Char] -> Maybe [Char] -> [Char]
forall a. a -> Maybe a -> a
fromMaybe [Char]
"" (Maybe [Char] -> [Char]) -> m (Maybe [Char]) -> m [Char]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (Maybe [Char])
forall (m :: * -> *). PandocMonad m => m (Maybe [Char])
P.getOutputFile
    [Char]
curDate <- TimeLocale -> [Char] -> ZonedTime -> [Char]
forall t. FormatTime t => TimeLocale -> [Char] -> t -> [Char]
formatTime TimeLocale
defaultTimeLocale [Char]
"%F" (ZonedTime -> [Char]) -> m ZonedTime -> m [Char]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m ZonedTime
forall (m :: * -> *). PandocMonad m => m ZonedTime
P.getZonedTime
    [Char]
curMtime <- m [Char] -> (PandocError -> m [Char]) -> m [Char]
forall a. m a -> (PandocError -> m a) -> m a
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
catchError
                 (([Char] -> m UTCTime) -> [[Char]] -> m [UTCTime]
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 [Char] -> m UTCTime
forall (m :: * -> *). PandocMonad m => [Char] -> m UTCTime
P.getModificationTime [[Char]]
inps m [UTCTime] -> ([UTCTime] -> m [Char]) -> m [Char]
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
                   (\case
                       Maybe (NonEmpty UTCTime)
Nothing ->
                         TimeLocale -> [Char] -> ZonedTime -> [Char]
forall t. FormatTime t => TimeLocale -> [Char] -> t -> [Char]
formatTime TimeLocale
defaultTimeLocale [Char]
"%T" (ZonedTime -> [Char]) -> m ZonedTime -> m [Char]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m ZonedTime
forall (m :: * -> *). PandocMonad m => m ZonedTime
P.getZonedTime
                       Just NonEmpty UTCTime
ts -> [Char] -> m [Char]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Char] -> m [Char]) -> [Char] -> m [Char]
forall a b. (a -> b) -> a -> b
$
                         TimeLocale -> [Char] -> UTCTime -> [Char]
forall t. FormatTime t => TimeLocale -> [Char] -> t -> [Char]
formatTime TimeLocale
defaultTimeLocale [Char]
"%T" (UTCTime -> [Char]) -> UTCTime -> [Char]
forall a b. (a -> b) -> a -> b
$ NonEmpty UTCTime -> UTCTime
forall a. Ord a => NonEmpty a -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum NonEmpty UTCTime
ts)
                    (Maybe (NonEmpty UTCTime) -> m [Char])
-> ([UTCTime] -> Maybe (NonEmpty UTCTime)) -> [UTCTime] -> m [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [UTCTime] -> Maybe (NonEmpty UTCTime)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty)
                (m [Char] -> PandocError -> m [Char]
forall a b. a -> b -> a
const ([Char] -> m [Char]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Char]
""))
    T2TMeta -> m T2TMeta
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (T2TMeta -> m T2TMeta) -> T2TMeta -> m T2TMeta
forall a b. (a -> b) -> a -> b
$ Text -> Text -> [Char] -> [Char] -> T2TMeta
T2TMeta ([Char] -> Text
T.pack [Char]
curDate) ([Char] -> Text
T.pack [Char]
curMtime)
                     ([Char] -> [[Char]] -> [Char]
forall a. [a] -> [[a]] -> [a]
intercalate [Char]
", " [[Char]]
inps) [Char]
outp

-- | Read Txt2Tags from an input string returning a Pandoc document
readTxt2Tags :: (PandocMonad m, ToSources a)
             => ReaderOptions
             -> a
             -> m Pandoc
readTxt2Tags :: forall (m :: * -> *) a.
(PandocMonad m, ToSources a) =>
ReaderOptions -> a -> m Pandoc
readTxt2Tags ReaderOptions
opts a
s = do
  let sources :: Sources
sources = Column -> Sources -> Sources
ensureFinalNewlines Column
2 (a -> Sources
forall a. ToSources a => a -> Sources
toSources a
s)
  T2TMeta
meta <- m T2TMeta
forall (m :: * -> *). PandocMonad m => m T2TMeta
getT2TMeta
  let parsed :: Either PandocError Pandoc
parsed = (Reader T2TMeta (Either PandocError Pandoc)
 -> T2TMeta -> Either PandocError Pandoc)
-> T2TMeta
-> Reader T2TMeta (Either PandocError Pandoc)
-> Either PandocError Pandoc
forall a b c. (a -> b -> c) -> b -> a -> c
flip Reader T2TMeta (Either PandocError Pandoc)
-> T2TMeta -> Either PandocError Pandoc
forall r a. Reader r a -> r -> a
runReader T2TMeta
meta (Reader T2TMeta (Either PandocError Pandoc)
 -> Either PandocError Pandoc)
-> Reader T2TMeta (Either PandocError Pandoc)
-> Either PandocError Pandoc
forall a b. (a -> b) -> a -> b
$
        ParsecT Sources ParserState (Reader T2TMeta) Pandoc
-> ParserState
-> Sources
-> Reader T2TMeta (Either PandocError Pandoc)
forall (m :: * -> *) t st a.
(Monad m, ToSources t) =>
ParsecT Sources st m a -> st -> t -> m (Either PandocError a)
readWithM ParsecT Sources ParserState (Reader T2TMeta) Pandoc
parseT2T (ParserState
forall a. Default a => a
def {stateOptions :: ReaderOptions
stateOptions = ReaderOptions
opts}) Sources
sources
  case Either PandocError Pandoc
parsed of
    Right Pandoc
result -> Pandoc -> m Pandoc
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Pandoc
result
    Left PandocError
e       -> PandocError -> m Pandoc
forall a. PandocError -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError PandocError
e

-- | Read Txt2Tags (ignoring all macros) from an input string returning
-- a Pandoc document
-- readTxt2TagsNoMacros :: PandocMonad m => ReaderOptions -> Text -> m Pandoc
-- readTxt2TagsNoMacros = readTxt2Tags

parseT2T :: T2T Pandoc
parseT2T :: ParsecT Sources ParserState (Reader T2TMeta) Pandoc
parseT2T = do
  -- Parse header if standalone flag is set
  Bool
standalone <- (ReaderOptions -> Bool)
-> ParsecT Sources ParserState (Reader T2TMeta) Bool
forall st s (m :: * -> *) t b.
(HasReaderOptions st, Stream s m t) =>
(ReaderOptions -> b) -> ParsecT s st m b
forall s (m :: * -> *) t b.
Stream s m t =>
(ReaderOptions -> b) -> ParsecT s ParserState m b
getOption ReaderOptions -> Bool
readerStandalone
  Bool
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
standalone ParsecT Sources ParserState (Reader T2TMeta) ()
parseHeader
  Blocks
body <- [Blocks] -> Blocks
forall a. Monoid a => [a] -> a
mconcat ([Blocks] -> Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) [Blocks]
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>  ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) [Blocks]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill ParsecT Sources ParserState (Reader T2TMeta) Blocks
block ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m ()
eof
  Meta
meta' <- ParserState -> Meta
stateMeta (ParserState -> Meta)
-> ParsecT Sources ParserState (Reader T2TMeta) ParserState
-> ParsecT Sources ParserState (Reader T2TMeta) Meta
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) ParserState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  Pandoc -> ParsecT Sources ParserState (Reader T2TMeta) Pandoc
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Pandoc -> ParsecT Sources ParserState (Reader T2TMeta) Pandoc)
-> Pandoc -> ParsecT Sources ParserState (Reader T2TMeta) Pandoc
forall a b. (a -> b) -> a -> b
$ Meta -> [Block] -> Pandoc
Pandoc Meta
meta' (Blocks -> [Block]
forall a. Many a -> [a]
B.toList Blocks
body)

parseHeader :: T2T ()
parseHeader :: ParsecT Sources ParserState (Reader T2TMeta) ()
parseHeader = do
  () ()
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a b.
a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
blankline ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT Sources ParserState (Reader T2TMeta) ()
header
  Meta
meta <- ParserState -> Meta
stateMeta (ParserState -> Meta)
-> ParsecT Sources ParserState (Reader T2TMeta) ParserState
-> ParsecT Sources ParserState (Reader T2TMeta) Meta
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) ParserState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Text
blanklines
  [(Text, Text)]
config <- ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) [(Text, Text)]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
setting (ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
setting)
  -- TODO: Handle settings better
  let settings :: Meta
settings = ((Text, Text) -> Meta -> Meta) -> Meta -> [(Text, Text)] -> Meta
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\(Text
k,Text
v) -> Text -> MetaValue -> Meta -> Meta
forall a b. (HasMeta a, ToMetaValue b) => Text -> b -> a -> a
forall b. ToMetaValue b => Text -> b -> Meta -> Meta
B.setMeta Text
k (Text -> MetaValue
MetaString Text
v)) Meta
meta [(Text, Text)]
config
  (ParserState -> ParserState)
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState (\ParserState
s -> ParserState
s {stateMeta :: Meta
stateMeta = Meta
settings}) ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Text
blanklines

header :: T2T ()
header :: ParsecT Sources ParserState (Reader T2TMeta) ()
header = ParsecT Sources ParserState (Reader T2TMeta) ()
titleline ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT Sources ParserState (Reader T2TMeta) ()
authorline ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT Sources ParserState (Reader T2TMeta) ()
dateline

headerline :: B.ToMetaValue a => Text -> T2T a -> T2T ()
headerline :: forall a.
ToMetaValue a =>
Text -> T2T a -> ParsecT Sources ParserState (Reader T2TMeta) ()
headerline Text
field T2T a
p = (() ()
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a b.
a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
blankline)
                        ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> (T2T a
p T2T a
-> (a -> ParsecT Sources ParserState (Reader T2TMeta) ())
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> (a -> ParsecT Sources ParserState (Reader T2TMeta) b)
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (ParserState -> ParserState)
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((ParserState -> ParserState)
 -> ParsecT Sources ParserState (Reader T2TMeta) ())
-> (a -> ParserState -> ParserState)
-> a
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> a -> ParserState -> ParserState
forall a b. (HasMeta a, ToMetaValue b) => Text -> b -> a -> a
forall b. ToMetaValue b => Text -> b -> ParserState -> ParserState
B.setMeta Text
field)

titleline :: T2T ()
titleline :: ParsecT Sources ParserState (Reader T2TMeta) ()
titleline =
  Text
-> T2T Inlines -> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a.
ToMetaValue a =>
Text -> T2T a -> ParsecT Sources ParserState (Reader T2TMeta) ()
headerline Text
"title" (Inlines -> Inlines
trimInlines (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 Sources ParserState (Reader T2TMeta) [Inlines]
-> T2T Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> T2T Inlines
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Inlines]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill T2T Inlines
inline ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
newline)

authorline :: T2T ()
authorline :: ParsecT Sources ParserState (Reader T2TMeta) ()
authorline =
  Text
-> ParsecT Sources ParserState (Reader T2TMeta) [Inlines]
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a.
ToMetaValue a =>
Text -> T2T a -> ParsecT Sources ParserState (Reader T2TMeta) ()
headerline Text
"author" (T2T Inlines
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Inlines]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
sepBy T2T Inlines
author (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
';') ParsecT Sources ParserState (Reader T2TMeta) [Inlines]
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Inlines]
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
newline)
  where
    author :: T2T Inlines
author = Inlines -> Inlines
trimInlines (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 Sources ParserState (Reader T2TMeta) [Inlines]
-> T2T Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> T2T Inlines
-> ParsecT Sources ParserState (Reader T2TMeta) [Inlines]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
';' ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
newline) ParsecT Sources ParserState (Reader T2TMeta) ()
-> T2T Inlines -> T2T Inlines
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> T2T Inlines
inline)

dateline :: T2T ()
dateline :: ParsecT Sources ParserState (Reader T2TMeta) ()
dateline = Text
-> T2T Inlines -> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a.
ToMetaValue a =>
Text -> T2T a -> ParsecT Sources ParserState (Reader T2TMeta) ()
headerline Text
"date" (Inlines -> Inlines
trimInlines (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 Sources ParserState (Reader T2TMeta) [Inlines]
-> T2T Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> T2T Inlines
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Inlines]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill T2T Inlines
inline ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
newline)

type Keyword = Text
type Value = Text

setting :: T2T (Keyword, Value)
setting :: ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
setting = do
  [Char] -> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m [Char]
string [Char]
"%!"
  Text
keyword <- ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
ignoreSpacesCap (ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m Text
many1Char ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
alphaNum)
  Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
':'
  Text
value <- ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
ignoreSpacesCap (ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st a.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m a -> ParsecT s st m Text
manyTillChar ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
anyChar ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
newline)
  (Text, Text)
-> ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
keyword, Text
value)

-- Blocks

parseBlocks :: T2T Blocks
parseBlocks :: ParsecT Sources ParserState (Reader T2TMeta) Blocks
parseBlocks = [Blocks] -> Blocks
forall a. Monoid a => [a] -> a
mconcat ([Blocks] -> Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) [Blocks]
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) [Blocks]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill ParsecT Sources ParserState (Reader T2TMeta) Blocks
block ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m ()
eof

block :: T2T Blocks
block :: ParsecT Sources ParserState (Reader T2TMeta) Blocks
block =
  [ParsecT Sources ParserState (Reader T2TMeta) Blocks]
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s (m :: * -> *) t u a.
Stream s m t =>
[ParsecT s u m a] -> ParsecT s u m a
choice
    [ Blocks
forall a. Monoid a => a
mempty Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a b.
a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Text
blanklines
    , ParsecT Sources ParserState (Reader T2TMeta) Blocks
quote
    , ParsecT Sources ParserState (Reader T2TMeta) Blocks
hrule -- hrule must go above title
    , ParsecT Sources ParserState (Reader T2TMeta) Blocks
title
    , ParsecT Sources ParserState (Reader T2TMeta) Blocks
commentBlock
    , ParsecT Sources ParserState (Reader T2TMeta) Blocks
verbatim
    , ParsecT Sources ParserState (Reader T2TMeta) Blocks
rawBlock
    , ParsecT Sources ParserState (Reader T2TMeta) Blocks
taggedBlock
    , ParsecT Sources ParserState (Reader T2TMeta) Blocks
list
    , ParsecT Sources ParserState (Reader T2TMeta) Blocks
table
    , ParsecT Sources ParserState (Reader T2TMeta) Blocks
para
    ]

title :: T2T Blocks
title :: ParsecT Sources ParserState (Reader T2TMeta) Blocks
title = ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) Blocks
 -> ParsecT Sources ParserState (Reader T2TMeta) Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a b. (a -> b) -> a -> b
$ Char -> ParsecT Sources ParserState (Reader T2TMeta) Blocks
balancedTitle Char
'+' ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> Char -> ParsecT Sources ParserState (Reader T2TMeta) Blocks
balancedTitle Char
'='

balancedTitle :: Char -> T2T Blocks
balancedTitle :: Char -> ParsecT Sources ParserState (Reader T2TMeta) Blocks
balancedTitle Char
c = ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) Blocks
 -> ParsecT Sources ParserState (Reader T2TMeta) Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a b. (a -> b) -> a -> b
$ do
  ParsecT Sources ParserState (Reader T2TMeta) Text
spaces
  Column
level <- [Char] -> Column
forall a. [a] -> Column
forall (t :: * -> *) a. Foldable t => t a -> Column
length ([Char] -> Column)
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) Column
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
c)
  Bool -> ParsecT Sources ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Column
level Column -> Column -> Bool
forall a. Ord a => a -> a -> Bool
<= Column
5) -- Max header level 5
  Text
heading <- ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st a.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m a -> ParsecT s st m Text
manyTillChar ([Char] -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
noneOf [Char]
"\n\r") (Column
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall s (m :: * -> *) t u a.
Stream s m t =>
Column -> ParsecT s u m a -> ParsecT s u m [a]
count Column
level (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
c))
  Maybe [Char]
label <- ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) (Maybe [Char])
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m (Maybe a)
optionMaybe (ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall end s (m :: * -> *) st t a.
(Show end, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m t
-> ParsecT s st m end -> ParsecT s st m a -> ParsecT s st m [a]
enclosed (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'[') (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
']') (ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
alphaNum ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> [Char] -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
oneOf [Char]
"_-"))
  ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
spaceChar ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
newline
  let attr :: Attr
attr = Attr -> ([Char] -> Attr) -> Maybe [Char] -> Attr
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Attr
nullAttr (\[Char]
x -> ([Char] -> Text
T.pack [Char]
x, [], [])) Maybe [Char]
label
  Blocks -> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Blocks -> ParsecT Sources ParserState (Reader T2TMeta) Blocks)
-> Blocks -> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a b. (a -> b) -> a -> b
$ Attr -> Column -> Inlines -> Blocks
B.headerWith Attr
attr Column
level (Inlines -> Inlines
trimInlines (Inlines -> Inlines) -> Inlines -> Inlines
forall a b. (a -> b) -> a -> b
$ Text -> Inlines
B.text Text
heading)

para :: T2T Blocks
para :: ParsecT Sources ParserState (Reader T2TMeta) Blocks
para = ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) Blocks
 -> ParsecT Sources ParserState (Reader T2TMeta) Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a b. (a -> b) -> a -> b
$ do
  Inlines
ils <- T2T Inlines
parseInlines
  Bool
nl <- Bool
-> ParsecT Sources ParserState (Reader T2TMeta) Bool
-> ParsecT Sources ParserState (Reader T2TMeta) Bool
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Bool
False (Bool
True Bool
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Bool
forall a b.
a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
newline)
  Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option (Inlines -> Blocks
B.plain Inlines
ils) (Bool -> ParsecT Sources ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
nl ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT Sources ParserState (Reader T2TMeta) Column
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT Sources ParserState (Reader T2TMeta) Column
listStart ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Blocks -> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Inlines -> Blocks
B.para Inlines
ils))
  where
    listStart :: ParsecT Sources ParserState (Reader T2TMeta) Column
listStart = ParsecT Sources ParserState (Reader T2TMeta) Column
-> ParsecT Sources ParserState (Reader T2TMeta) Column
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT Sources ParserState (Reader T2TMeta) Column
bulletListStart ParsecT Sources ParserState (Reader T2TMeta) Column
-> ParsecT Sources ParserState (Reader T2TMeta) Column
-> ParsecT Sources ParserState (Reader T2TMeta) Column
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT Sources ParserState (Reader T2TMeta) Column
orderedListStart

commentBlock :: T2T Blocks
commentBlock :: ParsecT Sources ParserState (Reader T2TMeta) Blocks
commentBlock = ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) Text
-> (Text -> Blocks)
-> Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a.
Monoid a =>
T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
blockMarkupArea ParsecT Sources ParserState (Reader T2TMeta) Text
forall (m :: * -> *) st. Monad m => ParsecT Sources st m Text
anyLine (Blocks -> Text -> Blocks
forall a b. a -> b -> a
const Blocks
forall a. Monoid a => a
mempty) Text
"%%%") ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a. Monoid a => T2T a
comment

-- Separator and Strong line treated the same
hrule :: T2T Blocks
hrule :: ParsecT Sources ParserState (Reader T2TMeta) Blocks
hrule = ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) Blocks
 -> ParsecT Sources ParserState (Reader T2TMeta) Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a b. (a -> b) -> a -> b
$ do
  ParsecT Sources ParserState (Reader T2TMeta) Text
spaces
  [Char]
line <- ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ([Char] -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
oneOf [Char]
"=-_")
  Bool -> ParsecT Sources ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard ([Char] -> Column
forall a. [a] -> Column
forall (t :: * -> *) a. Foldable t => t a -> Column
length [Char]
line Column -> Column -> Bool
forall a. Ord a => a -> a -> Bool
>= Column
20)
  Blocks
B.horizontalRule Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a b.
a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
blankline

quote :: T2T Blocks
quote :: ParsecT Sources ParserState (Reader T2TMeta) Blocks
quote = ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) Blocks
 -> ParsecT Sources ParserState (Reader T2TMeta) Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a b. (a -> b) -> a -> b
$ do
  ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead ParsecT Sources ParserState (Reader T2TMeta) Char
tab
  [Text]
rawQuote <-  ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) [Text]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (ParsecT Sources ParserState (Reader T2TMeta) Char
tab ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional ParsecT Sources ParserState (Reader T2TMeta) Text
spaces ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Sources ParserState (Reader T2TMeta) Text
forall (m :: * -> *) st. Monad m => ParsecT Sources st m Text
anyLine)
  Blocks
contents <- ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> Text -> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall (m :: * -> *) u a.
(Monad m, HasLastStrPosition u) =>
ParsecT Sources u m a -> Text -> ParsecT Sources u m a
parseFromString' ParsecT Sources ParserState (Reader T2TMeta) Blocks
parseBlocks (Text -> [Text] -> Text
T.intercalate Text
"\n" [Text]
rawQuote Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\n")
  Blocks -> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Blocks -> ParsecT Sources ParserState (Reader T2TMeta) Blocks)
-> Blocks -> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a b. (a -> b) -> a -> b
$ Blocks -> Blocks
B.blockQuote Blocks
contents

commentLine :: T2T Inlines
commentLine :: T2T Inlines
commentLine = T2T Inlines
forall a. Monoid a => T2T a
comment

-- List Parsing code from Org Reader

list :: T2T Blocks
list :: ParsecT Sources ParserState (Reader T2TMeta) Blocks
list = [ParsecT Sources ParserState (Reader T2TMeta) Blocks]
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s (m :: * -> *) t u a.
Stream s m t =>
[ParsecT s u m a] -> ParsecT s u m a
choice [ParsecT Sources ParserState (Reader T2TMeta) Blocks
bulletList, ParsecT Sources ParserState (Reader T2TMeta) Blocks
orderedList, ParsecT Sources ParserState (Reader T2TMeta) Blocks
definitionList]

bulletList :: T2T Blocks
bulletList :: ParsecT Sources ParserState (Reader T2TMeta) Blocks
bulletList = [Blocks] -> Blocks
B.bulletList ([Blocks] -> Blocks)
-> ([Blocks] -> [Blocks]) -> [Blocks] -> Blocks
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Blocks] -> [Blocks]
compactify
             ([Blocks] -> Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) [Blocks]
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) [Blocks]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (ParsecT Sources ParserState (Reader T2TMeta) Column
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a.
ParsecT Sources ParserState (Reader T2TMeta) Column
-> T2T a -> T2T a
listItem ParsecT Sources ParserState (Reader T2TMeta) Column
bulletListStart ParsecT Sources ParserState (Reader T2TMeta) Blocks
parseBlocks)

orderedList :: T2T Blocks
orderedList :: ParsecT Sources ParserState (Reader T2TMeta) Blocks
orderedList = [Blocks] -> Blocks
B.orderedList ([Blocks] -> Blocks)
-> ([Blocks] -> [Blocks]) -> [Blocks] -> Blocks
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Blocks] -> [Blocks]
compactify
              ([Blocks] -> Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) [Blocks]
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) [Blocks]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (ParsecT Sources ParserState (Reader T2TMeta) Column
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a.
ParsecT Sources ParserState (Reader T2TMeta) Column
-> T2T a -> T2T a
listItem ParsecT Sources ParserState (Reader T2TMeta) Column
orderedListStart ParsecT Sources ParserState (Reader T2TMeta) Blocks
parseBlocks)

definitionList :: T2T Blocks
definitionList :: ParsecT Sources ParserState (Reader T2TMeta) Blocks
definitionList = ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) Blocks
 -> ParsecT Sources ParserState (Reader T2TMeta) Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a b. (a -> b) -> a -> b
$
  [(Inlines, [Blocks])] -> Blocks
B.definitionList ([(Inlines, [Blocks])] -> Blocks)
-> ([(Inlines, [Blocks])] -> [(Inlines, [Blocks])])
-> [(Inlines, [Blocks])]
-> Blocks
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Inlines, [Blocks])] -> [(Inlines, [Blocks])]
compactifyDL ([(Inlines, [Blocks])] -> Blocks)
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Inlines, [Blocks])]
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
    ParsecT Sources ParserState (Reader T2TMeta) (Inlines, [Blocks])
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Inlines, [Blocks])]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (ParsecT Sources ParserState (Reader T2TMeta) Column
-> ParsecT Sources ParserState (Reader T2TMeta) (Inlines, [Blocks])
-> ParsecT Sources ParserState (Reader T2TMeta) (Inlines, [Blocks])
forall a.
ParsecT Sources ParserState (Reader T2TMeta) Column
-> T2T a -> T2T a
listItem ParsecT Sources ParserState (Reader T2TMeta) Column
definitionListStart ParsecT Sources ParserState (Reader T2TMeta) (Inlines, [Blocks])
definitionListEnd)

definitionListEnd :: T2T (Inlines, [Blocks])
definitionListEnd :: ParsecT Sources ParserState (Reader T2TMeta) (Inlines, [Blocks])
definitionListEnd = (,) (Inlines -> [Blocks] -> (Inlines, [Blocks]))
-> T2T Inlines
-> ParsecT
     Sources
     ParserState
     (Reader T2TMeta)
     ([Blocks] -> (Inlines, [Blocks]))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Inlines] -> Inlines
forall a. Monoid a => [a] -> a
mconcat ([Inlines] -> Inlines)
-> ParsecT Sources ParserState (Reader T2TMeta) [Inlines]
-> T2T Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> T2T Inlines
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Inlines]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill T2T Inlines
inline ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
newline) ParsecT
  Sources
  ParserState
  (Reader T2TMeta)
  ([Blocks] -> (Inlines, [Blocks]))
-> ParsecT Sources ParserState (Reader T2TMeta) [Blocks]
-> ParsecT Sources ParserState (Reader T2TMeta) (Inlines, [Blocks])
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) (a -> b)
-> ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((Blocks -> [Blocks] -> [Blocks]
forall a. a -> [a] -> [a]
:[]) (Blocks -> [Blocks])
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) [Blocks]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Blocks
parseBlocks)

genericListStart :: T2T Char
                 -> T2T Int
genericListStart :: ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Column
genericListStart ParsecT Sources ParserState (Reader T2TMeta) Char
listMarker = ParsecT Sources ParserState (Reader T2TMeta) Column
-> ParsecT Sources ParserState (Reader T2TMeta) Column
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) Column
 -> ParsecT Sources ParserState (Reader T2TMeta) Column)
-> ParsecT Sources ParserState (Reader T2TMeta) Column
-> ParsecT Sources ParserState (Reader T2TMeta) Column
forall a b. (a -> b) -> a -> b
$
  (Column
2Column -> Column -> Column
forall a. Num a => a -> a -> a
+) (Column -> Column)
-> ParsecT Sources ParserState (Reader T2TMeta) Column
-> ParsecT Sources ParserState (Reader T2TMeta) Column
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Char] -> Column
forall a. [a] -> Column
forall (t :: * -> *) a. Foldable t => t a -> Column
length ([Char] -> Column)
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) Column
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
spaceChar
            ParsecT Sources ParserState (Reader T2TMeta) Column
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Column
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Sources ParserState (Reader T2TMeta) Char
listMarker ParsecT Sources ParserState (Reader T2TMeta) Column
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Column
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Sources ParserState (Reader T2TMeta) Char
space ParsecT Sources ParserState (Reader T2TMeta) Column
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) Column
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT Sources ParserState (Reader T2TMeta) Char
space)

-- parses bullet list \start and returns its length (excl. following whitespace)
bulletListStart :: T2T  Int
bulletListStart :: ParsecT Sources ParserState (Reader T2TMeta) Column
bulletListStart = ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Column
genericListStart (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'-')

orderedListStart :: T2T Int
orderedListStart :: ParsecT Sources ParserState (Reader T2TMeta) Column
orderedListStart = ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Column
genericListStart (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'+' )

definitionListStart :: T2T Int
definitionListStart :: ParsecT Sources ParserState (Reader T2TMeta) Column
definitionListStart = ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Column
genericListStart (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
':')

-- parse raw text for one list item, excluding start marker and continuations
listItem :: T2T Int
         -> T2T a
         -> T2T a
listItem :: forall a.
ParsecT Sources ParserState (Reader T2TMeta) Column
-> T2T a -> T2T a
listItem ParsecT Sources ParserState (Reader T2TMeta) Column
start T2T a
end = T2T a -> T2T a
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (T2T a -> T2T a) -> T2T a -> T2T a
forall a b. (a -> b) -> a -> b
$ do
  Column
markerLength <- ParsecT Sources ParserState (Reader T2TMeta) Column
-> ParsecT Sources ParserState (Reader T2TMeta) Column
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT Sources ParserState (Reader T2TMeta) Column
start
  Text
firstLine <- ParsecT Sources ParserState (Reader T2TMeta) Text
forall (m :: * -> *) st. Monad m => ParsecT Sources st m Text
anyLineNewline
  Text
blank <- Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Text
"" (Text
"\n" Text
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b.
a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
blankline)
  Text
rest <- [Text] -> Text
T.concat ([Text] -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) [Text]
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) [Text]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (Column -> ParsecT Sources ParserState (Reader T2TMeta) Text
listContinuation Column
markerLength)
  T2T a -> Text -> T2T a
forall (m :: * -> *) u a.
(Monad m, HasLastStrPosition u) =>
ParsecT Sources u m a -> Text -> ParsecT Sources u m a
parseFromString' T2T a
end (Text -> T2T a) -> Text -> T2T a
forall a b. (a -> b) -> a -> b
$ Text
firstLine Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
blank Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
rest

-- continuation of a list item - indented and separated by blankline or endline.
-- Note: nested lists are parsed as continuations.
listContinuation :: Int
                 -> T2T Text
listContinuation :: Column -> ParsecT Sources ParserState (Reader T2TMeta) Text
listContinuation Column
markerLength = ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) Text
 -> ParsecT Sources ParserState (Reader T2TMeta) Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b. (a -> b) -> a -> b
$
  ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall b s (m :: * -> *) a st.
(Show b, Stream s m a) =>
ParsecT s st m b -> ParsecT s st m ()
notFollowedBy' (ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
blankline ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
blankline)
  ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (Text -> Text -> Text
forall a. Monoid a => a -> a -> a
mappend (Text -> Text -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Text] -> Text
T.concat ([Text] -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) [Text]
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) [Text]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT Sources ParserState (Reader T2TMeta) Text
listLine)
              ParsecT Sources ParserState (Reader T2TMeta) (Text -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) (a -> b)
-> ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m Text
manyChar ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
blankline)
 where listLine :: ParsecT Sources ParserState (Reader T2TMeta) Text
listLine = ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) Text
 -> ParsecT Sources ParserState (Reader T2TMeta) Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b. (a -> b) -> a -> b
$ Column -> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char, HasReaderOptions st) =>
Column -> ParsecT s st m Text
indentWith Column
markerLength ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Sources ParserState (Reader T2TMeta) Text
forall (m :: * -> *) st. Monad m => ParsecT Sources st m Text
anyLineNewline

-- Table

table :: T2T Blocks
table :: ParsecT Sources ParserState (Reader T2TMeta) Blocks
table = ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) Blocks
 -> ParsecT Sources ParserState (Reader T2TMeta) Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a b. (a -> b) -> a -> b
$ do
  [Blocks]
tableHeader <- ((Alignment, Blocks) -> Blocks)
-> [(Alignment, Blocks)] -> [Blocks]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Alignment, Blocks) -> Blocks
forall a b. (a, b) -> b
snd ([(Alignment, Blocks)] -> [Blocks])
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> ParsecT Sources ParserState (Reader T2TMeta) [Blocks]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Alignment, Blocks)]
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option [(Alignment, Blocks)]
forall a. Monoid a => a
mempty (ParsecT Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
headerRow)
  [[(Alignment, Blocks)]]
rows <- ParsecT Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> ParsecT
     Sources ParserState (Reader T2TMeta) [[(Alignment, Blocks)]]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (T2T Inlines
-> ParsecT Sources ParserState (Reader T2TMeta) [Inlines]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many T2T Inlines
commentLine ParsecT Sources ParserState (Reader T2TMeta) [Inlines]
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
tableRow)
  let columns :: [[(Alignment, Blocks)]]
columns = [[(Alignment, Blocks)]] -> [[(Alignment, Blocks)]]
forall a. [[a]] -> [[a]]
transpose [[(Alignment, Blocks)]]
rows
  let ncolumns :: Column
ncolumns = [[(Alignment, Blocks)]] -> Column
forall a. [a] -> Column
forall (t :: * -> *) a. Foldable t => t a -> Column
length [[(Alignment, Blocks)]]
columns
  let aligns :: [Alignment]
aligns = ([(Alignment, Blocks)] -> Alignment)
-> [[(Alignment, Blocks)]] -> [Alignment]
forall a b. (a -> b) -> [a] -> [b]
map (Alignment -> Maybe Alignment -> Alignment
forall a. a -> Maybe a -> a
fromMaybe Alignment
AlignDefault (Maybe Alignment -> Alignment)
-> ([(Alignment, Blocks)] -> Maybe Alignment)
-> [(Alignment, Blocks)]
-> Alignment
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Alignment, Blocks) -> Maybe Alignment -> Maybe Alignment)
-> Maybe Alignment -> [(Alignment, Blocks)] -> Maybe Alignment
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Alignment, Blocks) -> Maybe Alignment -> Maybe Alignment
forall a. (Alignment, a) -> Maybe Alignment -> Maybe Alignment
findAlign Maybe Alignment
forall a. Maybe a
Nothing) [[(Alignment, Blocks)]]
columns
  let rows' :: [[Blocks]]
rows' = ([(Alignment, Blocks)] -> [Blocks])
-> [[(Alignment, Blocks)]] -> [[Blocks]]
forall a b. (a -> b) -> [a] -> [b]
map (((Alignment, Blocks) -> Blocks)
-> [(Alignment, Blocks)] -> [Blocks]
forall a b. (a -> b) -> [a] -> [b]
map (Alignment, Blocks) -> Blocks
forall a b. (a, b) -> b
snd) [[(Alignment, Blocks)]]
rows
  let size :: Column
size = Column
-> (NonEmpty Column -> Column) -> Maybe (NonEmpty Column) -> Column
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Column
0 NonEmpty Column -> Column
forall a. Ord a => NonEmpty a -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (Maybe (NonEmpty Column) -> Column)
-> Maybe (NonEmpty Column) -> Column
forall a b. (a -> b) -> a -> b
$ [Column] -> Maybe (NonEmpty Column)
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty ([Column] -> Maybe (NonEmpty Column))
-> [Column] -> Maybe (NonEmpty Column)
forall a b. (a -> b) -> a -> b
$ ([Blocks] -> Column) -> [[Blocks]] -> [Column]
forall a b. (a -> b) -> [a] -> [b]
map [Blocks] -> Column
forall a. [a] -> Column
forall (t :: * -> *) a. Foldable t => t a -> Column
length [[Blocks]]
rows'
  let rowsPadded :: [[Blocks]]
rowsPadded = ([Blocks] -> [Blocks]) -> [[Blocks]] -> [[Blocks]]
forall a b. (a -> b) -> [a] -> [b]
map (Column -> [Blocks] -> [Blocks]
forall a. Monoid a => Column -> [a] -> [a]
pad Column
size) [[Blocks]]
rows'
  let headerPadded :: [Blocks]
headerPadded = if [Blocks] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Blocks]
tableHeader then [Blocks]
forall a. Monoid a => a
mempty else Column -> [Blocks] -> [Blocks]
forall a. Monoid a => Column -> [a] -> [a]
pad Column
size [Blocks]
tableHeader
  let toRow :: [Blocks] -> Row
toRow = Attr -> [Cell] -> Row
Row Attr
nullAttr ([Cell] -> Row) -> ([Blocks] -> [Cell]) -> [Blocks] -> Row
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Blocks -> Cell) -> [Blocks] -> [Cell]
forall a b. (a -> b) -> [a] -> [b]
map Blocks -> Cell
B.simpleCell
      toHeaderRow :: [Blocks] -> [Row]
toHeaderRow [Blocks]
l = [[Blocks] -> Row
toRow [Blocks]
l | Bool -> Bool
not ([Blocks] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Blocks]
l)]
  Blocks -> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Blocks -> ParsecT Sources ParserState (Reader T2TMeta) Blocks)
-> Blocks -> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a b. (a -> b) -> a -> b
$ Caption
-> [ColSpec] -> TableHead -> [TableBody] -> TableFoot -> Blocks
B.table Caption
B.emptyCaption
                    ([Alignment] -> [ColWidth] -> [ColSpec]
forall a b. [a] -> [b] -> [(a, b)]
zip [Alignment]
aligns (Column -> ColWidth -> [ColWidth]
forall a. Column -> a -> [a]
replicate Column
ncolumns ColWidth
ColWidthDefault))
                      (Attr -> [Row] -> TableHead
TableHead Attr
nullAttr ([Row] -> TableHead) -> [Row] -> TableHead
forall a b. (a -> b) -> a -> b
$ [Blocks] -> [Row]
toHeaderRow [Blocks]
headerPadded)
                      [Attr -> RowHeadColumns -> [Row] -> [Row] -> TableBody
TableBody Attr
nullAttr RowHeadColumns
0 [] ([Row] -> TableBody) -> [Row] -> TableBody
forall a b. (a -> b) -> a -> b
$ ([Blocks] -> Row) -> [[Blocks]] -> [Row]
forall a b. (a -> b) -> [a] -> [b]
map [Blocks] -> Row
toRow [[Blocks]]
rowsPadded]
                      (Attr -> [Row] -> TableFoot
TableFoot Attr
nullAttr [])

pad :: (Monoid a) => Int -> [a] -> [a]
pad :: forall a. Monoid a => Column -> [a] -> [a]
pad Column
n [a]
xs = [a]
xs [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Column -> a -> [a]
forall a. Column -> a -> [a]
replicate (Column
n Column -> Column -> Column
forall a. Num a => a -> a -> a
- [a] -> Column
forall a. [a] -> Column
forall (t :: * -> *) a. Foldable t => t a -> Column
length [a]
xs) a
forall a. Monoid a => a
mempty


findAlign :: (Alignment, a) -> Maybe Alignment -> Maybe Alignment
findAlign :: forall a. (Alignment, a) -> Maybe Alignment -> Maybe Alignment
findAlign (Alignment
x,a
_) (Just Alignment
y)
  | Alignment
x Alignment -> Alignment -> Bool
forall a. Eq a => a -> a -> Bool
== Alignment
y = Alignment -> Maybe Alignment
forall a. a -> Maybe a
Just Alignment
x
  | Bool
otherwise = Alignment -> Maybe Alignment
forall a. a -> Maybe a
Just Alignment
AlignDefault
findAlign (Alignment
x,a
_) Maybe Alignment
Nothing = Alignment -> Maybe Alignment
forall a. a -> Maybe a
Just Alignment
x

headerRow :: T2T [(Alignment, Blocks)]
headerRow :: ParsecT Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
headerRow = ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
forall a.
T2T a
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
genericRow ([Char] -> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m [Char]
string [Char]
"||")

tableRow :: T2T [(Alignment, Blocks)]
tableRow :: ParsecT Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
tableRow = ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
forall a.
T2T a
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
genericRow (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'|')

genericRow :: T2T a -> T2T [(Alignment, Blocks)]
genericRow :: forall a.
T2T a
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
genericRow T2T a
start = ParsecT Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
 -> ParsecT
      Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)])
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
forall a b. (a -> b) -> a -> b
$ do
  ParsecT Sources ParserState (Reader T2TMeta) Text
spaces ParsecT Sources ParserState (Reader T2TMeta) Text -> T2T a -> T2T a
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> T2T a
start
  ParsecT Sources ParserState (Reader T2TMeta) (Alignment, Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill ParsecT Sources ParserState (Reader T2TMeta) (Alignment, Blocks)
tableCell ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
newline ParsecT Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> [Char]
-> ParsecT
     Sources ParserState (Reader T2TMeta) [(Alignment, Blocks)]
forall s u (m :: * -> *) a.
ParsecT s u m a -> [Char] -> ParsecT s u m a
<?> [Char]
"genericRow"


tableCell :: T2T (Alignment, Blocks)
tableCell :: ParsecT Sources ParserState (Reader T2TMeta) (Alignment, Blocks)
tableCell = ParsecT Sources ParserState (Reader T2TMeta) (Alignment, Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) (Alignment, Blocks)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) (Alignment, Blocks)
 -> ParsecT
      Sources ParserState (Reader T2TMeta) (Alignment, Blocks))
-> ParsecT Sources ParserState (Reader T2TMeta) (Alignment, Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) (Alignment, Blocks)
forall a b. (a -> b) -> a -> b
$ do
  Column
leftSpaces <- [Char] -> Column
forall a. [a] -> Column
forall (t :: * -> *) a. Foldable t => t a -> Column
length ([Char] -> Column)
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) Column
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead (ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT Sources ParserState (Reader T2TMeta) Char
space) -- Case of empty cell means we must lookAhead
  [Inlines]
content <- T2T Inlines
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) [Inlines]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill T2T Inlines
inline (ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) ()
 -> ParsecT Sources ParserState (Reader T2TMeta) ())
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a b. (a -> b) -> a -> b
$ ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead ParsecT Sources ParserState (Reader T2TMeta) ()
cellEnd)
  Column
rightSpaces <- [Char] -> Column
forall a. [a] -> Column
forall (t :: * -> *) a. Foldable t => t a -> Column
length ([Char] -> Column)
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) Column
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many ParsecT Sources ParserState (Reader T2TMeta) Char
space
  let align :: Alignment
align =
        case Column -> Column -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Column
leftSpaces Column
rightSpaces of
              Ordering
LT -> Alignment
AlignLeft
              Ordering
EQ -> Alignment
AlignCenter
              Ordering
GT -> Alignment
AlignRight
  ParsecT Sources ParserState (Reader T2TMeta) ()
endOfCell
  (Alignment, Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) (Alignment, Blocks)
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Alignment
align, Inlines -> Blocks
B.plain (Inlines -> Inlines
B.trimInlines (Inlines -> Inlines) -> Inlines -> Inlines
forall a b. (a -> b) -> a -> b
$ [Inlines] -> Inlines
forall a. Monoid a => [a] -> a
mconcat [Inlines]
content))
  where
    cellEnd :: ParsecT Sources ParserState (Reader T2TMeta) ()
cellEnd = ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
newline ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> (ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT Sources ParserState (Reader T2TMeta) Char
space ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Sources ParserState (Reader T2TMeta) ()
endOfCell)

endOfCell :: T2T ()
endOfCell :: ParsecT Sources ParserState (Reader T2TMeta) ()
endOfCell = ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
skipMany1 (ParsecT Sources ParserState (Reader T2TMeta) Char
 -> ParsecT Sources ParserState (Reader T2TMeta) ())
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a b. (a -> b) -> a -> b
$ Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'|') ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ( () ()
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a b.
a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
newline)

-- Raw area

verbatim :: T2T Blocks
verbatim :: ParsecT Sources ParserState (Reader T2TMeta) Blocks
verbatim = ParsecT Sources ParserState (Reader T2TMeta) Text
-> (Text -> Blocks)
-> Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a.
Monoid a =>
T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
genericBlock ParsecT Sources ParserState (Reader T2TMeta) Text
forall (m :: * -> *) st. Monad m => ParsecT Sources st m Text
anyLineNewline Text -> Blocks
B.codeBlock Text
"```"

rawBlock :: T2T Blocks
rawBlock :: ParsecT Sources ParserState (Reader T2TMeta) Blocks
rawBlock = ParsecT Sources ParserState (Reader T2TMeta) Text
-> (Text -> Blocks)
-> Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a.
Monoid a =>
T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
genericBlock ParsecT Sources ParserState (Reader T2TMeta) Text
forall (m :: * -> *) st. Monad m => ParsecT Sources st m Text
anyLineNewline (Inlines -> Blocks
B.para (Inlines -> Blocks) -> (Text -> Inlines) -> Text -> Blocks
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Inlines
B.str) Text
"\"\"\""

taggedBlock :: T2T Blocks
taggedBlock :: ParsecT Sources ParserState (Reader T2TMeta) Blocks
taggedBlock = do
  Text
target <- ParsecT Sources ParserState (Reader T2TMeta) Text
getTarget
  ParsecT Sources ParserState (Reader T2TMeta) Text
-> (Text -> Blocks)
-> Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a.
Monoid a =>
T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
genericBlock ParsecT Sources ParserState (Reader T2TMeta) Text
forall (m :: * -> *) st. Monad m => ParsecT Sources st m Text
anyLineNewline (Text -> Text -> Blocks
B.rawBlock Text
target) Text
"'''"

-- Generic

genericBlock :: Monoid a => T2T a -> (a -> Blocks) -> Text -> T2T Blocks
genericBlock :: forall a.
Monoid a =>
T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
genericBlock T2T a
p a -> Blocks
f Text
s = T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a.
Monoid a =>
T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
blockMarkupArea T2T a
p a -> Blocks
f Text
s ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall a.
T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
blockMarkupLine T2T a
p a -> Blocks
f Text
s

blockMarkupArea :: Monoid a => T2T a -> (a -> Blocks) -> Text -> T2T Blocks
blockMarkupArea :: forall a.
Monoid a =>
T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
blockMarkupArea T2T a
p a -> Blocks
f Text
s = ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (do
  Text -> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) u.
(Stream s m Char, UpdateSourcePos s Char) =>
Text -> ParsecT s u m Text
textStr Text
s ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
blankline
  a -> Blocks
f (a -> Blocks) -> ([a] -> a) -> [a] -> Blocks
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> a
forall a. Monoid a => [a] -> a
mconcat ([a] -> Blocks)
-> ParsecT Sources ParserState (Reader T2TMeta) [a]
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> T2T a
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) [a]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill T2T a
p (ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m ()
eof ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (Text -> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) u.
(Stream s m Char, UpdateSourcePos s Char) =>
Text -> ParsecT s u m Text
textStr Text
s ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
blankline)))

blockMarkupLine :: T2T a -> (a -> Blocks) -> Text -> T2T Blocks
blockMarkupLine :: forall a.
T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
blockMarkupLine T2T a
p a -> Blocks
f Text
s = ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (a -> Blocks
f (a -> Blocks)
-> T2T a -> ParsecT Sources ParserState (Reader T2TMeta) Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) u.
(Stream s m Char, UpdateSourcePos s Char) =>
Text -> ParsecT s u m Text
textStr Text
s ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Sources ParserState (Reader T2TMeta) Char
space ParsecT Sources ParserState (Reader T2TMeta) Char -> T2T a -> T2T a
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> T2T a
p))

-- Can be in either block or inline position
comment :: Monoid a => T2T a
comment :: forall a. Monoid a => T2T a
comment = ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) a
 -> ParsecT Sources ParserState (Reader T2TMeta) a)
-> ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall a b. (a -> b) -> a -> b
$ do
  ParsecT Sources ParserState (Reader T2TMeta) ()
atStart
  T2T Inlines -> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy T2T Inlines
macro
  a
forall a. Monoid a => a
mempty a
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall a b.
a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'%' ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Sources ParserState (Reader T2TMeta) Text
forall (m :: * -> *) st. Monad m => ParsecT Sources st m Text
anyLine)

-- Inline

parseInlines :: T2T Inlines
parseInlines :: T2T Inlines
parseInlines = Inlines -> Inlines
trimInlines (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 Sources ParserState (Reader T2TMeta) [Inlines]
-> T2T Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> T2T Inlines
-> ParsecT Sources ParserState (Reader T2TMeta) [Inlines]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 T2T Inlines
inline

inline :: T2T Inlines
inline :: T2T Inlines
inline =
  [T2T Inlines] -> T2T Inlines
forall s (m :: * -> *) t u a.
Stream s m t =>
[ParsecT s u m a] -> ParsecT s u m a
choice
    [ T2T Inlines
endline
    , T2T Inlines
macro
    , T2T Inlines
commentLine
    , T2T Inlines
whitespace
    , T2T Inlines
url
    , T2T Inlines
link
    , T2T Inlines
image
    , T2T Inlines
bold
    , T2T Inlines
underline
    , T2T Inlines
code
    , T2T Inlines
raw
    , T2T Inlines
tagged
    , T2T Inlines
strike
    , T2T Inlines
italic
    , T2T Inlines
code
    , T2T Inlines
str
    , T2T Inlines
symbol
    ]

bold :: T2T Inlines
bold :: T2T Inlines
bold = T2T Inlines
-> (Inlines -> Inlines) -> Char -> (Text -> Inlines) -> T2T Inlines
forall a.
Monoid a =>
T2T a -> (a -> Inlines) -> Char -> (Text -> a) -> T2T Inlines
inlineMarkup T2T Inlines
inline Inlines -> Inlines
B.strong Char
'*' Text -> Inlines
B.str

underline :: T2T Inlines
underline :: T2T Inlines
underline = T2T Inlines
-> (Inlines -> Inlines) -> Char -> (Text -> Inlines) -> T2T Inlines
forall a.
Monoid a =>
T2T a -> (a -> Inlines) -> Char -> (Text -> a) -> T2T Inlines
inlineMarkup T2T Inlines
inline Inlines -> Inlines
B.underline Char
'_' Text -> Inlines
B.str

strike :: T2T Inlines
strike :: T2T Inlines
strike = T2T Inlines
-> (Inlines -> Inlines) -> Char -> (Text -> Inlines) -> T2T Inlines
forall a.
Monoid a =>
T2T a -> (a -> Inlines) -> Char -> (Text -> a) -> T2T Inlines
inlineMarkup T2T Inlines
inline Inlines -> Inlines
B.strikeout Char
'-' Text -> Inlines
B.str

italic :: T2T Inlines
italic :: T2T Inlines
italic = T2T Inlines
-> (Inlines -> Inlines) -> Char -> (Text -> Inlines) -> T2T Inlines
forall a.
Monoid a =>
T2T a -> (a -> Inlines) -> Char -> (Text -> a) -> T2T Inlines
inlineMarkup T2T Inlines
inline Inlines -> Inlines
B.emph Char
'/' Text -> Inlines
B.str

code :: T2T Inlines
code :: T2T Inlines
code = ParsecT Sources ParserState (Reader T2TMeta) Text
-> (Text -> Inlines) -> Char -> (Text -> Text) -> T2T Inlines
forall a.
Monoid a =>
T2T a -> (a -> Inlines) -> Char -> (Text -> a) -> T2T Inlines
inlineMarkup (Char -> Text
T.singleton (Char -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
anyChar) Text -> Inlines
B.code Char
'`' Text -> Text
forall a. a -> a
id

raw :: T2T Inlines
raw :: T2T Inlines
raw = ParsecT Sources ParserState (Reader T2TMeta) Text
-> (Text -> Inlines) -> Char -> (Text -> Text) -> T2T Inlines
forall a.
Monoid a =>
T2T a -> (a -> Inlines) -> Char -> (Text -> a) -> T2T Inlines
inlineMarkup (Char -> Text
T.singleton (Char -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
anyChar) Text -> Inlines
B.text Char
'"' Text -> Text
forall a. a -> a
id

tagged :: T2T Inlines
tagged :: T2T Inlines
tagged = do
  Text
target <- ParsecT Sources ParserState (Reader T2TMeta) Text
getTarget
  ParsecT Sources ParserState (Reader T2TMeta) Text
-> (Text -> Inlines) -> Char -> (Text -> Text) -> T2T Inlines
forall a.
Monoid a =>
T2T a -> (a -> Inlines) -> Char -> (Text -> a) -> T2T Inlines
inlineMarkup (Char -> Text
T.singleton (Char -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
anyChar) (Text -> Text -> Inlines
B.rawInline Text
target) Char
'\'' Text -> Text
forall a. a -> a
id

-- Parsec for markup indicated by a double character.
-- Inline markup is greedy and glued
-- Greedy meaning ***a*** = Bold [Str "*a*"]
-- Glued meaning that markup must be tight to content
-- Markup can't pass newlines
inlineMarkup :: Monoid a
             => T2T a -- Content parser
             -> (a -> Inlines) -- Constructor
             -> Char -- Fence
             -> (Text -> a) -- Special Case to handle ******
             -> T2T Inlines
inlineMarkup :: forall a.
Monoid a =>
T2T a -> (a -> Inlines) -> Char -> (Text -> a) -> T2T Inlines
inlineMarkup T2T a
p a -> Inlines
f Char
c Text -> a
special = T2T Inlines -> T2T Inlines
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (T2T Inlines -> T2T Inlines) -> T2T Inlines -> T2T Inlines
forall a b. (a -> b) -> a -> b
$ do
  Text
start <- ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m Text
many1Char (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
c)
  let l :: Column
l = Text -> Column
T.length Text
start
  Bool -> ParsecT Sources ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Column
l Column -> Column -> Bool
forall a. Ord a => a -> a -> Bool
>= Column
2)
  Bool
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Column
l Column -> Column -> Bool
forall a. Eq a => a -> a -> Bool
== Column
2) (ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT Sources ParserState (Reader T2TMeta) ()
 -> ParsecT Sources ParserState (Reader T2TMeta) ())
-> ParsecT Sources ParserState (Reader T2TMeta) ()
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a b. (a -> b) -> a -> b
$ ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT Sources ParserState (Reader T2TMeta) Char
space)
  -- We must make sure that there is no space before the start of the
  -- closing tags
  Maybe Text
body <-  ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) (Maybe Text)
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m (Maybe a)
optionMaybe (ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) Text
 -> ParsecT Sources ParserState (Reader T2TMeta) Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b. (a -> b) -> a -> b
$ ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st a.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m a -> ParsecT s st m Text
manyTillChar ([Char] -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
noneOf [Char]
"\n\r")
                (ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) [Char]
 -> ParsecT Sources ParserState (Reader T2TMeta) [Char])
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall a b. (a -> b) -> a -> b
$ ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead ([Char] -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
noneOf [Char]
" " ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [Char] -> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m [Char]
string [Char
c,Char
c] )))
  case Maybe Text
body of
    Just Text
middle -> do
      Char
lastChar <- ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
anyChar
      Text
end <- ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m Text
many1Char (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
c)
      let parser :: Text -> T2T a
parser Text
inp = T2T a -> Text -> T2T a
forall (m :: * -> *) u a.
(Monad m, HasLastStrPosition u) =>
ParsecT Sources u m a -> Text -> ParsecT Sources u m a
parseFromString' ([a] -> a
forall a. Monoid a => [a] -> a
mconcat ([a] -> a)
-> ParsecT Sources ParserState (Reader T2TMeta) [a] -> T2T a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> T2T a -> ParsecT Sources ParserState (Reader T2TMeta) [a]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many T2T a
p) Text
inp
      let start' :: a
start' = case Column -> Text -> Text
T.drop Column
2 Text
start of
                          Text
"" -> a
forall a. Monoid a => a
mempty
                          Text
xs -> Text -> a
special Text
xs
      a
body' <- Text -> T2T a
parser (Text
middle Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Char -> Text
T.singleton Char
lastChar)
      let end' :: a
end' = case Column -> Text -> Text
T.drop Column
2 Text
end of
                          Text
"" -> a
forall a. Monoid a => a
mempty
                          Text
xs -> Text -> a
special Text
xs
      Inlines -> T2T Inlines
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Inlines -> T2T Inlines) -> Inlines -> T2T Inlines
forall a b. (a -> b) -> a -> b
$ a -> Inlines
f (a
start' a -> a -> a
forall a. Monoid a => a -> a -> a
`mappend` a
body' a -> a -> a
forall a. Monoid a => a -> a -> a
`mappend` a
end')
    Maybe Text
Nothing -> do -- Either bad or case such as *****
      Bool -> ParsecT Sources ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Column
l Column -> Column -> Bool
forall a. Ord a => a -> a -> Bool
>= Column
5)
      let body' :: Text
body' = Column -> Text -> Text
T.replicate (Column
l Column -> Column -> Column
forall a. Num a => a -> a -> a
- Column
4) (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Char -> Text
T.singleton Char
c
      Inlines -> T2T Inlines
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Inlines -> T2T Inlines) -> Inlines -> T2T Inlines
forall a b. (a -> b) -> a -> b
$ a -> Inlines
f (Text -> a
special Text
body')

link :: T2T Inlines
link :: T2T Inlines
link = T2T Inlines -> T2T Inlines
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try T2T Inlines
imageLink T2T Inlines -> T2T Inlines -> T2T Inlines
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> T2T Inlines
titleLink

-- Link with title
titleLink :: T2T Inlines
titleLink :: T2T Inlines
titleLink = T2T Inlines -> T2T Inlines
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (T2T Inlines -> T2T Inlines) -> T2T Inlines -> T2T Inlines
forall a b. (a -> b) -> a -> b
$ do
  Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'['
  ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT Sources ParserState (Reader T2TMeta) Char
space
  [Text]
tokens <- ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Text]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
sepBy1 (ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m Text
manyChar (ParsecT Sources ParserState (Reader T2TMeta) Char
 -> ParsecT Sources ParserState (Reader T2TMeta) Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b. (a -> b) -> a -> b
$ [Char] -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
noneOf [Char]
" ]") ParsecT Sources ParserState (Reader T2TMeta) Char
space
  Bool -> ParsecT Sources ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard ([Text] -> Column
forall a. [a] -> Column
forall (t :: * -> *) a. Foldable t => t a -> Column
length [Text]
tokens Column -> Column -> Bool
forall a. Ord a => a -> a -> Bool
>= Column
2)
  Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
']'
  let link' :: Text
link' = [Text] -> Text
forall a. HasCallStack => [a] -> a
last [Text]
tokens
  Bool -> ParsecT Sources ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> ParsecT Sources ParserState (Reader T2TMeta) ())
-> Bool -> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Bool
T.null Text
link'
  let tit :: Text
tit = [Text] -> Text
T.unwords ([Text] -> [Text]
forall a. HasCallStack => [a] -> [a]
init [Text]
tokens)
  Inlines -> T2T Inlines
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Inlines -> T2T Inlines) -> Inlines -> T2T Inlines
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Inlines -> Inlines
B.link Text
link' Text
"" (Text -> Inlines
B.text Text
tit)

-- Link with image
imageLink :: T2T Inlines
imageLink :: T2T Inlines
imageLink = T2T Inlines -> T2T Inlines
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (T2T Inlines -> T2T Inlines) -> T2T Inlines -> T2T Inlines
forall a b. (a -> b) -> a -> b
$ do
  Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'['
  Inlines
body <- T2T Inlines
image
  ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT Sources ParserState (Reader T2TMeta) Char
space
  Text
l <- ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st a.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m a -> ParsecT s st m Text
manyTillChar ([Char] -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
noneOf [Char]
"\n\r ") (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
']')
  Inlines -> T2T Inlines
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> Text -> Inlines -> Inlines
B.link Text
l Text
"" Inlines
body)

macro :: T2T Inlines
macro :: T2T Inlines
macro = T2T Inlines -> T2T Inlines
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (T2T Inlines -> T2T Inlines) -> T2T Inlines -> T2T Inlines
forall a b. (a -> b) -> a -> b
$ do
  Text
name <- [Char] -> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m [Char]
string [Char]
"%%" ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> [Text] -> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
[Text] -> ParsecT s st m Text
oneOfStringsCI (((Text, T2TMeta -> Text) -> Text)
-> [(Text, T2TMeta -> Text)] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text, T2TMeta -> Text) -> Text
forall a b. (a, b) -> a
fst [(Text, T2TMeta -> Text)]
commands)
  ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional (ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) [Char]
 -> ParsecT Sources ParserState (Reader T2TMeta) [Char])
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall a b. (a -> b) -> a -> b
$ ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall end s (m :: * -> *) st t a.
(Show end, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m t
-> ParsecT s st m end -> ParsecT s st m a -> ParsecT s st m [a]
enclosed (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'(') (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
')') ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
anyChar)
  ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead (ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
spaceChar ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> [Char] -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
oneOf [Char]
specialChars ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Char
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
newline)
  T2T Inlines
-> ((T2TMeta -> Text) -> T2T Inlines)
-> Maybe (T2TMeta -> Text)
-> T2T Inlines
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Inlines -> T2T Inlines
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return Inlines
forall a. Monoid a => a
mempty) (\T2TMeta -> Text
f -> (T2TMeta -> Inlines) -> T2T Inlines
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (Text -> Inlines
B.str (Text -> Inlines) -> (T2TMeta -> Text) -> T2TMeta -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. T2TMeta -> Text
f)) (Text -> [(Text, T2TMeta -> Text)] -> Maybe (T2TMeta -> Text)
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
name [(Text, T2TMeta -> Text)]
commands)
  where
    commands :: [(Text, T2TMeta -> Text)]
commands = [ (Text
"date", T2TMeta -> Text
date), (Text
"mtime", T2TMeta -> Text
mtime)
               , (Text
"infile", [Char] -> Text
T.pack ([Char] -> Text) -> (T2TMeta -> [Char]) -> T2TMeta -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. T2TMeta -> [Char]
infile), (Text
"outfile", [Char] -> Text
T.pack ([Char] -> Text) -> (T2TMeta -> [Char]) -> T2TMeta -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. T2TMeta -> [Char]
outfile)]

-- raw URLs in text are automatically linked
url :: T2T Inlines
url :: T2T Inlines
url = T2T Inlines -> T2T Inlines
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (T2T Inlines -> T2T Inlines) -> T2T Inlines -> T2T Inlines
forall a b. (a -> b) -> a -> b
$ do
  (Text
rawUrl, Text
escapedUrl) <- ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
-> ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
uri ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
-> ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
-> ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
emailAddress'
  Inlines -> T2T Inlines
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Inlines -> T2T Inlines) -> Inlines -> T2T Inlines
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Inlines -> Inlines
B.link Text
rawUrl Text
"" (Text -> Inlines
B.str Text
escapedUrl)

emailAddress' :: T2T (Text, Text)
emailAddress' :: ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
emailAddress' = do
  (Text
base, Text
mailURI) <- ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m (Text, Text)
emailAddress
  Text
query <- Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Text
"" ParsecT Sources ParserState (Reader T2TMeta) Text
emailQuery
  (Text, Text)
-> ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
base Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
query, Text
mailURI Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
query)

emailQuery :: T2T Text
emailQuery :: ParsecT Sources ParserState (Reader T2TMeta) Text
emailQuery = do
  Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'?'
  [Text]
parts <- ParsecT Sources ParserState (Reader T2TMeta) Text
kv ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Text]
forall s (m :: * -> *) t u a end.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
`sepBy1` (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'&')
  Text -> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> ParsecT Sources ParserState (Reader T2TMeta) Text)
-> Text -> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b. (a -> b) -> a -> b
$ Text
"?" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
T.intercalate Text
"&" [Text]
parts

kv :: T2T Text
kv :: ParsecT Sources ParserState (Reader T2TMeta) Text
kv = do
  Text
k <- [Char] -> Text
T.pack ([Char] -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
alphaNum
  Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'='
  let vchar :: ParsecT Sources u (Reader T2TMeta) Char
vchar = ParsecT Sources u (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
alphaNum ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Char
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Char
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ([Char] -> ParsecT Sources u (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
oneOf [Char]
"%._/~:,=$@&+-;*" ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Char
forall a b.
ParsecT Sources u (Reader T2TMeta) a
-> ParsecT Sources u (Reader T2TMeta) b
-> ParsecT Sources u (Reader T2TMeta) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Char
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead ParsecT Sources u (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
alphaNum)
  Text
v <- [Char] -> Text
T.pack ([Char] -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) [Char]
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT Sources ParserState (Reader T2TMeta) Char
forall {u}. ParsecT Sources u (Reader T2TMeta) Char
vchar
  Text -> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
k Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"=" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
v)

uri :: T2T (Text, Text)
uri :: ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
uri = ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
-> ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
 -> ParsecT Sources ParserState (Reader T2TMeta) (Text, Text))
-> ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
-> ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
forall a b. (a -> b) -> a -> b
$ do
  Text
address <- ParsecT Sources ParserState (Reader T2TMeta) Text
t2tURI
  (Text, Text)
-> ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
address, Text -> Text
escapeURI Text
address)

-- The definition of a URI in the T2T source differs from the
-- actual definition. This is a transcription of the definition in
-- the source of v2.6
--isT2TURI :: String -> Bool
--isT2TURI (parse t2tURI "" -> Right _) = True
--isT2TURI _ = False

t2tURI :: T2T Text
t2tURI :: ParsecT Sources ParserState (Reader T2TMeta) Text
t2tURI = do
  Text
start <- ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
(<>) (Text -> Text -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Text
forall {st}. ParsecT Sources st (Reader T2TMeta) Text
proto ParsecT Sources ParserState (Reader T2TMeta) (Text -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) (a -> b)
-> ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Sources ParserState (Reader T2TMeta) Text
forall {st}. ParsecT Sources st (Reader T2TMeta) Text
urlLogin) ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT Sources ParserState (Reader T2TMeta) Text
forall {st}. ParsecT Sources st (Reader T2TMeta) Text
guess
  Text
domain <- ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m Text
many1Char ParsecT Sources ParserState (Reader T2TMeta) Char
forall {u}. ParsecT Sources u (Reader T2TMeta) Char
chars
  Text
sep <- ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m Text
manyChar (Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'/')
  Text
form' <- Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Text
forall a. Monoid a => a
mempty (Char -> Text -> Text
T.cons (Char -> Text -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'?' ParsecT Sources ParserState (Reader T2TMeta) (Text -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) (a -> b)
-> ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m Text
many1Char ParsecT Sources ParserState (Reader T2TMeta) Char
forall {u}. ParsecT Sources u (Reader T2TMeta) Char
form)
  Text
anchor' <- Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Text
forall a. Monoid a => a
mempty (Char -> Text -> Text
T.cons (Char -> Text -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'#' ParsecT Sources ParserState (Reader T2TMeta) (Text -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) (a -> b)
-> ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m Text
manyChar ParsecT Sources ParserState (Reader T2TMeta) Char
forall {u}. ParsecT Sources u (Reader T2TMeta) Char
anchor)
  Text -> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
start Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
domain Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
sep Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
form' Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
anchor')
  where
    protos :: [Text]
protos = [Text
"http", Text
"https", Text
"ftp", Text
"telnet", Text
"gopher", Text
"wais"]
    proto :: ParsecT Sources st (Reader T2TMeta) Text
proto = Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
(<>) (Text -> Text -> Text)
-> ParsecT Sources st (Reader T2TMeta) Text
-> ParsecT Sources st (Reader T2TMeta) (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text] -> ParsecT Sources st (Reader T2TMeta) Text
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
[Text] -> ParsecT s st m Text
oneOfStrings [Text]
protos ParsecT Sources st (Reader T2TMeta) (Text -> Text)
-> ParsecT Sources st (Reader T2TMeta) Text
-> ParsecT Sources st (Reader T2TMeta) Text
forall a b.
ParsecT Sources st (Reader T2TMeta) (a -> b)
-> ParsecT Sources st (Reader T2TMeta) a
-> ParsecT Sources st (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> ParsecT Sources st (Reader T2TMeta) Text
forall s (m :: * -> *) u.
(Stream s m Char, UpdateSourcePos s Char) =>
Text -> ParsecT s u m Text
textStr Text
"://"
    guess :: ParsecT Sources u (Reader T2TMeta) Text
guess = Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
(<>) (Text -> Text -> Text)
-> ParsecT Sources u (Reader T2TMeta) Text
-> ParsecT Sources u (Reader T2TMeta) (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
(<>) (Text -> Text -> Text)
-> ParsecT Sources u (Reader T2TMeta) Text
-> ParsecT Sources u (Reader T2TMeta) (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> ParsecT Sources u (Reader T2TMeta) Text
forall s (m :: * -> *) u.
(Stream s m Char, UpdateSourcePos s Char) =>
Text -> ParsecT s u m Text
stringAnyCase Text
"www" ParsecT Sources u (Reader T2TMeta) (Text -> Text)
-> ParsecT Sources u (Reader T2TMeta) Text
-> ParsecT Sources u (Reader T2TMeta) Text
forall a b.
ParsecT Sources u (Reader T2TMeta) (a -> b)
-> ParsecT Sources u (Reader T2TMeta) a
-> ParsecT Sources u (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> ParsecT Sources u (Reader T2TMeta) Text
-> ParsecT Sources u (Reader T2TMeta) Text
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Text
forall a. Monoid a => a
mempty (Char -> Text
T.singleton (Char -> Text)
-> ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> ParsecT Sources u (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
oneOf [Char]
"23"))
              ParsecT Sources u (Reader T2TMeta) Text
-> ParsecT Sources u (Reader T2TMeta) Text
-> ParsecT Sources u (Reader T2TMeta) Text
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> Text -> ParsecT Sources u (Reader T2TMeta) Text
forall s (m :: * -> *) u.
(Stream s m Char, UpdateSourcePos s Char) =>
Text -> ParsecT s u m Text
stringAnyCase Text
"ftp") ParsecT Sources u (Reader T2TMeta) (Text -> Text)
-> ParsecT Sources u (Reader T2TMeta) Text
-> ParsecT Sources u (Reader T2TMeta) Text
forall a b.
ParsecT Sources u (Reader T2TMeta) (a -> b)
-> ParsecT Sources u (Reader T2TMeta) a
-> ParsecT Sources u (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Char -> Text
T.singleton (Char -> Text)
-> ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Char -> ParsecT Sources u (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'.')
    login :: ParsecT Sources u (Reader T2TMeta) Char
login = ParsecT Sources u (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
alphaNum ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Char
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> [Char] -> ParsecT Sources u (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
oneOf [Char]
"_.-"
    pass :: ParsecT Sources st (Reader T2TMeta) Text
pass = ParsecT Sources st (Reader T2TMeta) Char
-> ParsecT Sources st (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m Text
manyChar ([Char] -> ParsecT Sources st (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
noneOf [Char]
" @")
    chars :: ParsecT Sources u (Reader T2TMeta) Char
chars = ParsecT Sources u (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
alphaNum ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Char
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> [Char] -> ParsecT Sources u (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
oneOf [Char]
"%._/~:,=$@&+-"
    anchor :: ParsecT Sources u (Reader T2TMeta) Char
anchor = ParsecT Sources u (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
alphaNum ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Char
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> [Char] -> ParsecT Sources u (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
oneOf [Char]
"%._0"
    form :: ParsecT Sources u (Reader T2TMeta) Char
form = ParsecT Sources u (Reader T2TMeta) Char
forall {u}. ParsecT Sources u (Reader T2TMeta) Char
chars ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Char
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> [Char] -> ParsecT Sources u (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
oneOf [Char]
";*"
    urlLogin :: ParsecT Sources u (Reader T2TMeta) Text
urlLogin = Text
-> ParsecT Sources u (Reader T2TMeta) Text
-> ParsecT Sources u (Reader T2TMeta) Text
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Text
forall a. Monoid a => a
mempty (ParsecT Sources u (Reader T2TMeta) Text
 -> ParsecT Sources u (Reader T2TMeta) Text)
-> ParsecT Sources u (Reader T2TMeta) Text
-> ParsecT Sources u (Reader T2TMeta) Text
forall a b. (a -> b) -> a -> b
$ ParsecT Sources u (Reader T2TMeta) Text
-> ParsecT Sources u (Reader T2TMeta) Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ((\Text
x Text
y Char
z -> Text
x Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
y Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Char -> Text
T.singleton Char
z) (Text -> Text -> Char -> Text)
-> ParsecT Sources u (Reader T2TMeta) Text
-> ParsecT Sources u (Reader T2TMeta) (Text -> Char -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m Text
many1Char ParsecT Sources u (Reader T2TMeta) Char
forall {u}. ParsecT Sources u (Reader T2TMeta) Char
login ParsecT Sources u (Reader T2TMeta) (Text -> Char -> Text)
-> ParsecT Sources u (Reader T2TMeta) Text
-> ParsecT Sources u (Reader T2TMeta) (Char -> Text)
forall a b.
ParsecT Sources u (Reader T2TMeta) (a -> b)
-> ParsecT Sources u (Reader T2TMeta) a
-> ParsecT Sources u (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> ParsecT Sources u (Reader T2TMeta) Text
-> ParsecT Sources u (Reader T2TMeta) Text
forall s (m :: * -> *) t a u.
Stream s m t =>
a -> ParsecT s u m a -> ParsecT s u m a
option Text
forall a. Monoid a => a
mempty (Char -> Text -> Text
T.cons (Char -> Text -> Text)
-> ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Char -> ParsecT Sources u (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
':' ParsecT Sources u (Reader T2TMeta) (Text -> Text)
-> ParsecT Sources u (Reader T2TMeta) Text
-> ParsecT Sources u (Reader T2TMeta) Text
forall a b.
ParsecT Sources u (Reader T2TMeta) (a -> b)
-> ParsecT Sources u (Reader T2TMeta) a
-> ParsecT Sources u (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Sources u (Reader T2TMeta) Text
forall {st}. ParsecT Sources st (Reader T2TMeta) Text
pass) ParsecT Sources u (Reader T2TMeta) (Char -> Text)
-> ParsecT Sources u (Reader T2TMeta) Char
-> ParsecT Sources u (Reader T2TMeta) Text
forall a b.
ParsecT Sources u (Reader T2TMeta) (a -> b)
-> ParsecT Sources u (Reader T2TMeta) a
-> ParsecT Sources u (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Char -> ParsecT Sources u (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'@')


image :: T2T Inlines
image :: T2T Inlines
image =  T2T Inlines -> T2T Inlines
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (T2T Inlines -> T2T Inlines) -> T2T Inlines -> T2T Inlines
forall a b. (a -> b) -> a -> b
$ do
  -- List taken from txt2tags source
  let extensions :: [Text]
extensions = [Text
".jpg", Text
".jpeg", Text
".gif", Text
".png", Text
".eps", Text
".bmp"]
  Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'['
  (Text
path, Text
ext) <- ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) (Text, Text)
forall s u (m :: * -> *) b.
ParsecT s u m Char -> ParsecT s u m b -> ParsecT s u m (Text, b)
manyUntilChar ([Char] -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
noneOf [Char]
"\n\t\r ") ([Text] -> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
[Text] -> ParsecT s st m Text
oneOfStrings [Text]
extensions)
  Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
']'
  Inlines -> T2T Inlines
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Inlines -> T2T Inlines) -> Inlines -> T2T Inlines
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Inlines -> Inlines
B.image (Text
path Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
ext) Text
"" Inlines
forall a. Monoid a => a
mempty

-- Characters used in markup
specialChars :: [Char]
specialChars :: [Char]
specialChars = [Char]
"%*-_/|:+;"

tab :: T2T Char
tab :: ParsecT Sources ParserState (Reader T2TMeta) Char
tab = Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
'\t'

space :: T2T Char
space :: ParsecT Sources ParserState (Reader T2TMeta) Char
space = Char -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
Char -> ParsecT s u m Char
char Char
' '

spaces :: T2T Text
spaces :: ParsecT Sources ParserState (Reader T2TMeta) Text
spaces = ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m Text
manyChar ParsecT Sources ParserState (Reader T2TMeta) Char
space

endline :: T2T Inlines
endline :: T2T Inlines
endline = T2T Inlines -> T2T Inlines
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (T2T Inlines -> T2T Inlines) -> T2T Inlines -> T2T Inlines
forall a b. (a -> b) -> a -> b
$ do
  ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s u m Char
newline
  ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
blankline
  ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT Sources ParserState (Reader T2TMeta) Blocks
hrule
  ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT Sources ParserState (Reader T2TMeta) Blocks
title
  ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT Sources ParserState (Reader T2TMeta) Blocks
verbatim
  ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT Sources ParserState (Reader T2TMeta) Blocks
rawBlock
  ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT Sources ParserState (Reader T2TMeta) Blocks
taggedBlock
  ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT Sources ParserState (Reader T2TMeta) Blocks
quote
  ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT Sources ParserState (Reader T2TMeta) Blocks
list
  ParsecT Sources ParserState (Reader T2TMeta) Blocks
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t a u.
(Stream s m t, Show a) =>
ParsecT s u m a -> ParsecT s u m ()
notFollowedBy ParsecT Sources ParserState (Reader T2TMeta) Blocks
table
  Inlines -> T2T Inlines
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return Inlines
B.softbreak

str :: T2T Inlines
str :: T2T Inlines
str = T2T Inlines -> T2T Inlines
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (T2T Inlines -> T2T Inlines) -> T2T Inlines -> T2T Inlines
forall a b. (a -> b) -> a -> b
$ Text -> Inlines
B.str (Text -> Inlines)
-> ParsecT Sources ParserState (Reader T2TMeta) Text -> T2T Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) Char
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParsecT s st m Char -> ParsecT s st m Text
many1Char ([Char] -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
noneOf ([Char] -> ParsecT Sources ParserState (Reader T2TMeta) Char)
-> [Char] -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall a b. (a -> b) -> a -> b
$ [Char]
specialChars [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"\n\r ")

whitespace :: T2T Inlines
whitespace :: T2T Inlines
whitespace = T2T Inlines -> T2T Inlines
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (T2T Inlines -> T2T Inlines) -> T2T Inlines -> T2T Inlines
forall a b. (a -> b) -> a -> b
$ Inlines
B.space Inlines
-> ParsecT Sources ParserState (Reader T2TMeta) Char -> T2T Inlines
forall a b.
a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Sources ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
spaceChar

symbol :: T2T Inlines
symbol :: T2T Inlines
symbol = Text -> Inlines
B.str (Text -> Inlines) -> (Char -> Text) -> Char -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Text
T.singleton (Char -> Inlines)
-> ParsecT Sources ParserState (Reader T2TMeta) Char -> T2T Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> ParsecT Sources ParserState (Reader T2TMeta) Char
forall (m :: * -> *) s u.
(Monad m, Stream s m Char, UpdateSourcePos s Char) =>
[Char] -> ParsecT s u m Char
oneOf [Char]
specialChars

-- Utility

getTarget :: T2T Text
getTarget :: ParsecT Sources ParserState (Reader T2TMeta) Text
getTarget = do
  Maybe MetaValue
mv <- Text -> Meta -> Maybe MetaValue
lookupMeta Text
"target" (Meta -> Maybe MetaValue)
-> (ParserState -> Meta) -> ParserState -> Maybe MetaValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParserState -> Meta
stateMeta (ParserState -> Maybe MetaValue)
-> ParsecT Sources ParserState (Reader T2TMeta) ParserState
-> ParsecT Sources ParserState (Reader T2TMeta) (Maybe MetaValue)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Sources ParserState (Reader T2TMeta) ParserState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  Text -> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a. a -> ParsecT Sources ParserState (Reader T2TMeta) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> ParsecT Sources ParserState (Reader T2TMeta) Text)
-> Text -> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b. (a -> b) -> a -> b
$ case Maybe MetaValue
mv of
              Just (MetaString Text
target)        -> Text
target
              Just (MetaInlines [Str Text
target]) -> Text
target
              Maybe MetaValue
_                               -> Text
"html"

atStart :: T2T ()
atStart :: ParsecT Sources ParserState (Reader T2TMeta) ()
atStart = ParsecT Sources ParserState (Reader T2TMeta) SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition ParsecT Sources ParserState (Reader T2TMeta) SourcePos
-> (SourcePos -> ParsecT Sources ParserState (Reader T2TMeta) ())
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> (a -> ParsecT Sources ParserState (Reader T2TMeta) b)
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> ParsecT Sources ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> ParsecT Sources ParserState (Reader T2TMeta) ())
-> (SourcePos -> Bool)
-> SourcePos
-> ParsecT Sources ParserState (Reader T2TMeta) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Column -> Column -> Bool
forall a. Eq a => a -> a -> Bool
== Column
1) (Column -> Bool) -> (SourcePos -> Column) -> SourcePos -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourcePos -> Column
sourceColumn

ignoreSpacesCap :: T2T Text -> T2T Text
ignoreSpacesCap :: ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
ignoreSpacesCap ParsecT Sources ParserState (Reader T2TMeta) Text
p = Text -> Text
T.toLower (Text -> Text)
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ParsecT Sources ParserState (Reader T2TMeta) Text
spaces ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Sources ParserState (Reader T2TMeta) Text
p ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
-> ParsecT Sources ParserState (Reader T2TMeta) Text
forall a b.
ParsecT Sources ParserState (Reader T2TMeta) a
-> ParsecT Sources ParserState (Reader T2TMeta) b
-> ParsecT Sources ParserState (Reader T2TMeta) a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Sources ParserState (Reader T2TMeta) Text
spaces)