ODT reader: create Figure elements for images that are figures.

Closes #10567.
This commit is contained in:
John MacFarlane
2025-02-01 10:39:31 -08:00
parent 8e18a811b4
commit fe3e7c948c
3 changed files with 74 additions and 3 deletions
+18 -1
View File
@@ -15,7 +15,9 @@ module Text.Pandoc.Readers.ODT ( readODT ) where
import Codec.Archive.Zip
import Text.Pandoc.XML.Light
import Text.Pandoc.Walk
import Data.Char (isDigit)
import qualified Data.ByteString.Lazy as B
import System.FilePath
@@ -46,9 +48,24 @@ readODT :: PandocMonad m
readODT opts bytes = case readODT' opts bytes of
Right (doc, mb) -> do
P.setMediaBag mb
return doc
return $ walk makeFigure doc
Left e -> throwError e
-- the ODT parser uses old-style figures: an image with title beginning
-- "fig:" in a paragraph by itself. Convert these to new Figure elements.
makeFigure :: Block -> Block
makeFigure (Para [ Image (ident, classes, kvs) capt (src, tit) ])
| "fig:" `T.isPrefixOf` tit
= Figure (ident, [], []) (Caption Nothing [Plain capt'])
[Plain [Image ("", classes, kvs) capt (src, "")]]
where
capt' = case capt of -- strip "Figure 1:" for consistency
(Str _ : Space : Str t : Space : xs)
| T.all (\c -> isDigit c || c == ':') t
, ":" `T.isSuffixOf` t -> xs
xs -> xs
makeFigure x = x
--
readODT' :: ReaderOptions
-> B.ByteString
+28 -1
View File
@@ -1 +1,28 @@
[Para [Image ("",[],[("width","5.292cm"),("height","5.292cm")]) [Str "Abbildung",Space,Str "1:",Space,Str "Image",Space,Str "caption"] ("Pictures/10000000000000FA000000FAD6A15225.jpg","fig:")]]
[ Figure
( "" , [] , [] )
(Caption
Nothing
[ Plain
[ Str "Image"
, Space
, Str "caption"
]
])
[ Plain
[ Image
( ""
, []
, [ ( "width" , "5.292cm" ) , ( "height" , "5.292cm" ) ]
)
[ Str "Abbildung"
, Space
, Str "1:"
, Space
, Str "Image"
, Space
, Str "caption"
]
( "Pictures/10000000000000FA000000FAD6A15225.jpg" , "" )
]
]
]
+28 -1
View File
@@ -1 +1,28 @@
[Para [Image ("",[],[("width","5.292cm"),("height","5.292cm")]) [Str "Abbildung",Space,Str "1:",Space,Str "Image",Space,Str "caption"] ("Pictures/10000000000000FA000000FAD6A15225.jpg","fig:")]]
[ Figure
( "" , [] , [] )
(Caption
Nothing
[ Plain
[ Str "Image"
, Space
, Str "caption"
]
])
[ Plain
[ Image
( ""
, []
, [ ( "width" , "5.292cm" ) , ( "height" , "5.292cm" ) ]
)
[ Str "Abbildung"
, Space
, Str "1:"
, Space
, Str "Image"
, Space
, Str "caption"
]
( "Pictures/10000000000000FA000000FAD6A15225.jpg" , "" )
]
]
]