{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Text.Pandoc.Writers.AsciiDoc
   Copyright   : Copyright (C) 2006-2024 John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley.edu>
   Stability   : alpha
   Portability : portable

Conversion of 'Pandoc' documents to asciidoc.

Note that some information may be lost in conversion, due to
expressive limitations of asciidoc.  Footnotes and table cells with
paragraphs (or other block items) are not possible in asciidoc.
If pandoc encounters one of these, it will insert a message indicating
that it has omitted the construct.

AsciiDoc:  <http://www.methods.co.nz/asciidoc/>
-}
module Text.Pandoc.Writers.AsciiDoc (
  writeAsciiDoc,
  writeAsciiDocLegacy,
  writeAsciiDoctor
  ) where
import Control.Monad (foldM)
import Control.Monad.State.Strict
    ( StateT, MonadState(get), gets, modify, evalStateT )
import Data.Char (isPunctuation, isSpace)
import Data.List (delete, intercalate, intersperse)
import Data.List.NonEmpty (NonEmpty(..))
import Data.Maybe (fromMaybe, isJust)
import qualified Data.Set as Set
import qualified Data.Text as T
import Data.Text (Text)
import System.FilePath (dropExtension)
import Text.Pandoc.Class.PandocMonad (PandocMonad, report)
import Text.Pandoc.Definition
import Text.Pandoc.ImageSize
import Text.Pandoc.Logging
import Text.Pandoc.Options
import Text.Pandoc.Parsing hiding (blankline, space)
import Text.DocLayout
import Text.Pandoc.Shared
import Text.Pandoc.URI
import Text.Pandoc.Templates (renderTemplate)
import Text.Pandoc.Writers.Shared
import Text.Pandoc.Walk (walk)

data WriterState = WriterState { WriterState -> Text
defListMarker       :: Text
                               , WriterState -> Int
orderedListLevel    :: Int
                               , WriterState -> Int
bulletListLevel     :: Int
                               , WriterState -> Bool
intraword           :: Bool
                               , WriterState -> Set Text
autoIds             :: Set.Set Text
                               , WriterState -> Bool
legacy              :: Bool
                               , WriterState -> Bool
inList              :: Bool
                               , WriterState -> Bool
hasMath             :: Bool
                               -- |0 is no table
                               -- 1 is top level table
                               -- 2 is a table in a table
                               , WriterState -> Int
tableNestingLevel   :: Int
                               }

defaultWriterState :: WriterState
defaultWriterState :: WriterState
defaultWriterState = WriterState { defListMarker :: Text
defListMarker      = Text
"::"
                                 , orderedListLevel :: Int
orderedListLevel   = Int
0
                                 , bulletListLevel :: Int
bulletListLevel    = Int
0
                                 , intraword :: Bool
intraword          = Bool
False
                                 , autoIds :: Set Text
autoIds            = Set Text
forall a. Set a
Set.empty
                                 , legacy :: Bool
legacy             = Bool
False
                                 , inList :: Bool
inList             = Bool
False
                                 , hasMath :: Bool
hasMath            = Bool
False
                                 , tableNestingLevel :: Int
tableNestingLevel  = Int
0
                                 }

-- | Convert Pandoc to AsciiDoc.
writeAsciiDoc :: PandocMonad m => WriterOptions -> Pandoc -> m Text
writeAsciiDoc :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
writeAsciiDoc WriterOptions
opts Pandoc
document =
  StateT WriterState m Text -> WriterState -> m Text
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (WriterOptions -> Pandoc -> StateT WriterState m Text
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> ADW m Text
pandocToAsciiDoc WriterOptions
opts Pandoc
document) WriterState
defaultWriterState

{-# DEPRECATED writeAsciiDoctor "Use writeAsciiDoc instead" #-}
-- | Deprecated synonym of 'writeAsciiDoc'.
writeAsciiDoctor :: PandocMonad m => WriterOptions -> Pandoc -> m Text
writeAsciiDoctor :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
writeAsciiDoctor = WriterOptions -> Pandoc -> m Text
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
writeAsciiDoc

-- | Convert Pandoc to legacy AsciiDoc.
writeAsciiDocLegacy :: PandocMonad m => WriterOptions -> Pandoc -> m Text
writeAsciiDocLegacy :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
writeAsciiDocLegacy WriterOptions
opts Pandoc
document =
  StateT WriterState m Text -> WriterState -> m Text
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT (WriterOptions -> Pandoc -> StateT WriterState m Text
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> ADW m Text
pandocToAsciiDoc WriterOptions
opts Pandoc
document)
    WriterState
defaultWriterState{ legacy = True }

type ADW = StateT WriterState

-- | Return asciidoc representation of document.
pandocToAsciiDoc :: PandocMonad m => WriterOptions -> Pandoc -> ADW m Text
pandocToAsciiDoc :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> ADW m Text
pandocToAsciiDoc WriterOptions
opts (Pandoc Meta
meta [Block]
blocks) = do
  let titleblock :: Bool
titleblock = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Inline] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Meta -> [Inline]
docTitle Meta
meta) Bool -> Bool -> Bool
&& [[Inline]] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Meta -> [[Inline]]
docAuthors Meta
meta) Bool -> Bool -> Bool
&&
                         [Inline] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Meta -> [Inline]
docDate Meta
meta)
  let colwidth :: Maybe Int
colwidth = if WriterOptions -> WrapOption
writerWrapText WriterOptions
opts WrapOption -> WrapOption -> Bool
forall a. Eq a => a -> a -> Bool
== WrapOption
WrapAuto
                    then Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> Int -> Maybe Int
forall a b. (a -> b) -> a -> b
$ WriterOptions -> Int
writerColumns WriterOptions
opts
                    else Maybe Int
forall a. Maybe a
Nothing
  Context Text
metadata <- WriterOptions
-> ([Block] -> StateT WriterState m (Doc Text))
-> ([Inline] -> StateT WriterState m (Doc Text))
-> Meta
-> StateT WriterState 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
              (WriterOptions -> [Block] -> StateT WriterState m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> ADW m (Doc Text)
blockListToAsciiDoc WriterOptions
opts)
              ((Doc Text -> Doc Text)
-> StateT WriterState m (Doc Text)
-> StateT WriterState m (Doc Text)
forall a b.
(a -> b) -> StateT WriterState m a -> StateT WriterState m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Doc Text -> Doc Text
forall a. Doc a -> Doc a
chomp (StateT WriterState m (Doc Text)
 -> StateT WriterState m (Doc Text))
-> ([Inline] -> StateT WriterState m (Doc Text))
-> [Inline]
-> StateT WriterState m (Doc Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterOptions -> [Inline] -> StateT WriterState m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts)
              Meta
meta
  Doc Text
main <- WriterOptions -> [Block] -> StateT WriterState m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> ADW m (Doc Text)
blockListToAsciiDoc WriterOptions
opts ([Block] -> StateT WriterState m (Doc Text))
-> [Block] -> StateT WriterState m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Bool -> Maybe Int -> [Block] -> [Block]
makeSections Bool
False (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
1) [Block]
blocks
  WriterState
st <- StateT WriterState m WriterState
forall s (m :: * -> *). MonadState s m => m s
get
  let context :: Context Text
context  = Text -> Doc Text -> Context Text -> Context Text
forall a b. ToContext a b => Text -> b -> Context a -> Context a
defField Text
"body" Doc Text
main
               (Context Text -> Context Text) -> Context Text -> Context Text
forall a b. (a -> b) -> a -> b
$ Text -> Bool -> Context Text -> Context Text
forall a b. ToContext a b => Text -> b -> Context a -> Context a
defField Text
"toc"
                  (WriterOptions -> Bool
writerTableOfContents WriterOptions
opts Bool -> Bool -> Bool
&&
                   Maybe (Template Text) -> Bool
forall a. Maybe a -> Bool
isJust (WriterOptions -> Maybe (Template Text)
writerTemplate WriterOptions
opts))
               (Context Text -> Context Text) -> Context Text -> Context Text
forall a b. (a -> b) -> a -> b
$ Text -> Bool -> Context Text -> Context Text
forall a b. ToContext a b => Text -> b -> Context a -> Context a
defField Text
"math" (WriterState -> Bool
hasMath WriterState
st Bool -> Bool -> Bool
&& Bool -> Bool
not (WriterState -> Bool
legacy WriterState
st))
               (Context Text -> Context Text) -> Context Text -> Context Text
forall a b. (a -> b) -> a -> b
$ Text -> Bool -> Context Text -> Context Text
forall a b. ToContext a b => Text -> b -> Context a -> Context a
defField Text
"titleblock" Bool
titleblock Context Text
metadata
  Text -> ADW m Text
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> ADW m Text) -> Text -> ADW m Text
forall a b. (a -> b) -> a -> b
$ Maybe Int -> Doc Text -> Text
forall a. HasChars a => Maybe Int -> Doc a -> a
render Maybe Int
colwidth (Doc Text -> Text) -> Doc Text -> Text
forall a b. (a -> b) -> a -> b
$
    case WriterOptions -> Maybe (Template Text)
