{-# LANGUAGE OverloadedStrings #-}
{- |
   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.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, crFilter, escapeURI)

type T2T = ParserT Text 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 -> FilePath
infile  :: FilePath -- ^ Input file
               , T2TMeta -> FilePath
outfile :: FilePath -- ^ Output file
               } deriving Int -> T2TMeta -> ShowS
[T2TMeta] -> ShowS
T2TMeta -> FilePath
(Int -> T2TMeta -> ShowS)
-> (T2TMeta -> FilePath) -> ([T2TMeta] -> ShowS) -> Show T2TMeta
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [T2TMeta] -> ShowS
$cshowList :: [T2TMeta] -> ShowS
show :: T2TMeta -> FilePath
$cshow :: T2TMeta -> FilePath
showsPrec :: Int -> T2TMeta -> ShowS
$cshowsPrec :: Int -> T2TMeta -> ShowS
Show

instance Default T2TMeta where
    def :: T2TMeta
def = Text -> Text -> FilePath -> FilePath -> T2TMeta
T2TMeta Text
"" Text
"" FilePath
"" FilePath
""

-- | Get the meta information required by Txt2Tags macros
getT2TMeta :: PandocMonad m => m T2TMeta
getT2TMeta :: m T2TMeta
getT2TMeta = do
    [FilePath]
inps <- m [FilePath]
forall (m :: * -> *). PandocMonad m => m [FilePath]
P.getInputFiles
    FilePath
outp <- FilePath -> Maybe FilePath -> FilePath
forall a. a -> Maybe a -> a
fromMaybe FilePath
"" (Maybe FilePath -> FilePath) -> m (Maybe FilePath) -> m FilePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (Maybe FilePath)
forall (m :: * -> *). PandocMonad m => m (Maybe FilePath)
P.getOutputFile
    FilePath
curDate <- TimeLocale -> FilePath -> ZonedTime -> FilePath
forall t. FormatTime t => TimeLocale -> FilePath -> t -> FilePath
formatTime TimeLocale
defaultTimeLocale FilePath
"%F" (ZonedTime -> FilePath) -> m ZonedTime -> m FilePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m ZonedTime
forall (m :: * -> *). PandocMonad m => m ZonedTime
P.getZonedTime
    let getModTime :: FilePath -> m FilePath
getModTime = (UTCTime -> FilePath) -> m UTCTime -> m FilePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (TimeLocale -> FilePath -> UTCTime -> FilePath
forall t. FormatTime t => TimeLocale -> FilePath -> t -> FilePath
formatTime TimeLocale
defaultTimeLocale FilePath
"%T") (m UTCTime -> m FilePath)
-> (FilePath -> m UTCTime) -> FilePath -> m FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                       FilePath -> m UTCTime
forall (m :: * -> *). PandocMonad m => FilePath -> m UTCTime
P.getModificationTime
    FilePath
curMtime <- case [FilePath]
inps of
                  [] -> TimeLocale -> FilePath -> ZonedTime -> FilePath
forall t. FormatTime t => TimeLocale -> FilePath -> t -> FilePath
formatTime TimeLocale
defaultTimeLocale FilePath
"%T" (ZonedTime -> FilePath) -> m ZonedTime -> m FilePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m ZonedTime
forall (m :: * -> *). PandocMonad m => m ZonedTime
P.getZonedTime
                  [FilePath]
_ -> m FilePath -> (PandocError -> m FilePath) -> m FilePath
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
catchError
                        ([FilePath] -> FilePath
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum ([FilePath] -> FilePath) -> m [FilePath] -> m FilePath
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (FilePath -> m FilePath) -> [FilePath] -> m [FilePath]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM FilePath -> m FilePath
getModTime [FilePath]
inps)
                        (m FilePath -> PandocError -> m FilePath
forall a b. a -> b -> a
const (FilePath -> m FilePath
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
""))
    T2TMeta -> m T2TMeta
forall (m :: * -> *) a. Monad m => a -> m a
return (T2TMeta -> m T2TMeta) -> T2TMeta -> m T2TMeta
forall a b. (a -> b) -> a -> b
$ Text -> Text -> FilePath -> FilePath -> T2TMeta
T2TMeta (FilePath -> Text
T.pack FilePath
curDate) (FilePath -> Text
T.pack FilePath
curMtime) (FilePath -> [FilePath] -> FilePath
forall a. [a] -> [[a]] -> [a]
intercalate FilePath
", " [FilePath]
inps) FilePath
outp

-- | Read Txt2Tags from an input string returning a Pandoc document
readTxt2Tags :: PandocMonad m
             => ReaderOptions
             -> Text
             -> m Pandoc
readTxt2Tags :: ReaderOptions -> Text -> m Pandoc
readTxt2Tags ReaderOptions
opts Text
s = do
  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
$
        ParserT Text ParserState (Reader T2TMeta) Pandoc
-> ParserState
-> Text
-> Reader T2TMeta (Either PandocError Pandoc)
forall s (m :: * -> *) st a.
(Stream s m Char, ToText s) =>
ParserT s st m a -> st -> s -> m (Either PandocError a)
readWithM ParserT Text ParserState (Reader T2TMeta) Pandoc
parseT2T (ParserState
forall a. Default a => a
def {stateOptions :: ReaderOptions
stateOptions = ReaderOptions
opts}) (Text -> Reader T2TMeta (Either PandocError Pandoc))
-> Text -> Reader T2TMeta (Either PandocError Pandoc)
forall a b. (a -> b) -> a -> b
$
        Text -> Text
crFilter Text
s Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n\n"
  case Either PandocError Pandoc
parsed of
    Right Pandoc
result -> Pandoc -> m Pandoc
forall (m :: * -> *) a. Monad m => a -> m a
return Pandoc
result
    Left PandocError
e       -> PandocError -> m Pandoc
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 :: ParserT Text ParserState (Reader T2TMeta) Pandoc
parseT2T = do
  -- Parse header if standalone flag is set
  Bool
