{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternGuards #-}
{- |
   Module      : Text.Pandoc.Writers.Jira
   Copyright   : © 2010-2023 Albert Krewinkel, John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
   Stability   : alpha
   Portability : portable

Conversion of 'Pandoc' documents to Jira markup.

JIRA:
<https://jira.atlassian.com/secure/WikiRendererHelpAction.jspa?section=all>
-}
module Text.Pandoc.Writers.Jira ( writeJira ) where
import Control.Monad.Reader (ReaderT, ask, asks, runReaderT)
import Control.Monad.State.Strict (StateT, evalStateT, gets, modify)
import Data.Text (Text)
import Text.Jira.Parser (plainText)
import Text.Jira.Printer (prettyBlocks, prettyInlines)
import Text.Pandoc.Class.PandocMonad (PandocMonad)
import Text.Pandoc.Definition
import Text.Pandoc.Options (WriterOptions (writerTemplate, writerWrapText),
                            WrapOption (..))
import Text.Pandoc.Shared (linesToPara, stringify)
import Text.Pandoc.Templates (renderTemplate)
import Text.Pandoc.Writers.Math (texMathToInlines)
import Text.Pandoc.Writers.Shared (defField, metaToContext, toLegacyTable)
import Text.DocLayout (literal, render)
import qualified Data.Text as T
import qualified Text.Jira.Markup as Jira

-- | Convert Pandoc to Jira.
writeJira :: PandocMonad m => WriterOptions -> Pandoc -> m Text
writeJira :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
writeJira WriterOptions
opts = WrapOption -> (Pandoc -> JiraConverter m Text) -> Pandoc -> m Text
forall (m :: * -> *) a.
PandocMonad m =>
WrapOption -> (a -> JiraConverter m Text) -> a -> m Text
runDefaultConverter (WriterOptions -> WrapOption
writerWrapText WriterOptions
opts) (WriterOptions -> Pandoc -> JiraConverter m Text
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> JiraConverter m Text
pandocToJira WriterOptions
opts)

-- | State to keep track of footnotes.
data ConverterState = ConverterState
  { ConverterState -> [Text]
stNotes   :: [Text] -- ^ Footnotes to be appended to the end of the text
  , ConverterState -> Bool
stInPanel :: Bool   -- ^ whether we are in a @{panel}@ block
  }

-- | Initial converter state.
startState :: ConverterState
startState :: ConverterState
startState = ConverterState
  { stNotes :: [Text]
stNotes = []
  , stInPanel :: Bool
stInPanel = Bool
False
  }

-- | Converter monad
type JiraConverter m = ReaderT WrapOption (StateT ConverterState m)

-- | Run a converter using the default state
runDefaultConverter :: PandocMonad m
                    => WrapOption
                    -> (a -> JiraConverter m Text)
                    -> a
                    -> m Text
runDefaultConverter :: forall (m :: * -> *) a.
PandocMonad m =>
WrapOption -> (a -> JiraConverter m Text) -> a -> m Text
runDefaultConverter WrapOption
wrap a -> JiraConverter m Text
c a
x = StateT ConverterState m Text -> ConverterState -> m Text
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (JiraConverter m Text -> WrapOption -> StateT ConverterState m Text
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (a -> JiraConverter m Text
c a
x) WrapOption
wrap) ConverterState
startState

-- | Return Jira representation of document.
pandocToJira :: PandocMonad m
             => WriterOptions -> Pandoc -> JiraConverter m Text
pandocToJira :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> JiraConverter m Text
pandocToJira WriterOptions
opts (Pandoc Meta
meta [Block]
blocks) = do
  WrapOption
wrap <- ReaderT WrapOption (StateT ConverterState m) WrapOption
forall r (m :: * -> *). MonadReader r m => m r
ask
  Context Text
metadata <- WriterOptions
-> ([Block]
    -> ReaderT WrapOption (StateT ConverterState m) (Doc Text))
-> ([Inline]
    -> ReaderT WrapOption (StateT ConverterState m) (Doc Text))
-> Meta
-> ReaderT WrapOption (StateT ConverterState m) (Context Text)
forall (m :: * -> *) a.
(Monad m, TemplateTarget a) =>
WriterOptions
-> ([Block] -> m (Doc a))
-> ([Inline] -> m (Doc a))
-> Meta
-> m (Context a)
metaToContext WriterOptions
opts
                 ((Text -> Doc Text)
-> JiraConverter m Text
-> ReaderT WrapOption (StateT ConverterState m) (Doc Text)
forall a b.
(a -> b)
-> ReaderT WrapOption (StateT ConverterState m) a
-> ReaderT WrapOption (StateT ConverterState m) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal (JiraConverter m Text
 -> ReaderT WrapOption (StateT ConverterState m) (Doc Text))
-> ([Block] -> JiraConverter m Text)
-> [Block]
-> ReaderT WrapOption (StateT ConverterState m) (Doc Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WrapOption
-> ([Block]
    -> JiraConverter
         (ReaderT WrapOption (StateT ConverterState m)) Text)
-> [Block]
-> JiraConverter m Text
forall (m :: * -> *) a.
PandocMonad m =>
WrapOption -> (a -> JiraConverter m Text) -> a -> m Text
runDefaultConverter WrapOption
wrap [Block]
-> JiraConverter
     (ReaderT WrapOption (StateT ConverterState m)) Text
forall (m :: * -> *).
PandocMonad m =>
[Block] -> JiraConverter m Text
blockListToJira)
                 ((Text -> Doc Text)
-> JiraConverter m Text
-> ReaderT WrapOption (StateT ConverterState m) (Doc Text)
forall a b.
(a -> b)
-> ReaderT WrapOption (StateT ConverterState m) a
-> ReaderT WrapOption (StateT ConverterState m) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal (JiraConverter m Text
 -> ReaderT WrapOption (StateT ConverterState m) (Doc Text))
-> ([Inline] -> JiraConverter m Text)
-> [Inline]
-> ReaderT WrapOption (StateT ConverterState m) (Doc Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WrapOption
-> ([Inline]
    -> JiraConverter
         (ReaderT WrapOption (StateT ConverterState m)) Text)
-> [Inline]
-> JiraConverter m Text
forall (m :: * -> *) a.
PandocMonad m =>
WrapOption -> (a -> JiraConverter m Text) -> a -> m Text
runDefaultConverter WrapOption
wrap [Inline]
-> JiraConverter
     (ReaderT WrapOption (StateT ConverterState m)) Text
forall (m :: * -> *).
PandocMonad m =>
[Inline] -> JiraConverter m Text
inlineListToJira) Meta
meta
  Text
body <- [Block] -> JiraConverter m Text
forall (m :: * -> *).
PandocMonad m =>
[Block] -> JiraConverter m Text
blockListToJira [Block]
blocks
  Text
notes <- (ConverterState -> Text) -> JiraConverter m Text
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ((ConverterState -> Text) -> JiraConverter m Text)
-> (ConverterState -> Text) -> JiraConverter m Text
forall a b. (a -> b) -> a -> b
$ Text -> [Text] -> Text
T.intercalate Text
"\n" ([Text] -> Text)
-> (ConverterState -> [Text]) -> ConverterState -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> [Text]
forall a. [a] -> [a]
reverse ([Text] -> [Text])
-> (ConverterState -> [Text]) -> ConverterState -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConverterState -> [Text]
stNotes
  let main :: Text
main = Text
body Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> if Text -> Bool
T.null Text
notes then Text
forall a. Monoid a => a
mempty else Text
"\n\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
notes
  let context :: Context Text
context = Text -> Text -> Context Text -> Context Text
forall a b. ToContext a b => Text -> b -> Context a -> Context a
defField Text
"body" Text
main Context Text
metadata
  Text -> JiraConverter m Text
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> JiraConverter m Text) -> Text -> JiraConverter m Text
forall a b. (a -> b) -> a -> b
$
    case WriterOptions -> Maybe (Template Text)
writerTemplate WriterOptions
opts of
      Maybe (Template Text)
Nothing  -> Text
main
      Just Template Text
tpl -> Maybe Int -> Doc Text -> Text
forall a. HasChars a => Maybe Int -> Doc a -> a
render Maybe Int
forall a. Maybe a
Nothing (Doc Text -> Text) -> Doc Text -> Text
forall a b. (a -> b) -> a -> b
$ Template Text -> Context Text -> Doc Text
forall a b.
(TemplateTarget a, ToContext a b) =>
Template a -> b -> Doc a
renderTemplate Template Text
tpl Context Text
context

blockListToJira :: PandocMonad m => [Block] -> JiraConverter m Text
blockListToJira :: forall (m :: * -> *).
PandocMonad m =>
[Block] -> JiraConverter m Text
blockListToJira = ([Block] -> Text)
-> ReaderT WrapOption (StateT ConverterState m) [Block]
-> ReaderT WrapOption (StateT ConverterState m) Text
forall a b.
(a -> b)
-> ReaderT WrapOption (StateT ConverterState m) a
-> ReaderT WrapOption (StateT ConverterState m) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Block] -> Text
prettyBlocks (ReaderT WrapOption (StateT ConverterState m) [Block]
 -> ReaderT WrapOption (StateT ConverterState m) Text)
-> ([Block]
    -> ReaderT WrapOption (StateT ConverterState m) [Block])
-> [Block]
-> ReaderT WrapOption (StateT ConverterState m) Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Block] -> ReaderT WrapOption (StateT ConverterState m) [Block]
forall (m :: * -> *).
PandocMonad m =>
[Block] -> JiraConverter m [Block]
toJiraBlocks

