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

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

Parsing of PPTX shapes (text boxes, images, tables, diagrams).
-}
module Text.Pandoc.Readers.Pptx.Shapes
  ( PptxShape(..)
  , PptxParagraph(..)
  , TableCell(..)
  , BulletType(..)
  , parseShapes
  , parseShape
  , parseShapeWith
  , inheritedBullets
  , shapeToBlocks
  , paragraphsToBlocks
  , isTitlePlaceholder
  , extractDrawingMLText
  ) where

import Codec.Archive.Zip (Archive, findEntryByPath, fromEntry)
import qualified Data.ByteString.Lazy as B
import Data.Char (isSpace)
import Data.List (find, groupBy)
import Data.Maybe (fromMaybe, listToMaybe, mapMaybe)
import qualified Data.Set as S
import qualified Data.Text as T
import Data.Text (Text)
import Text.Read (readMaybe)
import qualified Text.Pandoc.Builder as B
import Text.Pandoc.Class.PandocMonad (PandocMonad)
import qualified Text.Pandoc.Class.PandocMonad as P
import Text.Pandoc.Definition
import Text.Pandoc.Readers.OOXML.Shared
import Text.Pandoc.Readers.OOXML.Charts
import Text.Pandoc.Readers.Pptx.SmartArt
import Text.Pandoc.Shared (collapseFilePath)
import Text.Pandoc.Walk (walk)
import Text.Pandoc.XML.Light
import Text.TeXMath (writeTeX)
import Text.TeXMath.Readers.OMML (readOMML)
import qualified System.FilePath.Posix as Posix

-- | Paragraph with bullet/numbering information
data PptxParagraph = PptxParagraph
  { PptxParagraph -> Int
paraLevel   :: Int            -- Bullet level (0, 1, 2...)
  , PptxParagraph -> BulletType
paraBullet  :: BulletType
  , PptxParagraph -> [Inline]
paraText    :: [Inline]
  } deriving (Int -> PptxParagraph -> ShowS
[PptxParagraph] -> ShowS
PptxParagraph -> String
(Int -> PptxParagraph -> ShowS)
-> (PptxParagraph -> String)
-> ([PptxParagraph] -> ShowS)
-> Show PptxParagraph
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PptxParagraph -> ShowS
showsPrec :: Int -> PptxParagraph -> ShowS
$cshow :: PptxParagraph -> String
show :: PptxParagraph -> String
$cshowList :: [PptxParagraph] -> ShowS
showList :: [PptxParagraph] -> ShowS
Show)

-- | A table cell (@a:tc@): its text, its row and column span, and whether
-- another cell's span covers it from the left (@hMerge@) or from above
-- (@vMerge@)
data TableCell = TableCell
  { TableCell -> Text
tcText    :: Text
  , TableCell -> Int
tcRowSpan :: Int
  , TableCell -> Int
tcColSpan :: Int
  , TableCell -> Bool
tcHMerge  :: Bool
  , TableCell -> Bool
tcVMerge  :: Bool
  } deriving (Int -> TableCell -> ShowS
[TableCell] -> ShowS
TableCell -> String
(Int -> TableCell -> ShowS)
-> (TableCell -> String)
-> ([TableCell] -> ShowS)
-> Show TableCell
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TableCell -> ShowS
showsPrec :: Int -> TableCell -> ShowS
$cshow :: TableCell -> String
show :: TableCell -> String
$cshowList :: [TableCell] -> ShowS
showList :: [TableCell] -> ShowS
Show)

tcCovered :: TableCell -> Bool
tcCovered :: TableCell -> Bool
tcCovered TableCell
c = TableCell -> Bool
tcHMerge TableCell
c Bool -> Bool -> Bool
|| TableCell -> Bool
tcVMerge TableCell
c

-- | Bullet type
data BulletType
  = NoBullet
  | Bullet                        -- Has bullet (character detected or implicit)
  | WingdingsBullet              -- Detected via Wingdings symbol
  | Numbered ListAttributes      -- Automatic numbering (a:buAutoNum)
  deriving (Int -> BulletType -> ShowS
[BulletType] -> ShowS
BulletType -> String
(Int -> BulletType -> ShowS)
-> (BulletType -> String)
-> ([BulletType] -> ShowS)
-> Show BulletType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BulletType -> ShowS
showsPrec :: Int -> BulletType -> ShowS
$cshow :: BulletType -> String
show :: BulletType -> String
$cshowList :: [BulletType] -> ShowS
showList :: [BulletType] -> ShowS
Show, BulletType -> BulletType -> Bool
(BulletType -> BulletType -> Bool)
-> (BulletType -> BulletType -> Bool) -> Eq BulletType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BulletType -> BulletType -> Bool
== :: BulletType -> BulletType -> Bool
$c/= :: BulletType -> BulletType -> Bool
/= :: BulletType -> BulletType -> Bool
Eq)

-- | Shape types in PPTX slides
data PptxShape
  = PptxTextBox [PptxParagraph]         -- Parsed paragraphs with bullet info
  | PptxPicture
      { PptxShape -> Text
picRelId  :: Text               -- Relationship ID (lazy loading)
      , PptxShape -> Text
picTitle  :: Text
      , PptxShape -> Text
picAlt    :: Text
      }
  | PptxTable [[TableCell]]             -- Rows of cells
  | PptxDiagramRef
      { PptxShape -> Text
dgmDataRelId   :: Text          -- Relationship to data.xml
      , PptxShape -> Text
dgmLayoutRelId :: Text          -- Relationship to layout.xml
      }
  | PptxChartRef Text Text              -- Relationship to the chart, URI
  | PptxGraphic Text                    -- Placeholder for other graphics
  deriving (Int -> PptxShape -> ShowS
[PptxShape] -> ShowS
PptxShape -> String
(Int -> PptxShape -> ShowS)
-> (PptxShape -> String)
-> ([PptxShape] -> ShowS)
-> Show PptxShape
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PptxShape -> ShowS
showsPrec :: Int -> PptxShape -> ShowS
$cshow :: PptxShape -> String
show :: PptxShape -> String
$cshowList :: [PptxShape] -> ShowS
showList :: [PptxShape] -> ShowS
Show)

-- | Parse all shapes from shape tree
parseShapes :: NameSpaces -> Element -> [PptxShape]
parseShapes :: NameSpaces -> Element -> [PptxShape]
parseShapes NameSpaces
ns Element
spTreeElem =
  let shapeElems :: [Element]
shapeElems = [Content] -> [Element]
onlyElems ([Content] -> [Element]) -> [Content] -> [Element]
forall a b. (a -> b) -> a -> b
$ Element -> [Content]
elContent Element
spTreeElem
      -- Merge parent namespaces with element namespaces
      ns' :: NameSpaces
ns' = NameSpaces
ns NameSpaces -> NameSpaces -> NameSpaces
forall a. Semigroup a => a -> a -> a
<> Element -> NameSpaces
elemToNameSpaces Element
spTreeElem
   in (Element -> Maybe PptxShape) -> [Element] -> [PptxShape]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (NameSpaces -> Element -> Maybe PptxShape
parseShape NameSpaces
ns') [Element]
shapeElems

-- | Parse individual shape element
parseShape :: NameSpaces -> Element -> Maybe PptxShape
parseShape :: NameSpaces -> Element -> Maybe PptxShape
parseShape = (Int -> Maybe BulletType)
-> NameSpaces -> Element -> Maybe PptxShape
parseShapeWith (Maybe BulletType -> Int -> Maybe BulletType
forall a b. a -> b -> a
const Maybe BulletType
forall a. Maybe a
Nothing)

-- | Parse a shape, given the bullet its paragraphs inherit at each level
-- (0 for the first) when they don't set one themselves
parseShapeWith :: (Int -> Maybe BulletType) -> NameSpaces -> Element
               -> Maybe PptxShape
parseShapeWith :: (Int -> Maybe BulletType)
-> NameSpaces -> Element -> Maybe PptxShape
parseShapeWith Int -> Maybe BulletType
inherited NameSpaces
ns Element
el
  -- Text box: <p:sp> with <p:txBody>
  -- A slide number placeholder only holds the number PowerPoint shows
  | NameSpaces -> Element -> Bool
isSlideNumberPlaceholder NameSpaces
ns Element
el = Maybe PptxShape
forall a. Maybe a
Nothing
  | NameSpaces -> Text -> Text -> Element -> Bool
isElem NameSpaces
ns Text
"p" Text
"sp" Element
el =
      case NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"p" Text
"txBody" Element
el of
        Just Element
txBody ->
          let paras :: [PptxParagraph]
paras = (Int -> Maybe BulletType)
-> NameSpaces -> Element -> [PptxParagraph]
parseParagraphs Int -> Maybe BulletType
inherited NameSpaces
ns Element
txBody
           in if [PptxParagraph] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [PptxParagraph]
paras
              then Maybe PptxShape
forall a. Maybe a
Nothing
              else PptxShape -> Maybe PptxShape
forall a. a -> Maybe a
Just (PptxShape -> Maybe PptxShape) -> PptxShape -> Maybe PptxShape
forall a b. (a -> b) -> a -> b
$ [PptxParagraph] -> PptxShape
PptxTextBox [PptxParagraph]
paras
        Maybe Element
Nothing -> Maybe PptxShape
forall a. Maybe a
Nothing

  -- Picture: <p:pic>
  | NameSpaces -> Text -> Text -> Element -> Bool
isElem NameSpaces
ns Text
"p" Text
"pic" Element
el = do
      nvPicPr <- NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"p" Text
"nvPicPr" Element
el
      cNvPr <- findChildByName ns "p" "cNvPr" nvPicPr

      let title = Text -> (Text -> Text) -> Maybe Text -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" Text -> Text
forall a. a -> a
id (Maybe Text -> Text) -> Maybe Text -> Text
forall a b. (a -> b) -> a -> b
$ QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"name") Element
cNvPr
          alt = Text -> (Text -> Text) -> Maybe Text -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" Text -> Text