standalone <- (ReaderOptions -> Bool)
-> ParserT Text ParserState (Reader T2TMeta) Bool
forall st s (m :: * -> *) t b.
(HasReaderOptions st, Stream s m t) =>
(ReaderOptions -> b) -> ParserT s st m b
getOption ReaderOptions -> Bool
readerStandalone
  Bool
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
standalone ParsecT Text ParserState (Reader T2TMeta) ()
parseHeader
  Blocks
body <- [Blocks] -> Blocks
forall a. Monoid a => [a] -> a
mconcat ([Blocks] -> Blocks)
-> ParsecT Text ParserState (Reader T2TMeta) [Blocks]
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>  ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Blocks
block ParsecT Text 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 Text ParserState (Reader T2TMeta) ParserState
-> ParsecT Text ParserState (Reader T2TMeta) Meta
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) ParserState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  Pandoc -> ParserT Text ParserState (Reader T2TMeta) Pandoc
forall (m :: * -> *) a. Monad m => a -> m a
return (Pandoc -> ParserT Text ParserState (Reader T2TMeta) Pandoc)
-> Pandoc -> ParserT Text 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 Text ParserState (Reader T2TMeta) ()
parseHeader = do
  () ()
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Char
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
blankline ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT Text ParserState (Reader T2TMeta) ()
header
  Meta
meta <- ParserState -> Meta
stateMeta (ParserState -> Meta)
-> ParsecT Text ParserState (Reader T2TMeta) ParserState
-> ParsecT Text ParserState (Reader T2TMeta) Meta
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) ParserState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Text
blanklines
  [(Text, Text)]
config <- ParsecT Text ParserState (Reader T2TMeta) (Text, Text)
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) (Text, Text)
setting (ParsecT Text ParserState (Reader T2TMeta) (Text, Text)
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) (Text, Text)
setting)
  -- TODO: Handle settings better
  let settings :: Meta
settings = ((Text, Text) -> Meta -> Meta) -> Meta -> [(Text, Text)] -> Meta
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
B.setMeta Text
k (Text -> MetaValue
MetaString Text
v)) Meta
meta [(Text, Text)]
config
  (ParserState -> ParserState)
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Text
blanklines

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

headerline :: B.ToMetaValue a => Text -> T2T a -> T2T ()
headerline :: Text -> T2T a -> ParsecT Text ParserState (Reader T2TMeta) ()
headerline Text
field T2T a
p = (() ()
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Char
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
blankline)
                        ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) ())
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (ParserState -> ParserState)
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState ((ParserState -> ParserState)
 -> ParsecT Text ParserState (Reader T2TMeta) ())
-> (a -> ParserState -> ParserState)
-> a
-> ParsecT Text 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
B.setMeta Text
field)

titleline :: T2T ()
titleline :: ParsecT Text ParserState (Reader T2TMeta) ()
titleline =
  Text -> T2T Inlines -> ParsecT Text ParserState (Reader T2TMeta) ()
forall a.
ToMetaValue a =>
Text -> T2T a -> ParsecT Text 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 Text ParserState (Reader T2TMeta) [Inlines]
-> T2T Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> T2T Inlines
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
newline)

authorline :: T2T ()
authorline :: ParsecT Text ParserState (Reader T2TMeta) ()
authorline =
  Text
-> ParsecT Text ParserState (Reader T2TMeta) [Inlines]
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall a.
ToMetaValue a =>
Text -> T2T a -> ParsecT Text ParserState (Reader T2TMeta) ()
headerline Text
"author" (T2T Inlines
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
';') ParsecT Text ParserState (Reader T2TMeta) [Inlines]
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) [Inlines]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st 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 Text ParserState (Reader T2TMeta) [Inlines]
-> T2T Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> T2T Inlines -> ParsecT Text ParserState (Reader T2TMeta) [Inlines]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
';' ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
newline) ParsecT Text ParserState (Reader T2TMeta) ()
-> T2T Inlines -> T2T Inlines
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> T2T Inlines
inline)

dateline :: T2T ()
dateline :: ParsecT Text ParserState (Reader T2TMeta) ()
dateline = Text -> T2T Inlines -> ParsecT Text ParserState (Reader T2TMeta) ()
forall a.
ToMetaValue a =>
Text -> T2T a -> ParsecT Text 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 Text ParserState (Reader T2TMeta) [Inlines]
-> T2T Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> T2T Inlines
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
newline)

type Keyword = Text
type Value = Text

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

-- Blocks

parseBlocks :: T2T Blocks
parseBlocks :: ParsecT Text ParserState (Reader T2TMeta) Blocks
parseBlocks = [Blocks] -> Blocks
forall a. Monoid a => [a] -> a
mconcat ([Blocks] -> Blocks)
-> ParsecT Text ParserState (Reader T2TMeta) [Blocks]
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Blocks
block ParsecT Text ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m ()
eof

block :: T2T Blocks
block :: ParsecT Text ParserState (Reader T2TMeta) Blocks
block =
  [ParsecT Text ParserState (Reader T2TMeta) Blocks]
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Text
blanklines
    , ParsecT Text ParserState (Reader T2TMeta) Blocks
quote
    , ParsecT Text ParserState (Reader T2TMeta) Blocks
hrule -- hrule must go above title
    , ParsecT Text ParserState (Reader T2TMeta) Blocks
title
    , ParsecT Text ParserState (Reader T2TMeta) Blocks
commentBlock
    , ParsecT Text ParserState (Reader T2TMeta) Blocks
verbatim
    , ParsecT Text ParserState (Reader T2TMeta) Blocks
rawBlock
    , ParsecT Text ParserState (Reader T2TMeta) Blocks
taggedBlock
    , ParsecT Text ParserState (Reader T2TMeta) Blocks
list
    , ParsecT Text ParserState (Reader T2TMeta) Blocks
table
    , ParsecT Text ParserState (Reader T2TMeta) Blocks