inlineListToJira :: PandocMonad m => [Inline] -> JiraConverter m Text
inlineListToJira :: forall (m :: * -> *).
PandocMonad m =>
[Inline] -> JiraConverter m Text
inlineListToJira = ([Inline] -> Text)
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
-> ReaderT WrapOption (StateT ConverterState m) Text
forall a b.
(a -> b)
-> ReaderT WrapOption (StateT ConverterState m) a
-> ReaderT WrapOption (StateT ConverterState m) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Inline] -> Text
prettyInlines (ReaderT WrapOption (StateT ConverterState m) [Inline]
 -> ReaderT WrapOption (StateT ConverterState m) Text)
-> ([Inline]
    -> ReaderT WrapOption (StateT ConverterState m) [Inline])
-> [Inline]
-> ReaderT WrapOption (StateT ConverterState m) Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall (m :: * -> *).
PandocMonad m =>
[Inline] -> JiraConverter m [Inline]
toJiraInlines

toJiraBlocks :: PandocMonad m => [Block] -> JiraConverter m [Jira.Block]
toJiraBlocks :: forall (m :: * -> *).
PandocMonad m =>
[Block] -> JiraConverter m [Block]
toJiraBlocks [Block]
blocks = do
  let convert :: Block -> JiraConverter m [Block]
convert = \case
        BlockQuote [Block]
bs        -> Block -> [Block]
forall a. a -> [a]
singleton (Block -> [Block]) -> ([Block] -> Block) -> [Block] -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Block] -> Block
Jira.BlockQuote
                                ([Block] -> [Block])
-> JiraConverter m [Block] -> JiraConverter m [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Block] -> JiraConverter m [Block]
forall (m :: * -> *).
PandocMonad m =>
[Block] -> JiraConverter m [Block]
toJiraBlocks [Block]
bs
        BulletList [[Block]]
items     -> Block -> [Block]
forall a. a -> [a]
singleton (Block -> [Block]) -> ([[Block]] -> Block) -> [[Block]] -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ListStyle -> [[Block]] -> Block
Jira.List ListStyle
Jira.CircleBullets
                                ([[Block]] -> [Block])