forall a. a -> a
id (Maybe Text -> Text) -> Maybe Text -> Text
forall a b. (a -> b) -> a -> b
$ QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"descr") Element
cNvPr

      -- Get blip relationship ID
      blipFill <- findChildByName ns "p" "blipFill" el
      blip <- findChildByName ns "a" "blip" blipFill
      relId <- findAttrByName ns "r" "embed" blip

      return $ PptxPicture relId title alt

  -- GraphicFrame: table or diagram
  | NameSpaces -> Text -> Text -> Element -> Bool
isElem NameSpaces
ns Text
"p" Text
"graphicFrame" Element
el =
      case NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"a" Text
"graphic" 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
"a" Text
"graphicData" of
        Maybe Element
Nothing -> Maybe PptxShape
forall a. Maybe a
Nothing
        Just Element
graphicData ->
          case QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"uri") Element
graphicData of
            Maybe Text
Nothing -> PptxShape -> Maybe PptxShape
forall a. a -> Maybe a
Just (PptxShape -> Maybe PptxShape) -> PptxShape -> Maybe PptxShape
forall a b. (a -> b) -> a -> b
$ Text -> PptxShape
PptxGraphic Text
"no-uri"
            Just Text
uri ->
              if Text
"table" Text -> Text -> Bool
`T.isInfixOf` Text
uri
                then
                  -- Table
                  case NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"a" Text
"tbl" Element
graphicData of
                    Just Element
tbl ->
                      let rows :: [[TableCell]]
rows = NameSpaces -> Element -> [[TableCell]]
parseTableRows NameSpaces
ns Element
tbl
                       in PptxShape -> Maybe PptxShape
forall a. a -> Maybe a
Just (PptxShape -> Maybe PptxShape) -> PptxShape -> Maybe PptxShape
forall a b. (a -> b) -> a -> b
$ [[TableCell]] -> PptxShape
PptxTable [[TableCell]]
rows
                    Maybe Element
Nothing -> Maybe PptxShape
forall a. Maybe a
Nothing
                else if Text
"diagram" Text -> Text -> Bool
`T.isInfixOf` Text
uri
                  then
                    -- SmartArt diagram - dgm namespace is declared inline on relIds element
                    let dgmRelIds :: Maybe Element
dgmRelIds = (Element -> Bool) -> [Element] -> Maybe Element
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\Element
e -> QName -> Text
qName (Element -> QName
elName Element
e) Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"relIds") (Element -> [Element]
elChildren Element
graphicData)
                     in case Maybe Element
dgmRelIds of
                          Maybe Element
Nothing -> PptxShape -> Maybe PptxShape
forall a. a -> Maybe a
Just (PptxShape -> Maybe PptxShape) -> PptxShape -> Maybe PptxShape
forall a b. (a -> b) -> a -> b
$ Text -> PptxShape
PptxGraphic Text
"diagram-no-relIds"
                          Just Element
relIdsElem ->
                            -- Get r:dm and r:lo attributes (r namespace is in parent)
                            let ns' :: NameSpaces
ns' = NameSpaces
ns NameSpaces -> NameSpaces -> NameSpaces
forall a. Semigroup a => a -> a -> a
<> Element -> NameSpaces
elemToNameSpaces Element
relIdsElem
                             in case (NameSpaces -> Text -> Text -> Element -> Maybe Text
findAttrByName NameSpaces
ns' Text
"r" Text
"dm" Element
relIdsElem,
                                      NameSpaces -> Text -> Text -> Element -> Maybe Text
findAttrByName NameSpaces
ns' Text
"r" Text
"lo" Element
relIdsElem) of
                                  (Just Text
dataRelId, Just Text
layoutRelId) ->
                                    PptxShape -> Maybe PptxShape
forall a. a -> Maybe a
Just (PptxShape -> Maybe PptxShape) -> PptxShape -> Maybe PptxShape
forall a b. (a -> b) -> a -> b
$ Text -> Text -> PptxShape
PptxDiagramRef Text
dataRelId Text
layoutRelId
                                  (Maybe Text, Maybe Text)
_ -> PptxShape -> Maybe PptxShape
forall a. a -> Maybe a
Just (PptxShape -> Maybe PptxShape) -> PptxShape -> Maybe PptxShape
forall a b. (a -> b) -> a -> b
$ Text -> PptxShape
PptxGraphic Text
"diagram-missing-rels"
                  else if Text
uri Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
chartUri
                    then
                      -- The c prefix is usually declared on c:chart itself
                      case (Element -> Bool) -> [Element] -> Maybe Element
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"chart") (Text -> Bool) -> (Element -> Text) -> Element -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QName -> Text
qName (QName -> Text) -> (Element -> QName) -> Element -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> QName
elName)
                                (Element -> [Element]
elChildren Element
graphicData) of
                        Just Element
chartElem
                          | Just Text
relId <- NameSpaces -> Text -> Text -> Element -> Maybe Text
findAttrByName
                              (NameSpaces
ns NameSpaces -> NameSpaces -> NameSpaces
forall a. Semigroup a => a -> a -> a
<> Element -> NameSpaces
elemToNameSpaces Element
chartElem) Text
"r" Text
"id"
                              Element
chartElem
                          -> PptxShape -> Maybe PptxShape
forall a. a -> Maybe a
Just (PptxShape -> Maybe PptxShape) -> PptxShape -> Maybe PptxShape
forall a b. (a -> b) -> a -> b
$ Text -> Text -> PptxShape
PptxChartRef Text
relId Text
uri
                        Maybe Element
_ -> PptxShape -> Maybe PptxShape
forall a. a -> Maybe a
Just (PptxShape -> Maybe PptxShape) -> PptxShape -> Maybe PptxShape
forall a b. (a -> b) -> a -> b
$ Text -> PptxShape
PptxGraphic (Text
"other: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
uri)
                  else
                    -- Other graphic
                    PptxShape -> Maybe PptxShape
forall a. a -> Maybe a
Just (PptxShape -> Maybe PptxShape) -> PptxShape -> Maybe PptxShape
forall a b. (a -> b) -> a -> b
$ Text -> PptxShape
PptxGraphic (Text
"other: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
uri)

  -- Skip other shapes for now
  | Bool
otherwise = Maybe PptxShape
forall a. Maybe a
Nothing

-- | Parse table rows (simple text extraction)
parseTableRows :: NameSpaces -> Element -> [[TableCell]]
parseTableRows :: NameSpaces -> Element -> [[TableCell]]
parseTableRows NameSpaces
ns Element
tblElem =
  let trElems :: [Element]
trElems = NameSpaces -> Text -> Text -> Element -> [Element]
findChildrenByName NameSpaces
ns Text
"a" Text
"tr" Element
tblElem
   in (Element -> [TableCell]) -> [Element] -> [[TableCell]]
forall a b. (a -> b) -> [a] -> [b]
map (NameSpaces -> Element -> [TableCell]
parseTableRow NameSpaces
ns) [Element]
trElems

parseTableRow :: NameSpaces -> Element -> [TableCell]
parseTableRow :: NameSpaces -> Element -> [TableCell]
parseTableRow NameSpaces
ns Element
trElem =
  let tcElems :: [Element]