para
    ]

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

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

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

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

quote :: T2T Blocks
quote :: ParsecT Text ParserState (Reader T2TMeta) Blocks
quote = ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text ParserState (Reader T2TMeta) Blocks
 -> ParsecT Text ParserState (Reader T2TMeta) Blocks)
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall a b. (a -> b) -> a -> b
$ do
  ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
tab
  [Text]
rawQuote <-  ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
tab ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional ParsecT Text ParserState (Reader T2TMeta) Text
spaces ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Text ParserState (Reader T2TMeta) Text
forall (m :: * -> *) st. Monad m => ParserT Text st m Text
anyLine)
  Blocks
contents <- ParsecT Text ParserState (Reader T2TMeta) Blocks
-> Text -> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall s (m :: * -> *) u a.
(Stream s m Char, IsString s, HasLastStrPosition u) =>
ParserT s u m a -> Text -> ParserT s u m a
parseFromString' ParsecT Text 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 Text ParserState (Reader T2TMeta) Blocks
forall (m :: * -> *) a. Monad m => a -> m a
return (Blocks -> ParsecT Text ParserState (Reader T2TMeta) Blocks)
-> Blocks -> ParsecT Text 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 Text ParserState (Reader T2TMeta) Blocks
list = [ParsecT Text ParserState (Reader T2TMeta) Blocks]
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Blocks
bulletList, ParsecT Text ParserState (Reader T2TMeta) Blocks
orderedList, ParsecT Text ParserState (Reader T2TMeta) Blocks
definitionList]

bulletList :: T2T Blocks
bulletList :: ParsecT Text 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 Text ParserState (Reader T2TMeta) [Blocks]
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Int
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall a.
ParsecT Text ParserState (Reader T2TMeta) Int -> T2T a -> T2T a
listItem ParsecT Text ParserState (Reader T2TMeta) Int
bulletListStart ParsecT Text ParserState (Reader T2TMeta) Blocks
parseBlocks)

orderedList :: T2T Blocks
orderedList :: ParsecT Text 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 Text ParserState (Reader T2TMeta) [Blocks]
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Int
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall a.
ParsecT Text ParserState (Reader T2TMeta) Int -> T2T a -> T2T a
listItem ParsecT Text ParserState (Reader T2TMeta) Int
orderedListStart ParsecT Text ParserState (Reader T2TMeta) Blocks
parseBlocks)

definitionList :: T2T Blocks
definitionList :: ParsecT Text ParserState (Reader T2TMeta) Blocks
definitionList = ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text ParserState (Reader T2TMeta) Blocks
 -> ParsecT Text ParserState (Reader T2TMeta) Blocks)
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) [(Inlines, [Blocks])]
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
    ParsecT Text ParserState (Reader T2TMeta) (Inlines, [Blocks])
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Int
-> ParsecT Text ParserState (Reader T2TMeta) (Inlines, [Blocks])
-> ParsecT Text ParserState (Reader T2TMeta) (Inlines, [Blocks])
forall a.
ParsecT Text ParserState (Reader T2TMeta) Int -> T2T a -> T2T a
listItem ParsecT Text ParserState (Reader T2TMeta) Int
definitionListStart ParsecT Text ParserState (Reader T2TMeta) (Inlines, [Blocks])
definitionListEnd)

definitionListEnd :: T2T (Inlines, [Blocks])
definitionListEnd :: ParsecT Text ParserState (Reader T2TMeta) (Inlines, [Blocks])
definitionListEnd = (,) (Inlines -> [Blocks] -> (Inlines, [Blocks]))
-> T2T Inlines
-> ParsecT
     Text 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 Text ParserState (Reader T2TMeta) [Inlines]
-> T2T Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> T2T Inlines
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
newline) ParsecT
  Text ParserState (Reader T2TMeta) ([Blocks] -> (Inlines, [Blocks]))
-> ParsecT Text ParserState (Reader T2TMeta) [Blocks]
-> ParsecT Text ParserState (Reader T2TMeta) (Inlines, [Blocks])
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ((Blocks -> [Blocks] -> [Blocks]
forall a. a -> [a] -> [a]
:[]) (Blocks -> [Blocks])
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text ParserState (Reader T2TMeta) [Blocks]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) Blocks
parseBlocks)

genericListStart :: T2T Char
                 -> T2T Int
genericListStart :: ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Int
genericListStart ParsecT Text ParserState (Reader T2TMeta) Char
listMarker = ParsecT Text ParserState (Reader T2TMeta) Int
-> ParsecT Text ParserState (Reader T2TMeta) Int
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text ParserState (Reader T2TMeta) Int
 -> ParsecT Text ParserState (Reader T2TMeta) Int)
-> ParsecT Text ParserState (Reader T2TMeta) Int
-> ParsecT Text ParserState (Reader T2TMeta) Int
forall a b. (a -> b) -> a -> b
$
  (Int
2Int -> Int -> Int
forall a. Num a => a -> a -> a
+) (Int -> Int)
-> ParsecT Text ParserState (Reader T2TMeta) Int
-> ParsecT Text ParserState (Reader T2TMeta) Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (FilePath -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (FilePath -> Int)
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
-> ParsecT Text ParserState (Reader T2TMeta) Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
spaceChar
            ParsecT Text ParserState (Reader T2TMeta) Int
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Int
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Text ParserState (Reader T2TMeta) Char
listMarker ParsecT Text ParserState (Reader T2TMeta) Int
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Int
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Text ParserState (Reader T2TMeta) Char
space ParsecT Text ParserState (Reader T2TMeta) Int
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) Int
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
space)

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

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

