{-# LANGUAGE OverloadedStrings #-}
{- |
   Module      : Text.Pandoc.Readers.Pptx.Slides
   Copyright   : © 2025 Anton Antic
   License     : GNU GPL, version 2 or above

   Maintainer  : Anton Antic <anton@everworker.ai>
   Stability   : alpha
   Portability : portable

Conversion of PPTX slides to Pandoc AST blocks.
-}
module Text.Pandoc.Readers.Pptx.Slides
  ( pptxToOutput
  ) where

import Codec.Archive.Zip (Archive)
import Control.Applicative ((<|>))
import Data.List (find)
import qualified Data.Map as M
import Data.Maybe (fromMaybe, mapMaybe)
import qualified Data.Text as T
import Data.Text (Text)
import qualified Text.Pandoc.Builder as B
import Text.Pandoc.Class.PandocMonad (PandocMonad)
import Text.Pandoc.Definition
import Text.Pandoc.Options (ReaderOptions)
import Text.Pandoc.Readers.OOXML.Shared
import Text.Pandoc.Readers.Pptx.Parse
import Text.Pandoc.Readers.Pptx.Shapes
import Text.Pandoc.Shared (stringify)
import Text.Pandoc.XML.Light

-- | Convert Pptx intermediate representation to Pandoc AST
pptxToOutput :: PandocMonad m => ReaderOptions -> Pptx -> m (Meta, [Block])
pptxToOutput :: forall (m :: * -> *).
PandocMonad m =>
ReaderOptions -> Pptx -> m (Meta, [Block])
pptxToOutput ReaderOptions
_opts Pptx
pptx = do
  let slides :: [PptxSlide]
slides = Pptx -> [PptxSlide]
pptxSlides Pptx
pptx
      archive :: Archive
archive = Pptx -> Archive
pptxArchive Pptx
pptx

  -- Convert each slide to blocks
  slideBlocks <- [[Block]] -> [Block]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Block]] -> [Block]) -> m [[Block]] -> m [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (PptxSlide -> m [Block]) -> [PptxSlide] -> m [[Block]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Archive -> PptxSlide -> m [Block]
forall (m :: * -> *).
PandocMonad m =>
Archive -> PptxSlide -> m [Block]
slideToBlocks Archive
archive) [PptxSlide]
slides

  return (mempty, slideBlocks)

-- | Convert slide to blocks
slideToBlocks :: PandocMonad m => Archive -> PptxSlide -> m [Block]
slideToBlocks :: forall (m :: * -> *).
PandocMonad m =>
Archive -> PptxSlide -> m [Block]
slideToBlocks Archive
archive PptxSlide
slide = do
  let SlideId Int
n = PptxSlide -> SlideId
slideId PptxSlide
slide
      slideElem :: Element
slideElem = Element -> Element
resolveAlternateContent (PptxSlide -> Element
slideElement PptxSlide
slide)
      rels :: [(Text, Text)]
rels = PptxSlide -> [(Text, Text)]
slideRels PptxSlide
slide
      ns :: NameSpaces
ns = Element -> NameSpaces
elemToNameSpaces Element
slideElem

      -- Extract title from title placeholder
      title :: Text
title = NameSpaces -> Element -> Text
extractSlideTitle NameSpaces
ns Element
slideElem

      -- Create header
      slideIdent :: Text
slideIdent = Text
"slide-" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (Int -> String
forall a. Show a => a -> String
show Int
n)
      headerText :: Text
headerText = if Text -> Bool
T.null Text
title
                   then Text
"Slide " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (Int -> String
forall a. Show a => a -> String
show Int
n)
                   else Text
title
      -- A slide hidden from the slide show (show="0") is still read,
      -- with a "hidden" class on its header.
      classes :: [Text]
classes = [Text
"hidden" | QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"show") Element
slideElem Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"0"]
      header :: Block
header = Int -> Attr -> [Inline] -> Block
Header Int
2 (Text
slideIdent, [Text]
classes, [])
                 (Many Inline -> [Inline]
forall a. Many a -> [a]
B.toList (Text -> Many Inline
B.text Text
headerText))

  -- Parse shapes and convert to blocks
  case NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"p" Text
"cSld" Element
slideElem Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
       NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"p" Text
"spTree" of
    Maybe Element
Nothing -> [Block] -> m [Block]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Block] -> m [Block]) -> [Block] -> m [Block]
forall a b. (a -> b) -> a -> b
$ Block
header Block -> [Block] -> [Block]
forall a. a -> [a] -> [a]
: [Block]
notesBlocks
    Just Element
spTree -> do
      -- Filter out title placeholder shapes before parsing
      let allShapeElems :: [Element]
allShapeElems = NameSpaces -> [Element] -> [Element]
ungroupShapes NameSpaces
ns ([Content] -> [Element]
onlyElems ([Content] -> [Element]) -> [Content] -> [Element]
forall a b. (a -> b) -> a -> b
$ Element -> [Content]
elContent Element
spTree)
          nonTitleShapeElems :: [Element]