writerTemplate WriterOptions
opts of
       Maybe (Template Text)
Nothing  -> Doc Text
main
       Just Template Text
tpl -> 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

-- | Escape special characters for AsciiDoc.
escapeString :: PandocMonad m => Text -> ADW m (Doc Text)
escapeString :: forall (m :: * -> *). PandocMonad m => Text -> ADW m (Doc Text)
escapeString Text
t = do
  Int
parentTableLevel <- (WriterState -> Int) -> StateT WriterState m Int
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Int
tableNestingLevel
  let needsEscape :: Char -> Bool
needsEscape Char
'{' = Bool
True
      needsEscape Char
'|' = Int
parentTableLevel Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0
      needsEscape Char
_   = Bool
False
  let escChar :: Char -> Text
escChar Char
c | Char -> Bool
needsEscape Char
c = Text
"\\" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Char -> Text
T.singleton Char
c
                | Bool
otherwise     = Char -> Text
T.singleton Char
c
  if (Char -> Bool) -> Text -> Bool
T.any Char -> Bool
needsEscape Text
t
     then Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal (Text -> Doc Text) -> Text -> Doc Text
forall a b. (a -> b) -> a -> b
$ (Char -> Text) -> Text -> Text
T.concatMap Char -> Text
escChar Text
t
     else Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
t


-- | Ordered list start parser for use in Para below.
olMarker :: Parsec Text ParserState Char
olMarker :: Parsec Text ParserState Char
olMarker = do (Int
start, ListNumberStyle
style', ListNumberDelim
delim) <- ParsecT
  Text ParserState Identity (Int, ListNumberStyle, ListNumberDelim)
forall s (m :: * -> *).
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s ParserState m (Int, ListNumberStyle, ListNumberDelim)
anyOrderedListMarker
              if ListNumberDelim
delim ListNumberDelim -> ListNumberDelim -> Bool
forall a. Eq a => a -> a -> Bool
== ListNumberDelim
Period Bool -> Bool -> Bool
&&
                          (ListNumberStyle
style' ListNumberStyle -> ListNumberStyle -> Bool
forall a. Eq a => a -> a -> Bool
== ListNumberStyle
UpperAlpha Bool -> Bool -> Bool
|| (ListNumberStyle
style' ListNumberStyle -> ListNumberStyle -> Bool
forall a. Eq a => a -> a -> Bool
== ListNumberStyle
UpperRoman Bool -> Bool -> Bool
&&
                          Int
start Int -> [Int] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Int
1, Int
5, Int
10, Int
50, Int
100, Int
500, Int
1000]))
                          then Parsec Text ParserState Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
spaceChar Parsec Text ParserState Char
-> Parsec Text ParserState Char -> Parsec Text ParserState Char
forall a b.
ParsecT Text ParserState Identity a
-> ParsecT Text ParserState Identity b
-> ParsecT Text ParserState Identity b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Parsec Text ParserState Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
spaceChar
                          else Parsec Text ParserState Char
forall s (m :: * -> *) st.
(Stream s m Char, UpdateSourcePos s Char) =>
ParsecT s st m Char
spaceChar

-- | True if string begins with an ordered list marker
-- or would be interpreted as an AsciiDoc option command
needsEscaping :: Text -> Bool
needsEscaping :: Text -> Bool
needsEscaping Text
s = Text -> Bool
beginsWithOrderedListMarker Text
s Bool -> Bool -> Bool
|| Text -> Bool
isBracketed Text
s
  where
    beginsWithOrderedListMarker :: Text -> Bool
beginsWithOrderedListMarker Text
str =
      case Parsec Text ParserState Char
-> ParserState -> SourceName -> Text -> Either ParseError Char
forall s t u a.
Stream s Identity t =>
Parsec s u a -> u -> SourceName -> s -> Either ParseError a
runParser Parsec Text ParserState Char
olMarker ParserState
defaultParserState SourceName
"para start" (Int -> Text -> Text
T.take Int
10 Text
str) of
             Left  ParseError
_ -> Bool
False
             Right Char
_ -> Bool
True
    isBracketed :: Text -> Bool
isBracketed Text
t
      | Just (Char
'[', Text
t') <- Text -> Maybe (Char, Text)
T.uncons Text
t
      , Just (Text
_, Char
']')  <- Text -> Maybe (Text, Char)
T.unsnoc Text
t'
      = Bool
True
      | Bool
otherwise = Bool
False

-- | Convert Pandoc block element to asciidoc.
blockToAsciiDoc :: PandocMonad m
                => WriterOptions -- ^ Options
                -> Block         -- ^ Block element
                -> ADW m (Doc Text)
blockToAsciiDoc :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> ADW m (Doc Text)
blockToAsciiDoc WriterOptions
opts (Div (Text
id',Text
"section":[Text]
_,[(Text, Text)]
_)
                       (Header Int
level (Text
_,[Text]
cls,[(Text, Text)]
kvs) [Inline]
ils : [Block]
xs)) = do
  Doc Text
hdr <- WriterOptions -> Block -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> ADW m (Doc Text)
blockToAsciiDoc WriterOptions
opts (Int -> (Text, [Text], [(Text, Text)]) -> [Inline] -> Block
Header Int
level (Text
id',[Text]
cls,[(Text, Text)]
kvs) [Inline]
ils)
  Doc Text
rest <- WriterOptions -> [Block] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> ADW m (Doc Text)
blockListToAsciiDoc WriterOptions
opts [Block]
xs
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
hdr Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
rest
blockToAsciiDoc WriterOptions
opts (Plain [Inline]
inlines) = do
  Doc Text
contents <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
inlines
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
blankline
blockToAsciiDoc WriterOptions
opts (Para [Inline]
inlines) = do
  Doc Text
contents <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
inlines
  -- escape if para starts with ordered list marker
  let esc :: Doc Text
esc = if Text -> Bool
needsEscaping (Maybe Int -> Doc Text -> Text
forall a. HasChars a => Maybe Int -> Doc a -> a
render Maybe Int
forall a. Maybe a
Nothing Doc Text
contents)
               then SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"{empty}"
               else Doc Text
forall a. Doc a
empty
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
esc Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
blankline
blockToAsciiDoc WriterOptions
opts (LineBlock [[Inline]]
lns) = do
  let docify :: [Inline] -> StateT WriterState m (Doc Text)
docify [Inline]
line = if [Inline] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
line
                    then Doc Text -> StateT WriterState m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
forall a. Doc a
blankline
                    else WriterOptions -> [Inline] -> StateT WriterState m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
line
  let joinWithLinefeeds :: [Doc Text] -> Doc Text
joinWithLinefeeds = Doc Text -> Doc Text
forall a. IsString a => Doc a -> Doc a
nowrap (Doc Text -> Doc Text)
-> ([Doc Text] -> Doc Text) -> [Doc Text] -> Doc Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Doc Text] -> Doc Text
forall a. Monoid a => [a] -> a
mconcat ([Doc Text] -> Doc Text)
-> ([Doc Text] -> [Doc Text]) -> [Doc Text] -> Doc Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc Text -> [Doc Text] -> [Doc Text]
forall a. a -> [a] -> [a]
intersperse Doc Text
forall a. Doc a
cr
  Doc Text
contents <- [Doc Text] -> Doc Text
joinWithLinefeeds ([Doc Text] -> Doc Text)
-> StateT WriterState m [Doc Text] -> ADW m (Doc Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Inline] -> ADW m (Doc Text))
-> [[Inline]] -> StateT WriterState m [Doc Text]
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] -> ADW m (Doc Text)
forall {m :: * -> *}.
PandocMonad m =>
[Inline] -> StateT WriterState m (Doc Text)
docify [[Inline]]
lns
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
"[verse]" Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"--" Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"--" Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
forall a. Doc a
blankline
blockToAsciiDoc WriterOptions
_ b :: Block
b@(RawBlock Format
f Text
s)
  | Format
f Format -> Format -> Bool
forall a. Eq a => a -> a -> Bool
== Format
"asciidoc" = Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
s
  | Bool
otherwise         = do
      LogMessage -> StateT WriterState m ()
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report (LogMessage -> StateT WriterState m ())
-> LogMessage -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ Block -> LogMessage
BlockNotRendered Block
b
      Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
forall a. Doc a
empty
blockToAsciiDoc WriterOptions
_ Block
HorizontalRule =
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
forall a. Doc a
blankline Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"'''''" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
blankline
blockToAsciiDoc WriterOptions
opts (Header Int
level (Text
ident,[Text]
_,[(Text, Text)]
_) [Inline]
inlines) = do
  Doc Text