tcElems = NameSpaces -> Text -> Text -> Element -> [Element]
findChildrenByName NameSpaces
ns Text
"a" Text
"tc" Element
trElem
   in (Element -> TableCell) -> [Element] -> [TableCell]
forall a b. (a -> b) -> [a] -> [b]
map Element -> TableCell
parseCell [Element]
tcElems
  where
    parseCell :: Element -> TableCell
parseCell Element
tcElem = TableCell
      { tcText :: Text
tcText = case NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"a" Text
"txBody" Element
tcElem of
          -- Get text from txBody/a:p/a:r/a:t
          Just Element
txBody -> Element -> Text
extractDrawingMLText Element
txBody
          Maybe Element
Nothing -> Text
""
      , tcRowSpan :: Int
tcRowSpan = Text -> Element -> Int
forall {b}. (Num b, Ord b, Read b) => Text -> Element -> b
span' Text
"rowSpan" Element
tcElem
      , tcColSpan :: Int
tcColSpan = Text -> Element -> Int
forall {b}. (Num b, Ord b, Read b) => Text -> Element -> b
span' Text
"gridSpan" Element
tcElem
      , tcHMerge :: Bool
tcHMerge = Text -> Element -> Bool
flag Text
"hMerge" Element
tcElem
      , tcVMerge :: Bool
tcVMerge = Text -> Element -> Bool
flag Text
"vMerge" Element
tcElem
      }
    span' :: Text -> Element -> b
span' Text
name Element
el = b -> (b -> b) -> Maybe b -> b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe b
1 (b -> b -> b
forall a. Ord a => a -> a -> a
max b
1) (Maybe b -> b) -> Maybe b -> b
forall a b. (a -> b) -> a -> b
$
      QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
name) Element
el Maybe Text -> (Text -> Maybe b) -> Maybe b
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Maybe b
forall a. Read a => String -> Maybe a
readMaybe (String -> Maybe b) -> (Text -> String) -> Text -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack
    flag :: Text -> Element -> Bool
flag Text
name Element
el = QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
name) Element
el 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
forall a. a -> Maybe a
Just Text
"1", Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"true"]

-- | Convert shape to Pandoc blocks
shapeToBlocks :: PandocMonad m => Archive -> [(Text, Text)] -> PptxShape -> m [Block]
shapeToBlocks :: forall (m :: * -> *).
PandocMonad m =>
Archive -> [(Text, Text)] -> PptxShape -> m [Block]
shapeToBlocks Archive
_archive [(Text, Text)]
rels (PptxTextBox [PptxParagraph]
paras) =
  [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
$ [(Text, Text)] -> [Block] -> [Block]
resolveLinks [(Text, Text)]
rels ([Block] -> [Block]) -> [Block] -> [Block]
forall a b. (a -> b) -> a -> b
$ [PptxParagraph] -> [Block]
paragraphsToBlocks [PptxParagraph]
paras
shapeToBlocks Archive
archive [(Text, Text)]
rels (PptxPicture Text
relId Text
title Text
alt) = do
  -- Resolve relationship to get media path
  case Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
relId [(Text, Text)]
rels of
    Maybe Text
Nothing -> [Block] -> m [Block]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return []  -- Image not found
    Just Text
target -> do
      let mediaPath :: Text
mediaPath = Text -> Text
resolveMediaPath Text
target

      -- Load image bytes and add to MediaBag
      case Archive -> Text -> Maybe ByteString
loadMediaFromArchive Archive
archive Text
mediaPath of
        Maybe ByteString
Nothing -> [Block] -> m [Block]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return []
        Just ByteString
mediaBytes -> do
          String -> Maybe Text -> ByteString -> m ()
forall (m :: * -> *).
PandocMonad m =>
String -> Maybe Text -> ByteString -> m ()
P.insertMedia (Text -> String
T.unpack Text
mediaPath) Maybe Text
forall a. Maybe a
Nothing ByteString
mediaBytes

          let altText :: [Inline]
altText = if Text -> Bool
T.null Text
alt then [] else [Text -> Inline
Str Text
alt]
          [Block] -> m [Block]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [[Inline] -> Block
Para [Attr -> [Inline] -> (Text, Text) -> Inline
Image Attr
nullAttr [Inline]
altText (Text
mediaPath, Text
title)]]

shapeToBlocks Archive
_archive [(Text, Text)]
_rels (PptxTable [[TableCell]]
rows) =
  case [[TableCell]] -> [[TableCell]]
spannedRows [[TableCell]]
rows of
    [] -> [Block] -> m [Block]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return []
    ([TableCell]
headerRow:[[TableCell]]
bodyRows) -> do
      let makeCell :: TableCell -> Cell
makeCell (TableCell Text
text Int
rs Int
cs Bool
_ Bool
_) =
            Attr -> Alignment -> RowSpan -> ColSpan -> [Block] -> Cell
Cell Attr
nullAttr Alignment
AlignDefault (Int -> RowSpan
RowSpan Int
rs) (Int -> ColSpan
ColSpan Int
cs)
                 [[Inline] -> Block
Plain (Inlines -> [Inline]
forall a. Many a -> [a]
B.toList (Text -> Inlines
B.text Text
text))]
          visible :: [TableCell] -> [Cell]
visible = (TableCell -> Cell) -> [TableCell] -> [Cell]
forall a b. (a -> b) -> [a] -> [b]
map TableCell -> Cell
makeCell ([TableCell] -> [Cell])
-> ([TableCell] -> [TableCell]) -> [TableCell] -> [Cell]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TableCell -> Bool) -> [TableCell] -> [TableCell]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (TableCell -> Bool) -> TableCell -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TableCell -> Bool
tcCovered)
          headerCells :: [Cell]
headerCells = [TableCell] -> [Cell]
visible [TableCell]
headerRow
          bodyCells :: [[Cell]]
bodyCells = ([TableCell] -> [Cell]) -> [[TableCell]] -> [[Cell]]
forall a b. (a -> b) -> [a] -> [b]
map [TableCell] -> [Cell]
visible [[TableCell]]
bodyRows
          caption :: Caption
caption = Maybe [Inline] -> [Block] -> Caption
Caption Maybe [Inline]
forall a. Maybe a
Nothing []
          colSpec :: [(Alignment, ColWidth)]
colSpec = Int -> (Alignment, ColWidth) -> [(Alignment, ColWidth)]
forall a. Int -> a -> [a]
replicate ([TableCell] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [TableCell]
headerRow) (Alignment
AlignDefault, ColWidth
ColWidthDefault)
          headerRow' :: Row
headerRow' = Attr -> [Cell] -> Row
Row Attr
nullAttr [Cell]
headerCells
          bodyRows' :: [Row]
bodyRows' = ([Cell] -> Row) -> [[Cell]] -> [Row]
forall a b. (a -> b) -> [a] -> [b]
map (Attr -> [Cell] -> Row
Row Attr
nullAttr) [[Cell]]
bodyCells
          thead :: TableHead
