{- |
   Module      : Text.Pandoc.Writers.Native
   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 a 'Pandoc' document to a string representation.
-}
module Text.Pandoc.Writers.Native ( writeNative )
where
import Data.Text (Text)
import qualified Data.Text as T
import Text.Pandoc.Class.PandocMonad (PandocMonad)
import Text.Pandoc.Definition
import Text.Pandoc.Options (WriterOptions (..))
import Text.Show.Pretty (ppDoc)
import Text.PrettyPrint (renderStyle, Style(..), style, char)

-- | Prettyprint Pandoc document.
writeNative :: PandocMonad m => WriterOptions -> Pandoc -> m Text
writeNative :: forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Pandoc -> m Text
writeNative WriterOptions
opts (Pandoc Meta
meta [Block]
blocks) = do
  let style' :: Style
style' = Style
style{ lineLength = writerColumns opts,
                      ribbonsPerLine = 1.2 }
  Text -> m Text
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> m Text) -> Text -> m Text
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Style -> Doc -> String
renderStyle Style
style' (Doc -> String) -> Doc -> String
forall a b. (a -> b) -> a -> b
$
    case WriterOptions -> Maybe (Template Text)
writerTemplate WriterOptions
opts of
      Just Template Text
_  -> Pandoc -> Doc
forall a. Show a => a -> Doc
ppDoc (Meta -> [Block] -> Pandoc
Pandoc Meta
meta [Block]
blocks) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Char -> Doc
char Char
'\n'
      Maybe (Template Text)
Nothing -> [Block] -> Doc
forall a. Show a => a -> Doc
ppDoc [Block]
blocks