contents <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
inlines
  Set Text
ids <- (WriterState -> Set Text) -> StateT WriterState m (Set Text)
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Set Text
autoIds
  let autoId :: Text
autoId = Extensions -> [Inline] -> Set Text -> Text
uniqueIdent (WriterOptions -> Extensions
writerExtensions WriterOptions
opts) [Inline]
inlines Set Text
ids
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ autoIds = Set.insert autoId ids }
  let identifier :: Doc Text
identifier = if Text -> Bool
T.null Text
ident Bool -> Bool -> Bool
||
                      (Extension -> WriterOptions -> Bool
forall a. HasSyntaxExtensions a => Extension -> a -> Bool
isEnabled Extension
Ext_auto_identifiers WriterOptions
opts Bool -> Bool -> Bool
&& Text
ident Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
autoId)
                      then Doc Text
forall a. Doc a
empty
                      else Doc Text
"[[" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
ident Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"]]"
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
identifier Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$
           Doc Text -> Doc Text
forall a. IsString a => Doc a -> Doc a
nowrap (SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text (Int -> Char -> SourceName
forall a. Int -> a -> [a]
replicate (Int
level Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Char
'=') Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
space Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
contents) Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<>
           Doc Text
forall a. Doc a
blankline
blockToAsciiDoc WriterOptions
opts (Figure (Text, [Text], [(Text, Text)])
attr (Caption Maybe [Inline]
_ [Block]
longcapt) [Block]
body) = do
  -- Images in figures all get rendered as individual block-level images
  -- with the given caption. Non-image elements are rendered unchanged.
  Doc Text
capt <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts ([Block] -> [Inline]
blocksToInlines [Block]
longcapt)
  let renderFigElement :: Block -> ADW m (Doc Text)
renderFigElement = \case
        Plain [Image (Text, [Text], [(Text, Text)])
imgAttr [Inline]
alternate (Text
src, Text
tit)] -> do
          Doc Text
args <- WriterOptions
-> (Text, [Text], [(Text, Text)])
-> [Inline]
-> Text
-> Text
-> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions
-> (Text, [Text], [(Text, Text)])
-> [Inline]
-> Text
-> Text
-> ADW m (Doc Text)
imageArguments WriterOptions
opts (Text, [Text], [(Text, Text)])
imgAttr [Inline]
alternate Text
src Text
tit
          let figAttributes :: Doc Text
figAttributes = case (Text, [Text], [(Text, Text)])
attr of
                (Text
"", [Text]
_, [(Text, Text)]
_)    -> Doc Text
forall a. Doc a
empty
                (Text
ident, [Text]
_, [(Text, Text)]
_) -> Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal (Text -> Doc Text) -> Text -> Doc Text
forall a b. (a -> b) -> a -> b
$ Text
"[#" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
ident Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"]"
          -- .Figure caption
          -- image::images/logo.png[Company logo, title="blah"]
          Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
"." Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text -> Doc Text
forall a. IsString a => Doc a -> Doc a
nowrap Doc Text
capt Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$
            Doc Text
figAttributes Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$
            Doc Text
"image::" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
args Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
blankline
        Block
blk -> WriterOptions -> Block -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> ADW m (Doc Text)
blockToAsciiDoc WriterOptions
opts Block
blk
  [Doc Text] -> Doc Text
forall a. [Doc a] -> Doc a
vcat ([Doc Text] -> Doc Text)
-> StateT WriterState m [Doc Text] -> ADW m (Doc Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Block -> ADW m (Doc Text))
-> [Block] -> StateT WriterState m [Doc Text]
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 -> ADW m (Doc Text)
renderFigElement [Block]
body
blockToAsciiDoc WriterOptions
_ (CodeBlock (Text
_,[Text]
classes,[(Text, Text)]
_) Text
str) = Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text -> Doc Text
forall a. Doc a -> Doc a
flush (
  if [Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
classes
     then Doc Text
"...." Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
str Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
"...."
     else Doc Text
attrs Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
"----" Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
str Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
"----")
  Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
blankline
    where attrs :: Doc Text
attrs = Doc Text
"[" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal (Text -> [Text] -> Text
T.intercalate Text
"," [Text]
classes') Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"]"
          classes' :: [Text]
classes' = if Text
"numberLines" 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
"source%linesnum" Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: Text -> [Text] -> [Text]
forall a. Eq a => a -> [a] -> [a]
delete Text
"numberLines" [Text]
classes
                        else Text
"source" Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: [Text]
classes
blockToAsciiDoc WriterOptions
opts (BlockQuote [Block]
blocks) = do
  Doc Text
contents <- WriterOptions -> [Block] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> ADW m (Doc Text)
blockListToAsciiDoc WriterOptions
opts [Block]
blocks
  let isBlock :: Block -> Bool
isBlock (BlockQuote [Block]
_) = Bool
True
      isBlock Block
_              = Bool
False
  -- if there are nested block quotes, put in an open block
  let contents' :: Doc Text
contents' = if (Block -> Bool) -> [Block] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Block -> Bool
isBlock [Block]
blocks
                     then Doc Text
"--" Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
"--"
                     else Doc Text
contents
  let bar :: Doc Text
bar = SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"____"
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
bar Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text -> Doc Text
forall a. Doc a -> Doc a
chomp Doc Text
contents' Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
bar Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
blankline
blockToAsciiDoc WriterOptions
opts block :: Block
block@(Table (Text, [Text], [(Text, Text)])
_ Caption
blkCapt [ColSpec]
specs TableHead
thead [TableBody]
tbody TableFoot
tfoot) = do
  let ([Inline]
caption, [Alignment]
aligns, [Double]
widths, [[Block]]
headers, [[[Block]]]
rows) =
        Caption
-> [ColSpec]
-> TableHead
-> [TableBody]
-> TableFoot
-> ([Inline], [Alignment], [Double], [[Block]], [[[Block]]])
toLegacyTable Caption
blkCapt [ColSpec]
specs TableHead
thead [TableBody]
tbody TableFoot
tfoot
  Doc Text
caption' <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
caption
  let caption'' :: Doc Text
caption'' = if [Inline] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
caption
                     then Doc Text
forall a. Doc a
empty
                     else Doc Text
"." Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
caption' Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr
  let isSimple :: Bool
isSimple = (Double -> Bool) -> [Double] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
0) [Double]
widths
  let relativePercentWidths :: [Double]
relativePercentWidths = if Bool
isSimple
                                 then [Double]
widths
                                 else (Double -> Double) -> [Double] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map (Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ [Double] -> Double
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Double]
widths) [Double]
widths
  let widths'' :: [Integer]
      widths'' :: [Integer]
widths'' = (Double -> Integer) -> [Double] -> [Integer]
forall a b. (a -> b) -> [a] -> [b]
map (Double -> Integer
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (Double -> Integer) -> (Double -> Double) -> Double -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
100)) [Double]
relativePercentWidths
  -- ensure that the widths sum to 100
  let widths' :: [Integer]
widths' = case [Integer]
widths'' of
                     [Integer]
_ | Bool
isSimple -> [Integer]
widths''
                     (Integer
w:[Integer]
ws) | [Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (Integer
wInteger -> [Integer] -> [Integer]
forall a. a -> [a] -> [a]
:[Integer]
ws) Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
100
                               -> (Integer
100 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- [Integer] -> Integer
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Integer]
ws) Integer -> [Integer] -> [Integer]
forall a. a -> [a] -> [a]
: [Integer]
ws
                     [Integer]
ws        -> [Integer]
ws
  let totalwidth :: Integer
      totalwidth :: Integer
totalwidth = Double -> Integer
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor (Double -> Integer) -> Double -> Integer
forall a b. (a -> b) -> a -> b
$ [Double] -> Double
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Double]
widths Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
100
  let colspec :: Alignment -> a -> SourceName
colspec Alignment
al a
wi = (case Alignment
al of
                         Alignment
AlignLeft    -> SourceName
"<"
                         Alignment
AlignCenter  -> SourceName
"^"
                         Alignment
AlignRight   -> SourceName
">"
                         Alignment
AlignDefault -> SourceName
"") SourceName -> SourceName -> SourceName
forall a. [a] -> [a] -> [a]
++
                      if a
wi a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0 then SourceName
"" else a -> SourceName
forall a. Show a => a -> SourceName
show a
wi SourceName -> SourceName -> SourceName
forall a. [a] -> [a] -> [a]
++ SourceName
"%"
  let headerspec :: Doc Text
headerspec = 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]]
headers
                      then Doc Text
forall a. Doc a
empty
                      else SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"options=\"header\","
  let widthspec :: Doc Text
widthspec = if Integer
totalwidth Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0
                     then Doc Text
