git.iko.soy/typeable/pandoc
on master help sign in
Merge branch 'lineend'
commite42f3465164e7d4776c771cc36b1cc1ebfec3651 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 30 Jan 2011, 16:03 (16 years ago) parentc2998f5b1b6d0f
2 files changed+58−19
src/Text/Pandoc/UTF8.hs+44−1 src/pandoc.hs+14−18
src/Text/Pandoc/UTF8.hs+44−1
@@ -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
src/pandoc.hs+14−18
@@ -51,7 +51,7 @@ import Control.Monad (when, unless, liftM)
5151import Network.HTTP (simpleHTTP, mkRequest, getResponseBody, RequestMethod(..))
5252import Network.URI (parseURI, isURI, URI(..))
5353import qualified Data.ByteString.Lazy as B
54−import Data.ByteString.Lazy.UTF8 (toString, fromString)
54+import Data.ByteString.Lazy.UTF8 (toString )
5555import Codec.Binary.UTF8.String (decodeString, encodeString)
5656
5757copyrightMessage :: String
@@ -713,14 +713,6 @@ main = do
713713 Just r -> return r
714714 Nothing -> error ("Unknown reader: " ++ readerName')
715715
716− let writer = case lookup writerName' writers of
717− Nothing | writerName' == "epub" -> writeEPUB epubStylesheet
718− Nothing | writerName' == "odt" -> writeODT referenceODT
719− Just r -> \o ->
720− return . fromString . r o
721− Nothing -> error $ "Unknown writer: " ++
722− writerName'
723−
724716 templ <- getDefaultTemplate datadir writerName'
725717 let defaultTemplate = case templ of
726718 Right t -> t
@@ -855,12 +847,16 @@ main = do
855847 processBiblio cslfile' refs doc1
856848 else return doc1
857849
858− writerOutput <- writer writerOptions doc2
859−
860− let writerOutput' = if standalone'
861− then writerOutput
862− else writerOutput `B.snoc` 10
863−
864− if outputFile == "-"
865− then B.putStr writerOutput'
866− else B.writeFile (encodeString outputFile) writerOutput'
850+ case lookup writerName' writers of
851+ Nothing | writerName' == "epub" ->
852+ writeEPUB epubStylesheet writerOptions doc2
853+ >>= B.writeFile (encodeString outputFile)
854+ Nothing | writerName' == "odt" ->
855+ writeODT referenceODT writerOptions doc2
856+ >>= B.writeFile (encodeString outputFile)
857+ Just r -> writerFn outputFile result
858+ where writerFn "-" = UTF8.putStr
859+ writerFn f = UTF8.writeFile f
860+ result = r writerOptions doc2 ++
861+ ['\n' | not standalone']
862+ Nothing -> error $ "Unknown writer: " ++ writerName'