{-# LANGUAGE OverloadedStrings #-}
module Text.Pandoc.Readers.Pptx.Parse
( Pptx(..)
, PresentationDoc(..)
, PptxSlide(..)
, SlideId(..)
, archiveToPptx
) where
import Codec.Archive.Zip (Archive, Entry, findEntryByPath, fromEntry)
import qualified Data.ByteString.Lazy as B
import Data.List (find)
import Data.Maybe (mapMaybe)
import qualified Data.Text as T
import qualified Data.Text.Lazy.Encoding as TL
import Data.Text (Text)
import System.FilePath (splitFileName)
import qualified System.FilePath.Posix as Posix
import Text.Pandoc.Readers.OOXML.Shared
import Text.Pandoc.Shared (collapseFilePath)
import Text.Pandoc.XML.Light
import Text.Read (readMaybe)
newtype SlideId = SlideId Int deriving (Int -> SlideId -> ShowS
[SlideId] -> ShowS
SlideId -> [Char]
(Int -> SlideId -> ShowS)
-> (SlideId -> [Char]) -> ([SlideId] -> ShowS) -> Show SlideId
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SlideId -> ShowS
showsPrec :: Int -> SlideId -> ShowS
$cshow :: SlideId -> [Char]
show :: SlideId -> [Char]
$cshowList :: [SlideId] -> ShowS
showList :: [SlideId] -> ShowS
Show, SlideId -> SlideId -> Bool
(SlideId -> SlideId -> Bool)
-> (SlideId -> SlideId -> Bool) -> Eq SlideId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SlideId -> SlideId -> Bool
== :: SlideId -> SlideId -> Bool
$c/= :: SlideId -> SlideId -> Bool
/= :: SlideId -> SlideId -> Bool
Eq, Eq SlideId
Eq SlideId =>
(SlideId -> SlideId -> Ordering)
-> (SlideId -> SlideId -> Bool)
-> (SlideId -> SlideId -> Bool)
-> (SlideId -> SlideId -> Bool)
-> (SlideId -> SlideId -> Bool)
-> (SlideId -> SlideId -> SlideId)
-> (SlideId -> SlideId -> SlideId)
-> Ord SlideId
SlideId -> SlideId -> Bool
SlideId -> SlideId -> Ordering
SlideId -> SlideId -> SlideId
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: SlideId -> SlideId -> Ordering
compare :: SlideId -> SlideId -> Ordering
$c< :: SlideId -> SlideId -> Bool
< :: SlideId -> SlideId -> Bool
$c<= :: SlideId -> SlideId -> Bool
<= :: SlideId -> SlideId -> Bool
$c> :: SlideId -> SlideId -> Bool
> :: SlideId -> SlideId -> Bool
$c>= :: SlideId -> SlideId -> Bool
>= :: SlideId -> SlideId -> Bool
$cmax :: SlideId -> SlideId -> SlideId
max :: SlideId -> SlideId -> SlideId
$cmin :: SlideId -> SlideId -> SlideId
min :: SlideId -> SlideId -> SlideId
Ord)
data Pptx = Pptx
{ Pptx -> PresentationDoc
pptxPresentation :: PresentationDoc
, Pptx -> [PptxSlide]
pptxSlides :: [PptxSlide]
, Pptx -> Archive
pptxArchive :: Archive
} deriving (Int -> Pptx -> ShowS
[Pptx] -> ShowS
Pptx -> [Char]
(Int -> Pptx -> ShowS)
-> (Pptx -> [Char]) -> ([Pptx] -> ShowS) -> Show Pptx
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Pptx -> ShowS
showsPrec :: Int -> Pptx -> ShowS
$cshow :: Pptx -> [Char]
show :: Pptx -> [Char]
$cshowList :: [Pptx] -> ShowS
showList :: [Pptx] -> ShowS
Show)
data PptxSlide = PptxSlide
{ PptxSlide -> SlideId
slideId :: SlideId
, PptxSlide -> [Char]
slidePath :: FilePath
, PptxSlide -> Element
slideElement :: Element
, PptxSlide -> [(Text, Text)]
slideRels :: [(Text, Text)]
, PptxSlide -> Maybe Element
slideNotes :: Maybe Element
, PptxSlide -> Maybe Element
slideLayout :: Maybe Element
, PptxSlide -> Maybe Element
slideMaster :: Maybe Element
} deriving (Int -> PptxSlide -> ShowS
[PptxSlide] -> ShowS
PptxSlide -> [Char]
(Int -> PptxSlide -> ShowS)
-> (PptxSlide -> [Char])
-> ([PptxSlide] -> ShowS)
-> Show PptxSlide
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PptxSlide -> ShowS
showsPrec :: Int -> PptxSlide -> ShowS
$cshow :: PptxSlide -> [Char]
show :: PptxSlide -> [Char]
$cshowList :: [PptxSlide] -> ShowS
showList :: [PptxSlide] -> ShowS
Show)
data PresentationDoc = PresentationDoc
{ PresentationDoc -> NameSpaces
presNameSpaces :: NameSpaces
, PresentationDoc -> (Integer, Integer)
presSlideSize :: (Integer, Integer)
, PresentationDoc -> [(SlideId, Text)]
presSlideIds :: [(SlideId, Text)]
} deriving (Int -> PresentationDoc -> ShowS
[PresentationDoc] -> ShowS
PresentationDoc -> [Char]
(Int -> PresentationDoc -> ShowS)
-> (PresentationDoc -> [Char])
-> ([PresentationDoc] -> ShowS)
-> Show PresentationDoc
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PresentationDoc -> ShowS
showsPrec :: Int -> PresentationDoc -> ShowS
$cshow :: PresentationDoc -> [Char]
show :: PresentationDoc -> [Char]
$cshowList :: [PresentationDoc] -> ShowS
showList :: [PresentationDoc] -> ShowS
Show)
archiveToPptx :: Archive -> Either Text Pptx
archiveToPptx :: Archive -> Either Text Pptx
archiveToPptx Archive
archive = do
presPath <- Archive -> Either Text [Char]
getPresentationXmlPath Archive
archive
presElem <- loadXMLFromArchive archive presPath
presDoc <- elemToPresentation presElem
presRelsPath <- getPresentationRelsPath archive presPath
presRels <- loadRelationships archive presRelsPath
slides <- mapM (parseSlide archive presRels) (presSlideIds presDoc)
return $ Pptx presDoc slides archive
getPresentationXmlPath :: Archive -> Either Text FilePath
getPresentationXmlPath :: Archive -> Either Text [Char]
getPresentationXmlPath Archive
archive = do
relsEntry <- Text -> Maybe Entry -> Either Text Entry
forall a. Text -> Maybe a -> Either Text a
maybeToEither Text
"Missing _rels/.rels" (Maybe Entry -> Either Text Entry)
-> Maybe Entry -> Either Text Entry
forall a b. (a -> b) -> a -> b
$
[Char] -> Archive -> Maybe Entry
findEntryByPath [Char]
"_rels/.rels" Archive
archive
relsElem <- parseXMLFromEntry relsEntry
let relElems = [Content] -> [Element]
onlyElems ([Content] -> [Element]) -> [Content] -> [Element]
forall a b. (a -> b) -> a -> b
$ Element -> [Content]
elContent Element
relsElem
case find isOfficeDocRel relElems of
Maybe Element
Nothing -> Text -> Either Text [Char]
forall a b. a -> Either a b
Left (Text -> Either Text [Char]) -> Text -> Either Text [Char]
forall a b. (a -> b) -> a -> b
$ Text
"No presentation.xml relationship found. Found " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
[Char] -> Text
T.pack (Int -> [Char]
forall a. Show a => a -> [Char]
show ([Element] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Element]
relElems)) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" relationships."
Just Element
rel -> do
target <- Text -> Maybe Text -> Either Text Text
forall a. Text -> Maybe a -> Either Text a
maybeToEither Text
"Missing Target attribute" (Maybe Text -> Either Text Text) -> Maybe Text -> Either Text Text
forall a b. (a -> b) -> a -> b
$
QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"Target") Element
rel
return $ T.unpack target
where
isOfficeDocRel :: Element -> Bool
isOfficeDocRel Element
el =
case QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"Type") Element
el of
Just Text
relType -> Text
"/officeDocument" Text -> Text -> Bool
`T.isSuffixOf` Text
relType
Maybe Text
Nothing -> Bool
False
loadXMLFromArchive :: Archive -> FilePath -> Either Text Element
loadXMLFromArchive :: Archive -> [Char] -> Either Text Element
loadXMLFromArchive Archive
archive [Char]
path = do
entry <- Text -> Maybe Entry -> Either Text Entry
forall a. Text -> Maybe a -> Either Text a
maybeToEither (Text
"Entry not found: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack [Char]
path) (Maybe Entry -> Either Text Entry)
-> Maybe Entry -> Either Text Entry
forall a b. (a -> b) -> a -> b
$
[Char] -> Archive -> Maybe Entry
findEntryByPath [Char]
path Archive
archive
let xmlBytes = Entry -> ByteString
fromEntry Entry
entry
parseXMLFromBS xmlBytes
parseXMLFromBS :: B.ByteString -> Either Text Element
parseXMLFromBS :: ByteString -> Either Text Element
parseXMLFromBS = Text -> Either Text Element
parseXMLElement (Text -> Either Text Element)
-> (ByteString -> Text) -> ByteString -> Either Text Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
TL.decodeUtf8
parseXMLFromEntry :: Entry -> Either Text Element
parseXMLFromEntry :: Entry -> Either Text Element
parseXMLFromEntry = ByteString -> Either Text Element
parseXMLFromBS (ByteString -> Either Text Element)
-> (Entry -> ByteString) -> Entry -> Either Text Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Entry -> ByteString
fromEntry
elemToPresentation :: Element -> Either Text PresentationDoc
elemToPresentation :: Element -> Either Text PresentationDoc
elemToPresentation Element
presElem = do
let ns :: NameSpaces
ns = Element -> NameSpaces
elemToNameSpaces Element
presElem
let sizeElem :: Maybe Element
sizeElem = NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"p" Text
"sldSz" Element
presElem
(Integer
widthEMU, Integer
heightEMU) = case Maybe Element
sizeElem of
Just Element
el ->
let cx :: Integer
cx = Text -> Element -> Integer
readAttrInt Text
"cx" Element
el
cy :: Integer
cy = Text -> Element -> Integer
readAttrInt Text
"cy" Element
el
in (Integer
cx, Integer
cy)
Maybe Element
Nothing -> (Integer
9144000, Integer
6858000)
let width :: Integer
width = Integer
widthEMU Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
emusPerInch
height :: Integer
height = Integer
heightEMU Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
emusPerInch
let sldIdLstElem :: Maybe Element
sldIdLstElem = NameSpaces -> Text -> Text -> Element -> Maybe Element
findChildByName NameSpaces
ns Text
"p" Text
"sldIdLst" Element
presElem
slideRefs <- case Maybe Element
sldIdLstElem of
Maybe Element
Nothing -> [(SlideId, Text)] -> Either Text [(SlideId, Text)]
forall a. a -> Either Text a
forall (m :: * -> *) a. Monad m => a -> m a
return []
Just Element
el -> do
let sldIdElems :: [Element]
sldIdElems = QName -> Element -> [Element]
findChildren (NameSpaces -> Text -> Text -> QName
elemName NameSpaces
ns Text
"p" Text
"sldId") Element
el
((Int, Element) -> Either Text (SlideId, Text))
-> [(Int, Element)] -> Either Text [(SlideId, Text)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (NameSpaces -> (Int, Element) -> Either Text (SlideId, Text)
extractSlideRef NameSpaces
ns) ([Int] -> [Element] -> [(Int, Element)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
1..] [Element]
sldIdElems)
return $ PresentationDoc
{ presNameSpaces = ns
, presSlideSize = (width, height)
, presSlideIds = slideRefs
}
extractSlideRef :: NameSpaces -> (Int, Element) -> Either Text (SlideId, Text)
NameSpaces
ns (Int
idx, Element
sldIdElem) = do
relId <- Text -> Maybe Text -> Either Text Text
forall a. Text -> Maybe a -> Either Text a
maybeToEither (Text
"Missing r:id in slide " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Char] -> Text
T.pack (Int -> [Char]
forall a. Show a => a -> [Char]
show Int
idx)) (Maybe Text -> Either Text Text) -> Maybe Text -> Either Text Text
forall a b. (a -> b) -> a -> b
$
NameSpaces -> Text -> Text -> Element -> Maybe Text
findAttrByName NameSpaces
ns Text
"r" Text
"id" Element
sldIdElem
return (SlideId idx, relId)
readAttrInt :: Text -> Element -> Integer
readAttrInt :: Text -> Element -> Integer
readAttrInt Text
attrName Element
el =
case QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
attrName) Element
el of
Just Text
str -> case [Char] -> Maybe Integer
forall a. Read a => [Char] -> Maybe a
readMaybe (Text -> [Char]
T.unpack Text
str) of
Just Integer
n -> Integer
n
Maybe Integer
Nothing -> Integer
0
Maybe Text
Nothing -> Integer
0
getPresentationRelsPath :: Archive -> FilePath -> Either Text FilePath
getPresentationRelsPath :: Archive -> [Char] -> Either Text [Char]
getPresentationRelsPath Archive
_archive [Char]
presPath =
let ([Char]
dir, [Char]
file) = [Char] -> ([Char], [Char])
splitFileName [Char]
presPath
relsPath :: [Char]
relsPath = [Char]
dir [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"/_rels/" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
file [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
".rels"
in [Char] -> Either Text [Char]
forall a b. b -> Either a b
Right [Char]
relsPath
loadRelationships :: Archive -> FilePath -> Either Text [(Text, Text)]
loadRelationships :: Archive -> [Char] -> Either Text [(Text, Text)]
loadRelationships Archive
archive [Char]
relsPath =
case [Char] -> Archive -> Maybe Entry
findEntryByPath [Char]
relsPath Archive
archive of
Maybe Entry
Nothing -> [(Text, Text)] -> Either Text [(Text, Text)]
forall a b. b -> Either a b
Right []
Just Entry
entry -> do
relsElem <- Entry -> Either Text Element
parseXMLFromEntry Entry
entry
let relElems = [Content] -> [Element]
onlyElems ([Content] -> [Element]) -> [Content] -> [Element]
forall a b. (a -> b) -> a -> b
$ Element -> [Content]
elContent Element
relsElem
return $ mapMaybe extractRelationship relElems
where
extractRelationship :: Element -> Maybe (Text, Text)
extractRelationship Element
el = do
relId <- QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"Id") Element
el
target <- findAttr (unqual "Target") el
return (relId, target)
parseSlide :: Archive -> [(Text, Text)] -> (SlideId, Text) -> Either Text PptxSlide
parseSlide :: Archive
-> [(Text, Text)] -> (SlideId, Text) -> Either Text PptxSlide
parseSlide Archive
archive [(Text, Text)]
rels (SlideId
sid, Text
relId) = do
target <- Text -> Maybe Text -> Either Text Text
forall a. Text -> Maybe a -> Either Text a
maybeToEither (Text
"Relationship not found: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
relId) (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
let slidePath' = [Char]
"ppt/" [Char] -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> [Char]
T.unpack Text
target
slideElem <- loadXMLFromArchive archive slidePath'
slideRelsPath <- getPresentationRelsPath archive slidePath'
slideRels' <- loadRelationships archive slideRelsPath
let notesElem = do
notesTarget <- Archive -> [Char] -> Text -> Maybe Text
findRelTargetByType Archive
archive [Char]
slideRelsPath Text
"notesSlide"
either (const Nothing) Just $
loadXMLFromArchive archive (resolveTarget slidePath' notesTarget)
let related [Char]
source Text
typeName = do
t <- Archive -> [Char] -> Text -> Maybe Text
findRelTargetByType Archive
archive (ShowS
relsPathOf [Char]
source) Text
typeName
let path = [Char] -> Text -> [Char]
resolveTarget [Char]
source Text
t
el <- either (const Nothing) Just $ loadXMLFromArchive archive path
return (path, el)
layout = [Char] -> Text -> Maybe ([Char], Element)
related [Char]
slidePath' Text
"slideLayout"
master = Maybe ([Char], Element)
layout Maybe ([Char], Element)
-> (([Char], Element) -> Maybe ([Char], Element))
-> Maybe ([Char], Element)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \([Char]
layoutPath, Element
_) -> [Char] -> Text -> Maybe ([Char], Element)
related [Char]
layoutPath Text
"slideMaster"
return $ PptxSlide sid slidePath' slideElem slideRels' notesElem
(snd <$> layout) (snd <$> master)
relsPathOf :: FilePath -> FilePath
relsPathOf :: ShowS
relsPathOf [Char]
path =
let ([Char]
dir, [Char]
file) = [Char] -> ([Char], [Char])
splitFileName [Char]
path
in [Char]
dir [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"/_rels/" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
file [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
".rels"
findRelTargetByType :: Archive -> FilePath -> Text -> Maybe Text
findRelTargetByType :: Archive -> [Char] -> Text -> Maybe Text
findRelTargetByType Archive
archive [Char]
relsPath Text
typeName = do
entry <- [Char] -> Archive -> Maybe Entry
findEntryByPath [Char]
relsPath Archive
archive
relsElem <- either (const Nothing) Just $ parseXMLFromEntry entry
rel <- find hasType (onlyElems $ elContent relsElem)
findAttr (unqual "Target") rel
where
hasType :: Element -> Bool
hasType Element
el = Bool -> (Text -> Bool) -> Maybe Text -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False ((Text
"/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
typeName) Text -> Text -> Bool
`T.isSuffixOf`) (Maybe Text -> Bool) -> Maybe Text -> Bool
forall a b. (a -> b) -> a -> b
$
QName -> Element -> Maybe Text
findAttr (Text -> QName
unqual Text
"Type") Element
el
resolveTarget :: FilePath -> Text -> FilePath
resolveTarget :: [Char] -> Text -> [Char]
resolveTarget [Char]
source Text
target =
case Text -> [Char]
T.unpack Text
target of
Char
'/':[Char]
path -> [Char]
path
[Char]
path -> ShowS
collapseFilePath (ShowS
Posix.takeDirectory [Char]
source [Char] -> ShowS
Posix.</> [Char]
path)
maybeToEither :: Text -> Maybe a -> Either Text a
maybeToEither :: forall a. Text -> Maybe a -> Either Text a
maybeToEither Text
err Maybe a
Nothing = Text -> Either Text a
forall a b. a -> Either a b
Left Text
err
maybeToEither Text
_ (Just a
x) = a -> Either Text a
forall a b. b -> Either a b
Right a
x