forall a. Doc a
empty
                     else SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"width="
                          Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text -> Doc Text
forall a. HasChars a => Doc a -> Doc a
doubleQuotes (SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text (SourceName -> Doc Text) -> SourceName -> Doc Text
forall a b. (a -> b) -> a -> b
$ Integer -> SourceName
forall a. Show a => a -> SourceName
show Integer
totalwidth SourceName -> SourceName -> SourceName
forall a. [a] -> [a] -> [a]
++ SourceName
"%")
                          Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
","
  let tablespec :: Doc Text
tablespec = SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"["
         Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
widthspec
         Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"cols="
         Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text -> Doc Text
forall a. HasChars a => Doc a -> Doc a
doubleQuotes (SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text (SourceName -> Doc Text) -> SourceName -> Doc Text
forall a b. (a -> b) -> a -> b
$ SourceName -> [SourceName] -> SourceName
forall a. [a] -> [[a]] -> [a]
intercalate SourceName
","
             ([SourceName] -> SourceName) -> [SourceName] -> SourceName
forall a b. (a -> b) -> a -> b
$ (Alignment -> Integer -> SourceName)
-> [Alignment] -> [Integer] -> [SourceName]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Alignment -> Integer -> SourceName
forall {a}. (Eq a, Num a, Show a) => Alignment -> a -> SourceName
colspec [Alignment]
aligns [Integer]
widths')
         Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
","
         Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
headerspec Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"]"

  -- construct cells and recurse in case of nested tables
  Int
parentTableLevel <- (WriterState -> Int) -> StateT WriterState m Int
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Int
tableNestingLevel
  let currentNestingLevel :: Int
currentNestingLevel = Int
parentTableLevel Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1

  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ tableNestingLevel = currentNestingLevel }

  let separator :: Doc Text
separator = SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text (if Int
parentTableLevel Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
                          then SourceName
"|"  -- top level separator
                          else SourceName
"!") -- nested separator

  let makeCell :: [Block] -> StateT WriterState m (Doc Text)
makeCell [Plain [Inline]
x] = do Doc Text
d <- WriterOptions -> [Block] -> StateT WriterState m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> ADW m (Doc Text)
blockListToAsciiDoc WriterOptions
opts [[Inline] -> Block
Plain [Inline]
x]
                              Doc Text -> StateT WriterState m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> StateT WriterState m (Doc Text))
-> Doc Text -> StateT WriterState m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
separator Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text -> Doc Text
forall a. Doc a -> Doc a
chomp Doc Text
d
      makeCell [Para [Inline]
x]  = [Block] -> StateT WriterState m (Doc Text)
makeCell [[Inline] -> Block
Plain [Inline]
x]
      makeCell []        = Doc Text -> StateT WriterState m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
separator
      makeCell [Block]
bs        = if Int
currentNestingLevel Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
2
                             then do
                               --asciidoc only supports nesting once
                               LogMessage -> StateT WriterState m ()
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report (LogMessage -> StateT WriterState m ())
-> LogMessage -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ Block -> LogMessage
BlockNotRendered Block
block
                               Doc Text -> StateT WriterState m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
separator
                             else do
                               Doc Text
d <- WriterOptions -> [Block] -> StateT WriterState m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> ADW m (Doc Text)
blockListToAsciiDoc WriterOptions
opts [Block]
bs
                               Doc Text -> StateT WriterState m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> StateT WriterState m (Doc Text))
-> Doc Text -> StateT WriterState m (Doc Text)
forall a b. (a -> b) -> a -> b
$ (SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"a" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
separator) Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
d

  let makeRow :: [[Block]] -> StateT WriterState m (Doc Text)