definitionListStart :: T2T Int
definitionListStart :: ParsecT Text ParserState (Reader T2TMeta) Int
definitionListStart = ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Int
genericListStart (Char -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m 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 :: ParsecT Text ParserState (Reader T2TMeta) Int -> T2T a -> T2T a
listItem ParsecT Text ParserState (Reader T2TMeta) Int
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
  Int
markerLength <- ParsecT Text ParserState (Reader T2TMeta) Int
-> ParsecT Text ParserState (Reader T2TMeta) Int
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT Text ParserState (Reader T2TMeta) Int
start
  Text
firstLine <- ParsecT Text ParserState (Reader T2TMeta) Text
forall (m :: * -> *) st. Monad m => ParserT Text st m Text
anyLineNewline
  Text
blank <- Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
blankline)
  Text
rest <- [Text] -> Text
T.concat ([Text] -> Text)
-> ParsecT Text ParserState (Reader T2TMeta) [Text]
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) [Text]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many (Int -> ParsecT Text ParserState (Reader T2TMeta) Text
listContinuation Int
markerLength)
  T2T a -> Text -> T2T a
forall s (m :: * -> *) u a.
(Stream s m Char, IsString s, HasLastStrPosition u) =>
ParserT s u m a -> Text -> ParserT s 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 :: Int -> ParsecT Text ParserState (Reader T2TMeta) Text
listContinuation Int
markerLength = ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text ParserState (Reader T2TMeta) Text
 -> ParsecT Text ParserState (Reader T2TMeta) Text)
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall a b. (a -> b) -> a -> b
$
  ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall b s (m :: * -> *) a st.
(Show b, Stream s m a) =>
ParserT s st m b -> ParserT s st m ()
notFollowedBy' (ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
blankline ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Char
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
blankline)
  ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
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 Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Text] -> Text
T.concat ([Text] -> Text)
-> ParsecT Text ParserState (Reader T2TMeta) [Text]
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Text
listLine)
              ParsecT Text ParserState (Reader T2TMeta) (Text -> Text)
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParserT s st m Char -> ParserT s st m Text
manyChar ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
blankline)
 where listLine :: ParsecT Text ParserState (Reader T2TMeta) Text
listLine = ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text ParserState (Reader T2TMeta) Text
 -> ParsecT Text ParserState (Reader T2TMeta) Text)
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall a b. (a -> b) -> a -> b
$ Int -> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) st.
(Stream s m Char, HasReaderOptions st) =>
Int -> ParserT s st m Text
indentWith Int
markerLength ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Text ParserState (Reader T2TMeta) Text
forall (m :: * -> *) st. Monad m => ParserT Text st m Text
anyLineNewline

-- Table

table :: T2T Blocks
table :: ParsecT Text ParserState (Reader T2TMeta) Blocks
table = ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text ParserState (Reader T2TMeta) Blocks
 -> ParsecT Text ParserState (Reader T2TMeta) Blocks)
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall a b. (a -> b) -> a -> b
$ do
  [Blocks]
tableHeader <- ((Alignment, Blocks) -> Blocks)
-> [(Alignment, Blocks)] -> [Blocks]
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 Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> ParsecT Text ParserState (Reader T2TMeta) [Blocks]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Alignment, Blocks)]
-> ParsecT Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> ParsecT Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
headerRow)
  [[(Alignment, Blocks)]]
rows <- ParsecT Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> ParsecT
     Text 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 Text ParserState (Reader T2TMeta) [Inlines]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many T2T Inlines
commentLine ParsecT Text ParserState (Reader T2TMeta) [Inlines]
-> ParsecT Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> ParsecT Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Text 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 :: Int
ncolumns = [[(Alignment, Blocks)]] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[(Alignment, Blocks)]]
columns
  let aligns :: [Alignment]
aligns = ([(Alignment, Blocks)] -> Alignment)
-> [[(Alignment, Blocks)]] -> [Alignment]
forall a b. (a -> b) -> [a] -> [b]
map ((Alignment -> Alignment -> Alignment) -> [Alignment] -> Alignment
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 Alignment -> Alignment -> Alignment
findAlign ([Alignment] -> Alignment)
-> ([(Alignment, Blocks)] -> [Alignment])
-> [(Alignment, Blocks)]
-> Alignment
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Alignment, Blocks) -> Alignment)
-> [(Alignment, Blocks)] -> [Alignment]
forall a b. (a -> b) -> [a] -> [b]
map (Alignment, Blocks) -> Alignment
forall a b. (a, b) -> a
fst) [[(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 :: Int
size = [Int] -> Int
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (([Blocks] -> Int) -> [[Blocks]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map [Blocks] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[Blocks]]
rows')
  let rowsPadded :: [[Blocks]]
rowsPadded = ([Blocks] -> [Blocks]) -> [[Blocks]] -> [[Blocks]]
forall a b. (a -> b) -> [a] -> [b]
map (Int -> [Blocks] -> [Blocks]
forall a. Monoid a => Int -> [a] -> [a]
pad Int
size) [[Blocks]]
rows'
  let headerPadded :: [Blocks]
headerPadded = if [Blocks] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Blocks]
tableHeader then [Blocks]
forall a. Monoid a => a
mempty else Int -> [Blocks] -> [Blocks]
forall a. Monoid a => Int -> [a] -> [a]
pad Int
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 (t :: * -> *) a. Foldable t => t a -> Bool
null [Blocks]
l)]
  Blocks -> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall (m :: * -> *) a. Monad m => a -> m a
return (Blocks -> ParsecT Text ParserState (Reader T2TMeta) Blocks)
-> Blocks -> ParsecT Text 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 (Int -> ColWidth -> [ColWidth]
forall a. Int -> a -> [a]
replicate Int
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 :: Int -> [a] -> [a]
pad Int
n [a]
xs = [a]
xs [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ Int -> a -> [a]
forall a. Int -> a -> [a]
replicate (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs) a
forall a. Monoid a => a
mempty


findAlign :: Alignment -> Alignment -> Alignment
findAlign :: Alignment -> Alignment -> Alignment
findAlign Alignment
x Alignment
y
  | Alignment
x Alignment -> Alignment -> Bool
forall a. Eq a => a -> a -> Bool
== Alignment
y = Alignment
x
  | Bool
otherwise = Alignment
AlignDefault

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

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

genericRow :: T2T a -> T2T [(Alignment, Blocks)]
genericRow :: T2T a
-> ParsecT Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
genericRow T2T a
start = ParsecT Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> ParsecT Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
 -> ParsecT Text ParserState (Reader T2TMeta) [(Alignment, Blocks)])
-> ParsecT Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> ParsecT Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
forall a b. (a -> b) -> a -> b
$ do
  ParsecT Text ParserState (Reader T2TMeta) Text
spaces ParsecT Text ParserState (Reader T2TMeta) Text -> T2T a -> T2T a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> T2T a
start
  ParsecT Text ParserState (Reader T2TMeta) (Alignment, Blocks)
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) (Alignment, Blocks)
tableCell ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
newline ParsecT Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
-> FilePath
-> ParsecT Text ParserState (Reader T2TMeta) [(Alignment, Blocks)]
forall s u (m :: * -> *) a.
ParsecT s u m a -> FilePath -> ParsecT s u m a
<?> FilePath
"genericRow"


tableCell :: T2T (Alignment, Blocks)
tableCell :: ParsecT Text ParserState (Reader T2TMeta) (Alignment, Blocks)
tableCell = ParsecT Text ParserState (Reader T2TMeta) (Alignment, Blocks)
-> ParsecT Text ParserState (Reader T2TMeta) (Alignment, Blocks)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text ParserState (Reader T2TMeta) (Alignment, Blocks)
 -> ParsecT Text ParserState (Reader T2TMeta) (Alignment, Blocks))