-> ReaderT WrapOption (StateT ConverterState m) [[Block]]
-> JiraConverter m [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [[Block]] -> ReaderT WrapOption (StateT ConverterState m) [[Block]]
forall (m :: * -> *).
PandocMonad m =>
[[Block]] -> JiraConverter m [[Block]]
toJiraItems [[Block]]
items
        CodeBlock Attr
attr Text
cs    -> Attr -> Text -> JiraConverter m [Block]
forall (m :: * -> *).
PandocMonad m =>
Attr -> Text -> JiraConverter m [Block]
toJiraCode Attr
attr Text
cs
        DefinitionList [([Inline], [[Block]])]
items -> [([Inline], [[Block]])] -> JiraConverter m [Block]
forall (m :: * -> *).
PandocMonad m =>
[([Inline], [[Block]])] -> JiraConverter m [Block]
toJiraDefinitionList [([Inline], [[Block]])]
items
        Div Attr
attr [Block]
bs          -> Attr -> [Block] -> JiraConverter m [Block]
forall (m :: * -> *).
PandocMonad m =>
Attr -> [Block] -> JiraConverter m [Block]
toJiraPanel Attr
attr [Block]
bs
        Header Int
lvl Attr
attr [Inline]
xs   -> Int -> Attr -> [Inline] -> JiraConverter m [Block]
forall (m :: * -> *).
PandocMonad m =>
Int -> Attr -> [Inline] -> JiraConverter m [Block]
toJiraHeader Int
lvl Attr
attr [Inline]
xs
        Block
HorizontalRule       -> [Block] -> JiraConverter m [Block]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Block] -> JiraConverter m [Block])
-> (Block -> [Block]) -> Block -> JiraConverter m [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Block -> [Block]
forall a. a -> [a]
singleton (Block -> JiraConverter m [Block])
-> Block -> JiraConverter m [Block]
forall a b. (a -> b) -> a -> b
$ Block
Jira.HorizontalRule
        LineBlock [[Inline]]
xs         -> [Block] -> JiraConverter m [Block]
forall (m :: * -> *).
PandocMonad m =>
[Block] -> JiraConverter m [Block]
toJiraBlocks [[[Inline]] -> Block
linesToPara [[Inline]]
xs]
        OrderedList ListAttributes
_ [[Block]]
items  -> Block -> [Block]
forall a. a -> [a]
singleton (Block -> [Block]) -> ([[Block]] -> Block) -> [[Block]] -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ListStyle -> [[Block]] -> Block
Jira.List ListStyle
Jira.Enumeration
                                ([[Block]] -> [Block])
-> ReaderT WrapOption (StateT ConverterState m) [[Block]]
-> JiraConverter m [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [[Block]] -> ReaderT WrapOption (StateT ConverterState m) [[Block]]
forall (m :: * -> *).
PandocMonad m =>
[[Block]] -> JiraConverter m [[Block]]
toJiraItems [[Block]]
items
        Para [Inline]
xs              -> Block -> [Block]
forall a. a -> [a]
singleton (Block -> [Block]) -> ([Inline] -> Block) -> [Inline] -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Block
Jira.Para ([Inline] -> [Block])
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
-> JiraConverter m [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Inline] -> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall (m :: * -> *).
PandocMonad m =>
[Inline] -> JiraConverter m [Inline]
toJiraInlines [Inline]
xs
        Plain [Inline]
xs             -> Block -> [Block]
forall a. a -> [a]
singleton (Block -> [Block]) -> ([Inline] -> Block) -> [Inline] -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> Block
Jira.Para ([Inline] -> [Block])
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
-> JiraConverter m [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Inline] -> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall (m :: * -> *).
PandocMonad m =>
[Inline] -> JiraConverter m [Inline]
toJiraInlines [Inline]
xs
        RawBlock Format
fmt Text
cs      -> Format -> Text -> JiraConverter m [Block]
forall (m :: * -> *).
PandocMonad m =>
Format -> Text -> JiraConverter m [Block]
rawBlockToJira Format
fmt Text
cs
        Table Attr
_ Caption
blkCapt [ColSpec]
specs TableHead
thead [TableBody]
tbody TableFoot
tfoot -> Block -> [Block]
forall a. a -> [a]
singleton (Block -> [Block])
-> ReaderT WrapOption (StateT ConverterState m) Block
-> JiraConverter m [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> do
          let ([Inline]
_, [Alignment]
_, [Double]
_, [[Block]]
hd, [[[Block]]]
body) = Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> ([Inline], [Alignment], [Double], [[Block]], [[[Block]]])
toLegacyTable Caption
blkCapt [ColSpec]
specs TableHead
thead [TableBody]
tbody TableFoot
tfoot
          Maybe Row
headerRow <- if ([Block] -> Bool) -> [[Block]] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all [Block] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [[Block]]
hd
                       then Maybe Row
-> ReaderT WrapOption (StateT ConverterState m) (Maybe Row)
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Row
forall a. Maybe a
Nothing
                       else Row -> Maybe Row
forall a. a -> Maybe a
Just (Row -> Maybe Row)
-> ReaderT WrapOption (StateT ConverterState m) Row
-> ReaderT WrapOption (StateT ConverterState m) (Maybe Row)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Block] -> Cell)
-> [[Block]] -> ReaderT WrapOption (StateT ConverterState m) Row
forall (m :: * -> *).
PandocMonad m =>
([Block] -> Cell) -> [[Block]] -> JiraConverter m Row
toRow [Block] -> Cell
Jira.HeaderCell [[Block]]
hd
          [Row]
bodyRows <- ([[Block]] -> ReaderT WrapOption (StateT ConverterState m) Row)
-> [[[Block]]]
-> ReaderT WrapOption (StateT ConverterState m) [Row]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (([Block] -> Cell)
-> [[Block]] -> ReaderT WrapOption (StateT ConverterState m) Row
forall (m :: * -> *).
PandocMonad m =>
([Block] -> Cell) -> [[Block]] -> JiraConverter m Row
toRow [Block] -> Cell
Jira.BodyCell) [[[Block]]]
body
          let rows :: [Row]
rows = case Maybe Row
headerRow of
                       Just Row
header -> Row
header Row -> [Row] -> [Row]
forall a. a -> [a] -> [a]
: [Row]
bodyRows
                       Maybe Row
Nothing     -> [Row]
bodyRows
          Block -> ReaderT WrapOption (StateT ConverterState m) Block
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Block -> ReaderT WrapOption (StateT ConverterState m) Block)
-> Block -> ReaderT WrapOption (StateT ConverterState m) Block
forall a b. (a -> b) -> a -> b
$ [Row] -> Block
Jira.Table [Row]
rows
        Figure Attr
attr Caption
_ [Block]
body         -> Attr -> [Block] -> JiraConverter m [Block]
forall (m :: * -> *).
PandocMonad m =>
Attr -> [Block] -> JiraConverter m [Block]
toJiraPanel Attr
attr [Block]
body
  [[Block]]
jiraBlocks <- (Block -> JiraConverter m [Block])
-> [Block]
-> ReaderT WrapOption (StateT ConverterState m) [[Block]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Block -> JiraConverter m [Block]
convert [Block]
blocks
  [Block] -> JiraConverter m [Block]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Block] -> JiraConverter m [Block])
-> [Block] -> JiraConverter m [Block]
forall a b. (a -> b) -> a -> b
$ [[Block]] -> [Block]
forall a. Monoid a => [a] -> a
mconcat [[Block]]
jiraBlocks

toRow :: PandocMonad m
      => ([Jira.Block] -> Jira.Cell)
      -> [[Block]]
      -> JiraConverter m Jira.Row
toRow :: forall (m :: * -> *).
PandocMonad m =>
([Block] -> Cell) -> [[Block]] -> JiraConverter m Row
toRow [Block] -> Cell
mkCell [[Block]]
cells = [Cell] -> Row
Jira.Row ([Cell] -> Row)
-> ReaderT WrapOption (StateT ConverterState m) [Cell]
-> ReaderT WrapOption (StateT ConverterState m) Row
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
  ([Block] -> ReaderT WrapOption (StateT ConverterState m) Cell)