nonTitleShapeElems = (Element -> Bool) -> [Element] -> [Element]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Element -> Bool) -> Element -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NameSpaces -> Element -> Bool
isTitlePlaceholder NameSpaces
ns) [Element]
allShapeElems
          inherited :: Element -> Int -> Maybe BulletType
inherited = Maybe Element
-> Maybe Element -> Element -> Int -> Maybe BulletType
inheritedBullets (PptxSlide -> Maybe Element
slideLayout PptxSlide
slide) (PptxSlide -> Maybe Element
slideMaster PptxSlide
slide)
          shapes :: [PptxShape]
shapes = (Element -> Maybe PptxShape) -> [Element] -> [PptxShape]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (\Element
el -> (Int -> Maybe BulletType)
-> NameSpaces -> Element -> Maybe PptxShape
parseShapeWith (Element -> Int -> Maybe BulletType
inherited Element
el) NameSpaces
ns Element
el)
                            [Element]
nonTitleShapeElems
      shapeBlocks <- [[Block]] -> [Block]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Block]] -> [Block]) -> m [[Block]] -> m [Block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (PptxShape -> m [Block]) -> [PptxShape] -> m [[Block]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Archive -> [(Text, Text)] -> PptxShape -> m [Block]
forall (m :: * -> *).
PandocMonad m =>
Archive -> [(Text, Text)] -> PptxShape -> m [Block]
shapeToBlocks Archive
archive [(Text, Text)]
rels) [PptxShape]
shapes
      return $ header : shapeBlocks ++ notesBlocks
  where
    notesBlocks :: [Block]
notesBlocks = [Block] -> (Element -> [Block]) -> Maybe Element -> [Block]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Element -> [Block]
notesToBlocks (PptxSlide -> Maybe Element
slideNotes PptxSlide
slide)

-- | Convert a notes slide (@p:notes@) to a @notes@ div, the form the pptx
-- writer turns into speaker notes. The notes text is in the shape holding
-- the body placeholder; the slide image and slide number placeholders are
-- ignored. Notes without text yield nothing.
notesToBlocks :: Element -> [Block]
notesToBlocks :: Element -> [Block]
notesToBlocks Element
notesElem =
  case (Element -> Bool) -> [Element] -> [Element]
forall a. (a -> Bool) -> [a] -> [a]
filter Element -> Bool
isBody [Element]
shapeElems [Element] -> (Element -> [Block]) -> [Block]
forall a b. [a] -> (a -> [b]) -> [b]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Element -> [Block]
textBoxBlocks of
    [Block]
blocks | Text -> Bool
T.null (Text -> Text
T.strip ([Block] -> Text
forall a. Walkable Inline a => a -> Text
stringify [Block]
blocks)) -> []
           | Bool
otherwise -> [Attr -> [Block] -> Block
Div (Text
"", [Text
"notes"], []) [Block]
blocks]
  where
    ns :: NameSpaces
ns = Element -> NameSpaces
elemToNameSpaces Element
notesElem
    shapeElems :: [Element]
shapeElems = [Element] -> (Element -> [Element]) -> Maybe Element -> [Element]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Element -> [Element]
elChildren (Maybe Element -> [Element]) -> Maybe Element -> [Element]
forall a b. (a -> b) -> a -> b
$
      NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"p" Text
"cSld" Element
notesElem Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
      NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"p" Text
"spTree"
    isBody :: Element -> Bool
isBody Element
el =
      case NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"p" Text
"nvSpPr" Element
el Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
           NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"p" Text
"nvPr" Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
           NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"p" Text
"ph" of
        Just Element
ph -> QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"type") Element
ph Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"body"
        Maybe Element
Nothing -> Bool
False
    textBoxBlocks :: Element -> [Block]
textBoxBlocks Element
el = case NameSpaces -> Element -> Maybe PptxShape
parseShape NameSpaces
ns Element
el of
      Just (PptxTextBox [PptxParagraph]
paras) -> [PptxParagraph] -> [Block]
paragraphsToBlocks [PptxParagraph]
paras
      Maybe PptxShape
_ -> []

-- | Replace each group shape (@p:grpSp@, possibly nested) by the shapes
-- it contains, keeping document order.
ungroupShapes :: NameSpaces -> [Element] -> [Element]
ungroupShapes :: NameSpaces -> [Element] -> [Element]
ungroupShapes NameSpaces
ns = (Element -> [Element]) -> [Element] -> [Element]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Element -> [Element]
ungroup
  where
    ungroup :: Element -> [Element]
ungroup Element
el
      | NameSpaces -> Text -> Text -> Element -> Bool
isElem NameSpaces
ns Text
"p" Text
"grpSp" Element
el = NameSpaces -> [Element] -> [Element]
ungroupShapes NameSpaces
ns (Element -> [Element]
elChildren Element
el)
      | Bool
otherwise = [Element
el]

