{- |
   Module      : Text.Pandoc.Writers.Powerpoint
   Copyright   : Copyright (C) 2017-2020 Jesse Rosenthal
   License     : GNU GPL, version 2 or above

   Maintainer  : Jesse Rosenthal <jrosenthal@jhu.edu>
   Stability   : alpha
   Portability : portable

Conversion of 'Pandoc' documents to powerpoint (pptx). -}

{-
This is a wrapper around two modules:

  - Text.Pandoc.Writers.Powerpoint.Presentation (which converts a
    pandoc document into a Presentation datatype), and

  - Text.Pandoc.Writers.Powerpoint.Output (which converts a
    Presentation into a zip archive, which can be output).
-}

module Text.Pandoc.Writers.Powerpoint (writePowerpoint) where

import Codec.Archive.Zip
import Text.Pandoc.Definition
import Text.Pandoc.Walk
import Text.Pandoc.Class.PandocMonad (PandocMonad, report)
import Text.Pandoc.Options (WriterOptions)
import Text.Pandoc.Writers.Shared (fixDisplayMath)
import Text.Pandoc.Writers.Powerpoint.Presentation (documentToPresentation)
import Text.Pandoc.Writers.Powerpoint.Output (presentationToArchive)
import qualified Data.ByteString.Lazy as BL

writePowerpoint :: (PandocMonad m)
                => WriterOptions  -- ^ Writer options
                -> Pandoc         -- ^ Document to convert
                -> m BL.ByteString
writePowerpoint :: WriterOptions -> Pandoc -> m ByteString
writePowerpoint WriterOptions
opts (Pandoc Meta
meta [Block]
blks) = do
  let blks' :: [Block]
blks' = (Block -> Block) -> [Block] -> [Block]
forall a b. Walkable a b => (a -> a) -> b -> b
walk Block -> Block
fixDisplayMath [Block]
blks
  let (Presentation
pres, [LogMessage]
logMsgs) = WriterOptions -> Pandoc -> (Presentation, [LogMessage])
documentToPresentation WriterOptions
opts (Meta -> [Block] -> Pandoc
Pandoc Meta
meta [Block]
blks')
  (LogMessage -> m ()) -> [LogMessage] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ LogMessage -> m ()
forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
report [LogMessage]
logMsgs
  Archive
archv <- WriterOptions -> Presentation -> m Archive
forall (m :: * -> *).
PandocMonad m =>
WriterOptions -> Presentation -> m Archive
presentationToArchive WriterOptions
opts Presentation
pres
  ByteString -> m ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> m ByteString) -> ByteString -> m ByteString
forall a b. (a -> b) -> a -> b
$ Archive -> ByteString
fromArchive Archive
archv