-> [[Block]] -> ReaderT WrapOption (StateT ConverterState m) [Cell]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (([Block] -> Cell)
-> ReaderT WrapOption (StateT ConverterState m) [Block]
-> ReaderT WrapOption (StateT ConverterState m) Cell
forall a b.
(a -> b)
-> ReaderT WrapOption (StateT ConverterState m) a
-> ReaderT WrapOption (StateT ConverterState m) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Block] -> Cell
mkCell (ReaderT WrapOption (StateT ConverterState m) [Block]
 -> ReaderT WrapOption (StateT ConverterState m) Cell)
-> ([Block]
    -> ReaderT WrapOption (StateT ConverterState m) [Block])
-> [Block]
-> ReaderT WrapOption (StateT ConverterState m) Cell
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Block] -> ReaderT WrapOption (StateT ConverterState m) [Block]
forall (m :: * -> *).
PandocMonad m =>
[Block] -> JiraConverter m [Block]
toJiraBlocks) [[Block]]
cells

toJiraItems :: PandocMonad m => [[Block]] -> JiraConverter m [[Jira.Block]]
toJiraItems :: forall (m :: * -> *).
PandocMonad m =>
[[Block]] -> JiraConverter m [[Block]]
toJiraItems = ([Block] -> ReaderT WrapOption (StateT ConverterState m) [Block])
-> [[Block]]
-> ReaderT WrapOption (StateT ConverterState m) [[Block]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM [Block] -> ReaderT WrapOption (StateT ConverterState m) [Block]
forall (m :: * -> *).
PandocMonad m =>
[Block] -> JiraConverter m [Block]
toJiraBlocks

toJiraCode :: PandocMonad m
           => Attr
           -> Text
           -> JiraConverter m [Jira.Block]
toJiraCode :: forall (m :: * -> *).
PandocMonad m =>
Attr -> Text -> JiraConverter m [Block]
toJiraCode (Text
ident, [Text]
classes, [(Text, Text)]
_attribs) Text
code = do
  [Block] -> JiraConverter m [Block]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Block] -> JiraConverter m [Block])
-> (Block -> [Block]) -> Block -> JiraConverter m [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Block] -> [Block]
addAnchor Text
ident ([Block] -> [Block]) -> (Block -> [Block]) -> Block -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Block -> [Block]
forall a. a -> [a]
singleton (Block -> JiraConverter m [Block])
-> Block -> JiraConverter m [Block]
forall a b. (a -> b) -> a -> b
$
    case [Text]
classes of
      []  -> [Parameter] -> Text -> Block
Jira.NoFormat [Parameter]
forall a. Monoid a => a
mempty Text
code
      Text
l:[Text]
_ -> Language -> [Parameter] -> Text -> Block
Jira.Code (Text -> Language
Jira.Language Text
l) [Parameter]
forall a. Monoid a => a
mempty Text
code

-- | Prepends an anchor with the given identifier.
addAnchor :: Text -> [Jira.Block] -> [Jira.Block]
addAnchor :: Text -> [Block] -> [Block]
addAnchor Text
ident =
  if Text -> Bool
T.null Text
ident
  then [Block] -> [Block]
forall a. a -> a
id
  else \case
    Jira.Para [Inline]
xs : [Block]
bs -> ([Inline] -> Block
Jira.Para (Text -> Inline
Jira.Anchor Text
ident Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline]
xs) Block -> [Block] -> [Block]
forall a. a -> [a] -> [a]
: [Block]
bs)
    [Block]
bs                -> ([Inline] -> Block
Jira.Para (Inline -> [Inline]
forall a. a -> [a]
singleton (Text -> Inline
Jira.Anchor Text
ident)) Block -> [Block] -> [Block]
forall a. a -> [a] -> [a]
: [Block]
bs)

-- | Creates a Jira definition list
toJiraDefinitionList :: PandocMonad m
                     => [([Inline], [[Block]])]
                     -> JiraConverter m [Jira.Block]
toJiraDefinitionList :: forall (m :: * -> *).
PandocMonad m =>
[([Inline], [[Block]])] -> JiraConverter m [Block]
toJiraDefinitionList [([Inline], [[Block]])]
defItems = do
  let convertDefItem :: ([Inline], [[Block]])
-> ReaderT WrapOption (StateT ConverterState m) [Block]
convertDefItem ([Inline]
term, [[Block]]
defs) = do
        Block
jiraTerm <- [Inline] -> Block
Jira.Para ([Inline] -> Block)
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
-> ReaderT WrapOption (StateT ConverterState m) Block
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> InlineStyle
-> [Inline]
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall (m :: * -> *).
PandocMonad m =>
InlineStyle -> [Inline] -> JiraConverter m [Inline]
styled InlineStyle
Jira.Strong [Inline]
term
        [Block]
jiraDefs <- [[Block]] -> [Block]
forall a. Monoid a => [a] -> a
mconcat ([[Block]] -> [Block])
-> ReaderT WrapOption (StateT ConverterState m) [[Block]]
-> ReaderT WrapOption (StateT ConverterState m) [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Block] -> ReaderT WrapOption (StateT ConverterState m) [Block])
-> [[Block]]
-> ReaderT WrapOption (StateT ConverterState m) [[Block]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM [Block] -> ReaderT WrapOption (StateT ConverterState m) [Block]
forall (m :: * -> *).
PandocMonad m =>
[Block] -> JiraConverter m [Block]
toJiraBlocks [[Block]]
defs
        [Block] -> ReaderT WrapOption (StateT ConverterState m) [Block]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Block] -> ReaderT WrapOption (StateT ConverterState m) [Block])
-> [Block] -> ReaderT WrapOption (StateT ConverterState m) [Block]
forall a b. (a -> b) -> a -> b
$ Block
jiraTerm Block -> [Block] -> [Block]
forall a. a -> [a] -> [a]
: [Block]
jiraDefs
  Block -> [Block]
