@@ -25,7 +25,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2525 Stability : alpha
2626 Portability : portable
2727
28−UTF-8 aware string IO functions that will work with GHC 6.10 or 6.12.
28+UTF-8 aware string IO functions that will work with GHC 6.10, 6.12, or 7.
2929-}
3030module Text.Pandoc.UTF8 ( readFile
3131 , writeFile
@@ -34,9 +34,46 @@ module Text.Pandoc.UTF8 ( readFile
3434 , putStrLn
3535 , hPutStr
3636 , hPutStrLn
37+ , hGetContents
3738 )
3839
3940where
41+
42+#ifdef MIN_VERSION_base(4,2,0)
43+
44+import System.IO hiding (readFile, writeFile, getContents,
45+ putStr, putStrLn, hPutStr, hPutStrLn, hGetContents)
46+import Prelude hiding (readFile, writeFile, getContents, putStr, putStrLn )
47+import qualified System.IO as IO
48+
49+readFile :: FilePath -> IO String
50+readFile f = do
51+ h <- openFile f ReadMode
52+ hGetContents h
53+
54+writeFile :: FilePath -> String -> IO ()
55+writeFile f s = withFile f WriteMode $ \h -> hPutStr h s
56+
57+getContents :: IO String
58+getContents = hGetContents stdin
59+
60+putStr :: String -> IO ()
61+putStr s = hPutStr stdout s
62+
63+putStrLn :: String -> IO ()
64+putStrLn s = hPutStrLn stdout s
65+
66+hPutStr :: Handle -> String -> IO ()
67+hPutStr h s = hSetEncoding h utf8 >> IO.hPutStr h s
68+
69+hPutStrLn :: Handle -> String -> IO ()
70+hPutStrLn h s = hSetEncoding h utf8 >> IO.hPutStrLn h s
71+
72+hGetContents :: Handle -> IO String
73+hGetContents h = hSetEncoding h utf8_bom >> IO.hGetContents h
74+
75+#else
76+
4077import qualified Data.ByteString as B
4178import Codec.Binary.UTF8.String (encodeString)
4279import Data.ByteString.UTF8 (toString, fromString)
@@ -44,6 +81,7 @@ import Prelude hiding (readFile, writeFile, getContents, putStr, putStrLn)
4481import System.IO (Handle)
4582import Control.Monad (liftM)
4683
84+
4785bom :: B.ByteString
4886bom = B.pack [0xEF, 0xBB, 0xBF]
4987
@@ -60,6 +98,9 @@ writeFile f = B.writeFile (encodeString f) . fromString
6098getContents :: IO String
6199getContents = liftM (toString . stripBOM) B.getContents
62100
101+hGetContents :: Handle -> IO String
102+hGetContents h = liftM (toString . stripBOM) (B.hGetContents h)
103+
63104putStr :: String -> IO ()
64105putStr = B.putStr . fromString
65106
@@ -71,3 +112,5 @@ hPutStr h = B.hPutStr h . fromString
71112
72113hPutStrLn :: Handle -> String -> IO ()
73114hPutStrLn h s = hPutStr h (s ++ "\n")
115+
116+#endif