@@ -45,6 +45,7 @@ import Text.Pandoc.Builder (fromList, setMeta)
4545import Text.Pandoc.Builder (Blocks, Inlines, trimInlines)
4646import qualified Text.Pandoc.Builder as B
4747import Text.Pandoc.Class (PandocMonad, readFileFromDirs)
48+import Text.Pandoc.CSV (CSVOptions(..), defaultCSVOptions, parseCSV)
4849import Text.Pandoc.Definition
4950import Text.Pandoc.Error
5051import Text.Pandoc.ImageSize (lengthToDim, scaleDimension)
@@ -56,6 +57,8 @@ import Text.Printf (printf)
5657import Data.Text (Text)
5758import qualified Data.Text as T
5859
60+import Debug.Trace
61+
5962-- TODO:
6063-- [ ] .. parsed-literal
6164-- [ ] .. csv-table
@@ -688,6 +691,7 @@ directive' = do
688691 case label of
689692 "table" -> tableDirective top fields body'
690693 "list-table" -> listTableDirective top fields body'
694+ "csv-table" -> csvTableDirective top fields body'
691695 "line-block" -> lineBlockDirective body'
692696 "raw" -> return $ B.rawBlock (trim top) (stripTrailingNewlines body)
693697 "role" -> addNewRole top $ map (\(k,v) -> (k, trim v)) fields
@@ -820,6 +824,54 @@ listTableDirective top fields body = do
820824 takeCells _ = []
821825 normWidths ws = map (/ max 1 (sum ws)) ws
822826
827+ -- TODO
828+ -- [ ] delim:
829+ -- [ ] quote:
830+ -- [ ] keepspace:
831+ -- [ ] escape:
832+ -- [ ] widths:
833+ -- [ ] header-rows:
834+ -- [ ] header:
835+ -- [ ] url:
836+ -- [ ] file:
837+ -- [ ] encoding:
838+csvTableDirective :: PandocMonad m
839+ => String -> [(String, String)] -> String
840+ -> RSTParser m Blocks
841+csvTableDirective top fields rawcsv = do
842+ let res = parseCSV defaultCSVOptions (T.pack rawcsv)
843+ case res of
844+ Left e -> do
845+ throwError $ PandocParsecError "csv table" e
846+ Right rows -> do
847+ return $ B.rawBlock "rst" $ show rows
848+{-
849+ bs <- parseFromString' parseBlocks body
850+ title <- parseFromString' (trimInlines . mconcat <$> many inline) top
851+ let rows = takeRows $ B.toList bs
852+ headerRowsNum = fromMaybe (0 :: Int) $
853+ lookup "header-rows" fields >>= safeRead
854+ (headerRow,bodyRows,numOfCols) = case rows of
855+ x:xs -> if headerRowsNum > 0
856+ then (x, xs, length x)
857+ else ([], rows, length x)
858+ _ -> ([],[],0)
859+ widths = case trim <$> lookup "widths" fields of
860+ Just "auto" -> replicate numOfCols 0
861+ Just specs -> normWidths $ map (fromMaybe (0 :: Double) . safeRead) $
862+ splitBy (`elem` (" ," :: String)) specs
863+ _ -> replicate numOfCols 0
864+ return $ B.table title
865+ (zip (replicate numOfCols AlignDefault) widths)
866+ headerRow
867+ bodyRows
868+ where takeRows [BulletList rows] = map takeCells rows
869+ takeRows _ = []
870+ takeCells [BulletList cells] = map B.fromList cells
871+ takeCells _ = []
872+ normWidths ws = map (/ max 1 (sum ws)) ws
873+-}
874+
823875-- TODO:
824876-- - Only supports :format: fields with a single format for :raw: roles,
825877-- change Text.Pandoc.Definition.Format to fix