-> ParsecT Text ParserState (Reader T2TMeta) (Alignment, Blocks)
-> ParsecT Text ParserState (Reader T2TMeta) (Alignment, Blocks)
forall a b. (a -> b) -> a -> b
$ do
  Int
leftSpaces <- FilePath -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (FilePath -> Int)
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
-> ParsecT Text ParserState (Reader T2TMeta) Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) FilePath
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead (ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT Text ParserState (Reader T2TMeta) Char
space) -- Case of empty cell means we must lookAhead
  [Inlines]
content <- T2T Inlines
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text ParserState (Reader T2TMeta) ()
 -> ParsecT Text ParserState (Reader T2TMeta) ())
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall a b. (a -> b) -> a -> b
$ ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) ()
cellEnd)
  Int
rightSpaces <- FilePath -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (FilePath -> Int)
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
-> ParsecT Text ParserState (Reader T2TMeta) Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many ParsecT Text ParserState (Reader T2TMeta) Char
space
  let align :: Alignment
align =
        case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
leftSpaces Int
rightSpaces of
              Ordering
LT -> Alignment
AlignLeft
              Ordering
EQ -> Alignment
AlignCenter
              Ordering
GT -> Alignment
AlignRight
  ParsecT Text ParserState (Reader T2TMeta) ()
endOfCell
  (Alignment, Blocks)
-> ParsecT Text ParserState (Reader T2TMeta) (Alignment, Blocks)
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 Text ParserState (Reader T2TMeta) ()
cellEnd = ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
newline ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> (ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT Text ParserState (Reader T2TMeta) Char
space ParsecT Text ParserState (Reader T2TMeta) FilePath
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Text ParserState (Reader T2TMeta) ()
endOfCell)

endOfCell :: T2T ()
endOfCell :: ParsecT Text ParserState (Reader T2TMeta) ()
endOfCell = ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
skipMany1 (ParsecT Text ParserState (Reader T2TMeta) Char
 -> ParsecT Text ParserState (Reader T2TMeta) ())
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall a b. (a -> b) -> a -> b
$ Char -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'|') ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ( () ()
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
newline)

-- Raw area

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

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

-- Generic

genericBlock :: Monoid a => T2T a -> (a -> Blocks) -> Text -> T2T Blocks
genericBlock :: T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
genericBlock T2T a
p a -> Blocks
f Text
s = T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
forall a.
Monoid a =>
T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
blockMarkupArea T2T a
p a -> Blocks
f Text
s ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Blocks
forall a.
T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
blockMarkupLine T2T a
p a -> Blocks
f Text
s

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

blockMarkupLine :: T2T a -> (a -> Blocks) -> Text -> T2T Blocks
blockMarkupLine :: T2T a
-> (a -> Blocks)
-> Text
-> ParsecT Text ParserState (Reader T2TMeta) Blocks
blockMarkupLine T2T a
p a -> Blocks
f Text
s = ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Blocks
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text -> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) u.
Stream s m Char =>
Text -> ParsecT s u m Text
textStr Text
s ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Char
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Text ParserState (Reader T2TMeta) Char
space ParsecT Text ParserState (Reader T2TMeta) Char -> T2T a -> T2T a
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 :: T2T a
comment = 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
  ParsecT Text ParserState (Reader T2TMeta) ()
atStart
  T2T Inlines -> ParsecT Text 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 Text ParserState (Reader T2TMeta) Text -> T2T a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ (Char -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'%' ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ParsecT Text ParserState (Reader T2TMeta) Text
forall (m :: * -> *) st. Monad m => ParserT Text 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 Text ParserState (Reader T2TMeta) [Inlines]
-> T2T Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> T2T Inlines -> ParsecT Text 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 Text 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 Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
anyChar) Text -> Inlines
B.code Char
'`' Text -> Text
forall a. a -> a
id

raw :: T2T Inlines
raw :: T2T Inlines
raw = ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st 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 Text ParserState (Reader T2TMeta) Text
getTarget
  ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
anyChar) (Text -> Text -> Inlines
B.rawInline Text
target) Char
'\'' Text -> Text
forall a. a -> a
id

-- Parser 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 :: 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 Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParserT s st m Char -> ParserT s st m Text
many1Char (Char -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
c)
  let l :: Int
l = Text -> Int
T.length Text
start
  Bool -> ParsecT Text ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
2)
  Bool
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
l Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2) (ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT Text ParserState (Reader T2TMeta) ()
 -> ParsecT Text ParserState (Reader T2TMeta) ())