thead = Attr -> [Row] -> TableHead
TableHead Attr
nullAttr [Row
headerRow']
          tbody :: [TableBody]
tbody = [Attr -> RowHeadColumns -> [Row] -> [Row] -> TableBody
TableBody Attr
nullAttr RowHeadColumns
0 [] [Row]
bodyRows']
          tfoot :: TableFoot
tfoot = Attr -> [Row] -> TableFoot
TableFoot Attr
nullAttr []
      [Block] -> m [Block]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [Attr
-> Caption
-> [(Alignment, ColWidth)]
-> TableHead
-> [TableBody]
-> TableFoot
-> Block
Table Attr
nullAttr Caption
caption [(Alignment, ColWidth)]
colSpec TableHead
thead [TableBody]
tbody TableFoot
tfoot]

shapeToBlocks Archive
archive [(Text, Text)]
rels (PptxDiagramRef Text
dataRelId Text
layoutRelId) = do
  -- Parse SmartArt diagram
  case Archive
-> [(Text, Text)] -> Text -> Text -> Either Text PptxDiagram
parseDiagram Archive
archive [(Text, Text)]
rels Text
dataRelId Text
layoutRelId of
    Left Text
err -> do
      -- Failed to parse diagram, return placeholder
      [Block] -> m [Block]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [[Inline] -> Block
Para [Text -> Inline
Str (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ Text
"[Diagram parse error: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
err Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"]"]]
    Right PptxDiagram
diagram ->
      [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
$ PptxDiagram -> [Block]
diagramToBlocks PptxDiagram
diagram
shapeToBlocks Archive
archive [(Text, Text)]
rels (PptxChartRef Text
relId Text
uri) =
  -- A chart whose part can't be read keeps the placeholder
  [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
$ (Text -> [Block])
-> (Chart -> [Block]) -> Either Text Chart -> [Block]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ([Block] -> Text -> [Block]
forall a b. a -> b -> a
const [[Inline] -> Block
Para [Text -> Inline
Str (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ Text
"[Graphic: other: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
uri Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"]"]])
                  Chart -> [Block]
chartToBlocks (Either Text Chart -> [Block]) -> Either Text Chart -> [Block]
forall a b. (a -> b) -> a -> b
$ do
    target <- Either Text Text
-> (Text -> Either Text Text) -> Maybe Text -> Either Text Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Text -> Either Text Text
forall a b. a -> Either a b
Left (Text -> Either Text Text) -> Text -> Either Text Text
forall a b. (a -> b) -> a -> b
$ Text
"Relationship not found: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
relId) Text -> Either Text Text
forall a b. b -> Either a b
Right (Maybe Text -> Either Text Text) -> Maybe Text -> Either Text Text
forall a b. (a -> b) -> a -> b
$
              Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
relId [(Text, Text)]
rels
    readChart archive $ case T.unpack target of
      Char
'/':String
path -> String
path
      String
path -> ShowS
collapseFilePath (String
"ppt/slides" String -> ShowS
Posix.</> String
path)
shapeToBlocks Archive
_archive [(Text, Text)]
_rels (PptxGraphic Text
text) =
  -- Placeholder for other graphics (charts, etc.)
  [Block] -> m [Block]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return [[Inline] -> Block
Para [Text -> Inline
Str (Text -> Inline) -> Text -> Inline
forall a b. (a -> b) -> a -> b
$ Text
"[Graphic: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
text Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"]"]]

-- | Make the spans of a table's cells fit pandoc's table model. Each row
-- has a cell for every grid column, with the cells covered by a span
-- marked. As a cell can't span the header row and the body, the header
-- row's spans are kept to that row, and the body cells they covered
-- become empty cells of their own. Row spans are clipped to the rows
-- there are.
spannedRows :: [[TableCell]] -> [[TableCell]]
spannedRows :: [[TableCell]] -> [[TableCell]]
spannedRows [] = []
spannedRows ([TableCell]
header:[[TableCell]]
body) =
  (TableCell -> TableCell) -> [TableCell] -> [TableCell]
forall a b. (a -> b) -> [a] -> [b]
map (\TableCell
c -> TableCell
c{ tcRowSpan = 1 }) [TableCell]
header [TableCell] -> [[TableCell]] -> [[TableCell]]
forall a. a -> [a] -> [a]
: Set Int -> Int -> [[TableCell]] -> [[TableCell]]
forall {a}.
(Ord a, Num a, Enum a) =>
Set a -> Int -> [[TableCell]] -> [[TableCell]]
go Set Int
headerSpanCols ([[TableCell]] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[TableCell]]
body) [[TableCell]]
body
  where
    headerSpanCols :: Set Int
headerSpanCols = [Int] -> Set Int
forall a. Ord a => [a] -> Set a
S.fromList
      [ Int
col' | (Int
col, TableCell
c) <- [Int] -> [TableCell] -> [(Int, TableCell)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 :: Int ..] [TableCell]
header
             , TableCell -> Int
tcRowSpan TableCell
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1, Bool -> Bool
not (TableCell -> Bool
tcCovered TableCell
c)
             , Int
col' <- [Int
col .. Int
col Int -> Int -> Int
forall a. Num a => a -> a -> a
+ TableCell -> Int
tcColSpan TableCell
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ]
    -- cols: the columns still covered by a span from the header row;
    -- left: the number of rows from this one to the last
    go :: Set a -> Int -> [[TableCell]] -> [[TableCell]]
go Set a
_ Int
_ [] = []
    go Set a
cols Int
left ([TableCell]
row:[[TableCell]]
rows) =
      let cells :: [(a, TableCell)]
cells = [a] -> [TableCell] -> [(a, TableCell)]
forall a b. [a] -> [b] -> [(a, b)]
zip [a
0 ..] [TableCell]
row
          fix :: (a, TableCell) -> TableCell
fix (a
col, TableCell
c)
            | a
col a -> Set a -> Bool
forall a. Ord a => a -> Set a -> Bool
`S.member` Set a
cols Bool -> Bool -> Bool
&& TableCell -> Bool
tcVMerge TableCell
c =
                Text -> Int -> Int -> Bool -> Bool -> TableCell
TableCell Text
"" Int
1 Int
1 Bool
False Bool
False
            | Bool
otherwise = TableCell
c{ tcRowSpan = min (tcRowSpan c) left }
          stillCovered :: a -> Bool
stillCovered a
col = Bool -> (TableCell -> Bool) -> Maybe TableCell -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False TableCell -> Bool
tcVMerge (a -> [(a, TableCell)] -> Maybe TableCell
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup a
col [(a, TableCell)]
cells)
       in ((a, TableCell) -> TableCell) -> [(a, TableCell)] -> [TableCell]
forall a b. (a -> b) -> [a] -> [b]
map (a, TableCell) -> TableCell
fix [(a, TableCell)]
cells [TableCell] -> [[TableCell]] -> [[TableCell]]
forall a. a -> [a] -> [a]
: Set a -> Int -> [[TableCell]] -> [[TableCell]]
go ((a -> Bool) -> Set a -> Set a
forall a. (a -> Bool) -> Set a -> Set a
S.filter a -> Bool
stillCovered Set a
cols) (Int
left Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [[TableCell]]
rows

-- | Resolve media path (handle relative paths)
resolveMediaPath :: Text -> Text
resolveMediaPath :: Text -> Text
resolveMediaPath Text
target =
  if Text
"../media/" Text -> Text -> Bool
`T.isPrefixOf` Text
target
    then Text
"ppt/media/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
T.drop Int
9 Text
target  -- "../media/" = 9 chars
    else if Text
"media/" Text -> Text -> Bool
`T.isPrefixOf` Text
target
      then Text
"ppt/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
target
      else Text
target

-- | Load media file from archive
loadMediaFromArchive :: Archive -> Text -> Maybe B.ByteString
loadMediaFromArchive :: Archive -> Text -> Maybe ByteString
loadMediaFromArchive Archive
archive Text
path =
  case String -> Archive -> Maybe Entry
findEntryByPath (Text -> String
T.unpack Text
path) Archive
archive of
    Just Entry
entry -> ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ Entry -> ByteString
fromEntry Entry
entry
    Maybe Entry
Nothing -> Maybe ByteString
forall a. Maybe a
Nothing

-- | Parse paragraphs from text box
parseParagraphs :: (Int -> Maybe BulletType) -> NameSpaces -> Element
                -> [PptxParagraph]
parseParagraphs :: (Int -> Maybe BulletType)
-> NameSpaces -> Element -> [PptxParagraph]
parseParagraphs Int -> Maybe BulletType
inherited NameSpaces
ns Element
txBody =
  let pElems :: [Element]
pElems = NameSpaces -> Text -> Text -> Element -> [Element]
findChildrenByName NameSpaces
ns Text
"a" Text
"p" Element
txBody
   in (Element -> PptxParagraph) -> [Element] -> [PptxParagraph]
forall a b. (a -> b) -> [a] -> [b]
map ((Int -> Maybe BulletType) -> NameSpaces -> Element -> PptxParagraph
parseParagraph Int -> Maybe BulletType
inherited NameSpaces
ns) [Element]
pElems

-- | Parse individual paragraph
parseParagraph :: (Int -> Maybe BulletType) -> NameSpaces -> Element
               -> PptxParagraph
parseParagraph :: (Int -> Maybe BulletType) -> NameSpaces -> Element -> PptxParagraph
parseParagraph Int -> Maybe BulletType
inherited NameSpaces
ns Element
pElem =
  let level :: Int
level = NameSpaces -> Element -> Int
parseBulletLevel NameSpaces
ns Element
pElem
      bullet :: BulletType
bullet = Maybe BulletType -> NameSpaces -> Element -> BulletType
detectBulletType (Int -> Maybe BulletType
inherited Int
level) NameSpaces
ns Element
pElem
      text :: [Inline]
text = Element -> [Inline]
paragraphInlines Element
pElem
   in Int -> BulletType -> [Inline] -> PptxParagraph
PptxParagraph Int
level BulletType
bullet [Inline]
text

-- | Parse bullet level from paragraph properties
parseBulletLevel :: NameSpaces -> Element -> Int
parseBulletLevel :: NameSpaces -> Element -> Int
parseBulletLevel NameSpaces
ns Element
pElem =
  case NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"a" Text
"pPr" Element
pElem Maybe Element -> (Element -> Maybe Text) -> Maybe Text
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
       QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"lvl") Maybe Text -> (Text -> Maybe Int) -> Maybe Int
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
       (\Text
s -> String -> Maybe Int
forall a. Read a => String -> Maybe a
readMaybe (Text -> String
T.unpack Text
s) :: Maybe Int) of
    Just Int
lvl -> Int
lvl
    Maybe Int
Nothing -> Int
0  -- Default to level 0

-- | Detect bullet type: the paragraph's own, or else the one it inherits
detectBulletType :: Maybe BulletType -> NameSpaces -> Element -> BulletType
detectBulletType :: Maybe BulletType -> NameSpaces -> Element -> BulletType
detectBulletType Maybe BulletType
inherited NameSpaces
ns Element
pElem =
  case NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"a" Text
"pPr" Element
pElem Maybe Element -> (Element -> Maybe BulletType) -> Maybe BulletType
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Element -> Maybe BulletType
explicitBullet of
    Just BulletType
bullet -> BulletType
bullet
    Maybe BulletType
Nothing
      -- Check for Wingdings symbol (common in PowerPoint)
      | NameSpaces -> Element -> Bool
hasWingdingsSymbol NameSpaces
ns Element
pElem -> BulletType
WingdingsBullet
      | Bool
otherwise -> BulletType -> Maybe BulletType -> BulletType
forall a. a -> Maybe a -> a
fromMaybe BulletType
NoBullet Maybe BulletType
inherited

-- | The bullet that paragraph properties (@a:pPr@ or @a:lvlNpPr@) set,
-- if they set one
explicitBullet :: Element -> Maybe BulletType
explicitBullet :: Element -> Maybe BulletType
explicitBullet Element
pPr = [BulletType] -> Maybe BulletType
forall a. [a] -> Maybe a
listToMaybe ((Element -> Maybe BulletType) -> [Element] -> [BulletType]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe BulletType
bullet (Element -> [Element]
elChildren Element
pPr))
  where
    bullet :: Element -> Maybe BulletType
bullet Element
el = case QName -> Text
qName (Element -> QName
elName Element
el) of
      Text
"buChar"    -> BulletType -> Maybe BulletType
forall a. a -> Maybe a
Just BulletType
Bullet
      Text
"buBlip"    -> BulletType -> Maybe BulletType
forall a. a -> Maybe a
Just BulletType
Bullet
      Text
"buNone"    -> BulletType -> Maybe BulletType
forall a. a -> Maybe a
Just BulletType
NoBullet
      Text
"buAutoNum" -> BulletType -> Maybe BulletType
forall a. a -> Maybe a
Just (ListAttributes -> BulletType
Numbered (Element -> ListAttributes
forall {a}.
(Num a, Read a) =>
Element -> (a, ListNumberStyle, ListNumberDelim)
numbering Element
el))
      Text
_           -> Maybe BulletType
forall a. Maybe a
Nothing
    numbering :: Element -> (a, ListNumberStyle, ListNumberDelim)
numbering Element
el =
      let scheme :: Text
scheme = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"arabicPeriod" (QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"type") Element
el)
          start :: a
start = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
1 (Maybe a -> a) -> Maybe a -> a
forall a b. (a -> b) -> a -> b
$
                  QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"startAt") Element
el Maybe Text -> (Text -> Maybe a) -> Maybe a
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Maybe a
forall a. Read a => String -> Maybe a
readMaybe (String -> Maybe a) -> (Text -> String) -> Text -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack
          style :: ListNumberStyle
style | Text
"arabic" Text -> Text -> Bool
`T.isPrefixOf` Text
scheme = ListNumberStyle
Decimal
                | Text
"alphaLc" Text -> Text -> Bool
`T.isPrefixOf` Text
scheme = ListNumberStyle
LowerAlpha
                | Text
"alphaUc" Text -> Text -> Bool
`T.isPrefixOf` Text
scheme = ListNumberStyle
UpperAlpha
                | Text
"romanLc" Text -> Text -> Bool
`T.isPrefixOf` Text
scheme = ListNumberStyle
LowerRoman
                | Text
"romanUc" Text -> Text -> Bool
`T.isPrefixOf` Text
scheme = ListNumberStyle
UpperRoman
                | Bool
otherwise = ListNumberStyle
DefaultStyle
          delim :: ListNumberDelim
delim | Text
"ParenBoth" Text -> Text -> Bool
`T.isSuffixOf` Text
scheme = ListNumberDelim
TwoParens
                | Text
"ParenR" Text -> Text -> Bool
`T.isSuffixOf` Text
scheme = ListNumberDelim
OneParen
                | Text
"Period" Text -> Text -> Bool
`T.isSuffixOf` Text
scheme = ListNumberDelim
Period
                | Bool
otherwise = ListNumberDelim
DefaultDelim
       in (a
start, ListNumberStyle
style, ListNumberDelim
delim)

-- | The bullet a shape's paragraphs inherit at each level (0 for the
-- first), from the shape's own list style, then the matching placeholder
-- on the slide layout and on the slide master, then the master's text
-- style for that kind of placeholder (or for other text).
inheritedBullets :: Maybe Element  -- ^ Slide layout
                 -> Maybe Element  -- ^ Slide master
                 -> Element        -- ^ Shape
                 -> Int -> Maybe BulletType
