git.iko.soy/typeable/pandoc
on master help sign in
Don't encode/decode file paths if base >= 4.5.

Prior to base 4.5 (and perhaps earlier - check), filepaths and command line arguments were treated as unencoded lists of bytes, not unicode strings, so we had to work around that by encoding and decoding them. This commit adds CPP checks for base 4.5 that disable the encoding/decoding. Fixes a bug with multilingual filenames when pandoc was compiled with ghc 7.4. Closes #540.

commitcc39c313c5eeedf8bc1693e0d80a510891a7fc4d authorJohn MacFarlane <jgm@berkeley.edu> dateFri 22 Jun 2012, 21:24 (14 years ago) parentc24b799
2 files changed+33−9
src/Text/Pandoc/UTF8.hs+16−6 src/pandoc.hs+17−3
src/Text/Pandoc/UTF8.hs+16−6
@@ -39,21 +39,25 @@ module Text.Pandoc.UTF8 ( readFile
3939
4040where
4141
42+#if MIN_VERSION_base(4,5,0)
43+#else
44+import Codec.Binary.UTF8.String (encodeString)
45+#endif
46+
4247#if MIN_VERSION_base(4,2,0)
4348
4449import System.IO hiding (readFile, writeFile, getContents,
4550 putStr, putStrLn, hPutStr, hPutStrLn, hGetContents)
4651import Prelude hiding (readFile, writeFile, getContents, putStr, putStrLn )
47−import Codec.Binary.UTF8.String (encodeString)
4852import qualified System.IO as IO
4953
5054readFile :: FilePath -> IO String
5155readFile f = do
52− h <- openFile (encodeString f) ReadMode
56+ h <- openFile (encodePath f) ReadMode
5357 hGetContents h
5458
5559writeFile :: FilePath -> String -> IO ()
56−writeFile f s = withFile (encodeString f) WriteMode $ \h -> hPutStr h s
60+writeFile f s = withFile (encodePath f) WriteMode $ \h -> hPutStr h s
5761
5862getContents :: IO String
5963getContents = hGetContents stdin
@@ -76,7 +80,6 @@ hGetContents h = hSetEncoding h utf8_bom >> IO.hGetContents h
7680#else
7781
7882import qualified Data.ByteString as B
79−import Codec.Binary.UTF8.String (encodeString)
8083import Data.ByteString.UTF8 (toString, fromString)
8184import Prelude hiding (readFile, writeFile, getContents, putStr, putStrLn)
8285import System.IO (Handle)
@@ -91,10 +94,10 @@ stripBOM s | bom `B.isPrefixOf` s = B.drop 3 s
9194stripBOM s = s
9295
9396readFile :: FilePath -> IO String
94−readFile = liftM (toString . stripBOM) . B.readFile . encodeString
97+readFile = liftM (toString . stripBOM) . B.readFile . encodePath
9598
9699writeFile :: FilePath -> String -> IO ()
97−writeFile f = B.writeFile (encodeString f) . fromString
100+writeFile f = B.writeFile (encodePath f) . fromString
98101
99102getContents :: IO String
100103getContents = liftM (toString . stripBOM) B.getContents
@@ -115,3 +118,10 @@ hPutStrLn :: Handle -> String -> IO ()
115118hPutStrLn h s = hPutStr h (s ++ "\n")
116119
117120#endif
121+
122+encodePath :: FilePath -> FilePath
123+#if MIN_VERSION_base(4,5,0)
124+encodePath = id
125+#else
126+encodePath = encodeString
127+#endif
src/pandoc.hs+17−3
@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
12{-
23Copyright (C) 2006-2012 John MacFarlane <jgm@berkeley.edu>
34
@@ -56,8 +57,21 @@ import Network.HTTP (simpleHTTP, mkRequest, getResponseBody, RequestMethod(..))
5657import Network.URI (parseURI, isURI, URI(..))
5758import qualified Data.ByteString.Lazy as B
5859import Data.ByteString.Lazy.UTF8 (toString )
59−import Codec.Binary.UTF8.String (decodeString, encodeString)
6060import Text.CSL.Reference (Reference(..))
61+#if MIN_VERSION_base(4,5,0)
62+#else
63+import Codec.Binary.UTF8.String (decodeString, encodeString)
64+#endif
65+
66+encodePath, decodeArg :: FilePath -> FilePath
67+#if MIN_VERSION_base(4,5,0)
68+encodePath = id
69+decodeArg = id
70+#else
71+encodePath = encodeString
72+decodeArg = decodeString
73+#endif
74+
6175
6276copyrightMessage :: String
6377copyrightMessage = "\nCopyright (C) 2006-2012 John MacFarlane\n" ++
@@ -745,7 +759,7 @@ defaultWriterName x =
745759main :: IO ()
746760main = do
747761
748− rawArgs <- liftM (map decodeString) getArgs
762+ rawArgs <- liftM (map decodeArg) getArgs
749763 prg <- getProgName
750764 let compatMode = (prg == "hsmarkdown")
751765
@@ -1023,7 +1037,7 @@ main = do
10231037 else return doc1
10241038
10251039 let writeBinary :: B.ByteString -> IO ()
1026− writeBinary = B.writeFile (encodeString outputFile)
1040+ writeBinary = B.writeFile (encodePath outputFile)
10271041
10281042 let writerFn :: FilePath -> String -> IO ()
10291043 writerFn "-" = UTF8.putStr