@@ -30,21 +30,21 @@ Functions for producing an ODT file from OpenDocument XML.
3030-}
3131module Text.Pandoc.ODT ( saveOpenDocumentAsODT ) where
3232import Text.Pandoc.TH ( binaryContentsOf )
33−import Data.Maybe ( fromJust, isJust )
33+import Data.Maybe ( fromJust )
3434import Data.List ( partition, intersperse )
35−import Prelude hiding ( writeFile, readFile )
35+import Prelude hiding ( writeFile, readFile, getContents )
3636import System.IO.UTF8
3737import System.IO ( stderr )
3838import System.Directory
3939import System.FilePath ( (</>), takeDirectory, takeFileName, splitDirectories )
40−import System.Process ( runCommand, waitForProcess )
40+import System.Process ( runProcess, waitForProcess )
4141import System.Exit
4242import Text.XML.Light
4343import Text.XML.Light.Cursor
4444import Text.Pandoc.Shared ( withTempDir )
4545import Network.URI ( isURI )
46−import qualified Data.ByteString.Char8 as B ( writeFile, pack )
47−import Control.Monad ( unless )
46+import qualified Data.ByteString as B ( writeFile, pack )
47+import Data.ByteString.Internal ( c2w )
4848
4949-- | Produce an ODT file from OpenDocument XML.
5050saveOpenDocumentAsODT :: FilePath -- ^ Pathname of ODT file to be produced.
@@ -54,27 +54,26 @@ saveOpenDocumentAsODT :: FilePath -- ^ Pathname of ODT file to be produced.
5454saveOpenDocumentAsODT destinationODTPath sourceDirRelative xml = do
5555 let zipCmd = "zip"
5656 -- check for zip in path:
57− findExecutable zipCmd >>= \v -> unless (isJust v) $ error $ "The '" ++ zipCmd ++
58− "' command, which is needed to build an ODT file, was not found.\n" ++
59− "It can be obtained from http://www.info-zip.org/Zip.html\n" ++
60− "Debian (and Debian-based) linux: apt-get install zip\n" ++
61− "Windows: See http://gnuwin32.sourceforge.net/packages/zip.htm"
57+ zipPathMaybe <- findExecutable zipCmd
58+ let zipPath = case zipPathMaybe of
59+ Nothing -> error $ "The '" ++ zipCmd ++
60+ "' command, which is needed to build an ODT file, was not found.\n" ++
61+ "It can be obtained from http://www.info-zip.org/Zip.html\n" ++
62+ "Debian (and Debian-based) linux: apt-get install zip\n" ++
63+ "Windows: See http://gnuwin32.sourceforge.net/packages/zip.htm"
64+ Just x -> x
6265 withTempDir "pandoc-odt" $ \tempDir -> do
6366 let tempODT = tempDir </> "reference.odt"
64− B.writeFile tempODT $ B.pack $(binaryContentsOf $ "odt-styles" </> "reference.odt")
65− createDirectory $ tempDir </> "Pictures"
67+ B.writeFile tempODT $ B.pack $ map c2w $(binaryContentsOf $ "odt-styles" </> "reference.odt")
6668 xml' <- handlePictures tempODT sourceDirRelative xml
6769 writeFile (tempDir </> "content.xml") xml'
68− oldDir <- getCurrentDirectory
69− setCurrentDirectory tempDir
70− let zipCmdLine = "zip -9 -q -r " ++ tempODT ++ " " ++ "content.xml Pictures"
71− ec <- runCommand zipCmdLine >>= waitForProcess -- this requires compilation with -threaded
72− setCurrentDirectory oldDir
70+ ph <- runProcess zipPath ["-9", "-q", "-r", tempODT, "content.xml", "Pictures"]
71+ (Just tempDir) Nothing Nothing Nothing (Just stderr)
72+ ec <- waitForProcess ph -- requires compilation with -threaded
7373 case ec of
7474 ExitSuccess -> copyFile tempODT destinationODTPath
7575 _ -> error "Error creating ODT." >> exitWith ec
7676
77−
7877-- | Find <draw:image ... /> elements and copy the file (xlink:href attribute) into Pictures/ in
7978-- the zip file. If filename is a URL, attempt to download it. Modify xlink:href attributes
8079-- to point to the new locations in Pictures/. Return modified XML.
@@ -128,6 +127,7 @@ handleContent tempODT sourceDirRelative content@(Elem el) = do
128127 splitDirectories $ takeDirectory $ attrVal href
129128 let newLoc = "Pictures" </> pref ++ "_" ++ (takeFileName $ attrVal href)
130129 let tempDir = takeDirectory tempODT
130+ createDirectoryIfMissing False $ tempDir </> takeDirectory newLoc
131131 copyFile oldLoc $ tempDir </> newLoc
132132 let newAttrs = (href { attrVal = newLoc }) : rest
133133 return $ Elem (el { elAttribs = newAttrs })