git.iko.soy/typeable/pandoc
on master help sign in
Docx reader: image attributes
37931cbmb21
src/Text/Pandoc/Readers/Docx.hs+11−4
@@ -298,10 +298,17 @@ runToInlines (Footnote bps) = do
298runToInlines (Endnote bps) = do
298runToInlines (Endnote bps) = do
299 blksList <- concatReduce <$> (mapM bodyPartToBlocks bps)
299 blksList <- concatReduce <$> (mapM bodyPartToBlocks bps)
300 return $ note blksList
300 return $ note blksList
301−runToInlines (InlineDrawing fp bs) = do
301+runToInlines (InlineDrawing fp bs ext) = do
302 mediaBag <- gets docxMediaBag
302 mediaBag <- gets docxMediaBag
303 modify $ \s -> s { docxMediaBag = insertMedia fp Nothing bs mediaBag }
303 modify $ \s -> s { docxMediaBag = insertMedia fp Nothing bs mediaBag }
304− return $ image fp "" ""
304+ return $ imageWith (extentToAttr ext) fp "" ""
305+
306+extentToAttr :: Extent -> Attr
307+extentToAttr (Just (w, h)) =
308+ ("", [], [("width", showDim w), ("height", showDim h)] )
309+ where
310+ showDim d = show (d / 914400) ++ "in"
311+extentToAttr _ = nullAttr
305
312
306parPartToInlines :: ParPart -> DocxContext Inlines
313parPartToInlines :: ParPart -> DocxContext Inlines
307parPartToInlines (PlainRun r) = runToInlines r
314parPartToInlines (PlainRun r) = runToInlines r
@@ -348,10 +355,10 @@ parPartToInlines (BookMark _ anchor) =
348 unless inHdrBool
355 unless inHdrBool
349 (modify $ \s -> s { docxAnchorMap = M.insert anchor newAnchor anchorMap})
356 (modify $ \s -> s { docxAnchorMap = M.insert anchor newAnchor anchorMap})
350 return $ spanWith (newAnchor, ["anchor"], []) mempty
357 return $ spanWith (newAnchor, ["anchor"], []) mempty
351−parPartToInlines (Drawing fp bs) = do
358+parPartToInlines (Drawing fp bs ext) = do
352 mediaBag <- gets docxMediaBag
359 mediaBag <- gets docxMediaBag
353 modify $ \s -> s { docxMediaBag = insertMedia fp Nothing bs mediaBag }
360 modify $ \s -> s { docxMediaBag = insertMedia fp Nothing bs mediaBag }
354− return $ image fp "" ""
361+ return $ imageWith (extentToAttr ext) fp "" ""
355parPartToInlines (InternalHyperLink anchor runs) = do
362parPartToInlines (InternalHyperLink anchor runs) = do
356 ils <- concatReduce <$> mapM runToInlines runs
363 ils <- concatReduce <$> mapM runToInlines runs
357 return $ link ('#' : anchor) "" ils
364 return $ link ('#' : anchor) "" ils
src/Text/Pandoc/Readers/Docx/Parse.hs+21−6
@@ -35,6 +35,7 @@ module Text.Pandoc.Readers.Docx.Parse ( Docx(..)
35 , Body(..)
35 , Body(..)
36 , BodyPart(..)
36 , BodyPart(..)
37 , TblLook(..)
37 , TblLook(..)
38+ , Extent
38 , ParPart(..)
39 , ParPart(..)
39 , Run(..)
40 , Run(..)
40 , RunElem(..)
41 , RunElem(..)
@@ -62,6 +63,7 @@ import Control.Monad.Reader
62import Control.Applicative ((<|>))
63import Control.Applicative ((<|>))
63import qualified Data.Map as M
64import qualified Data.Map as M
64import Text.Pandoc.Compat.Except
65import Text.Pandoc.Compat.Except
66+import Text.Pandoc.Shared (safeRead)
65import Text.TeXMath.Readers.OMML (readOMML)
67import Text.TeXMath.Readers.OMML (readOMML)
66import Text.Pandoc.Readers.Docx.Fonts (getUnicode, Font(..))
68import Text.Pandoc.Readers.Docx.Fonts (getUnicode, Font(..))
67import Text.TeXMath (Exp)
69import Text.TeXMath (Exp)
@@ -196,20 +198,23 @@ data Row = Row [Cell]
196data Cell = Cell [BodyPart]
198data Cell = Cell [BodyPart]
197 deriving Show
199 deriving Show
198
200
201+-- (width, height) in EMUs
202+type Extent = Maybe (Double, Double)
203+
199data ParPart = PlainRun Run
204data ParPart = PlainRun Run
200 | Insertion ChangeId Author ChangeDate [Run]
205 | Insertion ChangeId Author ChangeDate [Run]
201 | Deletion ChangeId Author ChangeDate [Run]
206 | Deletion ChangeId Author ChangeDate [Run]
202 | BookMark BookMarkId Anchor
207 | BookMark BookMarkId Anchor
203 | InternalHyperLink Anchor [Run]
208 | InternalHyperLink Anchor [Run]
204 | ExternalHyperLink URL [Run]
209 | ExternalHyperLink URL [Run]
205− | Drawing FilePath B.ByteString
210+ | Drawing FilePath B.ByteString Extent
206 | PlainOMath [Exp]
211 | PlainOMath [Exp]
207 deriving Show
212 deriving Show
208
213
209data Run = Run RunStyle [RunElem]
214data Run = Run RunStyle [RunElem]
210 | Footnote [BodyPart]
215 | Footnote [BodyPart]
211 | Endnote [BodyPart]
216 | Endnote [BodyPart]
212− | InlineDrawing FilePath B.ByteString
217+ | InlineDrawing FilePath B.ByteString Extent
213 deriving Show
218 deriving Show
214
219
215data RunElem = TextRun String | LnBrk | Tab | SoftHyphen | NoBreakHyphen
220data RunElem = TextRun String | LnBrk | Tab | SoftHyphen | NoBreakHyphen
@@ -619,13 +624,13 @@ expandDrawingId s = do
619elemToParPart :: NameSpaces -> Element -> D ParPart
624elemToParPart :: NameSpaces -> Element -> D ParPart
620elemToParPart ns element
625elemToParPart ns element
621 | isElem ns "w" "r" element
626 | isElem ns "w" "r" element
622− , Just _ <- findChild (elemName ns "w" "drawing") element =
627+ , Just drawingElem <- findChild (elemName ns "w" "drawing") element =
623 let a_ns = "http://schemas.openxmlformats.org/drawingml/2006/main"
628 let a_ns = "http://schemas.openxmlformats.org/drawingml/2006/main"
624 drawing = findElement (QName "blip" (Just a_ns) (Just "a")) element
629 drawing = findElement (QName "blip" (Just a_ns) (Just "a")) element
625 >>= findAttr (QName "embed" (lookup "r" ns) (Just "r"))
630 >>= findAttr (QName "embed" (lookup "r" ns) (Just "r"))
626 in
631 in
627 case drawing of
632 case drawing of
628− Just s -> expandDrawingId s >>= (\(fp, bs) -> return $ Drawing fp bs)
633+ Just s -> expandDrawingId s >>= (\(fp, bs) -> return $ Drawing fp bs $ elemToExtent drawingElem)
629 Nothing -> throwError WrongElem
634 Nothing -> throwError WrongElem
630-- The below is an attempt to deal with images in deprecated vml format.
635-- The below is an attempt to deal with images in deprecated vml format.
631elemToParPart ns element
636elemToParPart ns element
@@ -635,7 +640,7 @@ elemToParPart ns element
635 >>= findAttr (elemName ns "r" "id")
640 >>= findAttr (elemName ns "r" "id")
636 in
641 in
637 case drawing of
642 case drawing of
638− Just s -> expandDrawingId s >>= (\(fp, bs) -> return $ Drawing fp bs)
643+ Just s -> expandDrawingId s >>= (\(fp, bs) -> return $ Drawing fp bs Nothing)
639 Nothing -> throwError WrongElem
644 Nothing -> throwError WrongElem
640elemToParPart ns element
645elemToParPart ns element
641 | isElem ns "w" "r" element =
646 | isElem ns "w" "r" element =
@@ -687,6 +692,16 @@ lookupFootnote s (Notes _ fns _) = fns >>= (M.lookup s)
687lookupEndnote :: String -> Notes -> Maybe Element
692lookupEndnote :: String -> Notes -> Maybe Element
688lookupEndnote s (Notes _ _ ens) = ens >>= (M.lookup s)
693lookupEndnote s (Notes _ _ ens) = ens >>= (M.lookup s)
689
694
695+elemToExtent :: Element -> Extent
696+elemToExtent drawingElem =
697+ case (getDim "cx", getDim "cy") of
698+ (Just w, Just h) -> Just (w, h)
699+ _ -> Nothing
700+ where
701+ wp_ns = "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
702+ getDim at = findElement (QName "extent" (Just wp_ns) (Just "wp")) drawingElem
703+ >>= findAttr (QName at Nothing Nothing) >>= safeRead
704+
690elemToRun :: NameSpaces -> Element -> D Run
705elemToRun :: NameSpaces -> Element -> D Run
691elemToRun ns element
706elemToRun ns element
692 | isElem ns "w" "r" element
707 | isElem ns "w" "r" element
@@ -697,7 +712,7 @@ elemToRun ns element
697 in
712 in
698 case drawing of
713 case drawing of
699 Just s -> expandDrawingId s >>=
714 Just s -> expandDrawingId s >>=
700− (\(fp, bs) -> return $ InlineDrawing fp bs)
715+ (\(fp, bs) -> return $ InlineDrawing fp bs $ elemToExtent drawingElem)
701 Nothing -> throwError WrongElem
716 Nothing -> throwError WrongElem
702elemToRun ns element
717elemToRun ns element
703 | isElem ns "w" "r" element
718 | isElem ns "w" "r" element
tests/docx/image_no_embed.native+1−1
@@ -1,2 +1,2 @@
1[Para [Str "An",Space,Str "image:"]
1[Para [Str "An",Space,Str "image:"]
2−,Para [Image ("",[],[]) [] ("media/image1.jpg","")]]
2+,Para [Image ("",[],[("width","6.5in"),("height","5.508333333333334in")]) [] ("media/image1.jpg","")]]
tests/docx/image_no_embed_writer.native+1−1
@@ -1,2 +1,2 @@
1[Para [Str "An",Space,Str "image:"]
1[Para [Str "An",Space,Str "image:"]
2−,Para [Image ("",[],[]) [] ("media/rId25.jpg","")]]
2+,Para [Image ("",[],[("width","0.4166666666666667in"),("height","0.4166666666666667in")]) [] ("media/rId25.jpg","")]]
tests/docx/inline_images.native+2−2
@@ -1,2 +1,2 @@
1−[Para [Str "This",Space,Str "picture",Space,Image ("",[],[]) [] ("media/image1.jpg",""),Space,Str "is",Space,Str "an",Space,Str "identicon."]
1+[Para [Str "This",Space,Str "picture",Space,Image ("",[],[("width","0.8888888888888888in"),("height","0.8888888888888888in")]) [] ("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."]]
2+,Para [Str "Here",Space,Str "is",Space,Link ("",[],[]) [Str "one",Space,Image ("",[],[("width","0.8888888888888888in"),("height","0.8888888888888888in")]) [] ("media/image2.jpg",""),Space,Str "that"] ("http://www.google.com",""),Space,Str "links."]]
tests/docx/inline_images_writer.native+2−2
@@ -1,2 +1,2 @@
1−[Para [Str "This",Space,Str "picture",Space,Image ("",[],[]) [] ("media/rId26.jpg",""),Space,Str "is",Space,Str "an",Space,Str "identicon."]
1+[Para [Str "This",Space,Str "picture",Space,Image ("",[],[("width","0.4166666666666667in"),("height","0.4166666666666667in")]) [] ("media/rId26.jpg",""),Space,Str "is",Space,Str "an",Space,Str "identicon."]
2−,Para [Str "Here",Space,Str "is",Space,Link ("",[],[]) [Str "one",Space,Image ("",[],[]) [] ("media/rId27.jpg",""),Space,Str "that"] ("http://www.google.com",""),Space,Str "links."]]
2+,Para [Str "Here",Space,Str "is",Space,Link ("",[],[]) [Str "one",Space,Image ("",[],[("width","0.4166666666666667in"),("height","0.4166666666666667in")]) [] ("media/rId26.jpg",""),Space,Str "that"] ("http://www.google.com",""),Space,Str "links."]]