git.iko.soy/typeable/pandoc
on master help sign in
Un-URI-escape image filenames in LaTeX, ConTeXt, RTF, Texinfo.

Also do this when copying image files into EPUBs and ODTs. Closes #263.

commitb5da7adfb8ba38ac662695989b81b6e0875ac2fe authorJohn MacFarlane <jgm@berkeley.edu> dateSat 16 Jul 2011, 14:04 (15 years ago) parentfd46f61
8 files changed+46−27
src/Text/Pandoc/Writers/ConTeXt.hs+5−1 src/Text/Pandoc/Writers/EPUB.hs+4−2 src/Text/Pandoc/Writers/LaTeX.hs+5−1 src/Text/Pandoc/Writers/ODT.hs+4−2 src/Text/Pandoc/Writers/OpenDocument.hs+1−1 src/Text/Pandoc/Writers/RTF.hs+18−13 src/Text/Pandoc/Writers/Texinfo.hs+7−5 tests/writer.opendocument+2−2
src/Text/Pandoc/Writers/ConTeXt.hs+5−1
@@ -36,6 +36,7 @@ import Data.List ( intercalate )
3636import Control.Monad.State
3737import Text.Pandoc.Pretty
3838import Text.Pandoc.Templates ( renderTemplate )
39+import Network.URI ( isAbsoluteURI, unEscapeString )
3940
4041data WriterState =
4142 WriterState { stNextRef :: Int -- number of next URL reference
@@ -282,7 +283,10 @@ inlineToConTeXt (Link txt (src, _)) = do
282283 brackets empty <> brackets label <>
283284 "\\from" <> brackets (text ref)
284285inlineToConTeXt (Image _ (src, _)) = do
285− return $ braces $ "\\externalfigure" <> brackets (text src)
286+ let src' = if isAbsoluteURI src
287+ then src
288+ else unEscapeString src
289+ return $ braces $ "\\externalfigure" <> brackets (text src')
286290inlineToConTeXt (Note contents) = do
287291 contents' <- blockListToConTeXt contents
288292 return $ text "\\footnote{" <>
src/Text/Pandoc/Writers/EPUB.hs+4−2
@@ -47,6 +47,7 @@ import Text.Pandoc.Writers.HTML
4747import Text.Pandoc.Writers.Markdown ( writePlain )
4848import Data.Char ( toLower )
4949import System.Directory ( copyFile )
50+import Network.URI ( unEscapeString )
5051
5152-- | Produce an EPUB file from a Pandoc document.
5253writeEPUB :: Maybe String -- ^ EPUB stylesheet specified at command line
@@ -238,9 +239,10 @@ transformInlines :: HTMLMathMethod
238239transformInlines _ _ _ (Image lab (src,_) : xs) | isNothing (imageTypeOf src) =
239240 return $ Emph lab : xs
240241transformInlines _ sourceDir picsRef (Image lab (src,tit) : xs) = do
242+ let src' = unEscapeString src
241243 pics <- readIORef picsRef
242− let oldsrc = sourceDir </> src
243− let ext = takeExtension src
244+ let oldsrc = sourceDir </> src'
245+ let ext = takeExtension src'
244246 newsrc <- case lookup oldsrc pics of
245247 Just n -> return n
246248 Nothing -> do
src/Text/Pandoc/Writers/LaTeX.hs+5−1
@@ -34,6 +34,7 @@ import Text.Pandoc.Generic
3434import Text.Pandoc.Shared
3535import Text.Pandoc.Templates
3636import Text.Printf ( printf )
37+import Network.URI ( isAbsoluteURI, unEscapeString )
3738import Data.List ( (\\), isSuffixOf, isPrefixOf, intercalate, intersperse )
3839import Data.Char ( toLower, isPunctuation )
3940import Control.Monad.State
@@ -425,7 +426,10 @@ inlineToLaTeX (Link txt (src, _)) =
425426 contents <> char '}'
426427inlineToLaTeX (Image _ (source, _)) = do
427428 modify $ \s -> s{ stGraphics = True }
428− return $ "\\includegraphics" <> braces (text source)
429+ let source' = if isAbsoluteURI source
430+ then source
431+ else unEscapeString source
432+ return $ "\\includegraphics" <> braces (text source')
429433inlineToLaTeX (Note contents) = do
430434 modify (\s -> s{stInNote = True})
431435 contents' <- blockListToLaTeX contents
src/Text/Pandoc/Writers/ODT.hs+4−2
@@ -41,6 +41,7 @@ import Text.Pandoc.Generic
4141import Text.Pandoc.Writers.OpenDocument ( writeOpenDocument )
4242import System.Directory
4343import Control.Monad (liftM)
44+import Network.URI ( unEscapeString )
4445
4546-- | Produce an ODT file from a Pandoc document.
4647writeODT :: Maybe FilePath -- ^ Path specified by --reference-odt
@@ -74,9 +75,10 @@ writeODT mbRefOdt opts doc = do
7475
7576transformPic :: FilePath -> IORef [Entry] -> Inline -> IO Inline
7677transformPic sourceDir entriesRef (Image lab (src,tit)) = do
78+ let src' = unEscapeString src
7779 entries <- readIORef entriesRef
78− let newsrc = "Pictures/" ++ show (length entries) ++ takeExtension src
79− catch (readEntry [] (sourceDir </> src) >>= \entry ->
80+ let newsrc = "Pictures/" ++ show (length entries) ++ takeExtension src'
81+ catch (readEntry [] (sourceDir </> src') >>= \entry ->
8082 modifyIORef entriesRef (entry{ eRelativePath = newsrc } :) >>
8183 return (Image lab (newsrc, tit)))
8284 (\_ -> return (Emph lab))
src/Text/Pandoc/Writers/OpenDocument.hs+1−1
@@ -394,7 +394,7 @@ inlineToOpenDocument o ils
394394 mkImg s = inTags False "draw:frame" [] $
395395 selfClosingTag "draw:image" [ ("xlink:href" , s )
396396 , ("xlink:type" , "simple")
397− , (" xlink:show" , "embed" )
397+ , ("xlink:show" , "embed" )
398398 , ("xlink:actuate", "onLoad")]
399399 mkNote l = do
400400 n <- length <$> gets stNotes
src/Text/Pandoc/Writers/RTF.hs+18−13
@@ -37,23 +37,28 @@ import Data.Char ( ord, isDigit, toLower )
3737import System.FilePath ( takeExtension )
3838import qualified Data.ByteString as B
3939import Text.Printf ( printf )
40+import Network.URI ( isAbsoluteURI, unEscapeString )
4041
4142-- | Convert Image inlines into a raw RTF embedded image, read from a file.
4243-- If file not found or filetype not jpeg or png, leave the inline unchanged.
4344rtfEmbedImage :: Inline -> IO Inline
44−rtfEmbedImage x@(Image _ (src,_))
45− | map toLower (takeExtension src) `elem` [".jpg",".jpeg",".png"] = do
46− imgdata <- catch (B.readFile src) (\_ -> return B.empty)
47− let bytes = map (printf "%02x") $ B.unpack imgdata
48− let filetype = case map toLower (takeExtension src) of
49− ".jpg" -> "\\jpegblip"
50− ".jpeg" -> "\\jpegblip"
51− ".png" -> "\\pngblip"
52− _ -> error "Unknown file type"
53− let raw = "{\\pict" ++ filetype ++ " " ++ concat bytes ++ "}"
54− return $ if B.null imgdata
55− then x
56− else RawInline "rtf" raw
45+rtfEmbedImage x@(Image _ (src,_)) = do
46+ let ext = map toLower (takeExtension src)
47+ if ext `elem` [".jpg",".jpeg",".png"] && not (isAbsoluteURI src)
48+ then do
49+ let src' = unEscapeString src
50+ imgdata <- catch (B.readFile src') (\_ -> return B.empty)
51+ let bytes = map (printf "%02x") $ B.unpack imgdata
52+ let filetype = case ext of
53+ ".jpg" -> "\\jpegblip"
54+ ".jpeg" -> "\\jpegblip"
55+ ".png" -> "\\pngblip"
56+ _ -> error "Unknown file type"
57+ let raw = "{\\pict" ++ filetype ++ " " ++ concat bytes ++ "}"
58+ return $ if B.null imgdata
59+ then x
60+ else RawInline "rtf" raw
61+ else return x
5762rtfEmbedImage x = return x
5863
5964-- | Convert Pandoc to a string in rich text format.
src/Text/Pandoc/Writers/Texinfo.hs+7−5
@@ -37,6 +37,8 @@ import Data.Ord ( comparing )
3737import Data.Char ( chr, ord )
3838import Control.Monad.State
3939import Text.Pandoc.Pretty
40+import Network.URI ( isAbsoluteURI, unEscapeString )
41+import System.FilePath
4042
4143data WriterState =
4244 WriterState { stStrikeout :: Bool -- document contains strikeout
@@ -412,11 +414,11 @@ inlineToTexinfo (Image alternate (source, _)) = do
412414 return $ text ("@image{" ++ base ++ ",,,") <> content <> text "," <>
413415 text (ext ++ "}")
414416 where
415− (revext, revbase) = break (=='.') (reverse source)
416− ext = reverse revext
417− base = case revbase of
418− ('.' : rest) -> reverse rest
419− _ -> reverse revbase
417+ ext = drop 1 $ takeExtension source'
418+ base = takeBaseName source'
419+ source' = if isAbsoluteURI source
420+ then source
421+ else unEscapeString source
420422
421423inlineToTexinfo (Note contents) = do
422424 contents' <- blockListToTexinfo contents
tests/writer.opendocument+2−2
@@ -1571,9 +1571,9 @@ link in pointy braces</text:span></text:a>.</text:p>
15711571<text:h text:style-name="Heading_20_1" text:outline-level="1">Images</text:h>
15721572<text:p text:style-name="First_20_paragraph">From &#8220;Voyage dans la
15731573Lune&#8221; by Georges Melies (1902):</text:p>
1574−<text:p text:style-name="Text_20_body"><draw:frame><draw:image xlink:href="lalune.jpg" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad" /></draw:frame></text:p>
1574+<text:p text:style-name="Text_20_body"><draw:frame><draw:image xlink:href="lalune.jpg" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad" /></draw:frame></text:p>
15751575<text:p text:style-name="Text_20_body">Here is a movie
1576−<draw:frame><draw:image xlink:href="movie.jpg" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad" /></draw:frame>
1576+<draw:frame><draw:image xlink:href="movie.jpg" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad" /></draw:frame>
15771577icon.</text:p>
15781578<text:p text:style-name="Horizontal_20_Line" />
15791579<text:h text:style-name="Heading_20_1" text:outline-level="1">Footnotes</text:h>