inheritedBullets :: Maybe Element
-> Maybe Element -> Element -> Int -> Maybe BulletType
inheritedBullets Maybe Element
layout Maybe Element
master Element
shape Int
level =
  [BulletType] -> Maybe BulletType
forall a. [a] -> Maybe a
listToMaybe ([BulletType] -> Maybe BulletType)
-> [BulletType] -> Maybe BulletType
forall a b. (a -> b) -> a -> b
$ (Maybe Element -> Maybe BulletType)
-> [Maybe Element] -> [BulletType]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Maybe Element -> (Element -> Maybe BulletType) -> Maybe BulletType
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Element -> Maybe BulletType
explicitBullet) ([Maybe Element] -> [BulletType])
-> [Maybe Element] -> [BulletType]
forall a b. (a -> b) -> a -> b
$
    [Element -> Maybe Element
lstLevel Element
shape] [Maybe Element] -> [Maybe Element] -> [Maybe Element]
forall a. [a] -> [a] -> [a]
++
    case Element -> Maybe (Text, Maybe Text)
placeholder Element
shape of
      Just (Text
phType, Maybe Text
phIdx) ->
        [ Maybe Element
layout 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
>>= Text -> Maybe Text -> Element -> Maybe Element
findPlaceholder Text
phType Maybe Text
phIdx 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
>>= Element -> Maybe Element
lstLevel
        , Maybe Element
master 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
>>= Text -> Maybe Text -> Element -> Maybe Element
findPlaceholder (Text -> Text
forall {a}. (Eq a, IsString a) => a -> a
masterType Text
phType) Maybe Text
forall a. Maybe a
Nothing 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
>>= Element -> Maybe Element
lstLevel
        , Text -> Maybe Element
textStyle (Text -> Text
forall {a} {a}. (Eq a, IsString a, IsString a) => a -> a
styleFor Text
phType) ]
      Maybe (Text, Maybe Text)
Nothing -> [Text -> Maybe Element
textStyle Text
"otherStyle"]
  where
    lvlName :: Text
lvlName = Text
"lvl" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (Int -> String
forall a. Show a => a -> String
show (Int
level Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"pPr"
    child :: Text -> Element -> Maybe Element
child Text
name Element
el = (Element -> Bool) -> [Element] -> Maybe Element
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
name) (Text -> Bool) -> (Element -> Text) -> Element -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QName -> Text
qName (QName -> Text) -> (Element -> QName) -> Element -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> QName
elName) (Element -> [Element]
elChildren Element
el)
    lstLevel :: Element -> Maybe Element
lstLevel Element
sp = Text -> Element -> Maybe Element
child Text
"txBody" Element
sp 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
>>= Text -> Element -> Maybe Element
child Text
"lstStyle" 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
>>= Text -> Element -> Maybe Element
child Text
lvlName
    textStyle :: Text -> Maybe Element
textStyle Text
name = Maybe Element
master 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
>>= Text -> Element -> Maybe Element
child Text
"txStyles" 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
>>= Text -> Element -> Maybe Element
child Text
name 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
>>=
                     Text -> Element -> Maybe Element
child Text
lvlName
    placeholder :: Element -> Maybe (Text, Maybe Text)
placeholder Element
sp = do
      ph <- Text -> Element -> Maybe Element
