git.iko.soy/typeable/pandoc
on master help sign in
Use new UTF8 module in Shared, ODT, and the executables.
1cc11e6John MacFarlane
src/Text/Pandoc/ODT.hs−1
@@ -32,7 +32,6 @@ import Data.List ( find )
32import System.FilePath ( (</>), takeFileName )
32import System.FilePath ( (</>), takeFileName )
33import qualified Data.ByteString.Lazy as B
33import qualified Data.ByteString.Lazy as B
34import Data.ByteString.Lazy.UTF8 ( fromString )
34import Data.ByteString.Lazy.UTF8 ( fromString )
35−import Prelude hiding ( writeFile, readFile )
36import Codec.Archive.Zip
35import Codec.Archive.Zip
37import Control.Applicative ( (<$>) )
36import Control.Applicative ( (<$>) )
38import Text.ParserCombinators.Parsec
37import Text.ParserCombinators.Parsec
src/Text/Pandoc/Shared.hs+5−11
@@ -112,6 +112,7 @@ module Text.Pandoc.Shared (
112 ) where
112 ) where
113
113
114import Text.Pandoc.Definition
114import Text.Pandoc.Definition
115+import qualified Text.Pandoc.UTF8 as UTF8 (readFile, putStrLn)
115import Text.ParserCombinators.Parsec
116import Text.ParserCombinators.Parsec
116import Text.PrettyPrint.HughesPJ ( Doc, fsep, ($$), (<>), empty, isEmpty, text, nest )
117import Text.PrettyPrint.HughesPJ ( Doc, fsep, ($$), (<>), empty, isEmpty, text, nest )
117import qualified Text.PrettyPrint.HughesPJ as PP
118import qualified Text.PrettyPrint.HughesPJ as PP
@@ -123,13 +124,6 @@ import Network.URI ( parseURI, URI (..), isAllowedInURI, escapeURIString, unEsca
123import Codec.Binary.UTF8.String ( encodeString, decodeString )
124import Codec.Binary.UTF8.String ( encodeString, decodeString )
124import System.Directory
125import System.Directory
125import System.FilePath ( (</>) )
126import System.FilePath ( (</>) )
126−-- Note: ghc >= 6.12 (base >=4.2) supports unicode through iconv
127−-- So we use System.IO.UTF8 only if we have an earlier version
128−#if MIN_VERSION_base(4,2,0)
129−#else
130−import Prelude hiding ( putStr, putStrLn, writeFile, readFile, getContents )
131−import System.IO.UTF8
132−#endif
133import Data.Generics
127import Data.Generics
134import qualified Control.Monad.State as S
128import qualified Control.Monad.State as S
135import Control.Monad (join)
129import Control.Monad (join)
@@ -676,7 +670,7 @@ readWith parser state input =
676testStringWith :: (Show a) => GenParser Char ParserState a
670testStringWith :: (Show a) => GenParser Char ParserState a
677 -> String
671 -> String
678 -> IO ()
672 -> IO ()
679−testStringWith parser str = putStrLn $ show $
673+testStringWith parser str = UTF8.putStrLn $ show $
680 readWith parser defaultParserState str
674 readWith parser defaultParserState str
681
675
682-- | Parsing options.
676-- | Parsing options.
@@ -1074,6 +1068,6 @@ inDirectory path action = do
1074readDataFile :: Maybe FilePath -> FilePath -> IO String
1068readDataFile :: Maybe FilePath -> FilePath -> IO String
1075readDataFile userDir fname =
1069readDataFile userDir fname =
1076 case userDir of
1070 case userDir of
1077− Nothing -> getDataFileName fname >>= readFile
1071+ Nothing -> getDataFileName fname >>= UTF8.readFile
1078− Just u -> catch (readFile $ u </> fname)
1072+ Just u -> catch (UTF8.readFile $ u </> fname)
1079− (\_ -> getDataFileName fname >>= readFile)
1073+ (\_ -> getDataFileName fname >>= UTF8.readFile)
src/markdown2pdf.hs+8−15
@@ -9,14 +9,7 @@ import Control.Exception (tryJust, bracket)
9import System.IO (stderr)
9import System.IO (stderr)
10import System.IO.Error (isDoesNotExistError)
10import System.IO.Error (isDoesNotExistError)
11import System.Environment ( getArgs, getProgName )
11import System.Environment ( getArgs, getProgName )
12−-- Note: ghc >= 6.12 (base >=4.2) supports unicode through iconv
12+import qualified Text.Pandoc.UTF8 as UTF8
13−-- So we use System.IO.UTF8 only if we have an earlier version
14−#if MIN_VERSION_base(4,2,0)
15−import System.IO (hPutStrLn)
16−#else
17−import Prelude hiding ( putStr, putStrLn, writeFile, readFile, getContents )
18−import System.IO.UTF8
19−#endif
20import System.Exit (ExitCode (..), exitWith)
13import System.Exit (ExitCode (..), exitWith)
21import System.FilePath
14import System.FilePath
22import System.Directory
15import System.Directory
@@ -57,7 +50,7 @@ runLatexRaw latexProgram file = do
57 takeDirectory file, dropExtension file] >> return ()
50 takeDirectory file, dropExtension file] >> return ()
58 let pdfFile = replaceExtension file "pdf"
51 let pdfFile = replaceExtension file "pdf"
59 let logFile = replaceExtension file "log"
52 let logFile = replaceExtension file "log"
60− txt <- tryJust (guard . isDoesNotExistError) (readFile logFile)
53+ txt <- tryJust (guard . isDoesNotExistError) (UTF8.readFile logFile)
61 let checks = checkLatex $ either (const "") id txt
54 let checks = checkLatex $ either (const "") id txt
62 case checks of
55 case checks of
63 -- err , bib , ref , msg
56 -- err , bib , ref , msg
@@ -122,13 +115,13 @@ runBibtex file = do
122exit :: String -> IO a
115exit :: String -> IO a
123exit x = do
116exit x = do
124 progName <- getProgName
117 progName <- getProgName
125− hPutStrLn stderr $ progName ++ ": " ++ x
118+ UTF8.hPutStrLn stderr $ progName ++ ": " ++ x
126 exitWith $ ExitFailure 1
119 exitWith $ ExitFailure 1
127
120
128saveStdin :: FilePath -> IO (Either String FilePath)
121saveStdin :: FilePath -> IO (Either String FilePath)
129saveStdin file = do
122saveStdin file = do
130− text <- getContents
123+ text <- UTF8.getContents
131− writeFile file text
124+ UTF8.writeFile file text
132 fileExist <- doesFileExist file
125 fileExist <- doesFileExist file
133 case fileExist of
126 case fileExist of
134 False -> return $ Left $! "Could not create " ++ file
127 False -> return $ Left $! "Could not create " ++ file
@@ -137,7 +130,7 @@ saveStdin file = do
137saveOutput :: FilePath -> FilePath -> IO ()
130saveOutput :: FilePath -> FilePath -> IO ()
138saveOutput input output = do
131saveOutput input output = do
139 copyFile input output
132 copyFile input output
140− hPutStrLn stderr $! "Created " ++ output
133+ UTF8.hPutStrLn stderr $! "Created " ++ output
141
134
142main :: IO ()
135main :: IO ()
143main = bracket
136main = bracket
@@ -170,8 +163,8 @@ main = bracket
170 any (\o -> (o ++ "=") `isPrefixOf` x) goodoptslong
163 any (\o -> (o ++ "=") `isPrefixOf` x) goodoptslong
171 unless (all isGoodopt opts) $ do
164 unless (all isGoodopt opts) $ do
172 (code, out, _err) <- readProcessWithExitCode "pandoc" ["--help"] ""
165 (code, out, _err) <- readProcessWithExitCode "pandoc" ["--help"] ""
173− putStrLn "markdown2pdf [OPTIONS] [FILES]\nOptions:"
166+ UTF8.putStrLn "markdown2pdf [OPTIONS] [FILES]\nOptions:"
174− putStr $ unlines $
167+ UTF8.putStr $ unlines $
175 filter (\l -> any (`isInfixOf` l) goodoptslong) $ lines out
168 filter (\l -> any (`isInfixOf` l) goodoptslong) $ lines out
176 exitWith code
169 exitWith code
177
170
src/pandoc.hs+22−29
@@ -45,14 +45,7 @@ import Data.Char ( toLower, isDigit )
45import Data.List ( intercalate, isSuffixOf )
45import Data.List ( intercalate, isSuffixOf )
46import System.Directory ( getAppUserDataDirectory )
46import System.Directory ( getAppUserDataDirectory )
47import System.IO ( stdout, stderr )
47import System.IO ( stdout, stderr )
48−-- Note: ghc >= 6.12 (base >=4.2) supports unicode through iconv
48+import qualified Text.Pandoc.UTF8 as UTF8
49−-- So we use System.IO.UTF8 only if we have an earlier version
50−#if MIN_VERSION_base(4,2,0)
51−import System.IO ( hPutStr, hPutStrLn )
52−#else
53−import Prelude hiding ( putStr, putStrLn, writeFile, readFile, getContents )
54−import System.IO.UTF8
55−#endif
56#ifdef _CITEPROC
49#ifdef _CITEPROC
57import Text.CSL
50import Text.CSL
58import Text.Pandoc.Biblio
51import Text.Pandoc.Biblio
@@ -344,7 +337,7 @@ options =
344 "references" -> return ReferenceObfuscation
337 "references" -> return ReferenceObfuscation
345 "javascript" -> return JavascriptObfuscation
338 "javascript" -> return JavascriptObfuscation
346 "none" -> return NoObfuscation
339 "none" -> return NoObfuscation
347− _ -> hPutStrLn stderr ("Error: Unknown obfuscation method: " ++ arg) >>
340+ _ -> UTF8.hPutStrLn stderr ("Error: Unknown obfuscation method: " ++ arg) >>
348 exitWith (ExitFailure 6)
341 exitWith (ExitFailure 6)
349 return opt { optEmailObfuscation = method })
342 return opt { optEmailObfuscation = method })
350 "none|javascript|references")
343 "none|javascript|references")
@@ -378,7 +371,7 @@ options =
378 return opt{ optTransforms =
371 return opt{ optTransforms =
379 headerShift shift : oldTransforms }
372 headerShift shift : oldTransforms }
380 else do
373 else do
381− hPutStrLn stderr $ "base-header-level must be a number >= 1"
374+ UTF8.hPutStrLn stderr $ "base-header-level must be a number >= 1"
382 exitWith $ ExitFailure 19)
375 exitWith $ ExitFailure 19)
383 "LEVEL")
376 "LEVEL")
384 "" -- "Headers base level"
377 "" -- "Headers base level"
@@ -386,7 +379,7 @@ options =
386 , Option "" ["template"]
379 , Option "" ["template"]
387 (ReqArg
380 (ReqArg
388 (\arg opt -> do
381 (\arg opt -> do
389− text <- readFile arg
382+ text <- UTF8.readFile arg
390 return opt{ optTemplate = text,
383 return opt{ optTemplate = text,
391 optStandalone = True })
384 optStandalone = True })
392 "FILENAME")
385 "FILENAME")
@@ -400,7 +393,7 @@ options =
400 let newvars = optVariables opt ++ [(k,v)]
393 let newvars = optVariables opt ++ [(k,v)]
401 return opt{ optVariables = newvars }
394 return opt{ optVariables = newvars }
402 _ -> do
395 _ -> do
403− hPutStrLn stderr $ "Could not parse `" ++ arg ++ "' as a key/value pair (k=v or k:v)"
396+ UTF8.hPutStrLn stderr $ "Could not parse `" ++ arg ++ "' as a key/value pair (k=v or k:v)"
404 exitWith $ ExitFailure 17)
397 exitWith $ ExitFailure 17)
405 "FILENAME")
398 "FILENAME")
406 "" -- "Use custom template"
399 "" -- "Use custom template"
@@ -418,7 +411,7 @@ options =
418 , Option "H" ["include-in-header"]
411 , Option "H" ["include-in-header"]
419 (ReqArg
412 (ReqArg
420 (\arg opt -> do
413 (\arg opt -> do
421− text <- readFile arg
414+ text <- UTF8.readFile arg
422 -- add new ones to end, so they're included in order specified
415 -- add new ones to end, so they're included in order specified
423 let newvars = optVariables opt ++ [("header-includes",text)]
416 let newvars = optVariables opt ++ [("header-includes",text)]
424 return opt { optVariables = newvars,
417 return opt { optVariables = newvars,
@@ -429,7 +422,7 @@ options =
429 , Option "B" ["include-before-body"]
422 , Option "B" ["include-before-body"]
430 (ReqArg
423 (ReqArg
431 (\arg opt -> do
424 (\arg opt -> do
432− text <- readFile arg
425+ text <- UTF8.readFile arg
433 -- add new ones to end, so they're included in order specified
426 -- add new ones to end, so they're included in order specified
434 let newvars = optVariables opt ++ [("include-before",text)]
427 let newvars = optVariables opt ++ [("include-before",text)]
435 return opt { optVariables = newvars,
428 return opt { optVariables = newvars,
@@ -440,7 +433,7 @@ options =
440 , Option "A" ["include-after-body"]
433 , Option "A" ["include-after-body"]
441 (ReqArg
434 (ReqArg
442 (\arg opt -> do
435 (\arg opt -> do
443− text <- readFile arg
436+ text <- UTF8.readFile arg
444 -- add new ones to end, so they're included in order specified
437 -- add new ones to end, so they're included in order specified
445 let newvars = optVariables opt ++ [("include-after",text)]
438 let newvars = optVariables opt ++ [("include-after",text)]
446 return opt { optVariables = newvars,
439 return opt { optVariables = newvars,
@@ -451,7 +444,7 @@ options =
451 , Option "C" ["custom-header"]
444 , Option "C" ["custom-header"]
452 (ReqArg
445 (ReqArg
453 (\arg opt -> do
446 (\arg opt -> do
454− text <- readFile arg
447+ text <- UTF8.readFile arg
455 let newVars = ("legacy-header", text) : optVariables opt
448 let newVars = ("legacy-header", text) : optVariables opt
456 return opt { optVariables = newVars
449 return opt { optVariables = newVars
457 , optStandalone = True })
450 , optStandalone = True })
@@ -479,7 +472,7 @@ options =
479 (\arg _ -> do
472 (\arg _ -> do
480 templ <- getDefaultTemplate Nothing arg
473 templ <- getDefaultTemplate Nothing arg
481 case templ of
474 case templ of
482− Right t -> hPutStr stdout t
475+ Right t -> UTF8.hPutStr stdout t
483 Left e -> error $ show e
476 Left e -> error $ show e
484 exitWith ExitSuccess)
477 exitWith ExitSuccess)
485 "FORMAT")
478 "FORMAT")
@@ -521,7 +514,7 @@ options =
521 (NoArg
514 (NoArg
522 (\_ -> do
515 (\_ -> do
523 prg <- getProgName
516 prg <- getProgName
524− hPutStrLn stdout (prg ++ " " ++ pandocVersion ++ compileInfo ++
517+ UTF8.hPutStrLn stdout (prg ++ " " ++ pandocVersion ++ compileInfo ++
525 copyrightMessage)
518 copyrightMessage)
526 exitWith ExitSuccess ))
519 exitWith ExitSuccess ))
527 "" -- "Print version"
520 "" -- "Print version"
@@ -530,7 +523,7 @@ options =
530 (NoArg
523 (NoArg
531 (\_ -> do
524 (\_ -> do
532 prg <- getProgName
525 prg <- getProgName
533− hPutStr stdout (usageMessage prg options)
526+ UTF8.hPutStr stdout (usageMessage prg options)
534 exitWith ExitSuccess ))
527 exitWith ExitSuccess ))
535 "" -- "Show help"
528 "" -- "Show help"
536 ]
529 ]
@@ -603,8 +596,8 @@ main = do
603
596
604 unless (null errors) $
597 unless (null errors) $
605 do name <- getProgName
598 do name <- getProgName
606− mapM_ (\e -> hPutStr stderr (name ++ ": ") >> hPutStr stderr e) errors
599+ mapM_ (\e -> UTF8.hPutStr stderr (name ++ ": ") >> UTF8.hPutStr stderr e) errors
607− hPutStrLn stderr $ "Try " ++ name ++ " --help for more information."
600+ UTF8.hPutStrLn stderr $ "Try " ++ name ++ " --help for more information."
608 exitWith $ ExitFailure 2
601 exitWith $ ExitFailure 2
609
602
610 let defaultOpts' = if compatMode
603 let defaultOpts' = if compatMode
@@ -653,13 +646,13 @@ main = do
653 } = opts
646 } = opts
654
647
655 when dumpArgs $
648 when dumpArgs $
656− do hPutStrLn stdout outputFile
649+ do UTF8.hPutStrLn stdout outputFile
657− mapM_ (\arg -> hPutStrLn stdout arg) args
650+ mapM_ (\arg -> UTF8.hPutStrLn stdout arg) args
658 exitWith ExitSuccess
651 exitWith ExitSuccess
659
652
660 -- warn about deprecated options
653 -- warn about deprecated options
661 case lookup "legacy-header" variables of
654 case lookup "legacy-header" variables of
662− Just _ -> hPutStrLn stderr $
655+ Just _ -> UTF8.hPutStrLn stderr $
663 "Warning: The -C/--custom-header is deprecated.\n" ++
656 "Warning: The -C/--custom-header is deprecated.\n" ++
664 "Please transition to using --template instead."
657 "Please transition to using --template instead."
665 Nothing -> return ()
658 Nothing -> return ()
@@ -765,7 +758,7 @@ main = do
765 writerIdentifierPrefix = idPrefix }
758 writerIdentifierPrefix = idPrefix }
766
759
767 when (isNonTextOutput writerName' && outputFile == "-") $
760 when (isNonTextOutput writerName' && outputFile == "-") $
768− do hPutStrLn stderr ("Error: Cannot write " ++ writerName ++ " output to stdout.\n" ++
761+ do UTF8.hPutStrLn stderr ("Error: Cannot write " ++ writerName ++ " output to stdout.\n" ++
769 "Specify an output file using the -o option.")
762 "Specify an output file using the -o option.")
770 exitWith $ ExitFailure 5
763 exitWith $ ExitFailure 5
771
764
@@ -775,10 +768,10 @@ main = do
775
768
776 let readSources [] = mapM readSource ["-"]
769 let readSources [] = mapM readSource ["-"]
777 readSources srcs = mapM readSource srcs
770 readSources srcs = mapM readSource srcs
778− readSource "-" = getContents
771+ readSource "-" = UTF8.getContents
779 readSource src = case parseURI src of
772 readSource src = case parseURI src of
780 Just u -> readURI u
773 Just u -> readURI u
781− Nothing -> readFile src
774+ Nothing -> UTF8.readFile src
782 readURI uri = simpleHTTP (mkRequest GET uri) >>= getResponseBody >>=
775 readURI uri = simpleHTTP (mkRequest GET uri) >>= getResponseBody >>=
783 return . toString -- treat all as UTF8
776 return . toString -- treat all as UTF8
784
777
@@ -800,5 +793,5 @@ main = do
800 case writerName' of
793 case writerName' of
801 "odt" -> saveOpenDocumentAsODT datadir outputFile sourceDirRelative referenceODT writerOutput
794 "odt" -> saveOpenDocumentAsODT datadir outputFile sourceDirRelative referenceODT writerOutput
802 _ -> if outputFile == "-"
795 _ -> if outputFile == "-"
803− then putStr writerOutput
796+ then UTF8.putStr writerOutput
804− else writeFile outputFile writerOutput
797+ else UTF8.writeFile outputFile writerOutput