forall a. a -> [a]
singleton (Block -> [Block]) -> ([[Block]] -> Block) -> [[Block]] -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ListStyle -> [[Block]] -> Block
Jira.List ListStyle
Jira.CircleBullets ([[Block]] -> [Block])
-> ReaderT WrapOption (StateT ConverterState m) [[Block]]
-> JiraConverter m [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (([Inline], [[Block]]) -> JiraConverter m [Block])
-> [([Inline], [[Block]])]
-> ReaderT WrapOption (StateT ConverterState m) [[Block]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ([Inline], [[Block]]) -> JiraConverter m [Block]
forall {m :: * -> *}.
PandocMonad m =>
([Inline], [[Block]])
-> ReaderT WrapOption (StateT ConverterState m) [Block]
convertDefItem [([Inline], [[Block]])]
defItems

-- | Creates a Jira panel
toJiraPanel :: PandocMonad m
            => Attr -> [Block]
            -> JiraConverter m [Jira.Block]
toJiraPanel :: forall (m :: * -> *).
PandocMonad m =>
Attr -> [Block] -> JiraConverter m [Block]
toJiraPanel (Text
ident, [Text]
classes, [(Text, Text)]
attribs) [Block]
blocks = do
  Bool
inPanel <- (ConverterState -> Bool)
-> ReaderT WrapOption (StateT ConverterState m) Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ConverterState -> Bool
stInPanel
  if Bool
inPanel Bool -> Bool -> Bool
|| (Text
"panel" Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Text]
classes Bool -> Bool -> Bool
&& [(Text, Text)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Text, Text)]
attribs)
    then Text -> [Block] -> [Block]
addAnchor Text
ident ([Block] -> [Block])
-> JiraConverter m [Block] -> JiraConverter m [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Block] -> JiraConverter m [Block]
forall (m :: * -> *).
PandocMonad m =>
[Block] -> JiraConverter m [Block]
toJiraBlocks [Block]
blocks
    else do
      (ConverterState -> ConverterState)
-> ReaderT WrapOption (StateT ConverterState m) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((ConverterState -> ConverterState)
 -> ReaderT WrapOption (StateT ConverterState m) ())
-> (ConverterState -> ConverterState)
-> ReaderT WrapOption (StateT ConverterState m) ()
forall a b. (a -> b) -> a -> b
$ \ConverterState
st -> ConverterState
st{ stInPanel :: Bool
stInPanel = Bool
True }
      [Block]
jiraBlocks <- [Block] -> JiraConverter m [Block]
forall (m :: * -> *).
PandocMonad m =>
[Block] -> JiraConverter m [Block]
toJiraBlocks [Block]
blocks
      (ConverterState -> ConverterState)
-> ReaderT WrapOption (StateT ConverterState m) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((ConverterState -> ConverterState)
 -> ReaderT WrapOption (StateT ConverterState m) ())
-> (ConverterState -> ConverterState)
-> ReaderT WrapOption (StateT ConverterState m) ()
forall a b. (a -> b) -> a -> b
$ \ConverterState
st -> ConverterState
st{ stInPanel :: Bool
stInPanel = Bool
inPanel }
      let params :: [Parameter]
params = ((Text, Text) -> Parameter) -> [(Text, Text)] -> [Parameter]
forall a b. (a -> b) -> [a] -> [b]
map ((Text -> Text -> Parameter) -> (Text, Text) -> Parameter
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Text -> Text -> Parameter
Jira.Parameter) [(Text, Text)]
attribs
      [Block] -> JiraConverter m [Block]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Block] -> JiraConverter m [Block])
-> [Block] -> JiraConverter m [Block]
forall a b. (a -> b) -> a -> b
$ Block -> [Block]
forall a. a -> [a]
singleton ([Parameter] -> [Block] -> Block
Jira.Panel [Parameter]
params ([Block] -> Block) -> [Block] -> Block
forall a b. (a -> b) -> a -> b
$ Text -> [Block] -> [Block]
addAnchor Text
ident [Block]
jiraBlocks)

-- | Creates a Jira header
toJiraHeader :: PandocMonad m
             => Int -> Attr -> [Inline]
             -> JiraConverter m [Jira.Block]
toJiraHeader :: forall (m :: * -> *).
PandocMonad m =>
Int -> Attr -> [Inline] -> JiraConverter m [Block]
toJiraHeader Int
lvl (Text
ident, [Text]
_, [(Text, Text)]
_) [Inline]
inlines =
  let anchor :: Inline
anchor = Text -> Inline
Jira.Anchor Text
ident
  in Block -> [Block]
forall a. a -> [a]
singleton (Block -> [Block]) -> ([Inline] -> Block) -> [Inline] -> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Inline] -> Block
Jira.Header Int
lvl ([Inline] -> Block) -> ([Inline] -> [Inline]) -> [Inline] -> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Inline
anchor Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:) ([Inline] -> [Block])
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
-> ReaderT WrapOption (StateT ConverterState m) [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Inline] -> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall (m :: * -> *).
PandocMonad m =>
[Inline] -> JiraConverter m [Inline]
toJiraInlines [Inline]
inlines

-- | Handles raw block. Jira is included verbatim, everything else is
-- discarded.
rawBlockToJira :: PandocMonad m
               => Format -> Text
               -> JiraConverter m [Jira.Block]
rawBlockToJira :: forall (m :: * -> *).
PandocMonad m =>
Format -> Text -> JiraConverter m [Block]
rawBlockToJira Format
fmt Text
cs = do
  [Inline]
rawInlines <- Format -> Text -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
Format -> Text -> JiraConverter m [Inline]
toJiraRaw Format
fmt Text
cs
  [Block] -> JiraConverter m [Block]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Block] -> JiraConverter m [Block])
-> [Block] -> JiraConverter m [Block]
forall a b. (a -> b) -> a -> b
$
    if [Inline] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
rawInlines
    then [Block]
forall a. Monoid a => a
mempty
    else Block -> [Block]
forall a. a -> [a]
singleton ([Inline] -> Block
Jira.Para [Inline]
rawInlines)

toJiraRaw :: PandocMonad m
          => Format -> Text -> JiraConverter m [Jira.Inline]
toJiraRaw :: forall (m :: * -> *).
PandocMonad m =>
Format -> Text -> JiraConverter m [Inline]
toJiraRaw Format
fmt Text
cs = case Format
fmt of
  Format Text