-> ParsecT Text ParserState (Reader T2TMeta) ()
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall a b. (a -> b) -> a -> b
$ ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text 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 Text ParserState (Reader T2TMeta) Text
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text ParserState (Reader T2TMeta) Text
 -> ParsecT Text ParserState (Reader T2TMeta) Text)
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall a b. (a -> b) -> a -> b
$ ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st a.
Stream s m t =>
ParserT s st m Char -> ParserT s st m a -> ParserT s st m Text
manyTillChar (FilePath -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m Char
noneOf FilePath
"\n\r")
                (ParsecT Text ParserState (Reader T2TMeta) FilePath
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text ParserState (Reader T2TMeta) FilePath
 -> ParsecT Text ParserState (Reader T2TMeta) FilePath)
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
forall a b. (a -> b) -> a -> b
$ ParsecT Text ParserState (Reader T2TMeta) FilePath
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead (FilePath -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m Char
noneOf FilePath
" " ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> FilePath -> ParsecT Text ParserState (Reader T2TMeta) FilePath
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m FilePath
string [Char
c,Char
c] )))
  case Maybe Text
body of
    Just Text
middle -> do
      Char
lastChar <- ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
anyChar
      Text
end <- ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParserT s st m Char -> ParserT s st m Text
many1Char (Char -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
c)
      let parser :: Text -> T2T a
parser Text
inp = T2T a -> Text -> T2T a
forall s (m :: * -> *) u a.
(Stream s m Char, IsString s, HasLastStrPosition u) =>
ParserT s u m a -> Text -> ParserT s u m a
parseFromString' ([a] -> a
forall a. Monoid a => [a] -> a
mconcat ([a] -> a)
-> ParsecT Text ParserState (Reader T2TMeta) [a] -> T2T a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> T2T a -> ParsecT Text 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 Int -> Text -> Text
T.drop Int
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 Int -> Text -> Text
T.drop Int
2 Text
end of
                          Text
"" -> a
forall a. Monoid a => a
mempty
                          Text
xs -> Text -> a
special Text
xs
      Inlines -> T2T Inlines
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 Text ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
5)
      let body' :: Text
body' = Int -> Text -> Text
T.replicate (Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
4) (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Char -> Text
T.singleton Char
c
      Inlines -> T2T Inlines
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 Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'['
  ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
space
  [Text]
tokens <- ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParserT s st m Char -> ParserT s st m Text
manyChar (ParsecT Text ParserState (Reader T2TMeta) Char
 -> ParsecT Text ParserState (Reader T2TMeta) Text)
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall a b. (a -> b) -> a -> b
$ FilePath -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m Char
noneOf FilePath
" ]") ParsecT Text ParserState (Reader T2TMeta) Char
space
  Bool -> ParsecT Text ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard ([Text] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
tokens Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
2)
  Char -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
']'
  let link' :: Text
link' = [Text] -> Text
forall a. [a] -> a
last [Text]
tokens
  Bool -> ParsecT Text ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> ParsecT Text ParserState (Reader T2TMeta) ())
-> Bool -> ParsecT Text 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. [a] -> [a]
init [Text]
tokens)
  Inlines -> T2T Inlines
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 Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'['
  Inlines
body <- T2T Inlines
image
  ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 ParsecT Text ParserState (Reader T2TMeta) Char
space
  Text
l <- ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st a.
Stream s m t =>
ParserT s st m Char -> ParserT s st m a -> ParserT s st m Text
manyTillChar (FilePath -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m Char
noneOf FilePath
"\n\r ") (Char -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
']')
  Inlines -> T2T Inlines
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 <- FilePath -> ParsecT Text ParserState (Reader T2TMeta) FilePath
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m FilePath
string FilePath
"%%" ParsecT Text ParserState (Reader T2TMeta) FilePath
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> [Text] -> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) st.
Stream s m Char =>
[Text] -> ParserT 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 Text ParserState (Reader T2TMeta) FilePath
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
optional (ParsecT Text ParserState (Reader T2TMeta) FilePath
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text ParserState (Reader T2TMeta) FilePath
 -> ParsecT Text ParserState (Reader T2TMeta) FilePath)
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
forall a b. (a -> b) -> a -> b
$ ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) FilePath
forall end s (m :: * -> *) st t a.
(Show end, Stream s m Char) =>
ParserT s st m t
-> ParserT s st m end -> ParserT s st m a -> ParserT s st m [a]
enclosed (Char -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'(') (Char -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
')') ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
anyChar)
  ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
spaceChar ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Char
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> FilePath -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m Char
oneOf FilePath
specialChars ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st 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 (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", FilePath -> Text
T.pack (FilePath -> Text) -> (T2TMeta -> FilePath) -> T2TMeta -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. T2TMeta -> FilePath
infile), (Text
"outfile", FilePath -> Text
T.pack (FilePath -> Text) -> (T2TMeta -> FilePath) -> T2TMeta -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. T2TMeta -> FilePath
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 Text ParserState (Reader T2TMeta) (Text, Text)
-> ParsecT Text ParserState (Reader T2TMeta) (Text, Text)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT Text ParserState (Reader T2TMeta) (Text, Text)
uri ParsecT Text ParserState (Reader T2TMeta) (Text, Text)
-> ParsecT Text ParserState (Reader T2TMeta) (Text, Text)
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) (Text, Text)
forall s (m :: * -> *) st.
Stream s m Char =>
ParserT s st m (Text, Text)
emailAddress
  Inlines -> T2T Inlines
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)

uri :: T2T (Text, Text)
uri :: ParsecT Text ParserState (Reader T2TMeta) (Text, Text)
uri = ParsecT Text ParserState (Reader T2TMeta) (Text, Text)
-> ParsecT Text ParserState (Reader T2TMeta) (Text, Text)
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try (ParsecT Text ParserState (Reader T2TMeta) (Text, Text)
 -> ParsecT Text ParserState (Reader T2TMeta) (Text, Text))