makeRow [[Block]]
cells = [Doc Text] -> Doc Text
forall a. [Doc a] -> Doc a
hsep ([Doc Text] -> Doc Text)
-> StateT WriterState m [Doc Text]
-> StateT WriterState m (Doc Text)
forall a b.
(a -> b) -> StateT WriterState m a -> StateT WriterState m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` ([Block] -> StateT WriterState m (Doc Text))
-> [[Block]] -> StateT WriterState m [Doc Text]
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] -> StateT WriterState m (Doc Text)
forall {m :: * -> *}.
PandocMonad m =>
[Block] -> StateT WriterState m (Doc Text)
makeCell [[Block]]
cells
  [Doc Text]
rows' <- ([[Block]] -> ADW m (Doc Text))
-> [[[Block]]] -> StateT WriterState m [Doc Text]
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]] -> ADW m (Doc Text)
forall {m :: * -> *}.
PandocMonad m =>
[[Block]] -> StateT WriterState m (Doc Text)
makeRow [[[Block]]]
rows
  Doc Text
head' <- [[Block]] -> ADW m (Doc Text)
forall {m :: * -> *}.
PandocMonad m =>
[[Block]] -> StateT WriterState m (Doc Text)
makeRow [[Block]]
headers
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ tableNestingLevel = parentTableLevel }
  let head'' :: Doc Text
head'' = 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]]
headers then Doc Text
forall a. Doc a
empty else Doc Text
head'
  let colwidth :: Int
colwidth = if WriterOptions -> WrapOption
writerWrapText WriterOptions
opts WrapOption -> WrapOption -> Bool
forall a. Eq a => a -> a -> Bool
== WrapOption
WrapAuto
                    then WriterOptions -> Int
writerColumns WriterOptions
opts
                    else Int
100000
  let maxwidth :: Int
maxwidth = NonEmpty Int -> Int
forall a. Ord a => NonEmpty a -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (NonEmpty Int -> Int) -> NonEmpty Int -> Int
forall a b. (a -> b) -> a -> b
$ (Doc Text -> Int) -> NonEmpty (Doc Text) -> NonEmpty Int
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Doc Text -> Int
forall a. (IsString a, HasChars a) => Doc a -> Int
offset (Doc Text
head' Doc Text -> [Doc Text] -> NonEmpty (Doc Text)
forall a. a -> [a] -> NonEmpty a
:| [Doc Text]
rows')
  let body :: Doc Text
body = if Int
maxwidth Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
colwidth then [Doc Text] -> Doc Text
forall a. [Doc a] -> Doc a
vsep [Doc Text]
rows' else [Doc Text] -> Doc Text
forall a. [Doc a] -> Doc a
vcat [Doc Text]
rows'
  let border :: Doc Text
border = Doc Text
separator Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"==="
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$
    Doc Text
caption'' Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
tablespec Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
border Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
head'' Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
body Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
border Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
forall a. Doc a
blankline
blockToAsciiDoc WriterOptions
opts (BulletList [[Block]]
items) = do
  Bool
inlist <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
inList
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ inList = True }
  [Doc Text]
contents <- ([Block] -> ADW m (Doc Text))
-> [[Block]] -> StateT WriterState m [Doc Text]
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 (WriterOptions -> [Block] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> ADW m (Doc Text)
bulletListItemToAsciiDoc WriterOptions
opts) [[Block]]
items
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ inList = inlist }
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ [Doc Text] -> Doc Text
forall a. Monoid a => [a] -> a
mconcat [Doc Text]
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
blankline
blockToAsciiDoc WriterOptions
opts (OrderedList (Int
start, ListNumberStyle
sty, ListNumberDelim
_delim) [[Block]]
items) = do
  let listStyle :: [Text]
listStyle = case ListNumberStyle
sty of
                       ListNumberStyle
DefaultStyle -> []
                       ListNumberStyle
Decimal      -> [Text
"arabic"]
                       ListNumberStyle
Example      -> []
                       ListNumberStyle
_            -> [Text -> Text
T.toLower (ListNumberStyle -> Text
forall a. Show a => a -> Text
tshow ListNumberStyle
sty)]
  let listStart :: [Text]
listStart = [Text
"start=" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
tshow Int
start | Int
start Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
1]
  let listoptions :: Doc Text
listoptions = case Text -> [Text] -> Text
T.intercalate Text
", " ([Text]
listStyle [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Text]
listStart) of
                          Text
"" -> Doc Text
forall a. Doc a
empty
                          Text
x  -> Doc Text -> Doc Text
forall a. HasChars a => Doc a -> Doc a
brackets (Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
x)
  Bool
inlist <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
inList
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ inList = True }
  [Doc Text]
contents <- ([Block] -> ADW m (Doc Text))
-> [[Block]] -> StateT WriterState m [Doc Text]
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 (WriterOptions -> [Block] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> ADW m (Doc Text)
orderedListItemToAsciiDoc WriterOptions
opts) [[Block]]
items
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ inList = inlist }
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
listoptions Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ [Doc Text] -> Doc Text
forall a. Monoid a => [a] -> a
mconcat [Doc Text]
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
blankline
blockToAsciiDoc WriterOptions
opts (DefinitionList [([Inline], [[Block]])]
items) = do
  Bool
inlist <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
inList
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ inList = True }
  [Doc Text]
contents <- (([Inline], [[Block]]) -> ADW m (Doc Text))
-> [([Inline], [[Block]])] -> StateT WriterState m [Doc Text]
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 (WriterOptions -> ([Inline], [[Block]]) -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> ([Inline], [[Block]]) -> ADW m (Doc Text)
definitionListItemToAsciiDoc WriterOptions
opts) [([Inline], [[Block]])]
items
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ inList = inlist }
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ [Doc Text] -> Doc Text
forall a. Monoid a => [a] -> a
mconcat [Doc Text]
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
blankline
blockToAsciiDoc WriterOptions
opts (Div (Text
ident,[Text]
classes,[(Text, Text)]
_) [Block]
bs) = do
  let identifier :: Doc Text
identifier = if Text -> Bool
T.null Text
ident then Doc Text
forall a. Doc a
empty else Doc Text
"[[" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
ident Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"]]"
  let admonitions :: [Text]
admonitions = [Text
"attention",Text
"caution",Text
"danger",Text
"error",Text
"hint",
                     Text
"important",Text
"note",Text
"tip",Text
"warning"]
  Doc Text
contents <-
       case [Text]
classes of
         (Text
l:[Text]
_) | Text
l Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
admonitions -> do
             let ([Block]
titleBs, [Block]
bodyBs) =
                     case [Block]
bs of
                       (Div (Text
_,[Text
"title"],[(Text, Text)]
_) [Block]
ts : [Block]
rest) -> ([Block]
ts, [Block]
rest)
                       [Block]
_ -> ([], [Block]
bs)
             Doc Text
admonitionTitle <- if [Block] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Block]
titleBs Bool -> Bool -> Bool
||
                                   -- If title matches class, omit
                                   (Text -> Text
T.toLower (Text -> Text
T.strip ([Block] -> Text
forall a. Walkable Inline a => a -> Text
stringify [Block]
titleBs))) Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
l
                                   then Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
forall a. Monoid a => a
mempty
                                   else (Doc Text
"." Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<>) (Doc Text -> Doc Text) -> ADW m (Doc Text) -> ADW m (Doc Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
                                         WriterOptions -> [Block] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> ADW m (Doc Text)
blockListToAsciiDoc WriterOptions
opts [Block]
titleBs
             Doc Text
admonitionBody <- WriterOptions -> [Block] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> ADW m (Doc Text)
blockListToAsciiDoc WriterOptions
opts [Block]
bodyBs
             Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
"[" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal (Text -> Text
T.toUpper Text
l) Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"]" Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$
                      Doc Text -> Doc Text
forall a. Doc a -> Doc a
chomp Doc Text
admonitionTitle Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$
                      Doc Text
"====" Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$
                      Doc Text -> Doc Text
forall a. Doc a -> Doc a
chomp Doc Text
admonitionBody Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$
                      Doc Text
"===="
         [Text]
_ -> WriterOptions -> [Block] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> ADW m (Doc Text)
blockListToAsciiDoc WriterOptions
opts [Block]
bs
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
identifier Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
forall a. Doc a
blankline

-- | Convert bullet list item (list of blocks) to asciidoc.
bulletListItemToAsciiDoc :: PandocMonad m
                         => WriterOptions -> [Block] -> ADW m (Doc Text)
bulletListItemToAsciiDoc :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> ADW m (Doc Text)
bulletListItemToAsciiDoc WriterOptions
opts [Block]
blocks = do
  Int
lev <- (WriterState -> Int) -> StateT WriterState m Int
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Int
bulletListLevel
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
s -> WriterState
s{ bulletListLevel = lev + 1 }
  Bool
isLegacy <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
legacy
  let blocksWithTasks :: [Block]
blocksWithTasks = if Bool
isLegacy
                          then [Block]
blocks
                          else ([Block] -> [Block]
taskListItemToAsciiDoc [Block]
blocks)
  Doc Text
contents <- (Doc Text -> Block -> ADW m (Doc Text))
-> Doc Text -> [Block] -> ADW m (Doc Text)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM (WriterOptions -> Doc Text -> Block -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Doc Text -> Block -> ADW m (Doc Text)
addBlock WriterOptions
opts) Doc Text
forall a. Doc a
empty [Block]
blocksWithTasks
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
s -> WriterState
s{ bulletListLevel = lev }
  let marker :: Doc Text
marker = SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text (Int -> Char -> SourceName
forall a. Int -> a -> [a]
replicate (Int
lev Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Char
'*')
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
marker Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
" " Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> [Block] -> Doc Text
listBegin [Block]
blocksWithTasks Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<>
    Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr

-- | Convert a list item containing text starting with @U+2610 BALLOT BOX@
-- or @U+2612 BALLOT BOX WITH X@ to asciidoctor checkbox syntax (e.g. @[x]@).
taskListItemToAsciiDoc :: [Block] -> [Block]
taskListItemToAsciiDoc :: [Block] -> [Block]
taskListItemToAsciiDoc = ([Inline] -> [Inline]) -> Extensions -> [Block] -> [Block]
handleTaskListItem [Inline] -> [Inline]
toOrg Extensions
listExt
  where
    toOrg :: [Inline] -> [Inline]
toOrg (Str Text
"☐" : Inline
Space : [Inline]
is) = Text -> Inline
Str Text
"[ ]" Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: Inline
Space Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline]
is
    toOrg (Str Text
"☒" : Inline
Space : [Inline]
is) = Text -> Inline
Str Text
"[x]" Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: Inline
Space Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
: [Inline]
is
    toOrg [Inline]
is = [Inline]
is
    listExt :: Extensions
listExt = [Extension] -> Extensions
extensionsFromList [Extension
Ext_task_lists]

addBlock :: PandocMonad m
         => WriterOptions -> Doc Text -> Block -> ADW m (Doc Text)
addBlock :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Doc Text -> Block -> ADW m (Doc Text)
addBlock WriterOptions
opts Doc Text
d Block
b = do
  Doc Text
x <- Doc Text -> Doc Text
forall a. Doc a -> Doc a
chomp (Doc Text -> Doc Text) -> ADW m (Doc Text) -> ADW m (Doc Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> WriterOptions -> Block -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> ADW m (Doc Text)
blockToAsciiDoc WriterOptions
opts Block
b
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$
    case Block
b of
        BulletList{} -> Doc Text
d Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
x
        OrderedList{} -> Doc Text
d Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
x
        Para (Math MathType
DisplayMath Text
_:[Inline]
_) -> Doc Text
d Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
x
        Plain (Math MathType
DisplayMath Text
_:[Inline]
_) -> Doc Text
d Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
x
        Para{} | Doc Text -> Bool
forall a. Doc a -> Bool
isEmpty Doc Text
d -> Doc Text
x
        Plain{} | Doc Text -> Bool
forall a. Doc a -> Bool
isEmpty Doc Text
d -> Doc Text
x
        Block
_ -> Doc Text
d Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"+" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
x

listBegin :: [Block] -> Doc Text
listBegin :: [Block] -> Doc Text
listBegin [Block]
blocks =
        case [Block]
blocks of
          Para (Math MathType
DisplayMath Text
_:[Inline]
_) : [Block]
_  -> Doc Text
"{blank}"
          Plain (Math MathType
DisplayMath Text
_:[Inline]
_) : [Block]
_ -> Doc Text
"{blank}"
          Para [Inline]
_ : [Block]
_                       -> Doc Text
forall a. Doc a
empty
          Plain [Inline]
_ : [Block]
_                      -> Doc Text
forall a. Doc a
empty
          Block
_ : [Block]
_                            -> Doc Text
"{blank}"
          []                               -> Doc Text
"{blank}"

-- | Convert ordered list item (a list of blocks) to asciidoc.
orderedListItemToAsciiDoc :: PandocMonad m
                          => WriterOptions -- ^ options
                          -> [Block]       -- ^ list item (list of blocks)
                          -> ADW m (Doc Text)
orderedListItemToAsciiDoc :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> ADW m (Doc Text)
orderedListItemToAsciiDoc WriterOptions
opts [Block]
blocks = do
  Int
lev <- (WriterState -> Int) -> StateT WriterState m Int
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Int
orderedListLevel
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
s -> WriterState
s{ orderedListLevel = lev + 1 }
  Doc Text
contents <- (Doc Text -> Block -> ADW m (Doc Text))
-> Doc Text -> [Block] -> ADW m (Doc Text)
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM (WriterOptions -> Doc Text -> Block -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Doc Text -> Block -> ADW m (Doc Text)
addBlock WriterOptions
opts) Doc Text
forall a. Doc a
empty [Block]
blocks
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
s -> WriterState
s{ orderedListLevel = lev }
  let marker :: Doc Text
marker = SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text (Int -> Char -> SourceName
forall a. Int -> a -> [a]
replicate (Int
lev Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Char
'.')
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
marker Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
" " Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> [Block] -> Doc Text
listBegin [Block]
blocks Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr

-- | Convert definition list item (label, list of blocks) to asciidoc.
definitionListItemToAsciiDoc :: PandocMonad m
                             => WriterOptions
                             -> ([Inline],[[Block]])
                             -> ADW m (Doc Text)
definitionListItemToAsciiDoc :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> ([Inline], [[Block]]) -> ADW m (Doc Text)
definitionListItemToAsciiDoc WriterOptions
opts ([Inline]
label, [[Block]]
defs) = do
  Doc Text
labelText <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
label
  Text
marker <- (WriterState -> Text) -> StateT WriterState m Text
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Text
defListMarker
  if Text
marker Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"::"
     then (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\WriterState
st -> WriterState
st{ defListMarker = ";;"})
     else (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\WriterState
st -> WriterState
st{ defListMarker = "::"})
  let divider :: Doc Text
divider = Doc Text
forall a. Doc a
cr Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"+" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr
  let defsToAsciiDoc :: PandocMonad m => [Block] -> ADW m (Doc Text)
      defsToAsciiDoc :: forall {m :: * -> *}.
PandocMonad m =>
[Block] -> StateT WriterState m (Doc Text)
defsToAsciiDoc [Block]
ds = ([Doc Text] -> Doc Text
forall a. [Doc a] -> Doc a
vcat ([Doc Text] -> Doc Text)
-> ([Doc Text] -> [Doc Text]) -> [Doc Text] -> Doc Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc Text -> [Doc Text] -> [Doc Text]
forall a. a -> [a] -> [a]
intersperse Doc Text
divider ([Doc Text] -> [Doc Text])
-> ([Doc Text] -> [Doc Text]) -> [Doc Text] -> [Doc Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Doc Text -> Doc Text) -> [Doc Text] -> [Doc Text]
forall a b. (a -> b) -> [a] -> [b]
map Doc Text -> Doc Text
forall a. Doc a -> Doc a
chomp)
           ([Doc Text] -> Doc Text)
-> StateT WriterState m [Doc Text]
-> StateT WriterState m (Doc Text)
forall a b.
(a -> b) -> StateT WriterState m a -> StateT WriterState m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` (Block -> StateT WriterState m (Doc Text))
-> [Block] -> StateT WriterState m [Doc Text]
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 (WriterOptions -> Block -> StateT WriterState m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> ADW m (Doc Text)
blockToAsciiDoc WriterOptions
opts) [Block]
ds
  [Doc Text]
