git.iko.soy/typeable/pandoc
on master help sign in
Use new UTF8 module in Shared, ODT, and the executables.
commit1cc11e60863abdbe3d0e05b73f53d0377cd8fec2 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 6 May 2010, 20:29 (16 years ago) parent7be82b4
4 files changed+35−56
src/Text/Pandoc/ODT.hs−1 src/Text/Pandoc/Shared.hs+5−11 src/markdown2pdf.hs+8−15 src/pandoc.hs+22−29
src/Text/Pandoc/ODT.hs−1
@@ -32,7 +32,6 @@ import Data.List ( find )
3232import System.FilePath ( (</>), takeFileName )
3333import qualified Data.ByteString.Lazy as B
3434import Data.ByteString.Lazy.UTF8 ( fromString )
35−import Prelude hiding ( writeFile, readFile )
3635import Codec.Archive.Zip
3736import Control.Applicative ( (<$>) )
3837import Text.ParserCombinators.Parsec
src/Text/Pandoc/Shared.hs+5−11
@@ -112,6 +112,7 @@ module Text.Pandoc.Shared (
112112 ) where
113113
114114import Text.Pandoc.Definition
115+import qualified Text.Pandoc.UTF8 as UTF8 (readFile, putStrLn)
115116import Text.ParserCombinators.Parsec
116117import Text.PrettyPrint.HughesPJ ( Doc, fsep, ($$), (<>), empty, isEmpty, text, nest )
117118import qualified Text.PrettyPrint.HughesPJ as PP
@@ -123,13 +124,6 @@ import Network.URI ( parseURI, URI (..), isAllowedInURI, escapeURIString, unEsca
123124import Codec.Binary.UTF8.String ( encodeString, decodeString )
124125import System.Directory
125126import 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
133127import Data.Generics
134128import qualified Control.Monad.State as S
135129import Control.Monad (join)
@@ -676,7 +670,7 @@ readWith parser state input =
676670testStringWith :: (Show a) => GenParser Char ParserState a
677671 -> String
678672 -> IO ()
679−testStringWith parser str = putStrLn $ show $
673+testStringWith parser str = UTF8.putStrLn $ show $
680674 readWith parser defaultParserState str
681675
682676-- | Parsing options.
@@ -1074,6 +1068,6 @@ inDirectory path action = do
10741068readDataFile :: Maybe FilePath -> FilePath -> IO String
10751069readDataFile userDir fname =
10761070 case userDir of
1077− Nothing -> getDataFileName fname >>= readFile
1078− Just u -> catch (readFile $ u </> fname)
1079− (\_ -> getDataFileName fname >>= readFile)
1071+ Nothing -> getDataFileName fname >>= UTF8.readFile
1072+ Just u -> catch (UTF8.readFile $ u </> fname)
1073+ (\_ -> getDataFileName fname >>= UTF8.readFile)
src/markdown2pdf.hs+8−15
@@ -9,14 +9,7 @@ import Control.Exception (tryJust, bracket)
99import System.IO (stderr)
1010import System.IO.Error (isDoesNotExistError)
1111import System.Environment ( getArgs, getProgName )
12−-- Note: ghc >= 6.12 (base >=4.2) supports unicode through iconv
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
12+import qualified Text.Pandoc.UTF8 as UTF8
2013import System.Exit (ExitCode (..), exitWith)
2114import System.FilePath
2215import System.Directory
@@ -57,7 +50,7 @@ runLatexRaw latexProgram file = do
5750 takeDirectory file, dropExtension file] >> return ()
5851 let pdfFile = replaceExtension file "pdf"
5952 let logFile = replaceExtension file "log"
60− txt <- tryJust (guard . isDoesNotExistError) (readFile logFile)
53+ txt <- tryJust (guard . isDoesNotExistError) (UTF8.readFile logFile)
6154 let checks = checkLatex $ either (const "") id txt
6255 case checks of
6356 -- err , bib , ref , msg
@@ -122,13 +115,13 @@ runBibtex file = do
122115exit :: String -> IO a
123116exit x = do
124117 progName <- getProgName
125− hPutStrLn stderr $ progName ++ ": " ++ x
118+ UTF8.hPutStrLn stderr $ progName ++ ": " ++ x
126119 exitWith $ ExitFailure 1
127120
128121saveStdin :: FilePath -> IO (Either String FilePath)
129122saveStdin file = do
130− text <- getContents
131− writeFile file text
123+ text <- UTF8.getContents
124+ UTF8.writeFile file text
132125 fileExist <- doesFileExist file
133126 case fileExist of
134127 False -> return $ Left $! "Could not create " ++ file
@@ -137,7 +130,7 @@ saveStdin file = do
137130saveOutput :: FilePath -> FilePath -> IO ()
138131saveOutput input output = do
139132 copyFile input output
140− hPutStrLn stderr $! "Created " ++ output
133+ UTF8.hPutStrLn stderr $! "Created " ++ output
141134
142135main :: IO ()
143136main = bracket
@@ -170,8 +163,8 @@ main = bracket
170163 any (\o -> (o ++ "=") `isPrefixOf` x) goodoptslong
171164 unless (all isGoodopt opts) $ do
172165 (code, out, _err) <- readProcessWithExitCode "pandoc" ["--help"] ""
173− putStrLn "markdown2pdf [OPTIONS] [FILES]\nOptions:"
174− putStr $ unlines $
166+ UTF8.putStrLn "markdown2pdf [OPTIONS] [FILES]\nOptions:"
167+ UTF8.putStr $ unlines $
175168 filter (\l -> any (`isInfixOf` l) goodoptslong) $ lines out
176169 exitWith code
177170
src/pandoc.hs+22−29
@@ -45,14 +45,7 @@ import Data.Char ( toLower, isDigit )
4545import Data.List ( intercalate, isSuffixOf )
4646import System.Directory ( getAppUserDataDirectory )
4747import System.IO ( stdout, stderr )
48−-- Note: ghc >= 6.12 (base >=4.2) supports unicode through iconv
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
48+import qualified Text.Pandoc.UTF8 as UTF8
5649#ifdef _CITEPROC
5750import Text.CSL
5851import Text.Pandoc.Biblio
@@ -344,7 +337,7 @@ options =
344337 "references" -> return ReferenceObfuscation
345338 "javascript" -> return JavascriptObfuscation
346339 "none" -> return NoObfuscation
347− _ -> hPutStrLn stderr ("Error: Unknown obfuscation method: " ++ arg) >>
340+ _ -> UTF8.hPutStrLn stderr ("Error: Unknown obfuscation method: " ++ arg) >>
348341 exitWith (ExitFailure 6)
349342 return opt { optEmailObfuscation = method })
350343 "none|javascript|references")
@@ -378,7 +371,7 @@ options =
378371 return opt{ optTransforms =
379372 headerShift shift : oldTransforms }
380373 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"
382375 exitWith $ ExitFailure 19)
383376 "LEVEL")
384377 "" -- "Headers base level"
@@ -386,7 +379,7 @@ options =
386379 , Option "" ["template"]
387380 (ReqArg
388381 (\arg opt -> do
389− text <- readFile arg
382+ text <- UTF8.readFile arg
390383 return opt{ optTemplate = text,
391384 optStandalone = True })
392385 "FILENAME")
@@ -400,7 +393,7 @@ options =
400393 let newvars = optVariables opt ++ [(k,v)]
401394 return opt{ optVariables = newvars }
402395 _ -> 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)"
404397 exitWith $ ExitFailure 17)
405398 "FILENAME")
406399 "" -- "Use custom template"
@@ -418,7 +411,7 @@ options =
418411 , Option "H" ["include-in-header"]
419412 (ReqArg
420413 (\arg opt -> do
421− text <- readFile arg
414+ text <- UTF8.readFile arg
422415 -- add new ones to end, so they're included in order specified
423416 let newvars = optVariables opt ++ [("header-includes",text)]
424417 return opt { optVariables = newvars,
@@ -429,7 +422,7 @@ options =
429422 , Option "B" ["include-before-body"]
430423 (ReqArg
431424 (\arg opt -> do
432− text <- readFile arg
425+ text <- UTF8.readFile arg
433426 -- add new ones to end, so they're included in order specified
434427 let newvars = optVariables opt ++ [("include-before",text)]
435428 return opt { optVariables = newvars,
@@ -440,7 +433,7 @@ options =
440433 , Option "A" ["include-after-body"]
441434 (ReqArg
442435 (\arg opt -> do
443− text <- readFile arg
436+ text <- UTF8.readFile arg
444437 -- add new ones to end, so they're included in order specified
445438 let newvars = optVariables opt ++ [("include-after",text)]
446439 return opt { optVariables = newvars,
@@ -451,7 +444,7 @@ options =
451444 , Option "C" ["custom-header"]
452445 (ReqArg
453446 (\arg opt -> do
454− text <- readFile arg
447+ text <- UTF8.readFile arg
455448 let newVars = ("legacy-header", text) : optVariables opt
456449 return opt { optVariables = newVars
457450 , optStandalone = True })
@@ -479,7 +472,7 @@ options =
479472 (\arg _ -> do
480473 templ <- getDefaultTemplate Nothing arg
481474 case templ of
482− Right t -> hPutStr stdout t
475+ Right t -> UTF8.hPutStr stdout t
483476 Left e -> error $ show e
484477 exitWith ExitSuccess)
485478 "FORMAT")
@@ -521,7 +514,7 @@ options =
521514 (NoArg
522515 (\_ -> do
523516 prg <- getProgName
524− hPutStrLn stdout (prg ++ " " ++ pandocVersion ++ compileInfo ++
517+ UTF8.hPutStrLn stdout (prg ++ " " ++ pandocVersion ++ compileInfo ++
525518 copyrightMessage)
526519 exitWith ExitSuccess ))
527520 "" -- "Print version"
@@ -530,7 +523,7 @@ options =
530523 (NoArg
531524 (\_ -> do
532525 prg <- getProgName
533− hPutStr stdout (usageMessage prg options)
526+ UTF8.hPutStr stdout (usageMessage prg options)
534527 exitWith ExitSuccess ))
535528 "" -- "Show help"
536529 ]
@@ -603,8 +596,8 @@ main = do
603596
604597 unless (null errors) $
605598 do name <- getProgName
606− mapM_ (\e -> hPutStr stderr (name ++ ": ") >> hPutStr stderr e) errors
607− hPutStrLn stderr $ "Try " ++ name ++ " --help for more information."
599+ mapM_ (\e -> UTF8.hPutStr stderr (name ++ ": ") >> UTF8.hPutStr stderr e) errors
600+ UTF8.hPutStrLn stderr $ "Try " ++ name ++ " --help for more information."
608601 exitWith $ ExitFailure 2
609602
610603 let defaultOpts' = if compatMode
@@ -653,13 +646,13 @@ main = do
653646 } = opts
654647
655648 when dumpArgs $
656− do hPutStrLn stdout outputFile
657− mapM_ (\arg -> hPutStrLn stdout arg) args
649+ do UTF8.hPutStrLn stdout outputFile
650+ mapM_ (\arg -> UTF8.hPutStrLn stdout arg) args
658651 exitWith ExitSuccess
659652
660653 -- warn about deprecated options
661654 case lookup "legacy-header" variables of
662− Just _ -> hPutStrLn stderr $
655+ Just _ -> UTF8.hPutStrLn stderr $
663656 "Warning: The -C/--custom-header is deprecated.\n" ++
664657 "Please transition to using --template instead."
665658 Nothing -> return ()
@@ -765,7 +758,7 @@ main = do
765758 writerIdentifierPrefix = idPrefix }
766759
767760 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" ++
769762 "Specify an output file using the -o option.")
770763 exitWith $ ExitFailure 5
771764
@@ -775,10 +768,10 @@ main = do
775768
776769 let readSources [] = mapM readSource ["-"]
777770 readSources srcs = mapM readSource srcs
778− readSource "-" = getContents
771+ readSource "-" = UTF8.getContents
779772 readSource src = case parseURI src of
780773 Just u -> readURI u
781− Nothing -> readFile src
774+ Nothing -> UTF8.readFile src
782775 readURI uri = simpleHTTP (mkRequest GET uri) >>= getResponseBody >>=
783776 return . toString -- treat all as UTF8
784777
@@ -800,5 +793,5 @@ main = do
800793 case writerName' of
801794 "odt" -> saveOpenDocumentAsODT datadir outputFile sourceDirRelative referenceODT writerOutput
802795 _ -> if outputFile == "-"
803− then putStr writerOutput
804− else writeFile outputFile writerOutput
796+ then UTF8.putStr writerOutput
797+ else UTF8.writeFile outputFile writerOutput