-> ParsecT Text ParserState (Reader T2TMeta) (Text, Text)
-> ParsecT Text ParserState (Reader T2TMeta) (Text, Text)
forall a b. (a -> b) -> a -> b
$ do
  Text
address <- ParsecT Text ParserState (Reader T2TMeta) Text
t2tURI
  (Text, Text)
-> ParsecT Text ParserState (Reader T2TMeta) (Text, Text)
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 Text ParserState (Reader T2TMeta) Text
t2tURI = do
  Text
start <- ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) Text
forall st. ParsecT Text st (Reader T2TMeta) Text
proto ParsecT Text ParserState (Reader T2TMeta) (Text -> Text)
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Text ParserState (Reader T2TMeta) Text
forall st. ParsecT Text st (Reader T2TMeta) Text
urlLogin) ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Text
forall st. ParsecT Text st (Reader T2TMeta) Text
guess
  Text
domain <- ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParserT s st m Char -> ParserT s st m Text
many1Char ParsecT Text ParserState (Reader T2TMeta) Char
forall u. ParsecT Text u (Reader T2TMeta) Char
chars
  Text
sep <- ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParserT s st m Char -> ParserT s st m Text
manyChar (Char -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'/')
  Text
form' <- Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Char -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'?' ParsecT Text ParserState (Reader T2TMeta) (Text -> Text)
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParserT s st m Char -> ParserT s st m Text
many1Char ParsecT Text ParserState (Reader T2TMeta) Char
forall u. ParsecT Text u (Reader T2TMeta) Char
form)
  Text
anchor' <- Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Char -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'#' ParsecT Text ParserState (Reader T2TMeta) (Text -> Text)
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParserT s st m Char -> ParserT s st m Text
manyChar ParsecT Text ParserState (Reader T2TMeta) Char
forall u. ParsecT Text u (Reader T2TMeta) Char
anchor)
  Text -> ParsecT Text ParserState (Reader T2TMeta) Text
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 Text st (Reader T2TMeta) Text
proto = Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
(<>) (Text -> Text -> Text)
-> ParsecT Text st (Reader T2TMeta) Text
-> ParsecT Text st (Reader T2TMeta) (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Text] -> ParsecT Text st (Reader T2TMeta) Text
forall s (m :: * -> *) st.
Stream s m Char =>
[Text] -> ParserT s st m Text
oneOfStrings [Text]
protos ParsecT Text st (Reader T2TMeta) (Text -> Text)
-> ParsecT Text st (Reader T2TMeta) Text
-> ParsecT Text st (Reader T2TMeta) Text
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> ParsecT Text st (Reader T2TMeta) Text
forall s (m :: * -> *) u.
Stream s m Char =>
Text -> ParsecT s u m Text
textStr Text
"://"
    guess :: ParsecT Text u (Reader T2TMeta) Text
guess = Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
(<>) (Text -> Text -> Text)
-> ParsecT Text u (Reader T2TMeta) Text
-> ParsecT Text 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 Text u (Reader T2TMeta) Text
-> ParsecT Text u (Reader T2TMeta) (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> ParsecT Text u (Reader T2TMeta) Text
forall s (m :: * -> *) u.
Stream s m Char =>
Text -> ParsecT s u m Text
stringAnyCase Text
"www" ParsecT Text u (Reader T2TMeta) (Text -> Text)
-> ParsecT Text u (Reader T2TMeta) Text
-> ParsecT Text u (Reader T2TMeta) Text
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> ParsecT Text u (Reader T2TMeta) Text
-> ParsecT Text 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 Text u (Reader T2TMeta) Char
-> ParsecT Text u (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> ParsecT Text u (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m Char
oneOf FilePath
"23"))
              ParsecT Text u (Reader T2TMeta) Text
-> ParsecT Text u (Reader T2TMeta) Text
-> ParsecT Text 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 Text u (Reader T2TMeta) Text
forall s (m :: * -> *) u.
Stream s m Char =>
Text -> ParsecT s u m Text
stringAnyCase Text
"ftp") ParsecT Text u (Reader T2TMeta) (Text -> Text)
-> ParsecT Text u (Reader T2TMeta) Text
-> ParsecT Text u (Reader T2TMeta) Text
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Char -> Text
T.singleton (Char -> Text)
-> ParsecT Text u (Reader T2TMeta) Char
-> ParsecT Text u (Reader T2TMeta) Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Char -> ParsecT Text u (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'.')
    login :: ParsecT Text u (Reader T2TMeta) Char