defs' <- ([Block] -> ADW m (Doc Text))
-> [[Block]] -> StateT WriterState m [Doc Text]
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] -> ADW m (Doc Text)
forall {m :: * -> *}.
PandocMonad m =>
[Block] -> StateT WriterState m (Doc Text)
defsToAsciiDoc [[Block]]
defs
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\WriterState
st -> WriterState
st{ defListMarker = marker })
  let contents :: Doc Text
contents = Int -> Doc Text -> Doc Text
forall a. IsString a => Int -> Doc a -> Doc a
nest Int
2 (Doc Text -> Doc Text) -> Doc Text -> Doc Text
forall a b. (a -> b) -> a -> b
$ [Doc Text] -> Doc Text
forall a. [Doc a] -> Doc a
vcat ([Doc Text] -> Doc Text) -> [Doc Text] -> Doc Text
forall a b. (a -> b) -> a -> b
$ Doc Text -> [Doc Text] -> [Doc Text]
forall a. a -> [a] -> [a]
intersperse Doc Text
divider ([Doc Text] -> [Doc Text]) -> [Doc Text] -> [Doc Text]
forall a b. (a -> b) -> a -> b
$ (Doc Text -> Doc Text) -> [Doc Text] -> [Doc Text]
forall a b. (a -> b) -> [a] -> [b]
map Doc Text -> Doc Text
forall a. Doc a -> Doc a
chomp [Doc Text]
defs'
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
labelText Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
marker Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr

-- | Convert list of Pandoc block elements to asciidoc.
blockListToAsciiDoc :: PandocMonad m
                    => WriterOptions -- ^ Options
                    -> [Block]       -- ^ List of block elements
                    -> ADW m (Doc Text)
blockListToAsciiDoc :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Block] -> ADW m (Doc Text)
blockListToAsciiDoc WriterOptions
opts [Block]
blocks =
  [Doc Text] -> Doc Text