-- | Extract title from title placeholder
extractSlideTitle :: NameSpaces -> Element -> Text
extractSlideTitle :: NameSpaces -> Element -> Text
extractSlideTitle NameSpaces
ns Element
slideElem =
  case NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"p" Text
"cSld" Element
slideElem Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
       NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"p" Text
"spTree" of
    Maybe Element
Nothing -> Text
""
    Just Element
spTree ->
      -- Find shape with ph type="title"
      let shapes :: [Element]
shapes = [Content] -> [Element]
onlyElems ([Content] -> [Element]) -> [Content] -> [Element]
forall a b. (a -> b) -> a -> b
$ Element -> [Content]
elContent Element
spTree
          titleShape :: Maybe Element
titleShape = (Element -> Bool) -> [Element] -> Maybe Element
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (NameSpaces -> Element -> Bool
isTitlePlaceholder NameSpaces
ns) [Element]
shapes
       in Text -> (Element -> Text) -> Maybe Element -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" Element -> Text
extractDrawingMLText Maybe Element
titleShape

-- isTitlePlaceholder is imported from Shapes module

-- | Replace each markup-compatibility block (@mc:AlternateContent@) by
-- the content of its first @mc:Choice@ whose required namespaces the
-- reader understands, or else by its @mc:Fallback@. PowerPoint and the
-- pptx writer put the shapes holding equations (@a14:m@) in such blocks.
resolveAlternateContent :: Element -> Element
resolveAlternateContent :: Element -> Element
resolveAlternateContent = NameSpaces -> Element -> Element
resolve NameSpaces
forall a. Monoid a => a
mempty
  where
    resolve :: NameSpaces -> Element -> Element
resolve NameSpaces
ns Element
el =
      let ns' :: NameSpaces
ns' = Element -> NameSpaces
elemToNameSpaces Element
el NameSpaces -> NameSpaces -> NameSpaces
forall a. Semigroup a => a -> a -> a
<> NameSpaces
ns
       in Element
el{ elContent = concatMap (content ns') (elContent el) }
    content :: NameSpaces -> Content -> [Content]
content NameSpaces
ns (Elem Element
el)
      | Text -> Element -> Bool
isMce Text
"AlternateContent" Element
el =
          let ns' :: NameSpaces
ns' = Element -> NameSpaces
elemToNameSpaces Element
el NameSpaces -> NameSpaces -> NameSpaces
forall a. Semigroup a => a -> a -> a
<> NameSpaces
ns
              branches :: [Element]
branches = Element -> [Element]
elChildren Element
el
              chosen :: Maybe Element
chosen = (Element -> Bool) -> [Element] -> Maybe Element
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (NameSpaces -> Element -> Bool
understood NameSpaces
ns') [Element]
branches Maybe Element -> Maybe Element -> Maybe Element
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
                       (Element -> Bool) -> [Element] -> Maybe Element
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (Text -> Element -> Bool
isMce Text
"Fallback") [Element]
branches
           in case Maybe Element
chosen of
                Just Element
branch ->
                  (Content -> [Content]) -> [Content] -> [Content]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (NameSpaces -> Content -> [Content]
content (Element -> NameSpaces
elemToNameSpaces Element
branch NameSpaces -> NameSpaces -> NameSpaces
forall a. Semigroup a => a -> a -> a
<> NameSpaces
ns'))
                            (Element -> [Content]
elContent Element
branch)
                Maybe Element
Nothing -> []
      | Bool
otherwise = [Element -> Content
Elem (NameSpaces -> Element -> Element
resolve NameSpaces
ns Element
el)]
    content NameSpaces
_ Content
c = [Content
c]
    understood :: NameSpaces -> Element -> Bool
understood NameSpaces
ns Element
branch =
      Text -> Element -> Bool
isMce Text
"Choice" Element
branch Bool -> Bool -> Bool
&&
      (Text -> Bool) -> [Text] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (\Text
prefix -> Text -> NameSpaces -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Text
prefix (Element -> NameSpaces
elemToNameSpaces Element
branch NameSpaces -> NameSpaces -> NameSpaces
forall a. Semigroup a => a -> a -> a
<> NameSpaces
ns)
                         Maybe Text -> [Maybe Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (Text -> Maybe Text) -> [Text] -> [Maybe Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Maybe Text
forall a. a -> Maybe a
Just [Text]
understoodNamespaces)
          (Text -> [Text]
T.words (Text -> [Text]) -> Text -> [Text]
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"" (Maybe Text -> Text) -> Maybe Text -> Text
forall a b. (a -> b) -> a -> b
$ QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"Requires") Element
branch)
    isMce :: Text -> Element -> Bool
isMce Text
name Element
el =
      QName -> Text
qName (Element -> QName
elName Element
el) Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
name Bool -> Bool -> Bool
&&
      QName -> Maybe Text
qURI (Element -> QName
elName Element
el) Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
==
        Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"http://schemas.openxmlformats.org/markup-compatibility/2006"
    understoodNamespaces :: [Text]
understoodNamespaces =
      [ Text
"http://schemas.microsoft.com/office/drawing/2010/main" ] -- a14