child Text
"nvSpPr" Element
sp 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
>>= Text -> Element -> Maybe Element
child 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
>>= Text -> Element -> Maybe Element
child Text
"ph"
      return ( fromMaybe "obj" (findAttr (unqual "type") ph)
             , findAttr (unqual "idx") ph )
    -- A placeholder is matched by index if it has one, else by type
    findPlaceholder :: Text -> Maybe Text -> Element -> Maybe Element
findPlaceholder Text
phType Maybe Text
phIdx Element
root = do
      tree <- Text -> Element -> Maybe Element
child Text
"cSld" Element
root 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
>>= Text -> Element -> Maybe Element
child Text
"spTree"
      let phs = [ (Element
sp, (Text, Maybe Text)
p) | Element
sp <- Element -> [Element]
elChildren Element
tree
                          , Just (Text, Maybe Text)
p <- [Element -> Maybe (Text, Maybe Text)
placeholder Element
sp] ]
          byIdx = [ Element
sp | Just Text
i <- [Maybe Text
phIdx], (Element
sp, (Text
_, Maybe Text
j)) <- [(Element, (Text, Maybe Text))]
phs, Maybe Text
j Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Maybe Text
forall a. a -> Maybe a
Just Text
i ]
          byType = [ Element
sp | (Element
sp, (Text
t, Maybe Text
_)) <- [(Element, (Text, Maybe Text))]
phs, Text -> Text
forall {a}. (Eq a, IsString a) => a -> a
masterType Text
t Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
phType Bool -> Bool -> Bool
||
                                              Text
t Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
phType ]
      listToMaybe (byIdx ++ byType)
    masterType :: a -> a
masterType a
t = if a
t a -> [a] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [a
"obj", a
"body"] then a
"body" else a
t
    styleFor :: a -> a
styleFor a
t
      | a
t a -> [a] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [a
"title", a
"ctrTitle"] = a
"titleStyle"
      | a
t a -> [a] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [a
"obj", a
"body", a
"subTitle"] = a
"bodyStyle"
      | Bool
otherwise = a
"otherStyle"

-- | Check if paragraph starts with Wingdings symbol
hasWingdingsSymbol :: NameSpaces -> Element -> Bool
hasWingdingsSymbol :: NameSpaces -> Element -> Bool
hasWingdingsSymbol NameSpaces
ns Element
pElem =
  let runs :: [Element]
runs = NameSpaces -> Text -> Text -> Element -> [Element]
findChildrenByName NameSpaces
ns Text
"a" Text
"r" Element
pElem
      checkRun :: Element -> Bool
checkRun Element
r = case NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"a" Text
"rPr" Element
r 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
"a" Text
"sym" of
                     Just Element
symElem ->
                       case QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"typeface") Element
symElem of
                         Just Text
typeface -> Text
"Wingdings" Text -> Text -> Bool
`T.isInfixOf` Text
typeface
                         Maybe Text
Nothing -> Bool
False
                     Maybe Element
Nothing -> Bool
False
   in (Element -> Bool) -> [Element] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Element -> Bool
checkRun [Element]
runs

-- | Inlines of a paragraph: the text of its runs and fields with their
-- bold, italic, superscript and subscript, its line breaks, its
-- hyperlinks, and its equations (@a14:m@) as math. A hyperlink's target
-- is a relationship id at this point, which 'resolveLinks' replaces.
paragraphInlines :: Element -> [Inline]
paragraphInlines :: Element -> [Inline]
paragraphInlines Element
pElem =
  Inlines -> [Inline]
forall a. Many a -> [a]
B.toList (Inlines -> [Inline]) -> Inlines -> [Inline]
forall a b. (a -> b) -> a -> b
$ ([(Maybe Text, Either (RunFormat, Text) Inlines)] -> Inlines)
-> [[(Maybe Text, Either (RunFormat, Text) Inlines)]] -> Inlines
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap [(Maybe Text, Either (RunFormat, Text) Inlines)] -> Inlines
linked ([[(Maybe Text, Either (RunFormat, Text) Inlines)]] -> Inlines)
-> [[(Maybe Text, Either (RunFormat, Text) Inlines)]] -> Inlines
forall a b. (a -> b) -> a -> b
$ ((Maybe Text, Either (RunFormat, Text) Inlines)
 -> (Maybe Text, Either (RunFormat, Text) Inlines) -> Bool)
-> [(Maybe Text, Either (RunFormat, Text) Inlines)]
-> [[(Maybe Text, Either (RunFormat, Text) Inlines)]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (\(Maybe Text, Either (RunFormat, Text) Inlines)
a (Maybe Text, Either (RunFormat, Text) Inlines)
b -> (Maybe Text, Either (RunFormat, Text) Inlines) -> Maybe Text
forall a b. (a, b) -> a
fst (Maybe Text, Either (RunFormat, Text) Inlines)
a Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
== (Maybe Text, Either (RunFormat, Text) Inlines) -> Maybe Text
forall a b. (a, b) -> a
fst (Maybe Text, Either (RunFormat, Text) Inlines)
b) ([(Maybe Text, Either (RunFormat, Text) Inlines)]
 -> [[(Maybe Text, Either (RunFormat, Text) Inlines)]])
-> [(Maybe Text, Either (RunFormat, Text) Inlines)]
-> [[(Maybe Text, Either (RunFormat, Text) Inlines)]]
forall a b. (a -> b) -> a -> b
$
    (Element -> [(Maybe Text, Either (RunFormat, Text) Inlines)])
-> [Element] -> [(Maybe Text, Either (RunFormat, Text) Inlines)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Element -> [(Maybe Text, Either (RunFormat, Text) Inlines)]
piece (Element -> [Element]
elChildren Element
pElem)
  where
    piece :: Element -> [(Maybe Text, Either (RunFormat, Text) Inlines)]
piece Element
el = case QName -> Text
qName (Element -> QName
elName Element
el) of
      Text
"r"   -> [(Element -> Maybe Text
runLink Element
el, (RunFormat, Text) -> Either (RunFormat, Text) Inlines
forall a b. a -> Either a b
Left (Element -> RunFormat
runFormat Element
el, Element -> Text
runText Element
el))]
      Text
"fld" -> [(Element -> Maybe Text
runLink Element
el, (RunFormat, Text) -> Either (RunFormat, Text) Inlines
forall a b. a -> Either a b
Left (Element -> RunFormat
runFormat Element
el, Element -> Text
runText Element
el))]
      Text
"br"  -> [(Maybe Text
forall a. Maybe a
Nothing, Inlines -> Either (RunFormat, Text) Inlines
forall a b. b -> Either a b
Right Inlines
B.linebreak)]
      Text
"m"   -> [(Maybe Text
forall a. Maybe a
Nothing, Inlines -> Either (RunFormat, Text) Inlines
forall a b. b -> Either a b
Right ((Element -> Inlines) -> [Element] -> Inlines
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Element -> Inlines
mathInlines (Element -> [Element]
elChildren Element
el)))]
      Text
_     -> []
    linked :: [(Maybe Text, Either (RunFormat, Text) Inlines)] -> Inlines
linked [(Maybe Text, Either (RunFormat, Text) Inlines)]
grp =
      let inls :: Inlines
inls = ([Either (RunFormat, Text) Inlines] -> Inlines)
-> [[Either (RunFormat, Text) Inlines]] -> Inlines
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap [Either (RunFormat, Text) Inlines] -> Inlines
formatted ([[Either (RunFormat, Text) Inlines]] -> Inlines)
-> [[Either (RunFormat, Text) Inlines]] -> Inlines
forall a b. (a -> b) -> a -> b
$ (Either (RunFormat, Text) Inlines
 -> Either (RunFormat, Text) Inlines -> Bool)
-> [Either (RunFormat, Text) Inlines]
-> [[Either (RunFormat, Text) Inlines]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy Either (RunFormat, Text) Inlines
-> Either (RunFormat, Text) Inlines -> Bool
forall {a} {b} {b} {b} {b}.
Eq a =>
Either (a, b) b -> Either (a, b) b -> Bool
sameFormat (((Maybe Text, Either (RunFormat, Text) Inlines)
 -> Either (RunFormat, Text) Inlines)
-> [(Maybe Text, Either (RunFormat, Text) Inlines)]
-> [Either (RunFormat, Text) Inlines]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe Text, Either (RunFormat, Text) Inlines)
-> Either (RunFormat, Text) Inlines
forall a b. (a, b) -> b
snd [(Maybe Text, Either (RunFormat, Text) Inlines)]
grp)
       in case [(Maybe Text, Either (RunFormat, Text) Inlines)]
grp of
            (Just Text
relId, Either (RunFormat, Text) Inlines
_) : [(Maybe Text, Either (RunFormat, Text) Inlines)]
_ ->
              Attr -> Text -> Text -> Inlines -> Inlines
B.linkWith (Text
"", [], [(Text
linkRelAttr, Text
relId)]) Text
"" Text
"" Inlines
inls
            [(Maybe Text, Either (RunFormat, Text) Inlines)]
_ -> Inlines
inls
    sameFormat :: Either (a, b) b -> Either (a, b) b -> Bool
sameFormat (Left (a
f, b
_)) (Left (a
g, b
_)) = a
f a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
g
    sameFormat Either (a, b) b
_ Either (a, b) b
_ = Bool
False
    -- Runs with the same format are joined before it is applied, so
    -- that adjacent bold runs make one strong span.
    formatted :: [Either (RunFormat, Text) Inlines] -> Inlines
formatted pieces :: [Either (RunFormat, Text) Inlines]
pieces@(Left (RunFormat
fmt, Text
_) : [Either (RunFormat, Text) Inlines]
_) =
      RunFormat -> Text -> Inlines
applyFormat RunFormat
fmt ([Text] -> Text
T.concat [Text
t | Left (RunFormat
_, Text
t) <- [Either (RunFormat, Text) Inlines]
pieces])
    formatted [Either (RunFormat, Text) Inlines]
pieces = [Inlines] -> Inlines
forall a. Monoid a => [a] -> a
mconcat [Inlines
ils | Right Inlines
ils <- [Either (RunFormat, Text) Inlines]
pieces]

-- | The format of a run: bold, italic, and its baseline shift (positive
-- for superscript, negative for subscript)
type RunFormat = (Bool, Bool, Int)

runFormat :: Element -> RunFormat
runFormat :: Element -> RunFormat
runFormat Element
run =
  case (Element -> Bool) -> [Element] -> Maybe Element
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"rPr") (Text -> Bool) -> (Element -> Text) -> Element -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QName -> Text
qName (QName -> Text) -> (Element -> QName) -> Element -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> QName
elName) (Element -> [Element]
elChildren Element
run) of
    Maybe Element