forall a. Monoid a => [a] -> a
mconcat ([Doc Text] -> Doc Text)
-> StateT WriterState m [Doc Text]
-> StateT WriterState m (Doc Text)
forall a b.
(a -> b) -> StateT WriterState m a -> StateT WriterState m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` (Block -> StateT WriterState m (Doc Text))
-> [Block] -> StateT WriterState m [Doc Text]
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 (WriterOptions -> Block -> StateT WriterState m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Block -> ADW m (Doc Text)
blockToAsciiDoc WriterOptions
opts) [Block]
blocks

data SpacyLocation = End | Start

-- | Convert list of Pandoc inline elements to asciidoc.
inlineListToAsciiDoc :: PandocMonad m =>
                        WriterOptions ->
                        [Inline] ->
                        ADW m (Doc Text)
inlineListToAsciiDoc :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
lst = do
  Bool
oldIntraword <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
intraword
  Bool -> ADW m ()
forall (m :: * -> *). PandocMonad m => Bool -> ADW m ()
setIntraword Bool
False
  Doc Text
result <- [Inline] -> ADW m (Doc Text)
forall {m :: * -> *}.
PandocMonad m =>
[Inline] -> StateT WriterState m (Doc Text)
go [Inline]
lst
  Bool -> ADW m ()
forall (m :: * -> *). PandocMonad m => Bool -> ADW m ()
setIntraword Bool
oldIntraword
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
result
 where go :: [Inline] -> StateT WriterState m (Doc Text)
go [] = Doc Text -> StateT WriterState m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
forall a. Doc a
empty
       go (Inline
y:Inline
x:[Inline]
xs)
         | Bool -> Bool
not (SpacyLocation -> Inline -> Bool
isSpacy SpacyLocation
End Inline
y) = do
           Doc Text
y' <- if SpacyLocation -> Inline -> Bool
isSpacy SpacyLocation
Start Inline
x
                    then WriterOptions -> Inline -> StateT WriterState m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> ADW m (Doc Text)
inlineToAsciiDoc WriterOptions
opts Inline
y
                    else StateT WriterState m (Doc Text) -> StateT WriterState m (Doc Text)
forall (m :: * -> *) a. PandocMonad m => ADW m a -> ADW m a
withIntraword (StateT WriterState m (Doc Text)
 -> StateT WriterState m (Doc Text))
-> StateT WriterState m (Doc Text)
-> StateT WriterState m (Doc Text)
forall a b. (a -> b) -> a -> b
$ WriterOptions -> Inline -> StateT WriterState m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> ADW m (Doc Text)
inlineToAsciiDoc WriterOptions
opts Inline
y
           Doc Text
x' <- StateT WriterState m (Doc Text) -> StateT WriterState m (Doc Text)
forall (m :: * -> *) a. PandocMonad m => ADW m a -> ADW m a
withIntraword (StateT WriterState m (Doc Text)
 -> StateT WriterState m (Doc Text))
-> StateT WriterState m (Doc Text)
-> StateT WriterState m (Doc Text)
forall a b. (a -> b) -> a -> b
$ WriterOptions -> Inline -> StateT WriterState m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> ADW m (Doc Text)
inlineToAsciiDoc WriterOptions
opts Inline
x
           Doc Text
xs' <- [Inline] -> StateT WriterState m (Doc Text)
go [Inline]
xs
           Doc Text -> StateT WriterState m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text
y' Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
x' Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
xs')
         | Bool -> Bool
not (SpacyLocation -> Inline -> Bool
isSpacy SpacyLocation
Start Inline
x) = do
           Doc Text
y' <- StateT WriterState m (Doc Text) -> StateT WriterState m (Doc Text)
forall (m :: * -> *) a. PandocMonad m => ADW m a -> ADW m a
withIntraword (StateT WriterState m (Doc Text)
 -> StateT WriterState m (Doc Text))
-> StateT WriterState m (Doc Text)
-> StateT WriterState m (Doc Text)
forall a b. (a -> b) -> a -> b
$ WriterOptions -> Inline -> StateT WriterState m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> ADW m (Doc Text)
inlineToAsciiDoc WriterOptions
opts Inline
y
           Doc Text
xs' <- [Inline] -> StateT WriterState m (Doc Text)
go (Inline
xInline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
:[Inline]
xs)
           Doc Text -> StateT WriterState m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text
y' Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
xs')
       go (Inline
x:[Inline]
xs) = do
           Doc Text
x' <- WriterOptions -> Inline -> StateT WriterState m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> ADW m (Doc Text)
inlineToAsciiDoc WriterOptions
opts Inline
x
           Doc Text
xs' <- [Inline] -> StateT WriterState m (Doc Text)
go [Inline]
xs
           Doc Text -> StateT WriterState m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text
x' Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
xs')
       isSpacy :: SpacyLocation -> Inline -> Bool
       isSpacy :: SpacyLocation -> Inline -> Bool
isSpacy SpacyLocation
_ Inline
Space = Bool
True
       isSpacy SpacyLocation
_ Inline
LineBreak = Bool
True
       isSpacy SpacyLocation
_ Inline
SoftBreak = Bool
True
       -- Note that \W characters count as spacy in AsciiDoc
       -- for purposes of determining interword:
       isSpacy SpacyLocation
End (Str Text
xs) = case Text -> Maybe (Text, Char)
T.unsnoc Text
xs of
                                   Just (Text
_, Char
c) -> Char -> Bool
isPunctuation Char
c Bool -> Bool -> Bool
|| Char -> Bool
isSpace Char
c
                                   Maybe (Text, Char)
_           -> Bool
False
       isSpacy SpacyLocation
Start (Str Text
xs)
         | Just (Char
c, Text
_) <- Text -> Maybe (Char, Text)
T.uncons Text
xs = Char -> Bool
isPunctuation Char
c Bool -> Bool -> Bool
|| Char -> Bool
isSpace Char
c
       isSpacy SpacyLocation
_ Inline
_ = Bool
True

setIntraword :: PandocMonad m => Bool -> ADW m ()
setIntraword :: forall (m :: * -> *). PandocMonad m => Bool -> ADW m ()
setIntraword Bool
b = (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ intraword = b }

withIntraword :: PandocMonad m => ADW m a -> ADW m a
withIntraword :: forall (m :: * -> *) a. PandocMonad m => ADW m a -> ADW m a
withIntraword ADW m a
p = Bool -> ADW m ()
forall (m :: * -> *). PandocMonad m => Bool -> ADW m ()
setIntraword Bool
True ADW m () -> ADW m a -> ADW m a
forall a b.
StateT WriterState m a
-> StateT WriterState m b -> StateT WriterState m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> ADW m a
p ADW m a -> ADW m () -> ADW m a
forall a b.
StateT WriterState m a
-> StateT WriterState m b -> StateT WriterState m a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Bool -> ADW m ()
forall (m :: * -> *). PandocMonad m => Bool -> ADW m ()
setIntraword Bool
False

-- | Convert Pandoc inline element to asciidoc.
inlineToAsciiDoc :: PandocMonad m => WriterOptions -> Inline -> ADW m (Doc Text)
inlineToAsciiDoc :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> ADW m (Doc Text)
inlineToAsciiDoc WriterOptions
opts (Emph [Strong [Inline]
xs]) =
  WriterOptions -> Inline -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> ADW m (Doc Text)
inlineToAsciiDoc WriterOptions
opts ([Inline] -> Inline
Strong [[Inline] -> Inline
Emph [Inline]
xs])  -- see #5565
inlineToAsciiDoc WriterOptions
opts (Emph [Inline]
lst) = do
  Doc Text
contents <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
lst
  Bool
isIntraword <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
intraword
  let marker :: Doc Text
marker = if Bool
isIntraword then Doc Text
"__" else Doc Text
"_"
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
marker Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
marker
inlineToAsciiDoc WriterOptions
opts (Underline [Inline]
lst) = do
  Doc Text
contents <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
lst
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
"[.underline]#" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"#"
inlineToAsciiDoc WriterOptions
opts (Strong [Inline]
lst) = do
  Doc Text
contents <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
lst
  Bool
isIntraword <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
intraword
  let marker :: Doc Text
marker = if Bool
isIntraword then Doc Text
"**" else Doc Text
"*"
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
marker Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
marker
inlineToAsciiDoc WriterOptions
opts (Strikeout [Inline]
lst) = do
  Doc Text
contents <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
lst
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
"[line-through]#" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"#"
inlineToAsciiDoc WriterOptions
opts (Superscript [Inline]
lst) = do
  Doc Text
contents <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
lst
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
"^" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"^"
inlineToAsciiDoc WriterOptions
opts (Subscript [Inline]
lst) = do
  Doc Text
contents <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
lst
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
"~" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"~"
inlineToAsciiDoc WriterOptions
opts (SmallCaps [Inline]
lst) = WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
lst
inlineToAsciiDoc WriterOptions
opts (Quoted QuoteType
qt [Inline]
lst) = do
  Bool
isLegacy <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
legacy
  WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts ([Inline] -> ADW m (Doc Text)) -> [Inline] -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$
    case QuoteType
qt of
      QuoteType
SingleQuote
        | Bool
isLegacy     -> [Text -> Inline
Str Text
"`"] [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Inline]
lst [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Text -> Inline
Str Text
"'"]
        | Bool
otherwise    -> [Text -> Inline
Str Text
"'`"] [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Inline]
lst [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Text -> Inline
Str Text
"`'"]
      QuoteType
DoubleQuote
        | Bool
isLegacy     -> [Text -> Inline
Str Text
"``"] [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Inline]
lst [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Text -> Inline
Str Text
"''"]
        | Bool
otherwise    -> [Text -> Inline
Str Text
"\"`"] [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Inline]
lst [Inline] -> [Inline] -> [Inline]
forall a. [a] -> [a] -> [a]
++ [Text -> Inline
Str Text
"`\""]
inlineToAsciiDoc WriterOptions
_ (Code (Text, [Text], [(Text, Text)])
_ Text
str) = do
  Bool
isLegacy <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
legacy
  let escChar :: Char -> Text
escChar Char
'`' = Text
"\\'"
      escChar Char
c   = Char -> Text
T.singleton Char
c
  let contents :: Doc Text
contents = Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal ((Char -> Text) -> Text -> Text
T.concatMap Char -> Text
escChar Text
str)
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$
    if Bool
isLegacy
       then SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"`"  Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"`"
       else SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"`+" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"+`"
inlineToAsciiDoc WriterOptions
_ (Str Text
str) = Text -> ADW m (Doc Text)
forall (m :: * -> *). PandocMonad m => Text -> ADW m (Doc Text)
escapeString Text
str
inlineToAsciiDoc WriterOptions
_ (Math MathType
InlineMath Text
str) = do
  Bool
isLegacy <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
legacy
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ hasMath = True }
  let content :: Doc Text
content = if Bool
isLegacy
                then Doc Text
"$" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
str Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"$"
                else Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
str
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
"latexmath:[" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
content Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"]"
inlineToAsciiDoc WriterOptions
_ (Math MathType
DisplayMath Text
str) = do
  Bool
isLegacy <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
legacy
  (WriterState -> WriterState) -> StateT WriterState m ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((WriterState -> WriterState) -> StateT WriterState m ())
-> (WriterState -> WriterState) -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ \WriterState
st -> WriterState
st{ hasMath = True }
  let content :: Doc Text
content = if Bool
isLegacy
                   then Doc Text
"\\[" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
str Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"\\]"
                   else Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
str
  Bool
inlist <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
inList
  let sepline :: Doc Text
sepline = if Bool
inlist
                   then SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"+"
                   else Doc Text
forall a. Doc a
blankline
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$
      (Doc Text
forall a. Doc a
cr Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
sepline) Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
"[latexmath]" Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
"++++" Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$
      Doc Text
content Doc Text -> Doc Text -> Doc Text
forall a. Doc a -> Doc a -> Doc a
$$ Doc Text
"++++" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr
inlineToAsciiDoc WriterOptions
_ il :: Inline
il@(RawInline Format
f Text
s)
  | Format
f Format -> Format -> Bool
forall a. Eq a => a -> a -> Bool
== Format
"asciidoc" = Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
s
  | Bool
otherwise         = do
      LogMessage -> StateT WriterState m ()
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report (LogMessage -> StateT WriterState m ())
-> LogMessage -> StateT WriterState m ()
forall a b. (a -> b) -> a -> b
$ Inline -> LogMessage
InlineNotRendered Inline
il
      Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
forall a. Doc a
empty
inlineToAsciiDoc WriterOptions
_ Inline
LineBreak = Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
" +" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
forall a. Doc a
cr
inlineToAsciiDoc WriterOptions
_ Inline
Space = Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
forall a. Doc a
space
inlineToAsciiDoc WriterOptions
opts Inline
SoftBreak =
  case WriterOptions -> WrapOption
writerWrapText WriterOptions
opts of
       WrapOption
WrapAuto     -> Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
forall a. Doc a
space
       WrapOption
WrapPreserve -> Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
forall a. Doc a
cr
       WrapOption
