git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1495 from jkr/inline-drawings
44dad52John MacFarlane
pandoc.cabal+1
@@ -178,6 +178,7 @@ Extra-Source-Files:
178 tests/docx.hanging_indent.docx,
178 tests/docx.hanging_indent.docx,
179 tests/docx.headers.docx,
179 tests/docx.headers.docx,
180 tests/docx.image.docx,
180 tests/docx.image.docx,
181+ tests/docx.inline_images.docx,
181 tests/docx.inline_code.docx,
182 tests/docx.inline_code.docx,
182 tests/docx.inline_formatting.docx,
183 tests/docx.inline_formatting.docx,
183 tests/docx.links.docx,
184 tests/docx.links.docx,
src/Text/Pandoc/Readers/Docx.hs+7
@@ -321,6 +321,13 @@ runToInlines (Footnote bps) =
321 concatMapM bodyPartToBlocks bps >>= (\blks -> return [Note blks])
321 concatMapM bodyPartToBlocks bps >>= (\blks -> return [Note blks])
322runToInlines (Endnote bps) =
322runToInlines (Endnote bps) =
323 concatMapM bodyPartToBlocks bps >>= (\blks -> return [Note blks])
323 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+
324
331
325parPartToInlines :: ParPart -> DocxContext [Inline]
332parPartToInlines :: ParPart -> DocxContext [Inline]
326parPartToInlines (PlainRun r) = runToInlines r
333parPartToInlines (PlainRun r) = runToInlines r
src/Text/Pandoc/Readers/Docx/Parse.hs+15−3
@@ -282,6 +282,7 @@ defaultGroupStyle = GroupStyle {groupChr = Nothing, groupPos = Nothing}
282data Run = Run RunStyle [RunElem]
282data Run = Run RunStyle [RunElem]
283 | Footnote [BodyPart]
283 | Footnote [BodyPart]
284 | Endnote [BodyPart]
284 | Endnote [BodyPart]
285+ | InlineDrawing FilePath B.ByteString
285 deriving Show
286 deriving Show
286
287
287data RunElem = TextRun String | LnBrk | Tab
288data RunElem = TextRun String | LnBrk | Tab
@@ -874,14 +875,14 @@ lookupRelationship :: RelId -> [Relationship] -> Maybe Target
874lookupRelationship relid rels =
875lookupRelationship relid rels =
875 lookup relid (map (\(Relationship pair) -> pair) rels)
876 lookup relid (map (\(Relationship pair) -> pair) rels)
876
877
877−expandDrawingId :: String -> D ParPart
878+expandDrawingId :: String -> D (FilePath, B.ByteString)
878expandDrawingId s = do
879expandDrawingId s = do
879 target <- asks (lookupRelationship s . envRelationships)
880 target <- asks (lookupRelationship s . envRelationships)
880 case target of
881 case target of
881 Just filepath -> do
882 Just filepath -> do
882 bytes <- asks (lookup (combine "word" filepath) . envMedia)
883 bytes <- asks (lookup (combine "word" filepath) . envMedia)
883 case bytes of
884 case bytes of
884− Just bs -> return $ Drawing filepath bs
885+ Just bs -> return (filepath, bs)
885 Nothing -> throwError DocxError
886 Nothing -> throwError DocxError
886 Nothing -> throwError DocxError
887 Nothing -> throwError DocxError
887
888
@@ -894,7 +895,7 @@ elemToParPart ns element
894 >>= findAttr (QName "embed" (lookup "r" ns) (Just "r"))
895 >>= findAttr (QName "embed" (lookup "r" ns) (Just "r"))
895 in
896 in
896 case drawing of
897 case drawing of
897− Just s -> expandDrawingId s
898+ Just s -> expandDrawingId s >>= (\(fp, bs) -> return $ Drawing fp bs)
898 Nothing -> throwError WrongElem
899 Nothing -> throwError WrongElem
899elemToParPart ns element
900elemToParPart ns element
900 | isElem ns "w" "r" element =
901 | isElem ns "w" "r" element =
@@ -943,6 +944,17 @@ lookupEndnote :: String -> Notes -> Maybe Element
943lookupEndnote s (Notes _ _ ens) = ens >>= (M.lookup s)
944lookupEndnote s (Notes _ _ ens) = ens >>= (M.lookup s)
944
945
945elemToRun :: NameSpaces -> Element -> D Run
946elemToRun :: 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
946elemToRun ns element
958elemToRun ns element
947 | isElem ns "w" "r" element
959 | isElem ns "w" "r" element
948 , Just ref <- findChild (elemName ns "w" "footnoteReference") element
960 , Just ref <- findChild (elemName ns "w" "footnoteReference") element
tests/Tests/Readers/Docx.hs+5−1
@@ -107,9 +107,13 @@ tests = [ testGroup "inlines"
107 "docx.links.docx"
107 "docx.links.docx"
108 "docx.links.native"
108 "docx.links.native"
109 , testCompare
109 , testCompare
110− "inline image with reference output"
110+ "inline image"
111 "docx.image.docx"
111 "docx.image.docx"
112 "docx.image_no_embed.native"
112 "docx.image_no_embed.native"
113+ , testCompare
114+ "inline image in links"
115+ "docx.inline_images.docx"
116+ "docx.inline_images.native"
113 , testCompare
117 , testCompare
114 "handling unicode input"
118 "handling unicode input"
115 "docx.unicode.docx"
119 "docx.unicode.docx"
tests/docx.inline_images.docxnew file

Binary file not shown.

tests/docx.inline_images.nativenew file+2
@@ -0,0 +1,2 @@
1+[Para [Str "This",Space,Str "picture",Space,Image [] ("media/image1.jpg",""),Space,Str "is",Space,Str "an",Space,Str "identicon."]
2+,Para [Str "Here",Space,Str "is",Space,Link [Str "one",Space,Image [] ("media/image2.jpg",""),Space,Str "that"] ("http://www.google.com",""),Space,Str "links."]]