git.iko.soy/typeable/pandoc
on master help sign in
Docx reader: Handle inline drawings.

Previous drawings that were under some other toplevel run (i.e., a hyperlink) wouldn't be properly handled. This should fix that.

commit7bd7d4d476f5091d39bac11d53588a80d4bef9ee authorJesse Rosenthal <jrosenthal@jhu.edu> dateThu 7 Aug 2014, 15:01 (12 years ago) parent482f7f8
2 files changed+22−3
src/Text/Pandoc/Readers/Docx.hs+7 src/Text/Pandoc/Readers/Docx/Parse.hs+15−3
src/Text/Pandoc/Readers/Docx.hs+7
@@ -321,6 +321,13 @@ runToInlines (Footnote bps) =
321321 concatMapM bodyPartToBlocks bps >>= (\blks -> return [Note blks])
322322runToInlines (Endnote bps) =
323323 concatMapM bodyPartToBlocks bps >>= (\blks -> return [Note blks])
324+runToInlines (InlineDrawing fp bs) = do
325+ mediaBag <- gets docxMediaBag
326+ modify $ \s -> s { docxMediaBag = insertMedia fp Nothing bs mediaBag }
327+ return [Image [] (fp, "")]
328+
329+
330+
324331
325332parPartToInlines :: ParPart -> DocxContext [Inline]
326333parPartToInlines (PlainRun r) = runToInlines r
src/Text/Pandoc/Readers/Docx/Parse.hs+15−3
@@ -282,6 +282,7 @@ defaultGroupStyle = GroupStyle {groupChr = Nothing, groupPos = Nothing}
282282data Run = Run RunStyle [RunElem]
283283 | Footnote [BodyPart]
284284 | Endnote [BodyPart]
285+ | InlineDrawing FilePath B.ByteString
285286 deriving Show
286287
287288data RunElem = TextRun String | LnBrk | Tab
@@ -874,14 +875,14 @@ lookupRelationship :: RelId -> [Relationship] -> Maybe Target
874875lookupRelationship relid rels =
875876 lookup relid (map (\(Relationship pair) -> pair) rels)
876877
877−expandDrawingId :: String -> D ParPart
878+expandDrawingId :: String -> D (FilePath, B.ByteString)
878879expandDrawingId s = do
879880 target <- asks (lookupRelationship s . envRelationships)
880881 case target of
881882 Just filepath -> do
882883 bytes <- asks (lookup (combine "word" filepath) . envMedia)
883884 case bytes of
884− Just bs -> return $ Drawing filepath bs
885+ Just bs -> return (filepath, bs)
885886 Nothing -> throwError DocxError
886887 Nothing -> throwError DocxError
887888
@@ -894,7 +895,7 @@ elemToParPart ns element
894895 >>= findAttr (QName "embed" (lookup "r" ns) (Just "r"))
895896 in
896897 case drawing of
897− Just s -> expandDrawingId s
898+ Just s -> expandDrawingId s >>= (\(fp, bs) -> return $ Drawing fp bs)
898899 Nothing -> throwError WrongElem
899900elemToParPart ns element
900901 | isElem ns "w" "r" element =
@@ -943,6 +944,17 @@ lookupEndnote :: String -> Notes -> Maybe Element
943944lookupEndnote s (Notes _ _ ens) = ens >>= (M.lookup s)
944945
945946elemToRun :: NameSpaces -> Element -> D Run
947+elemToRun ns element
948+ | isElem ns "w" "r" element
949+ , Just drawingElem <- findChild (elemName ns "w" "drawing") element =
950+ let a_ns = "http://schemas.openxmlformats.org/drawingml/2006/main"
951+ drawing = findElement (QName "blip" (Just a_ns) (Just "a")) drawingElem
952+ >>= findAttr (QName "embed" (lookup "r" ns) (Just "r"))
953+ in
954+ case drawing of
955+ Just s -> expandDrawingId s >>=
956+ (\(fp, bs) -> return $ InlineDrawing fp bs)
957+ Nothing -> throwError WrongElem
946958elemToRun ns element
947959 | isElem ns "w" "r" element
948960 , Just ref <- findChild (elemName ns "w" "footnoteReference") element