From 7d20ffe3e0192d9a8c4cc476a21e5060d601de65 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 6 May 2010 23:04:44 -0700 Subject: UTF8: Modified readFile and getContents to strip BOM if present. --- src/Text/Pandoc/UTF8.hs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/Text/Pandoc/UTF8.hs b/src/Text/Pandoc/UTF8.hs index fb912a373..3dd61176c 100644 --- a/src/Text/Pandoc/UTF8.hs +++ b/src/Text/Pandoc/UTF8.hs @@ -43,14 +43,21 @@ import Prelude hiding (readFile, writeFile, getContents, putStr, putStrLn) import System.IO (Handle) import Control.Monad (liftM) +bom :: B.ByteString +bom = B.pack [0xEF, 0xBB, 0xBF] + +stripBOM :: B.ByteString -> B.ByteString +stripBOM s | bom `B.isPrefixOf` s = B.drop 3 s +stripBOM s = s + readFile :: FilePath -> IO String -readFile = liftM toString . B.readFile +readFile = liftM (toString . stripBOM) . B.readFile writeFile :: FilePath -> String -> IO () writeFile f = B.writeFile f . fromString getContents :: IO String -getContents = liftM toString B.getContents +getContents = liftM (toString . stripBOM) B.getContents putStr :: String -> IO () putStr = B.putStr . fromString -- cgit v1.2.3