Nothing -> (Bool
False, Bool
False, Int
0)
    Just Element
rPr ->
      ( Text -> Element -> Bool
flag Text
"b" Element
rPr, Text -> Element -> Bool
flag Text
"i" Element
rPr
      , Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
0 (Maybe Int -> Int) -> Maybe Int -> Int
forall a b. (a -> b) -> a -> b
$
          QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"baseline") Element
rPr Maybe Text -> (Text -> Maybe Int) -> Maybe Int
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Maybe Int
forall a. Read a => String -> Maybe a
readMaybe (String -> Maybe Int) -> (Text -> String) -> Text -> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
T.unpack )
  where
    flag :: Text -> Element -> Bool
flag Text
name Element
el = QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
name) Element
el 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
forall a. a -> Maybe a
Just Text
"1", Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"true"]

runText :: Element -> Text
runText :: Element -> Text
runText = [Text] -> Text
T.concat ([Text] -> Text) -> (Element -> [Text]) -> Element -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Element -> Text) -> [Element] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Element -> Text
strContent ([Element] -> [Text])
-> (Element -> [Element]) -> Element -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
          (Element -> Bool) -> [Element] -> [Element]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"t") (Text -> Bool) -> (Element -> Text) -> Element -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QName -> Text
qName (QName -> Text) -> (Element -> QName) -> Element -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> QName
elName) ([Element] -> [Element])
-> (Element -> [Element]) -> Element -> [Element]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> [Element]
elChildren

-- | Text with a run format. Spaces at the edges stay outside the
-- formatting, as Markdown emphasis can't start or end with a space.
applyFormat :: RunFormat -> Text -> B.Inlines
applyFormat :: RunFormat -> Text -> Inlines
applyFormat (Bool
bold, Bool
italic, Int
baseline) Text
txt
  | Bool -> Bool
not (Bool
bold Bool -> Bool -> Bool
|| Bool
italic Bool -> Bool -> Bool
|| Int
baseline Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0) Bool -> Bool -> Bool
|| Text -> Bool
T.null Text
core = Text -> Inlines
B.text Text
txt
  | Bool
otherwise = Text -> Inlines
edge Text
leading Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> Inlines -> Inlines
wrap (Text -> Inlines
B.text Text
core) Inlines -> Inlines -> Inlines
forall a. Semigroup a => a -> a -> a
<> Text -> Inlines
edge Text
trailing
  where
    isBlank :: Char -> Bool