"jira" -> [Inline] -> JiraConverter m [Inline]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Inline] -> JiraConverter m [Inline])
-> (Inline -> [Inline]) -> Inline -> JiraConverter m [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inline -> [Inline]
forall a. a -> [a]
singleton (Inline -> JiraConverter m [Inline])
-> Inline -> JiraConverter m [Inline]
forall a b. (a -> b) -> a -> b
$ Text -> Inline
Jira.Str Text
cs
  Format
_             -> [Inline] -> JiraConverter m [Inline]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (m :: * -> *) a. Monad m => a -> m a
return [Inline]
forall a. Monoid a => a
mempty


--
-- Inlines
--

toJiraInlines :: PandocMonad m => [Inline] -> JiraConverter m [Jira.Inline]
toJiraInlines :: forall (m :: * -> *).
PandocMonad m =>
[Inline] -> JiraConverter m [Inline]
toJiraInlines [Inline]
inlines = do
  let convert :: Inline -> JiraConverter m [Inline]
convert = \case
        Cite [Citation]
_ [Inline]
xs          -> [Inline] -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
[Inline] -> JiraConverter m [Inline]
toJiraInlines [Inline]
xs
        Code Attr
_ Text
cs          -> [Inline] -> JiraConverter m [Inline]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Inline] -> JiraConverter m [Inline])
-> (Inline -> [Inline]) -> Inline -> JiraConverter m [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inline -> [Inline]
forall a. a -> [a]
singleton (Inline -> JiraConverter m [Inline])
-> Inline -> JiraConverter m [Inline]
forall a b. (a -> b) -> a -> b
$
                              [Inline] -> Inline
Jira.Monospaced (Text -> [Inline]
escapeSpecialChars Text
cs)
        Emph [Inline]
xs            -> InlineStyle -> [Inline] -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
InlineStyle -> [Inline] -> JiraConverter m [Inline]
styled InlineStyle
Jira.Emphasis [Inline]
xs
        Underline [Inline]
xs       -> InlineStyle -> [Inline] -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
InlineStyle -> [Inline] -> JiraConverter m [Inline]
styled InlineStyle
Jira.Insert [Inline]
xs
        Image Attr
attr [Inline]
cap (Text, Text)
tgt -> (Text -> Text -> JiraConverter m [Inline])
-> (Text, Text) -> JiraConverter m [Inline]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Attr -> [Inline] -> Text -> Text -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
Attr -> [Inline] -> Text -> Text -> JiraConverter m [Inline]
imageToJira Attr
attr [Inline]
cap) (Text, Text)
tgt
        Inline
LineBreak          -> [Inline] -> JiraConverter m [Inline]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Inline] -> JiraConverter m [Inline])
-> (Inline -> [Inline]) -> Inline -> JiraConverter m [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inline -> [Inline]
forall a. a -> [a]
singleton (Inline -> JiraConverter m [Inline])
-> Inline -> JiraConverter m [Inline]
forall a b. (a -> b) -> a -> b
$ Inline
Jira.Linebreak
        Link Attr
attr [Inline]
xs (Text, Text)
tgt   -> Attr -> (Text, Text) -> [Inline] -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
Attr -> (Text, Text) -> [Inline] -> JiraConverter m [Inline]
toJiraLink Attr
attr (Text, Text)
tgt [Inline]
xs
        Math MathType
mtype Text
cs      -> MathType -> Text -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
MathType -> Text -> JiraConverter m [Inline]
mathToJira MathType
mtype Text
cs
        Note [Block]
bs            -> [Block] -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
[Block] -> JiraConverter m [Inline]
registerNotes [Block]
bs
        Quoted QuoteType
qt [Inline]
xs       -> QuoteType -> [Inline] -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
QuoteType -> [Inline] -> JiraConverter m [Inline]
quotedToJira QuoteType
qt [Inline]
xs
        RawInline Format
fmt Text
cs   -> Format -> Text -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
Format -> Text -> JiraConverter m [Inline]
toJiraRaw Format
fmt Text
cs
        SmallCaps [Inline]
xs       -> InlineStyle -> [Inline] -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
InlineStyle -> [Inline] -> JiraConverter m [Inline]
styled InlineStyle
Jira.Strong [Inline]
xs
        Inline
SoftBreak          -> do
                                Bool
preserveBreak <- (WrapOption -> Bool)
-> ReaderT WrapOption (StateT ConverterState m) Bool
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks (WrapOption -> WrapOption -> Bool
forall a. Eq a => a -> a -> Bool
== WrapOption
WrapPreserve)
                                [Inline] -> JiraConverter m [Inline]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Inline] -> JiraConverter m [Inline])
-> (Inline -> [Inline]) -> Inline -> JiraConverter m [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inline -> [Inline]
forall a. a -> [a]
singleton (Inline -> JiraConverter m [Inline])
-> Inline -> JiraConverter m [Inline]
forall a b. (a -> b) -> a -> b
$ if Bool
preserveBreak
                                  then Inline
Jira.Linebreak
                                  else Inline
Jira.Space
        Inline
Space              -> [Inline] -> JiraConverter m [Inline]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Inline] -> JiraConverter m [Inline])
-> (Inline -> [Inline]) -> Inline -> JiraConverter m [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inline -> [Inline]
forall a. a -> [a]
singleton (Inline -> JiraConverter m [Inline])
-> Inline -> JiraConverter m [Inline]
forall a b. (a -> b) -> a -> b
$ Inline
Jira.Space
        Span Attr
attr [Inline]
xs       -> Attr -> [Inline] -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
Attr -> [Inline] -> JiraConverter m [Inline]
spanToJira Attr
attr [Inline]
xs
        Str Text
s              -> [Inline] -> JiraConverter m [Inline]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Inline] -> JiraConverter m [Inline])
-> [Inline] -> JiraConverter m [Inline]
forall a b. (a -> b) -> a -> b
$ Text -> [Inline]
escapeSpecialChars Text
s
        Strikeout [Inline]
xs       -> InlineStyle -> [Inline] -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
InlineStyle -> [Inline] -> JiraConverter m [Inline]
styled InlineStyle
Jira.Strikeout [Inline]
xs
        Strong [Inline]
xs          -> InlineStyle -> [Inline] -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
InlineStyle -> [Inline] -> JiraConverter m [Inline]
styled InlineStyle
Jira.Strong [Inline]
xs
        Subscript [Inline]
xs       -> InlineStyle -> [Inline] -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
InlineStyle -> [Inline] -> JiraConverter m [Inline]
styled InlineStyle
Jira.Subscript [Inline]
xs
        Superscript [Inline]
xs     -> InlineStyle -> [Inline] -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
InlineStyle -> [Inline] -> JiraConverter m [Inline]
styled InlineStyle
Jira.Superscript [Inline]
xs
  [[Inline]]
jiraInlines <- (Inline -> JiraConverter m [Inline])
-> [Inline]
-> ReaderT WrapOption (StateT ConverterState m) [[Inline]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Inline -> JiraConverter m [Inline]
convert [Inline]
inlines
  [Inline] -> JiraConverter m [Inline]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Inline] -> JiraConverter m [Inline])
-> [Inline] -> JiraConverter m [Inline]
forall a b. (a -> b) -> a -> b
$ [[Inline]] -> [Inline]
forall a. Monoid a => [a] -> a
mconcat [[Inline]]
jiraInlines

