git.iko.soy/typeable/pandoc
on master help sign in
Add `--eol` flag and writer option to control line endings.

* Add `--eol=crlf|lf` CLI option. * Add `optEol` to `WriterOptions` [API change] * In `Text.Pandoc.UTF8`, add new functions parameterized on `Newline`: `writeFileWith`, `putStrWith`, `putStrLnWith`, `hPutStrWith`, `hPutStrLnWith`. [API change] * Document option in MANUAL.txt. Closes #3663. Closes #2097.

commit6b8240fc2f45ced4f16403316cab76df15ceaf7a authorStefan Dresselhaus <sdressel@techfak.uni-bielefeld.de> dateWed 17 May 2017, 15:13 (9 years ago) parent399a362
3 files changed+61−13
MANUAL.txt+7 src/Text/Pandoc/App.hs+21−5 src/Text/Pandoc/UTF8.hs+33−8
MANUAL.txt+7
@@ -593,7 +593,14 @@ General writer options
593593: Print a system default data file. Files in the user data directory
594594 are ignored.
595595
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+
596602`--dpi`=*NUMBER*
603+
597604: Specify the dpi (dots per inch) value for conversion from pixels
598605 to inch/centimeters and vice versa. The default is 96dpi.
599606 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,
6565import System.Environment (getArgs, getEnvironment, getProgName)
6666import System.Exit (ExitCode (..), exitSuccess)
6767import System.FilePath
68−import System.IO (stdout)
68+import System.IO (stdout, nativeNewline, Newline(..))
6969import System.IO.Error (isDoesNotExistError)
7070import Text.Pandoc
7171import Text.Pandoc.Builder (setMeta)
@@ -411,6 +411,8 @@ convertWithOpts opts = do
411411 return $ ("csl", jatsEncoded) : optMetadata opts
412412 else return $ optMetadata opts
413413
414+ let eol = fromMaybe nativeNewline $ optEol opts
415+
414416 runIO' $ do
415417 (doc, media) <- withMediaBag $ sourceToDoc sources >>=
416418 ( (if isJust (optExtractMedia opts)
@@ -463,7 +465,7 @@ convertWithOpts opts = do
463465 else id
464466 output <- f writerOptions doc
465467 selfcontain (output ++ ['\n' | not standalone]) >>=
466− writerFn outputFile . handleEntities
468+ writerFn eol outputFile . handleEntities
467469
468470type Transform = Pandoc -> Pandoc
469471
@@ -567,6 +569,7 @@ data Opt = Opt
567569 , optIncludeBeforeBody :: [FilePath] -- ^ Files to include before
568570 , optIncludeAfterBody :: [FilePath] -- ^ Files to include after body
569571 , optIncludeInHeader :: [FilePath] -- ^ Files to include in header
572+ , optEol :: Maybe Newline -- ^ Enforce line-endings
570573 }
571574
572575-- | Defaults for command-line options.
@@ -635,6 +638,7 @@ defaultOpts = Opt
635638 , optIncludeBeforeBody = []
636639 , optIncludeAfterBody = []
637640 , optIncludeInHeader = []
641+ , optEol = Nothing
638642 }
639643
640644addMetadata :: (String, String) -> Pandoc -> Pandoc
@@ -783,9 +787,9 @@ writeFnBinary :: MonadIO m => FilePath -> B.ByteString -> m ()
783787writeFnBinary "-" = liftIO . B.putStr
784788writeFnBinary f = liftIO . B.writeFile (UTF8.encodePath f)
785789
786−writerFn :: MonadIO m => FilePath -> String -> m ()
787−writerFn "-" = liftIO . UTF8.putStr
788−writerFn f = liftIO . UTF8.writeFile f
790+writerFn :: MonadIO m => Newline -> FilePath -> String -> m ()
791+writerFn eol "-" = liftIO . UTF8.putStrWith eol
792+writerFn eol f = liftIO . UTF8.writeFileWith eol f
789793
790794lookupHighlightStyle :: Maybe String -> IO (Maybe Style)
791795lookupHighlightStyle Nothing = return Nothing
@@ -958,6 +962,18 @@ options =
958962 "NUMBER")
959963 "" -- "Dpi (default 96)"
960964
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+
961977 , Option "" ["wrap"]
962978 (ReqArg
963979 (\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
2828UTF-8 aware string IO functions that will work with GHC 6.10, 6.12, or 7.
2929-}
3030module Text.Pandoc.UTF8 ( readFile
31− , writeFile
3231 , getContents
32+ , writeFileWith
33+ , writeFile
34+ , putStrWith
3335 , putStr
36+ , putStrLnWith
3437 , putStrLn
38+ , hPutStrWith
3539 , hPutStr
40+ , hPutStrLnWith
3641 , hPutStrLn
3742 , hGetContents
3843 , toString
@@ -61,23 +66,43 @@ readFile f = do
6166 h <- openFile (encodePath f) ReadMode
6267 hGetContents h
6368
64−writeFile :: FilePath -> String -> IO ()
65−writeFile f s = withFile (encodePath f) WriteMode $ \h -> hPutStr h s
66−
6769getContents :: IO String
6870getContents = hGetContents stdin
6971
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+
7082putStr :: 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
7287
7388putStrLn :: 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
7595
7696hPutStr :: Handle -> String -> IO ()
77−hPutStr h s = hSetEncoding h utf8 >> IO.hPutStr h s
97+hPutStr = hPutStrWith nativeNewline
98+
99+hPutStrLnWith :: Newline -> Handle -> String -> IO ()
100+hPutStrLnWith eol h s =
101+ hSetNewlineMode h (NewlineMode eol eol) >>
102+ hSetEncoding h utf8 >> IO.hPutStrLn h s
78103
79104hPutStrLn :: Handle -> String -> IO ()
80−hPutStrLn h s = hSetEncoding h utf8 >> IO.hPutStrLn h s
105+hPutStrLn = hPutStrLnWith nativeNewline
81106
82107hGetContents :: Handle -> IO String
83108hGetContents = fmap toString . B.hGetContents