@@ -42,6 +42,7 @@ module Text.Pandoc.Readers.Odt.ContentReader
42import Control.Arrow
42import Control.Arrow
43import Control.Applicative hiding ( liftA, liftA2, liftA3 )
43import Control.Applicative hiding ( liftA, liftA2, liftA3 )
45+import qualified Data.ByteString.Lazy as B
45import qualified Data.Map as M
46import qualified Data.Map as M
46import Data.List ( find )
47import Data.List ( find )
47import Data.Maybe
48import Data.Maybe
@@ -50,6 +51,7 @@ import qualified Text.XML.Light as XML
51import Text.Pandoc.Definition
52import Text.Pandoc.Definition
52import Text.Pandoc.Builder
53import Text.Pandoc.Builder
54+import Text.Pandoc.MediaBag (insertMedia, MediaBag)
53import Text.Pandoc.Shared
55import Text.Pandoc.Shared
55import Text.Pandoc.Readers.Odt.Base
57import Text.Pandoc.Readers.Odt.Base
@@ -68,6 +70,7 @@ import qualified Data.Set as Set
68--------------------------------------------------------------------------------
70--------------------------------------------------------------------------------
70type Anchor = String
72type Anchor = String
73+type Media = [(FilePath, B.ByteString)]
72data ReaderState
75data ReaderState
73 = ReaderState { -- | A collection of styles read somewhere else.
76 = ReaderState { -- | A collection of styles read somewhere else.
@@ -87,14 +90,17 @@ data ReaderState
87 -- | A map from internal anchor names to "pretty" ones.
90 -- | A map from internal anchor names to "pretty" ones.
88 -- The mapping is a purely cosmetic one.
91 -- The mapping is a purely cosmetic one.
89 , bookmarkAnchors :: M.Map Anchor Anchor
92 , bookmarkAnchors :: M.Map Anchor Anchor
90−
93+ -- | A map of files / binary data from the archive
95+ -- | Hold binary resources used in the document
96+ , odtMediaBag :: MediaBag
91-- , sequences
97-- , sequences
92-- , trackedChangeIDs
98-- , trackedChangeIDs
94 deriving ( Show )
100 deriving ( Show )
96−readerState :: Styles -> ReaderState
102+readerState :: Styles -> Media -> ReaderState
97−readerState styles = ReaderState styles [] 0 Nothing M.empty
103+readerState styles media = ReaderState styles [] 0 Nothing M.empty media mempty
100pushStyle' :: Style -> ReaderState -> ReaderState
106pushStyle' :: Style -> ReaderState -> ReaderState
@@ -134,6 +140,16 @@ putPrettyAnchor ugly pretty state@ReaderState{..}
134usedAnchors :: ReaderState -> [Anchor]
140usedAnchors :: ReaderState -> [Anchor]
135usedAnchors ReaderState{..} = M.elems bookmarkAnchors
141usedAnchors ReaderState{..} = M.elems bookmarkAnchors
143+getMediaBag :: ReaderState -> MediaBag
144+getMediaBag ReaderState{..} = odtMediaBag
146+getMediaEnv :: ReaderState -> Media
147+getMediaEnv ReaderState{..} = envMedia
149+insertMedia' :: (FilePath, B.ByteString) -> ReaderState -> ReaderState
150+insertMedia' (fp, bs) state@ReaderState{..}
151+ = state { odtMediaBag = insertMedia fp Nothing bs odtMediaBag }
137--------------------------------------------------------------------------------
153--------------------------------------------------------------------------------
138-- Reader type and associated tools
154-- Reader type and associated tools
139--------------------------------------------------------------------------------
155--------------------------------------------------------------------------------
@@ -190,6 +206,22 @@ popStyle = keepingTheValue (
190getCurrentListLevel :: OdtReaderSafe _x ListLevel
206getCurrentListLevel :: OdtReaderSafe _x ListLevel
191getCurrentListLevel = getExtraState >>^ currentListLevel
207getCurrentListLevel = getExtraState >>^ currentListLevel
210+updateMediaWithResource :: OdtReaderSafe (FilePath, B.ByteString) (FilePath, B.ByteString)
211+updateMediaWithResource = keepingTheValue (
212+ (keepingTheValue getExtraState
219+lookupResource :: OdtReaderSafe String (FilePath, B.ByteString)
220+lookupResource = proc target -> do
221+ state <- getExtraState -< ()
222+ case lookup target (getMediaEnv state) of
223+ Just bs -> returnV (target, bs) -<< ()
224+ Nothing -> returnV ("", B.empty) -< ()
194type AnchorPrefix = String
226type AnchorPrefix = String
@@ -559,6 +591,7 @@ read_paragraph = matchingElement NsText "p"
559 , read_reference_start
591 , read_reference_start
560 , read_bookmark_ref
592 , read_bookmark_ref
561 , read_reference_ref
593 , read_reference_ref
562 ] read_plain_text
595 ] read_plain_text
@@ -583,6 +616,7 @@ read_header = matchingElement NsText "h"
583 , read_reference_start
616 , read_reference_start
584 , read_bookmark_ref
617 , read_bookmark_ref
585 , read_reference_ref
618 , read_reference_ref
586 ] read_plain_text
620 ] read_plain_text
587 ) -< blocks
621 ) -< blocks
588 anchor <- getHeaderAnchor -< children
622 anchor <- getHeaderAnchor -< children
@@ -687,6 +721,46 @@ read_table_cell = matchingElement NsTable "table-cell"
687 $ matchChildContent' [ read_paragraph
721 $ matchChildContent' [ read_paragraph
724+----------------------
726+----------------------
729+read_frame :: InlineMatcher
730+read_frame = matchingElement NsDraw "frame"
732+ w <- ( findAttr' NsSVG "width" ) -< ()
733+ h <- ( findAttr' NsSVG "height" ) -< ()
734+ attr <- arr (uncurry image_attributes) -< (w, h)
735+ titleNodes <- ( matchChildContent' [ read_frame_title ] ) -< blocks
736+ title <- arr inlineListToIdentifier -< (toList titleNodes)
737+ src <- matchChildContent' [ read_image_src ] -< blocks
738+ resource <- lookupResource -< src
739+ _ <- updateMediaWithResource -< resource
740+ arr (uncurry4 imageWith ) -< (attr, src, title, mempty)
742+image_attributes :: Maybe String -> Maybe String -> Attr
743+image_attributes x y =
744+ ( "", [], (dim "width" x) ++ (dim "height" y))
746+ dim _ (Just "") = []
747+ dim name (Just v) = [(name, v)]
750+read_image_src :: (Namespace, ElementName, OdtReader Anchor Anchor)
751+read_image_src = matchingElement NsDraw "image"
753+ imgSrc <- findAttr NsXLink "href" -< ()
755+ Right src -> returnV src -<< ()
756+ Left _ -> returnV "" -< ()
758+read_frame_title :: InlineMatcher
759+read_frame_title = matchingElement NsSVG "title"
760+ $ (matchChildContent [] read_plain_text)
690----------------------
764----------------------
691-- Internal links
765-- Internal links
692----------------------
766----------------------
@@ -783,8 +857,11 @@ read_text = matchChildContent' [ read_header
786−read_body :: OdtReader _x Pandoc
860+read_body :: OdtReader _x (Pandoc, MediaBag)
787read_body = executeIn NsOffice "body"
861read_body = executeIn NsOffice "body"
788 $ executeIn NsOffice "text"
862 $ executeIn NsOffice "text"
789− $ liftAsSuccess read_text
863+ $ liftAsSuccess
790−
864+ $ proc inlines -> do
865+ txt <- read_text -< inlines
866+ state <- getExtraState -< ()
867+ returnA -< (txt, getMediaBag state)