singleton :: a -> [a]
singleton :: forall a. a -> [a]
singleton = (a -> [a] -> [a]
forall a. a -> [a] -> [a]
:[])

styled :: PandocMonad m
       => Jira.InlineStyle -> [Inline]
       -> JiraConverter m [Jira.Inline]
styled :: forall (m :: * -> *).
PandocMonad m =>
InlineStyle -> [Inline] -> JiraConverter m [Inline]
styled InlineStyle
s = ([Inline] -> [Inline])
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall a b.
(a -> b)
-> ReaderT WrapOption (StateT ConverterState m) a
-> ReaderT WrapOption (StateT ConverterState m) b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Inline -> [Inline]
forall a. a -> [a]
singleton (Inline -> [Inline])
-> ([Inline] -> Inline) -> [Inline] -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InlineStyle -> [Inline] -> Inline
Jira.Styled InlineStyle
s) (ReaderT WrapOption (StateT ConverterState m) [Inline]
 -> ReaderT WrapOption (StateT ConverterState m) [Inline])
-> ([Inline]
    -> ReaderT WrapOption (StateT ConverterState m) [Inline])
-> [Inline]
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Inline] -> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall (m :: * -> *).
PandocMonad m =>
[Inline] -> JiraConverter m [Inline]
toJiraInlines

-- | Converts a plain text value to Jira inlines, ensuring that all
-- special characters will be handled appropriately.
escapeSpecialChars :: Text -> [Jira.Inline]
escapeSpecialChars :: Text -> [Inline]
escapeSpecialChars Text
t = case Text -> Either ParseError [Inline]
plainText Text
t of
  Right [Inline]
xs -> [Inline]
xs
  Left ParseError
_  -> Inline -> [Inline]
forall a. a -> [a]
singleton (Inline -> [Inline]) -> Inline -> [Inline]
forall a b. (a -> b) -> a -> b
$ Text -> Inline
Jira.Str Text
t

imageToJira :: PandocMonad m
            => Attr -> [Inline] -> Text -> Text
            -> JiraConverter m [Jira.Inline]
imageToJira :: forall (m :: * -> *).
PandocMonad m =>
Attr -> [Inline] -> Text -> Text -> JiraConverter m [Inline]
imageToJira (Text
_, [Text]
classes, [(Text, Text)]
kvs) [Inline]
caption Text
src Text
title =
  let imageWithParams :: [Parameter] -> Inline
imageWithParams [Parameter]
ps = [Parameter] -> URL -> Inline
Jira.Image [Parameter]
ps (Text -> URL
Jira.URL Text
src)
      alt :: Text
alt = [Inline] -> Text
forall a. Walkable Inline a => a -> Text
stringify [Inline]
caption
  in [Inline] -> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Inline] -> ReaderT WrapOption (StateT ConverterState m) [Inline])
-> ([Parameter] -> [Inline])
-> [Parameter]
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inline -> [Inline]
forall a. a -> [a]
singleton (Inline -> [Inline])
-> ([Parameter] -> Inline) -> [Parameter] -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Parameter] -> Inline
imageWithParams ([Parameter]
 -> ReaderT WrapOption (StateT ConverterState m) [Inline])
-> [Parameter]
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall a b. (a -> b) -> a -> b
$
     if Text
"thumbnail" Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
classes
     then [Text -> Text -> Parameter
Jira.Parameter Text
"thumbnail" Text
""]
     else ((Text, Text) -> Parameter) -> [(Text, Text)] -> [Parameter]
forall a b. (a -> b) -> [a] -> [b]
map ((Text -> Text -> Parameter) -> (Text, Text) -> Parameter
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Text -> Text -> Parameter
Jira.Parameter)
          ([(Text, Text)] -> [Parameter])
-> ([(Text, Text)] -> [(Text, Text)])
-> [(Text, Text)]
-> [Parameter]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (if Text -> Bool
T.null Text
title then [(Text, Text)] -> [(Text, Text)]
forall a. a -> a
id else ((Text
"title", Text
title)(Text, Text) -> [(Text, Text)] -> [(Text, Text)]
forall a. a -> [a] -> [a]
:))
          ([(Text, Text)] -> [(Text, Text)])
-> ([(Text, Text)] -> [(Text, Text)])
-> [(Text, Text)]
-> [(Text, Text)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (if Text -> Bool
T.null Text
alt then [(Text, Text)] -> [(Text, Text)]
forall a. a -> a
id else ((Text
"alt", Text
alt)(Text, Text) -> [(Text, Text)] -> [(Text, Text)]
forall a. a -> [a] -> [a]
:))
          ([(Text, Text)] -> [Parameter]) -> [(Text, Text)] -> [Parameter]
forall a b. (a -> b) -> a -> b
$ [(Text, Text)]
kvs

-- | Creates a Jira Link element.
toJiraLink :: PandocMonad m
           => Attr
           -> Target
           -> [Inline]
           -> JiraConverter m [Jira.Inline]
toJiraLink :: forall (m :: * -> *).
PandocMonad m =>
Attr -> (Text, Text) -> [Inline] -> JiraConverter m [Inline]
toJiraLink (Text
_, [Text]
classes, [(Text, Text)]
_) (Text
url, Text
_) [Inline]
alias = do
  let (LinkType
linkType, Text
url') = Text -> (LinkType, Text)
toLinkType Text
url
  [Inline]
description <- if Text
url Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [[Inline] -> Text
forall a. Walkable Inline a => a -> Text
stringify [Inline]
alias, Text
"mailto:" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Inline] -> Text
forall a. Walkable Inline a => a -> Text
stringify [Inline]
alias]
                 then [Inline] -> JiraConverter m [Inline]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Inline]
forall a. Monoid a => a
mempty
                 else [Inline] -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
[Inline] -> JiraConverter m [Inline]
toJiraInlines [Inline]
alias
  [Inline] -> JiraConverter m [Inline]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Inline] -> JiraConverter m [Inline])
