From 449ddeb53b9bc2ca28deaedf03a4ea78d9e0d53c Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 11 Jan 2013 16:19:06 -0800 Subject: Refactoring: * Shared now exports fetchItem (instead of getItem) and openURL * fetchItem has different parameters than getItem and includes some logic formerly in the ODT and Docx writers * getItem still used in SelfContained --- src/Text/Pandoc/Writers/Docx.hs | 10 ++-------- src/Text/Pandoc/Writers/EPUB.hs | 1 + src/Text/Pandoc/Writers/ODT.hs | 16 +++++----------- 3 files changed, 8 insertions(+), 19 deletions(-) (limited to 'src/Text/Pandoc/Writers') diff --git a/src/Text/Pandoc/Writers/Docx.hs b/src/Text/Pandoc/Writers/Docx.hs index e8d49ead0..577f9fc84 100644 --- a/src/Text/Pandoc/Writers/Docx.hs +++ b/src/Text/Pandoc/Writers/Docx.hs @@ -52,8 +52,6 @@ import Data.Unique (hashUnique, newUnique) import System.Random (randomRIO) import Text.Printf (printf) import qualified Control.Exception as E -import Network.URI (isAbsoluteURI) -import System.FilePath (()) data WriterState = WriterState{ stTextProperties :: [Element] @@ -627,14 +625,10 @@ inlineToOpenXML opts (Image alt (src, tit)) = do Just (_,_,elt,_) -> return [elt] Nothing -> do let sourceDir = writerSourceDirectory opts - let src' = case src of - s | isAbsoluteURI s -> s - | isAbsoluteURI sourceDir -> sourceDir ++ "/" ++ s - | otherwise -> sourceDir s - res <- liftIO $ E.try $ getItem Nothing src' + res <- liftIO $ E.try $ fetchItem sourceDir src case res of Left (_ :: E.SomeException) -> do - liftIO $ warn $ "Could not find image `" ++ src' ++ "', skipping..." + liftIO $ warn $ "Could not find image `" ++ src ++ "', skipping..." -- emit alt text inlinesToOpenXML opts alt Right (img, _) -> do diff --git a/src/Text/Pandoc/Writers/EPUB.hs b/src/Text/Pandoc/Writers/EPUB.hs index 493b762ac..8a020002e 100644 --- a/src/Text/Pandoc/Writers/EPUB.hs +++ b/src/Text/Pandoc/Writers/EPUB.hs @@ -107,6 +107,7 @@ writeEPUB opts doc@(Pandoc meta _) = do Pandoc _ blocks <- bottomUpM (transformInlines (writerHTMLMathMethod opts') sourceDir picsRef) doc pics <- readIORef picsRef + -- TODO make this work with URLs: let readPicEntry (oldsrc, newsrc) = readEntry [] oldsrc >>= \e -> return e{ eRelativePath = newsrc } picEntries <- mapM readPicEntry pics diff --git a/src/Text/Pandoc/Writers/ODT.hs b/src/Text/Pandoc/Writers/ODT.hs index 8bae293c4..dac718ac8 100644 --- a/src/Text/Pandoc/Writers/ODT.hs +++ b/src/Text/Pandoc/Writers/ODT.hs @@ -31,12 +31,11 @@ Conversion of 'Pandoc' documents to ODT. module Text.Pandoc.Writers.ODT ( writeODT ) where import Data.IORef import Data.List ( isPrefixOf ) -import System.FilePath ( takeExtension, () ) import qualified Data.ByteString.Lazy as B import Text.Pandoc.UTF8 ( fromStringLazy ) import Codec.Archive.Zip import Text.Pandoc.Options ( WriterOptions(..) ) -import Text.Pandoc.Shared ( stringify, readDataFile, getItem, warn ) +import Text.Pandoc.Shared ( stringify, readDataFile, fetchItem, warn ) import Text.Pandoc.ImageSize ( imageSize, sizeInPoints ) import Text.Pandoc.MIME ( getMimeType ) import Text.Pandoc.Definition @@ -44,11 +43,11 @@ import Text.Pandoc.Generic import Text.Pandoc.Writers.OpenDocument ( writeOpenDocument ) import Control.Monad (liftM) import Control.Monad.Trans (liftIO) -import Network.URI ( unEscapeString, isAbsoluteURI ) import Text.Pandoc.XML import Text.Pandoc.Pretty import qualified Control.Exception as E import Data.Time.Clock.POSIX ( getPOSIXTime ) +import System.FilePath ( takeExtension ) -- | Produce an ODT file from a Pandoc document. writeODT :: WriterOptions -- ^ Writer options @@ -114,25 +113,20 @@ writeODT opts doc@(Pandoc (Meta title _ _) _) = do transformPic :: FilePath -> IORef [Entry] -> Inline -> IO Inline transformPic sourceDir entriesRef (Image lab (src,_)) = do - let src' = case unEscapeString src of - s | isAbsoluteURI s -> s - | isAbsoluteURI sourceDir -> sourceDir ++ "/" ++ s - | otherwise -> sourceDir s - res <- liftIO $ E.try $ getItem Nothing src' + res <- liftIO $ E.try $ fetchItem sourceDir src case res of Left (_ :: E.SomeException) -> do - liftIO $ warn $ "Could not find image `" ++ src' ++ "', skipping..." + liftIO $ warn $ "Could not find image `" ++ src ++ "', skipping..." return $ Emph lab Right (img, _) -> do let size = imageSize img let (w,h) = maybe (0,0) id $ sizeInPoints `fmap` size let tit' = show w ++ "x" ++ show h entries <- readIORef entriesRef - let newsrc = "Pictures/" ++ show (length entries) ++ takeExtension src' + let newsrc = "Pictures/" ++ show (length entries) ++ takeExtension src let toLazy = B.fromChunks . (:[]) epochtime <- floor `fmap` getPOSIXTime let entry = toEntry newsrc epochtime $ toLazy img - -- insert into entriesRef: sourceDir src', eRelativePath = newsrc modifyIORef entriesRef (entry:) return $ Image lab (newsrc, tit') transformPic _ _ x = return x -- cgit v1.2.3