login = ParsecT Text u (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
alphaNum ParsecT Text u (Reader T2TMeta) Char
-> ParsecT Text u (Reader T2TMeta) Char
-> ParsecT Text u (Reader T2TMeta) Char
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> FilePath -> ParsecT Text u (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m Char
oneOf FilePath
"_.-"
    pass :: ParserT Text st (Reader T2TMeta) Text
pass = ParserT Text st (Reader T2TMeta) Char
-> ParserT Text st (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParserT s st m Char -> ParserT s st m Text
manyChar (FilePath -> ParserT Text st (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m Char
noneOf FilePath
" @")
    chars :: ParsecT Text u (Reader T2TMeta) Char
chars = ParsecT Text u (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
alphaNum ParsecT Text u (Reader T2TMeta) Char
-> ParsecT Text u (Reader T2TMeta) Char
-> ParsecT Text u (Reader T2TMeta) Char
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> FilePath -> ParsecT Text u (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m Char
oneOf FilePath
"%._/~:,=$@&+-"
    anchor :: ParsecT Text u (Reader T2TMeta) Char
anchor = ParsecT Text u (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
alphaNum ParsecT Text u (Reader T2TMeta) Char
-> ParsecT Text u (Reader T2TMeta) Char
-> ParsecT Text u (Reader T2TMeta) Char
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> FilePath -> ParsecT Text u (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m Char
oneOf FilePath
"%._0"
    form :: ParsecT Text u (Reader T2TMeta) Char
form = ParsecT Text u (Reader T2TMeta) Char
forall u. ParsecT Text u (Reader T2TMeta) Char
chars ParsecT Text u (Reader T2TMeta) Char
-> ParsecT Text u (Reader T2TMeta) Char
-> ParsecT Text u (Reader T2TMeta) Char
forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> FilePath -> ParsecT Text u (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m Char
oneOf FilePath
";*"
    urlLogin :: ParsecT Text u (Reader T2TMeta) Text
urlLogin = Text
-> ParsecT Text u (Reader T2TMeta) Text
-> ParsecT Text 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 Text u (Reader T2TMeta) Text
 -> ParsecT Text u (Reader T2TMeta) Text)
-> ParsecT Text u (Reader T2TMeta) Text
-> ParsecT Text u (Reader T2TMeta) Text
forall a b. (a -> b) -> a -> b
$ ParsecT Text u (Reader T2TMeta) Text
-> ParsecT Text 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 Text u (Reader T2TMeta) Text
-> ParsecT Text u (Reader T2TMeta) (Text -> Char -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParserT Text u (Reader T2TMeta) Char
-> ParsecT Text u (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParserT s st m Char -> ParserT s st m Text
many1Char ParserT Text u (Reader T2TMeta) Char
forall u. ParsecT Text u (Reader T2TMeta) Char
login ParsecT Text u (Reader T2TMeta) (Text -> Char -> Text)
-> ParsecT Text u (Reader T2TMeta) Text
-> ParsecT Text u (Reader T2TMeta) (Char -> Text)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text
-> ParsecT Text u (Reader T2TMeta) Text
-> ParsecT Text 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)
-> ParserT Text u (Reader T2TMeta) Char
-> ParsecT Text u (Reader T2TMeta) (Text -> Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Char -> ParserT Text u (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
':' ParsecT Text u (Reader T2TMeta) (Text -> Text)
-> ParsecT Text u (Reader T2TMeta) Text
-> ParsecT Text u (Reader T2TMeta) Text
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ParsecT Text u (Reader T2TMeta) Text
forall st. ParsecT Text st (Reader T2TMeta) Text
pass) ParsecT Text u (Reader T2TMeta) (Char -> Text)
-> ParserT Text u (Reader T2TMeta) Char
-> ParsecT Text u (Reader T2TMeta) Text
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Char -> ParserT Text u (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m 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 Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
'['
  (Text
path, Text
ext) <- ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
-> ParsecT Text ParserState (Reader T2TMeta) (Text, Text)
forall s u (m :: * -> *) b.
ParserT s u m Char -> ParserT s u m b -> ParserT s u m (Text, b)
manyUntilChar (FilePath -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m Char
noneOf FilePath
"\n\t\r ") ([Text] -> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) st.
Stream s m Char =>
[Text] -> ParserT s st m Text
oneOfStrings [Text]
extensions)
  Char -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
']'
  Inlines -> T2T Inlines
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 :: FilePath
specialChars = FilePath
"%*-_/|:+;"

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

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

spaces :: T2T Text
spaces :: ParsecT Text ParserState (Reader T2TMeta) Text
spaces = ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParserT s st m Char -> ParserT s st m Text
manyChar ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
newline
  ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT s st m Char
blankline
  ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Blocks
hrule
  ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Blocks
title
  ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Blocks
verbatim
  ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Blocks
rawBlock
  ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Blocks
taggedBlock
  ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Blocks
quote
  ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Blocks
list
  ParsecT Text ParserState (Reader T2TMeta) Blocks
-> ParsecT Text 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 Text ParserState (Reader T2TMeta) Blocks
table
  Inlines -> T2T Inlines
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 Text ParserState (Reader T2TMeta) Text -> T2T Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) Char
-> ParsecT Text ParserState (Reader T2TMeta) Text
forall s (m :: * -> *) t st.
Stream s m t =>
ParserT s st m Char -> ParserT s st m Text
many1Char (FilePath -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m Char
noneOf (FilePath -> ParsecT Text ParserState (Reader T2TMeta) Char)
-> FilePath -> ParsecT Text ParserState (Reader T2TMeta) Char
forall a b. (a -> b) -> a -> b
$ FilePath
specialChars FilePath -> ShowS
forall a. [a] -> [a] -> [a]
++ FilePath
"\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 Text ParserState (Reader T2TMeta) Char -> T2T Inlines
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) st. Stream s m Char => ParserT 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 Text ParserState (Reader T2TMeta) Char -> T2T Inlines
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> ParsecT Text ParserState (Reader T2TMeta) Char
forall s (m :: * -> *) u.
Stream s m Char =>
FilePath -> ParsecT s u m Char
oneOf FilePath
specialChars

-- Utility

getTarget :: T2T Text
getTarget :: ParsecT Text 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 Text ParserState (Reader T2TMeta) ParserState
-> ParsecT Text ParserState (Reader T2TMeta) (Maybe MetaValue)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) ParserState
forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState
  Text -> ParsecT Text ParserState (Reader T2TMeta) Text
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> ParsecT Text ParserState (Reader T2TMeta) Text)
-> Text -> ParsecT Text 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 Text ParserState (Reader T2TMeta) ()
atStart = (SourcePos -> Int
sourceColumn (SourcePos -> Int)
-> ParsecT Text ParserState (Reader T2TMeta) SourcePos
-> ParsecT Text ParserState (Reader T2TMeta) Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT Text ParserState (Reader T2TMeta) SourcePos
forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition) ParsecT Text ParserState (Reader T2TMeta) Int
-> (Int -> ParsecT Text ParserState (Reader T2TMeta) ())
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Bool -> ParsecT Text ParserState (Reader T2TMeta) ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> ParsecT Text ParserState (Reader T2TMeta) ())
-> (Int -> Bool)
-> Int
-> ParsecT Text ParserState (Reader T2TMeta) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1)

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