{-|
Module      : Text.Jira.Parser.PlainText
Copyright   : © 2019–2023 Albert Krewinkel
License     : MIT

Maintainer  : Albert Krewinkel <tarleb@zeitkraut.de>
Stability   : alpha
Portability : portable

Functions for parsing markup-less strings.
-}

module Text.Jira.Parser.PlainText
  ( plainText
  ) where

import Data.Text (Text, append, pack)
import Text.Jira.Markup
import Text.Jira.Parser.Core
import Text.Jira.Parser.Inline (specialChars)
import Text.Jira.Parser.Shared (icon)
import Text.Parsec

-- | Parses into an @'Inline'@ elements which represent plain text. The
-- result consists of any number of @'Str'@, @'SpecialChar'@, or
-- @'Space'@ elements.
--
-- This parser can be used to convert un-escaped strings into proper
-- Jira markup elements.
plainText :: Text -> Either ParseError [Inline]
plainText :: Text -> Either ParseError [Inline]
plainText = forall a. JiraParser a -> Text -> Either ParseError a
parseJira ([Inline] -> [Inline]
normalizeInlines forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many JiraParser Inline
plainInlineParser)
  where
    plainInlineParser :: JiraParser Inline
    plainInlineParser :: JiraParser Inline
plainInlineParser = forall s (m :: * -> *) t u a.
Stream s m t =>
[ParsecT s u m a] -> ParsecT s u m a
choice
      [ Inline
Space forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m ()
skipMany1 (forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
char Char
' ')
      , forall u. Parsec Text u Inline
escapeIcon
      , forall u. Parsec Text u Inline
plainSpecialChar
      , Text -> Inline
Str forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
pack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
many1 (forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
noneOf (Char
' 'forall a. a -> [a] -> [a]
:String
specialChars))
      ] forall s u (m :: * -> *) a.
ParsecT s u m a -> String -> ParsecT s u m a
<?> String
"text-only inline"

-- | Escapes text which would otherwise render as an icon.
escapeIcon :: Parsec Text u Inline
escapeIcon :: forall u. Parsec Text u Inline
escapeIcon = Text -> Inline
Str forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
"\\" Text -> Text -> Text
`append`) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Icon -> Text
iconText forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try forall u. Parsec Text u Icon
icon

plainSpecialChar :: Parsec Text u Inline
plainSpecialChar :: forall u. Parsec Text u Inline
plainSpecialChar = Char -> Inline
SpecialChar forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
oneOf String
specialChars