git.iko.soy/typeable/pandoc
on master help sign in
Add `--eol` flag and writer option to control line endings.
6b8240fStefan Dresselhaus
MANUAL.txt+7
@@ -593,7 +593,14 @@ General writer options
593: Print a system default data file. Files in the user data directory
593: Print a system default data file. Files in the user data directory
594 are ignored.
594 are ignored.
595
595
596+`--eol=crlf`|`lf`
597+
598+: Manually specify line endings: `crlf` (Windows) or `lf`
599+ (MacOS/linux/unix). The default is to use the line endings
600+ appropriate for the OS.
601+
596`--dpi`=*NUMBER*
602`--dpi`=*NUMBER*
603+
597: Specify the dpi (dots per inch) value for conversion from pixels
604: Specify the dpi (dots per inch) value for conversion from pixels
598 to inch/centimeters and vice versa. The default is 96dpi.
605 to inch/centimeters and vice versa. The default is 96dpi.
599 Technically, the correct term would be ppi (pixels per inch).
606 Technically, the correct term would be ppi (pixels per inch).
src/Text/Pandoc/App.hs+21−5
@@ -65,7 +65,7 @@ import System.Directory (Permissions (..), doesFileExist, findExecutable,
65import System.Environment (getArgs, getEnvironment, getProgName)
65import System.Environment (getArgs, getEnvironment, getProgName)
66import System.Exit (ExitCode (..), exitSuccess)
66import System.Exit (ExitCode (..), exitSuccess)
67import System.FilePath
67import System.FilePath
68−import System.IO (stdout)
68+import System.IO (stdout, nativeNewline, Newline(..))
69import System.IO.Error (isDoesNotExistError)
69import System.IO.Error (isDoesNotExistError)
70import Text.Pandoc
70import Text.Pandoc
71import Text.Pandoc.Builder (setMeta)
71import Text.Pandoc.Builder (setMeta)
@@ -411,6 +411,8 @@ convertWithOpts opts = do
411 return $ ("csl", jatsEncoded) : optMetadata opts
411 return $ ("csl", jatsEncoded) : optMetadata opts
412 else return $ optMetadata opts
412 else return $ optMetadata opts
413
413
414+ let eol = fromMaybe nativeNewline $ optEol opts
415+
414 runIO' $ do
416 runIO' $ do
415 (doc, media) <- withMediaBag $ sourceToDoc sources >>=
417 (doc, media) <- withMediaBag $ sourceToDoc sources >>=
416 ( (if isJust (optExtractMedia opts)
418 ( (if isJust (optExtractMedia opts)
@@ -463,7 +465,7 @@ convertWithOpts opts = do
463 else id
465 else id
464 output <- f writerOptions doc
466 output <- f writerOptions doc
465 selfcontain (output ++ ['\n' | not standalone]) >>=
467 selfcontain (output ++ ['\n' | not standalone]) >>=
466− writerFn outputFile . handleEntities
468+ writerFn eol outputFile . handleEntities
467
469
468type Transform = Pandoc -> Pandoc
470type Transform = Pandoc -> Pandoc
469
471
@@ -567,6 +569,7 @@ data Opt = Opt
567 , optIncludeBeforeBody :: [FilePath] -- ^ Files to include before
569 , optIncludeBeforeBody :: [FilePath] -- ^ Files to include before
568 , optIncludeAfterBody :: [FilePath] -- ^ Files to include after body
570 , optIncludeAfterBody :: [FilePath] -- ^ Files to include after body
569 , optIncludeInHeader :: [FilePath] -- ^ Files to include in header
571 , optIncludeInHeader :: [FilePath] -- ^ Files to include in header
572+ , optEol :: Maybe Newline -- ^ Enforce line-endings
570 }
573 }
571
574
572-- | Defaults for command-line options.
575-- | Defaults for command-line options.
@@ -635,6 +638,7 @@ defaultOpts = Opt
635 , optIncludeBeforeBody = []
638 , optIncludeBeforeBody = []
636 , optIncludeAfterBody = []
639 , optIncludeAfterBody = []
637 , optIncludeInHeader = []
640 , optIncludeInHeader = []
641+ , optEol = Nothing
638 }
642 }
639
643
640addMetadata :: (String, String) -> Pandoc -> Pandoc
644addMetadata :: (String, String) -> Pandoc -> Pandoc
@@ -783,9 +787,9 @@ writeFnBinary :: MonadIO m => FilePath -> B.ByteString -> m ()
783writeFnBinary "-" = liftIO . B.putStr
787writeFnBinary "-" = liftIO . B.putStr
784writeFnBinary f = liftIO . B.writeFile (UTF8.encodePath f)
788writeFnBinary f = liftIO . B.writeFile (UTF8.encodePath f)
785
789
786−writerFn :: MonadIO m => FilePath -> String -> m ()
790+writerFn :: MonadIO m => Newline -> FilePath -> String -> m ()
787−writerFn "-" = liftIO . UTF8.putStr
791+writerFn eol "-" = liftIO . UTF8.putStrWith eol
788−writerFn f = liftIO . UTF8.writeFile f
792+writerFn eol f = liftIO . UTF8.writeFileWith eol f
789
793
790lookupHighlightStyle :: Maybe String -> IO (Maybe Style)
794lookupHighlightStyle :: Maybe String -> IO (Maybe Style)
791lookupHighlightStyle Nothing = return Nothing
795lookupHighlightStyle Nothing = return Nothing
@@ -958,6 +962,18 @@ options =
958 "NUMBER")
962 "NUMBER")
959 "" -- "Dpi (default 96)"
963 "" -- "Dpi (default 96)"
960
964
965+ , Option "" ["eol"]
966+ (ReqArg
967+ (\arg opt ->
968+ case toLower <$> arg of
969+ "crlf" -> return opt { optEol = Just CRLF }
970+ "lf" -> return opt { optEol = Just LF }
971+ -- mac-syntax (cr) is not supported in ghc-base.
972+ _ -> E.throwIO $ PandocOptionError
973+ "--eol must be one of crlf (Windows), lf (Unix)")
974+ "crlf|lf")
975+ "" -- "EOL (default OS-dependent)"
976+
961 , Option "" ["wrap"]
977 , Option "" ["wrap"]
962 (ReqArg
978 (ReqArg
963 (\arg opt ->
979 (\arg opt ->
src/Text/Pandoc/UTF8.hs+33−8
@@ -28,11 +28,16 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28UTF-8 aware string IO functions that will work with GHC 6.10, 6.12, or 7.
28UTF-8 aware string IO functions that will work with GHC 6.10, 6.12, or 7.
29-}
29-}
30module Text.Pandoc.UTF8 ( readFile
30module Text.Pandoc.UTF8 ( readFile
31− , writeFile
32 , getContents
31 , getContents
32+ , writeFileWith
33+ , writeFile
34+ , putStrWith
33 , putStr
35 , putStr
36+ , putStrLnWith
34 , putStrLn
37 , putStrLn
38+ , hPutStrWith
35 , hPutStr
39 , hPutStr
40+ , hPutStrLnWith
36 , hPutStrLn
41 , hPutStrLn
37 , hGetContents
42 , hGetContents
38 , toString
43 , toString
@@ -61,23 +66,43 @@ readFile f = do
61 h <- openFile (encodePath f) ReadMode
66 h <- openFile (encodePath f) ReadMode
62 hGetContents h
67 hGetContents h
63
68
64−writeFile :: FilePath -> String -> IO ()
65−writeFile f s = withFile (encodePath f) WriteMode $ \h -> hPutStr h s
66−
67getContents :: IO String
69getContents :: IO String
68getContents = hGetContents stdin
70getContents = hGetContents stdin
69
71
72+writeFileWith :: Newline -> FilePath -> String -> IO ()
73+writeFileWith eol f s =
74+ withFile (encodePath f) WriteMode $ \h -> hPutStrWith eol h s
75+
76+writeFile :: FilePath -> String -> IO ()
77+writeFile = writeFileWith nativeNewline
78+
79+putStrWith :: Newline -> String -> IO ()
80+putStrWith eol s = hPutStrWith eol stdout s
81+
70putStr :: String -> IO ()
82putStr :: String -> IO ()
71−putStr s = hPutStr stdout s
83+putStr = putStrWith nativeNewline
84+
85+putStrLnWith :: Newline -> String -> IO ()
86+putStrLnWith eol s = hPutStrLnWith eol stdout s
72
87
73putStrLn :: String -> IO ()
88putStrLn :: String -> IO ()
74−putStrLn s = hPutStrLn stdout s
89+putStrLn = putStrLnWith nativeNewline
90+
91+hPutStrWith :: Newline -> Handle -> String -> IO ()
92+hPutStrWith eol h s =
93+ hSetNewlineMode h (NewlineMode eol eol) >>
94+ hSetEncoding h utf8 >> IO.hPutStr h s
75
95
76hPutStr :: Handle -> String -> IO ()
96hPutStr :: Handle -> String -> IO ()
97+hPutStr = hPutStrWith nativeNewline
98+
99+hPutStrLnWith :: Newline -> Handle -> String -> IO ()
100+hPutStrLnWith eol h s =
101+ hSetNewlineMode h (NewlineMode eol eol) >>
77−hPutStr h s = hSetEncoding h utf8 >> IO.hPutStr h s
102+ hSetEncoding h utf8 >> IO.hPutStrLn h s
78
103
79hPutStrLn :: Handle -> String -> IO ()
104hPutStrLn :: Handle -> String -> IO ()
80−hPutStrLn h s = hSetEncoding h utf8 >> IO.hPutStrLn h s
105+hPutStrLn = hPutStrLnWith nativeNewline
81
106
82hGetContents :: Handle -> IO String
107hGetContents :: Handle -> IO String
83hGetContents = fmap toString . B.hGetContents
108hGetContents = fmap toString . B.hGetContents