git.iko.soy/typeable/pandoc
on master help sign in
csv reader: allow empty cells.
commitfb3df6cf19626f4936d9d984ef25ca54a4f85768 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 31 Jan 2020, 21:43 (7 years ago) parentf9514cc
2 files changed+10−8
src/Text/Pandoc/Readers/CSV.hs+5−7 test/command/csv.md+5−1
src/Text/Pandoc/Readers/CSV.hs+5−7
@@ -16,6 +16,7 @@ Conversion from CSV to a 'Pandoc' table.
1616-}
1717module Text.Pandoc.Readers.CSV ( readCSV ) where
1818import Prelude
19+import Control.Monad (guard)
1920import Data.Text (Text)
2021import qualified Data.Text as T
2122import Text.Pandoc.Definition
@@ -25,23 +26,20 @@ import Text.Parsec.Text (Parser)
2526import Text.Pandoc.Class (PandocMonad)
2627import Text.Pandoc.Shared (crFilter)
2728import Text.Pandoc.Error
28−import Text.Pandoc.Options (ReaderOptions(..))
29+import Text.Pandoc.Options (ReaderOptions)
2930import Control.Monad.Except (throwError)
3031
3132readCSV :: PandocMonad m
3233 => ReaderOptions -- ^ Reader options
3334 -> Text -- ^ Text to parse (assuming @'\n'@ line endings)
3435 -> m Pandoc
35−readCSV opts s = do
36− let columns = readerColumns opts
36+readCSV _opts s = do
3737 case parse pCSV "input" (crFilter s) of
3838 Right (r:rs) -> return $ B.doc $ B.table capt (zip aligns widths) hdrs rows
3939 where capt = mempty
4040 numcols = length r
4141 hdrs = map (B.plain . B.text) r
4242 rows = map (map (B.plain . B.text)) rs
43− maximum' [] = 0
44− maximum' xs = maximum xs
4543 aligns = replicate numcols AlignDefault
4644 widths = replicate numcols 0
4745 Right [] -> return $ B.doc mempty
@@ -85,7 +83,7 @@ pRecord :: Parser [Text]
8583pRecord = do
8684 x <- pField
8785 xs <- many $ pComma >> pField
88− () <$ newline <|> eof
86+ () <$ newline <|> (guard (not (T.null x) || not (null xs)) >> eof)
8987 return (x:xs)
9088
9189pField :: Parser Text
@@ -95,7 +93,7 @@ pComma :: Parser Char
9593pComma = char ','
9694
9795pUnescaped :: Parser Text
98−pUnescaped = T.strip . T.pack <$> many1 (noneOf "\n\r\",")
96+pUnescaped = T.strip . T.pack <$> many (noneOf "\n\r\",")
9997
10098pEscaped :: Parser Text
10199pEscaped = do
test/command/csv.md+5−1
@@ -3,6 +3,7 @@
33Fruit,Price,Quantity
44Apple,25 cents,33
55"""Navel"" Orange","35 cents",22
6+,,45
67^D
78[Table [] [AlignDefault,AlignDefault,AlignDefault] [0.0,0.0,0.0]
89 [[Plain [Str "Fruit"]]
@@ -13,5 +14,8 @@ Apple,25 cents,33
1314 ,[Plain [Str "33"]]]
1415 ,[[Plain [Str "\"Navel\"",Space,Str "Orange"]]
1516 ,[Plain [Str "35",Space,Str "cents"]]
16− ,[Plain [Str "22"]]]]]
17+ ,[Plain [Str "22"]]]
18+ ,[[]
19+ ,[]
20+ ,[Plain [Str "45"]]]]]
1721```