WrapNone     -> Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
forall a. Doc a
space
inlineToAsciiDoc WriterOptions
opts (Cite [Citation]
_ [Inline]
lst) = WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
lst
inlineToAsciiDoc WriterOptions
opts (Link (Text, [Text], [(Text, Text)])
_ [Inline]
txt (Text
src, Text
_tit)) = do
-- relative:  link:downloads/foo.zip[download foo.zip]
-- abs:  http://google.cod[Google]
-- or my@email.com[email john]
  let fixCommas :: Inline -> [Inline]
fixCommas (Str Text
t) =
        Inline -> [Inline] -> [Inline]
forall a. a -> [a] -> [a]
intersperse (Format -> Text -> Inline
RawInline (Text -> Format
Format Text
"asciidoc") Text
"&#44;")
          ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall a b. (a -> b) -> a -> b
$ (Text -> Inline) -> [Text] -> [Inline]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Inline
Str ([Text] -> [Inline]) -> [Text] -> [Inline]
forall a b. (a -> b) -> a -> b
$ HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"," Text
t -- see #8070
      fixCommas Inline
x = [Inline
x]

  Doc Text
linktext <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts ([Inline] -> ADW m (Doc Text)) -> [Inline] -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ ([Inline] -> [Inline]) -> [Inline] -> [Inline]
forall a b. Walkable a b => (a -> a) -> b -> b
walk ((Inline -> [Inline]) -> [Inline] -> [Inline]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Inline -> [Inline]
fixCommas) [Inline]
txt
  let isRelative :: Bool
isRelative = (Char -> Bool) -> Text -> Bool
T.all (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
':') Text
src
  let needsPassthrough :: Bool
needsPassthrough = Text
"--" Text -> Text -> Bool
`T.isInfixOf` Text
src
  let prefix :: Doc Text
prefix = if Bool
isRelative
                  then SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"link:"
                  else Doc Text
forall a. Doc a
empty
  let srcSuffix :: Text
srcSuffix = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
src (Text -> Text -> Maybe Text
T.stripPrefix Text
"mailto:" Text
src)
  let useAuto :: Bool
useAuto = case [Inline]
txt of
                      [Str Text
s] | Text -> Text
escapeURI Text
s Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
srcSuffix -> Bool
True
                      [Inline]
_       -> Bool
False
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$
    if Bool
needsPassthrough
       then
         if Bool
useAuto
            then Doc Text
"link:++" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
srcSuffix Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"++[]"
            else Doc Text
"link:++" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
src Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"++[" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
linktext Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"]"
       else
         if Bool
useAuto
            then Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
srcSuffix
            else Doc Text
prefix Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
src Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"[" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
linktext Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"]"
inlineToAsciiDoc WriterOptions
opts (Image (Text, [Text], [(Text, Text)])
attr [Inline]
alternate (Text
src, Text
tit)) =
  (Doc Text
"image:" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<>) (Doc Text -> Doc Text) -> ADW m (Doc Text) -> ADW m (Doc Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> WriterOptions
-> (Text, [Text], [(Text, Text)])
-> [Inline]
-> Text
-> Text
-> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions
-> (Text, [Text], [(Text, Text)])
-> [Inline]
-> Text
-> Text
-> ADW m (Doc Text)
imageArguments WriterOptions
opts (Text, [Text], [(Text, Text)])
attr [Inline]
alternate Text
src Text
tit
inlineToAsciiDoc WriterOptions
opts (Note [Para [Inline]
inlines]) =
  WriterOptions -> Inline -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Inline -> ADW m (Doc Text)
inlineToAsciiDoc WriterOptions
opts ([Block] -> Inline
Note [[Inline] -> Block
Plain [Inline]
inlines])
inlineToAsciiDoc WriterOptions
opts (Note [Plain [Inline]
inlines]) = do
  Doc Text
contents  <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
inlines
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text SourceName
"footnote:[" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"]"
-- asciidoc can't handle blank lines in notes
inlineToAsciiDoc WriterOptions
_ (Note [Block]
_) = Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
"[multiblock footnote omitted]"
inlineToAsciiDoc WriterOptions
opts (Span (Text
ident,[Text]
classes,[(Text, Text)]
_) [Inline]
ils) = do
  Doc Text
contents <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
ils
  Bool
isIntraword <- (WriterState -> Bool) -> StateT WriterState m Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets WriterState -> Bool
intraword
  let marker :: Doc Text
marker = if Bool
isIntraword then Doc Text
"##" else Doc Text
"#"
  if Text -> Bool
T.null Text
ident Bool -> Bool -> Bool
&& [Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
classes
     then Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return Doc Text
contents
     else do
       let modifier :: Doc Text
modifier = Doc Text -> Doc Text
forall a. HasChars a => Doc a -> Doc a
brackets (Doc Text -> Doc Text) -> Doc Text -> Doc Text
forall a b. (a -> b) -> a -> b
$ Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal (Text -> Doc Text) -> Text -> Doc Text
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.unwords ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$
            [ Text
"#" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
ident | Bool -> Bool
not (Text -> Bool
T.null Text
ident)] [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Text
"." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) [Text]
classes
       Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Doc Text
modifier Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
marker Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
contents Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
marker

-- | Provides the arguments for both `image:` and `image::`
-- e.g.: sunset.jpg[Sunset,300,200]
imageArguments :: PandocMonad m => WriterOptions ->
  Attr -> [Inline] -> Text -> Text ->
  ADW m (Doc Text)
imageArguments :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions
-> (Text, [Text], [(Text, Text)])
-> [Inline]
-> Text
-> Text
-> ADW m (Doc Text)
imageArguments WriterOptions
opts (Text, [Text], [(Text, Text)])
attr [Inline]
altText Text
src Text
title = do
  let txt :: [Inline]
txt = if [Inline] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Inline]
altText Bool -> Bool -> Bool
|| ([Inline]
altText [Inline] -> [Inline] -> Bool
forall a. Eq a => a -> a -> Bool
== [Text -> Inline
Str Text
""])
               then [Text -> Inline
Str (Text -> Inline) -> (SourceName -> Text) -> SourceName -> Inline
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceName -> Text
T.pack (SourceName -> Text)
-> (SourceName -> SourceName) -> SourceName -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceName -> SourceName
dropExtension (SourceName -> Inline) -> SourceName -> Inline
forall a b. (a -> b) -> a -> b
$ Text -> SourceName
T.unpack Text
src]
               else [Inline]
altText
  Doc Text
linktext <- WriterOptions -> [Inline] -> ADW m (Doc Text)
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> [Inline] -> ADW m (Doc Text)
inlineListToAsciiDoc WriterOptions
opts [Inline]
txt
  let linktitle :: Doc Text
linktitle = if Text -> Bool
T.null Text
title
                     then Doc Text
forall a. Doc a
empty
                     else Doc Text
",title=\"" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
title Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"\""
      showDim :: Direction -> [Doc Text]
showDim Direction
dir = case Direction -> (Text, [Text], [(Text, Text)]) -> Maybe Dimension
dimension Direction
dir (Text, [Text], [(Text, Text)])
attr of
                      Just (Percent Double
a) ->
                        [Doc Text
"scaledwidth=" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text (Dimension -> SourceName
forall a. Show a => a -> SourceName
show (Double -> Dimension
Percent Double
a))]
                      Just Dimension
dim         ->
                        [SourceName -> Doc Text
forall a. HasChars a => SourceName -> Doc a
text (Direction -> SourceName
forall a. Show a => a -> SourceName
show Direction
dir) Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"=" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<>
                          Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal (WriterOptions -> Dimension -> Text
showInPixel WriterOptions
opts Dimension
dim)]
                      Maybe Dimension
Nothing          ->
                        []
      dimList :: [Doc Text]
dimList = Direction -> [Doc Text]
showDim Direction
Width [Doc Text] -> [Doc Text] -> [Doc Text]
forall a. [a] -> [a] -> [a]
++ Direction -> [Doc Text]
showDim Direction
Height
      dims :: Doc Text
dims = if [Doc Text] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Doc Text]
dimList
                then Doc Text
forall a. Doc a
empty
                else Doc Text
"," Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> [Doc Text] -> Doc Text
forall a. Monoid a => [a] -> a
mconcat (Doc Text -> [Doc Text] -> [Doc Text]
forall a. a -> [a] -> [a]
intersperse Doc Text
"," [Doc Text]
dimList)
  Doc Text -> ADW m (Doc Text)
forall a. a -> StateT WriterState m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Doc Text -> ADW m (Doc Text)) -> Doc Text -> ADW m (Doc Text)
forall a b. (a -> b) -> a -> b
$ Text -> Doc Text
forall a. HasChars a => a -> Doc a
literal Text
src Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"[" Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
linktext Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
linktitle Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
dims Doc Text -> Doc Text -> Doc Text
forall a. Semigroup a => a -> a -> a
<> Doc Text
"]"