-> (Inline -> [Inline]) -> Inline -> JiraConverter m [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inline -> [Inline]
forall a. a -> [a]
singleton (Inline -> JiraConverter m [Inline])
-> Inline -> JiraConverter m [Inline]
forall a b. (a -> b) -> a -> b
$ LinkType -> [Inline] -> URL -> Inline
Jira.Link LinkType
linkType [Inline]
description (Text -> URL
Jira.URL Text
url')
 where
  toLinkType :: Text -> (LinkType, Text)
toLinkType Text
url'
    | Just Text
email <- Text -> Text -> Maybe Text
T.stripPrefix Text
"mailto:" Text
url' = (LinkType
Jira.Email, Text
email)
    | Text
"user-account" Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
classes              = (LinkType
Jira.User, Text -> Text
dropTilde Text
url)
    | Text
"attachment" Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
classes                = (LinkType
Jira.Attachment, Text
url)
    | Text
"smart-card" Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
classes                = (LinkType
Jira.SmartCard, Text
url)
    | Text
"smart-link" Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
classes                = (LinkType
Jira.SmartLink, Text
url)
    | Bool
otherwise                                  = (LinkType
Jira.External, Text
url)
  dropTilde :: Text -> Text
dropTilde Text
txt = case Text -> Maybe (Char, Text)
T.uncons Text
txt of
    Just (Char
'~', Text
username) -> Text
username
    Maybe (Char, Text)
_                    -> Text
txt

mathToJira :: PandocMonad m
           => MathType
           -> Text
           -> JiraConverter m [Jira.Inline]
mathToJira :: forall (m :: * -> *).
PandocMonad m =>
MathType -> Text -> JiraConverter m [Inline]
mathToJira MathType
mtype Text
cs = do
  [Inline]
mathInlines <- [Inline] -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
[Inline] -> JiraConverter m [Inline]
toJiraInlines ([Inline] -> JiraConverter m [Inline])
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
-> JiraConverter m [Inline]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MathType
-> Text -> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall (m :: * -> *).
PandocMonad m =>
MathType -> Text -> m [Inline]
texMathToInlines MathType
mtype Text
cs
  [Inline] -> JiraConverter m [Inline]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Inline] -> JiraConverter m [Inline])
-> [Inline] -> JiraConverter m [Inline]
forall a b. (a -> b) -> a -> b
$ case MathType
mtype of
    MathType
InlineMath  -> [Inline]
mathInlines
    MathType
DisplayMath -> Inline
Jira.Linebreak Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline]
mathInlines [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Inline
Jira.Linebreak]

quotedToJira :: PandocMonad m
             => QuoteType
             -> [Inline]
             -> JiraConverter m [Jira.Inline]
quotedToJira :: forall (m :: * -> *).
PandocMonad m =>
QuoteType -> [Inline] -> JiraConverter m [Inline]
quotedToJira QuoteType
qtype [Inline]
xs = do
  let quoteChar :: Text
quoteChar = case QuoteType
qtype of
                    QuoteType
DoubleQuote -> Text
"\""
                    QuoteType
SingleQuote -> Text
"'"
  let surroundWithQuotes :: [Inline] -> [Inline]
surroundWithQuotes = (Text -> Inline
Jira.Str Text
quoteChar Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:) ([Inline] -> [Inline])
-> ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Text -> Inline
Jira.Str Text
quoteChar])
  [Inline] -> [Inline]
surroundWithQuotes ([Inline] -> [Inline])
-> JiraConverter m [Inline] -> JiraConverter m [Inline]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Inline] -> JiraConverter m [Inline]
forall (m :: * -> *).
PandocMonad m =>
[Inline] -> JiraConverter m [Inline]
toJiraInlines [Inline]
xs

spanToJira :: PandocMonad m
           => Attr -> [Inline]
           -> JiraConverter m [Jira.Inline]
spanToJira :: forall (m :: * -> *).
PandocMonad m =>
Attr -> [Inline] -> JiraConverter m [Inline]
spanToJira (Text
ident, [Text]
_classes, [(Text, Text)]
attribs) [Inline]
inls =
  let wrap :: [Inline] -> [Inline]
wrap = case Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
"color" [(Text, Text)]
attribs of
               Maybe Text
Nothing -> [Inline] -> [Inline]
forall a. a -> a
id
               Just Text
color -> Inline -> [Inline]
forall a. a -> [a]
singleton (Inline -> [Inline])
-> ([Inline] -> Inline) -> [Inline] -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ColorName -> [Inline] -> Inline
Jira.ColorInline (Text -> ColorName
Jira.ColorName Text
color)
  in [Inline] -> [Inline]
wrap ([Inline] -> [Inline])
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case Text
ident of
    Text
"" -> [Inline] -> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall (m :: * -> *).
PandocMonad m =>
[Inline] -> JiraConverter m [Inline]
toJiraInlines [Inline]
inls
    Text
_  -> (Text -> Inline
Jira.Anchor Text
ident Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:) ([Inline] -> [Inline])
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
-> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Inline] -> ReaderT WrapOption (StateT ConverterState m) [Inline]
forall (m :: * -> *).
PandocMonad m =>
[Inline] -> JiraConverter m [Inline]
toJiraInlines [Inline]
inls

registerNotes :: PandocMonad m => [Block] -> JiraConverter m [Jira.Inline]
registerNotes :: forall (m :: * -> *).
PandocMonad m =>
[Block] -> JiraConverter m [Inline]
registerNotes [Block]
contents = do
  [Text]
curNotes <- (ConverterState -> [Text])
-> ReaderT WrapOption (StateT ConverterState m) [Text]
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ConverterState -> [Text]
stNotes
  let newnum :: Int
newnum = [Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
curNotes Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
  Text
contents' <- [Block] -> JiraConverter m Text
forall (m :: * -> *).
PandocMonad m =>
[Block] -> JiraConverter m Text
blockListToJira [Block]
contents
  let thisnote :: Text
thisnote = Text
"\\[" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (Int -> String
forall a. Show a => a -> String
show Int
newnum) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"] " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
contents' Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n"
  (ConverterState -> ConverterState)
-> ReaderT WrapOption (StateT ConverterState m) ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((ConverterState -> ConverterState)
 -> ReaderT WrapOption (StateT ConverterState m) ())
-> (ConverterState -> ConverterState)
-> ReaderT WrapOption (StateT ConverterState m) ()
forall a b. (a -> b) -> a -> b
$ \ConverterState
s -> ConverterState
s { stNotes :: [Text]
stNotes = Text
thisnote Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text]
curNotes }
  [Inline] -> JiraConverter m [Inline]
forall a. a -> ReaderT WrapOption (StateT ConverterState m) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Inline] -> JiraConverter m [Inline])
-> (Text -> [Inline]) -> Text -> JiraConverter m [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Inline -> [Inline]
forall a. a -> [a]
singleton (Inline -> [Inline]) -> (Text -> Inline) -> Text -> [Inline]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Inline
Jira.Str (Text -> JiraConverter m [Inline])
-> Text -> JiraConverter m [Inline]
forall a b. (a -> b) -> a -> b
$
    Text
"[" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (Int -> String
forall a. Show a => a -> String
show Int
newnum) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"]"