isBlank = (Char -> String -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` (String
" \t\r\n" :: String))
    core :: Text
core = (Char -> Bool) -> Text -> Text
T.dropAround Char -> Bool
isBlank Text
txt
    leading :: Text
leading = (Char -> Bool) -> Text -> Text
T.takeWhile Char -> Bool
isBlank Text
txt
    trailing :: Text
trailing = (Char -> Bool) -> Text -> Text
T.takeWhileEnd Char -> Bool
isBlank Text
txt
    edge :: Text -> Inlines
edge Text
t = if Text -> Bool
T.null Text
t then Inlines
forall a. Monoid a => a
mempty else Inlines
B.space
    wrap :: Inlines -> Inlines
wrap = (if Bool
bold then Inlines -> Inlines
B.strong else Inlines -> Inlines
forall a. a -> a
id) (Inlines -> Inlines) -> (Inlines -> Inlines) -> Inlines -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
           (if Bool
italic then Inlines -> Inlines
B.emph else Inlines -> Inlines
forall a. a -> a
id) (Inlines -> Inlines) -> (Inlines -> Inlines) -> Inlines -> Inlines
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
           (if Int
baseline Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then Inlines -> Inlines
B.superscript
              else if Int
baseline Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 then Inlines -> Inlines
B.subscript else Inlines -> Inlines
forall a. a -> a
id)

-- | The relationship id of a run's hyperlink (@a:hlinkClick@). Links
-- that act within the presentation, such as a jump to another slide,
-- carry an @action@ and are not kept.
runLink :: Element -> Maybe Text
runLink :: Element -> Maybe Text
runLink Element
run = do
  rPr <- (Element -> Bool) -> [Element] -> Maybe Element
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"rPr") (Text -> Bool) -> (Element -> Text) -> Element -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QName -> Text
qName (QName -> Text) -> (Element -> QName) -> Element -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> QName
elName) (Element -> [Element]
elChildren Element
run)
  link <- find ((== "hlinkClick") . qName . elName) (elChildren rPr)
  case findAttr (unqual "action") link of
    Just Text
_ -> Maybe Text
forall a. Maybe a
Nothing
    Maybe Text
Nothing -> (Attr -> Text) -> Maybe Attr -> Maybe Text
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Attr -> Text
attrVal (Maybe Attr -> Maybe Text) -> Maybe Attr -> Maybe Text
forall a b. (a -> b) -> a -> b
$ (Attr -> Bool) -> [Attr] -> Maybe Attr
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find Attr -> Bool
isRelId (Element -> [Attr]
elAttribs Element
link)
  where
    isRelId :: Attr -> Bool
isRelId Attr
a = QName -> Text
qName (Attr -> QName
attrKey Attr
a) Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"id" Bool -> Bool -> Bool
&& QName -> Maybe Text
qURI (Attr -> QName
attrKey Attr
a) Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Maybe Text
forall a. a -> Maybe a
Just Text
relNs
    relNs :: Text
relNs =
      Text
"http://schemas.openxmlformats.org/officeDocument/2006/relationships"

-- | The attribute that marks a hyperlink whose target is still a
-- relationship id
linkRelAttr :: Text
linkRelAttr :: Text
linkRelAttr = Text
"pptx-relationship"

-- | Replace the relationship id of each hyperlink read by
-- 'paragraphInlines' with the relationship's target. A link whose
-- relationship is missing is replaced by its text.
resolveLinks :: [(Text, Text)] -> [Block] -> [Block]
resolveLinks :: [(Text, Text)] -> [Block] -> [Block]
resolveLinks [(Text, Text)]
rels = (Inline -> Inline) -> [Block] -> [Block]
forall a b. Walkable a b => (a -> a) -> b -> b
walk Inline -> Inline
resolve
  where
    resolve :: Inline -> Inline
resolve (Link (Text
_, [Text]
_, [(Text
key, Text
relId)]) [Inline]
inls (Text, Text)
_)
      | Text
key Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
linkRelAttr =
          case Text -> [(Text, Text)] -> Maybe Text
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Text
relId [(Text, Text)]
rels of
            Just Text
target -> Attr -> [Inline] -> (Text, Text) -> Inline
Link Attr
nullAttr [Inline]
inls (Text
target, Text
"")
            Maybe Text
Nothing -> Attr -> [Inline] -> Inline
Span Attr
nullAttr [Inline]
inls
    resolve Inline
x = Inline
x

-- | An equation (@m:oMathPara@ or @m:oMath@) as math. If texmath can't
-- read it, its text is kept.
mathInlines :: Element -> B.Inlines
mathInlines :: Element -> Inlines
mathInlines Element
el =
  case Text -> Either Text [Exp]
readOMML (Element -> Text
showElement Element
el) of
    Right [Exp]
exps
      | QName -> Text
qName (Element -> QName
elName Element
el) Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"oMathPara" -> Text -> Inlines
B.displayMath ([Exp] -> Text
writeTeX [Exp]
exps)
      | Bool
otherwise -> Text -> Inlines
B.math ([Exp] -> Text
writeTeX [Exp]
exps)
    Left Text
_ -> Text -> Inlines
B.text (Text -> Inlines) -> Text -> Inlines
forall a b. (a -> b) -> a -> b
$ [Text] -> Text
T.concat ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (Element -> Text) -> [Element] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Element -> Text
strContent ([Element] -> [Text]) -> [Element] -> [Text]
forall a b. (a -> b) -> a -> b
$
                (QName -> Bool) -> Element -> [Element]
filterElementsName ((Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"t") (Text -> Bool) -> (QName -> Text) -> QName -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QName -> Text
qName) Element
el

-- | Extract text from DrawingML element (all its paragraphs)
extractDrawingMLText :: Element -> Text
extractDrawingMLText :: Element -> Text
extractDrawingMLText = Element -> Text
drawingMLText

-- | Convert paragraphs to blocks, grouping bulleted and numbered
-- paragraphs into lists. Paragraphs at a deeper level than a list item
-- go inside that item, as a nested list or as further paragraphs.
paragraphsToBlocks :: [PptxParagraph] -> [Block]
paragraphsToBlocks :: [PptxParagraph] -> [Block]
paragraphsToBlocks [] = []
paragraphsToBlocks (PptxParagraph
p:[PptxParagraph]
ps)
  | PptxParagraph -> BulletType
paraBullet PptxParagraph
p BulletType -> BulletType -> Bool
forall a. Eq a => a -> a -> Bool
== BulletType
NoBullet = [Inline] -> Block
Para (PptxParagraph -> [Inline]
paraText PptxParagraph
p) Block -> [Block] -> [Block]
forall a. a -> [a] -> [a]
: [PptxParagraph] -> [Block]
paragraphsToBlocks [PptxParagraph]
ps
  -- PowerPoint shows no bullet or number on an empty paragraph
  | PptxParagraph -> Bool
isEmpty PptxParagraph
p = [PptxParagraph] -> [Block]
paragraphsToBlocks [PptxParagraph]
ps
  | Bool
otherwise =
      let ([[Block]]
items, [PptxParagraph]
rest) = PptxParagraph -> [PptxParagraph] -> ([[Block]], [PptxParagraph])
listItems PptxParagraph
p (PptxParagraph
pPptxParagraph -> [PptxParagraph] -> [PptxParagraph]
forall a. a -> [a] -> [a]
:[PptxParagraph]
ps)
       in BulletType -> [[Block]] -> Block
listBlock (PptxParagraph -> BulletType
paraBullet PptxParagraph
p) [[Block]]
items Block -> [Block] -> [Block]
forall a. a -> [a] -> [a]
: [PptxParagraph] -> [Block]
paragraphsToBlocks [PptxParagraph]
rest
  where
    -- The items of a list that starts with the paragraph first, with the
    -- deeper paragraphs that follow each item
    listItems :: PptxParagraph -> [PptxParagraph] -> ([[Block]], [PptxParagraph])
listItems PptxParagraph
first (PptxParagraph
q:[PptxParagraph]
qs)
      | PptxParagraph -> Bool
isEmpty PptxParagraph
q, PptxParagraph -> BulletType
paraBullet PptxParagraph
q BulletType -> BulletType -> Bool
forall a. Eq a => a -> a -> Bool
/= BulletType
NoBullet = PptxParagraph -> [PptxParagraph] -> ([[Block]], [PptxParagraph])
listItems PptxParagraph
first [PptxParagraph]
qs
      | PptxParagraph -> Int
paraLevel PptxParagraph
q Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== PptxParagraph -> Int
paraLevel PptxParagraph
first, PptxParagraph -> PptxParagraph -> Bool
sameKind PptxParagraph
q PptxParagraph
first =
          let ([PptxParagraph]
inner, [PptxParagraph]
rest) = (PptxParagraph -> Bool)
-> [PptxParagraph] -> ([PptxParagraph], [PptxParagraph])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span ((Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> PptxParagraph -> Int
paraLevel PptxParagraph
first) (Int -> Bool) -> (PptxParagraph -> Int) -> PptxParagraph -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PptxParagraph -> Int
paraLevel) [PptxParagraph]
qs
              ([[Block]]
items, [PptxParagraph]
rest') = PptxParagraph -> [PptxParagraph] -> ([[Block]], [PptxParagraph])
listItems PptxParagraph
first [PptxParagraph]
rest
           in (([Inline] -> Block
Plain (PptxParagraph -> [Inline]
paraText PptxParagraph
q) Block -> [Block] -> [Block]
forall a. a -> [a] -> [a]
: [PptxParagraph] -> [Block]
paragraphsToBlocks [PptxParagraph]
inner) [Block] -> [[Block]] -> [[Block]]
forall a. a -> [a] -> [a]
: [[Block]]
items, [PptxParagraph]
rest')
    listItems PptxParagraph
_ [PptxParagraph]
qs = ([], [PptxParagraph]
qs)
    sameKind :: PptxParagraph -> PptxParagraph -> Bool
sameKind PptxParagraph
a PptxParagraph
b = case (PptxParagraph -> BulletType
paraBullet PptxParagraph
a, PptxParagraph -> BulletType
paraBullet PptxParagraph
b) of
      (Numbered ListAttributes
_, Numbered ListAttributes
_) -> Bool
True
      (Numbered ListAttributes
_, BulletType
_) -> Bool
False
      (BulletType
_, Numbered ListAttributes
_) -> Bool
False
      (BulletType
x, BulletType
y) -> (BulletType
x BulletType -> BulletType -> Bool
forall a. Eq a => a -> a -> Bool
== BulletType
NoBullet) Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== (BulletType
y BulletType -> BulletType -> Bool
forall a. Eq a => a -> a -> Bool
== BulletType
NoBullet)
    listBlock :: BulletType -> [[Block]] -> Block
listBlock (Numbered ListAttributes
attrs) [[Block]]
items = ListAttributes -> [[Block]] -> Block
OrderedList ListAttributes
attrs [[Block]]
items
    listBlock BulletType
_ [[Block]]
items = [[Block]] -> Block
BulletList [[Block]]
items
    isEmpty :: PptxParagraph -> Bool
isEmpty = (Inline -> Bool) -> [Inline] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Inline -> Bool
blank ([Inline] -> Bool)
-> (PptxParagraph -> [Inline]) -> PptxParagraph -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PptxParagraph -> [Inline]
paraText
    blank :: Inline -> Bool
blank Inline
il = case Inline
il of
      Inline
Space -> Bool
True
      Inline
SoftBreak -> Bool
True
      Inline
LineBreak -> Bool
True
      Str Text
t -> (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isSpace Text
t
      Inline
_ -> Bool
False

-- | Check if shape is a slide number placeholder
isSlideNumberPlaceholder :: NameSpaces -> Element -> Bool
isSlideNumberPlaceholder :: NameSpaces -> Element -> Bool
isSlideNumberPlaceholder NameSpaces
ns Element
el =
  Bool -> (Element -> Bool) -> Maybe Element -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False ((Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"sldNum") (Maybe Text -> Bool) -> (Element -> Maybe Text) -> Element -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"type")) (Maybe Element -> Bool) -> Maybe Element -> Bool
forall a b. (a -> b) -> a -> b
$
    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"

-- | Check if shape is title placeholder (also used in Slides module)
isTitlePlaceholder :: NameSpaces -> Element -> Bool
isTitlePlaceholder :: NameSpaces -> Element -> Bool
isTitlePlaceholder NameSpaces
ns 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
phElem ->
      case QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"type") Element
phElem of
        Just Text
phType -> Text
phType Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"title" Bool -> Bool -> Bool
|| Text
phType Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"ctrTitle"
        Maybe Text
Nothing -> Bool
False
    Maybe Element
Nothing -> Bool
False

-- | The graphic data URI of a chart
chartUri :: Text
chartUri :: Text
chartUri = Text
"http://schemas.openxmlformats.org/drawingml/2006/chart"