git.iko.soy/typeable/pandoc
on master help sign in
Generalize tableWith, gridTableWith
df23d96Albert Krewinkel
src/Text/Pandoc/Parsing.hs+26−23
@@ -762,21 +762,22 @@ lineBlockLines = try $ do
762
762
763-- | Parse a table using 'headerParser', 'rowParser',
763-- | Parse a table using 'headerParser', 'rowParser',
764-- 'lineParser', and 'footerParser'.
764-- 'lineParser', and 'footerParser'.
765−tableWith :: Stream s m Char
765+tableWith :: (Stream s m Char, HasReaderOptions st,
766+ Functor mf, Applicative mf, Monad mf)
766− => ParserT s ParserState m ([Blocks], [Alignment], [Int])
767+ => ParserT s st m (mf [Blocks], [Alignment], [Int])
767− -> ([Int] -> ParserT s ParserState m [Blocks])
768+ -> ([Int] -> ParserT s st m (mf [Blocks]))
768− -> ParserT s ParserState m sep
769+ -> ParserT s st m sep
769− -> ParserT s ParserState m end
770+ -> ParserT s st m end
770− -> ParserT s ParserState m Blocks
771+ -> ParserT s st m (mf Blocks)
771tableWith headerParser rowParser lineParser footerParser = try $ do
772tableWith headerParser rowParser lineParser footerParser = try $ do
772 (heads, aligns, indices) <- headerParser
773 (heads, aligns, indices) <- headerParser
773− lines' <- rowParser indices `sepEndBy1` lineParser
774+ lines' <- sequence <$> rowParser indices `sepEndBy1` lineParser
774 footerParser
775 footerParser
775 numColumns <- getOption readerColumns
776 numColumns <- getOption readerColumns
776 let widths = if (indices == [])
777 let widths = if (indices == [])
777 then replicate (length aligns) 0.0
778 then replicate (length aligns) 0.0
778 else widthsFromIndices numColumns indices
779 else widthsFromIndices numColumns indices
779− return $ B.table mempty (zip aligns widths) heads lines'
780+ return $ B.table mempty (zip aligns widths) <$> heads <*> lines'
780
781
781-- Calculate relative widths of table columns, based on indices
782-- Calculate relative widths of table columns, based on indices
782widthsFromIndices :: Int -- Number of columns on terminal
783widthsFromIndices :: Int -- Number of columns on terminal
@@ -809,10 +810,11 @@ widthsFromIndices numColumns' indices =
809-- (which may be grid), then the rows,
810-- (which may be grid), then the rows,
810-- which may be grid, separated by blank lines, and
811-- which may be grid, separated by blank lines, and
811-- ending with a footer (dashed line followed by blank line).
812-- ending with a footer (dashed line followed by blank line).
812−gridTableWith :: Stream [Char] m Char
813+gridTableWith :: (Stream [Char] m Char, HasReaderOptions st,
814+ Functor mf, Applicative mf, Monad mf)
813− => ParserT [Char] ParserState m Blocks -- ^ Block list parser
815+ => ParserT [Char] st m (mf Blocks) -- ^ Block list parser
814− -> Bool -- ^ Headerless table
816+ -> Bool -- ^ Headerless table
815− -> ParserT [Char] ParserState m Blocks
817+ -> ParserT [Char] st m (mf Blocks)
816gridTableWith blocks headless =
818gridTableWith blocks headless =
817 tableWith (gridTableHeader headless blocks) (gridTableRow blocks)
819 tableWith (gridTableHeader headless blocks) (gridTableRow blocks)
818 (gridTableSep '-') gridTableFooter
820 (gridTableSep '-') gridTableFooter
@@ -835,14 +837,14 @@ removeFinalBar =
835 reverse . dropWhile (`elem` " \t") . dropWhile (=='|') . reverse
837 reverse . dropWhile (`elem` " \t") . dropWhile (=='|') . reverse
836
838
837-- | Separator between rows of grid table.
839-- | Separator between rows of grid table.
838−gridTableSep :: Stream s m Char => Char -> ParserT s ParserState m Char
840+gridTableSep :: Stream s m Char => Char -> ParserT s st m Char
839gridTableSep ch = try $ gridDashedLines ch >> return '\n'
841gridTableSep ch = try $ gridDashedLines ch >> return '\n'
840
842
841-- | Parse header for a grid table.
843-- | Parse header for a grid table.
842−gridTableHeader :: Stream [Char] m Char
844+gridTableHeader :: (Stream [Char] m Char, Functor mf, Applicative mf, Monad mf)
843 => Bool -- ^ Headerless table
845 => Bool -- ^ Headerless table
844− -> ParserT [Char] ParserState m Blocks
846+ -> ParserT [Char] st m (mf Blocks)
845− -> ParserT [Char] ParserState m ([Blocks], [Alignment], [Int])
847+ -> ParserT [Char] st m (mf [Blocks], [Alignment], [Int])
846gridTableHeader headless blocks = try $ do
848gridTableHeader headless blocks = try $ do
847 optional blanklines
849 optional blanklines
848 dashes <- gridDashedLines '-'
850 dashes <- gridDashedLines '-'
@@ -862,25 +864,26 @@ gridTableHeader headless blocks = try $ do
862 then replicate (length dashes) ""
864 then replicate (length dashes) ""
863 else map (intercalate " ") $ transpose
865 else map (intercalate " ") $ transpose
864 $ map (gridTableSplitLine indices) rawContent
866 $ map (gridTableSplitLine indices) rawContent
865− heads <- mapM (parseFromString blocks) $ map trim rawHeads
867+ heads <- fmap sequence . mapM (parseFromString blocks) $ map trim rawHeads
866 return (heads, aligns, indices)
868 return (heads, aligns, indices)
867
869
868−gridTableRawLine :: Stream s m Char => [Int] -> ParserT s ParserState m [String]
870+gridTableRawLine :: Stream s m Char => [Int] -> ParserT s st m [String]
869gridTableRawLine indices = do
871gridTableRawLine indices = do
870 char '|'
872 char '|'
871 line <- many1Till anyChar newline
873 line <- many1Till anyChar newline
872 return (gridTableSplitLine indices line)
874 return (gridTableSplitLine indices line)
873
875
874-- | Parse row of grid table.
876-- | Parse row of grid table.
875−gridTableRow :: Stream [Char] m Char
877+gridTableRow :: (Stream [Char] m Char, Functor mf, Applicative mf, Monad mf)
876− => ParserT [Char] ParserState m Blocks
878+ => ParserT [Char] st m (mf Blocks)
877 -> [Int]
879 -> [Int]
878− -> ParserT [Char] ParserState m [Blocks]
880+ -> ParserT [Char] st m (mf [Blocks])
879gridTableRow blocks indices = do
881gridTableRow blocks indices = do
880 colLines <- many1 (gridTableRawLine indices)
882 colLines <- many1 (gridTableRawLine indices)
881 let cols = map ((++ "\n") . unlines . removeOneLeadingSpace) $
883 let cols = map ((++ "\n") . unlines . removeOneLeadingSpace) $
882 transpose colLines
884 transpose colLines
883− mapM (liftM compactifyCell . parseFromString blocks) cols
885+ cells <- sequence <$> mapM (parseFromString blocks) cols
886+ return $ fmap (map compactifyCell) cells
884
887
885removeOneLeadingSpace :: [String] -> [String]
888removeOneLeadingSpace :: [String] -> [String]
886removeOneLeadingSpace xs =
889removeOneLeadingSpace xs =
@@ -894,7 +897,7 @@ compactifyCell :: Blocks -> Blocks
894compactifyCell bs = head $ compactify [bs]
897compactifyCell bs = head $ compactify [bs]
895
898
896-- | Parse footer for a grid table.
899-- | Parse footer for a grid table.
897−gridTableFooter :: Stream s m Char => ParserT s ParserState m [Char]
900+gridTableFooter :: Stream s m Char => ParserT s st m [Char]
898gridTableFooter = blanklines
901gridTableFooter = blanklines
899
902
900---
903---
src/Text/Pandoc/Readers/RST.hs+9−3
@@ -32,6 +32,7 @@ Conversion from reStructuredText to 'Pandoc' document.
32-}
32-}
33module Text.Pandoc.Readers.RST ( readRST ) where
33module Text.Pandoc.Readers.RST ( readRST ) where
34import Control.Monad (guard, liftM, mzero, when)
34import Control.Monad (guard, liftM, mzero, when)
35+import Control.Monad.Identity (Identity(..))
35import Control.Monad.Except (throwError)
36import Control.Monad.Except (throwError)
36import Data.Char (isHexDigit, isSpace, toLower, toUpper)
37import Data.Char (isHexDigit, isSpace, toLower, toUpper)
37import Data.List (deleteFirstsBy, findIndex, intercalate, isInfixOf, isSuffixOf,
38import Data.List (deleteFirstsBy, findIndex, intercalate, isInfixOf, isSuffixOf,
@@ -1119,8 +1120,12 @@ simpleTable :: PandocMonad m
1119 => Bool -- ^ Headerless table
1120 => Bool -- ^ Headerless table
1120 -> RSTParser m Blocks
1121 -> RSTParser m Blocks
1121simpleTable headless = do
1122simpleTable headless = do
1123+ let wrapIdFst (a, b, c) = (Identity a, b, c)
1124+ wrapId = fmap Identity
1125+ tbl <- runIdentity <$> tableWith
1122− tbl <- tableWith (simpleTableHeader headless) simpleTableRow
1126+ (wrapIdFst <$> simpleTableHeader headless)
1127+ (wrapId <$> simpleTableRow)
1123− sep simpleTableFooter
1128+ sep simpleTableFooter
1124 -- Simple tables get 0s for relative column widths (i.e., use default)
1129 -- Simple tables get 0s for relative column widths (i.e., use default)
1125 case B.toList tbl of
1130 case B.toList tbl of
1126 [Table c a _w h l] -> return $ B.singleton $
1131 [Table c a _w h l] -> return $ B.singleton $
@@ -1134,7 +1139,8 @@ simpleTable headless = do
1134gridTable :: PandocMonad m
1139gridTable :: PandocMonad m
1135 => Bool -- ^ Headerless table
1140 => Bool -- ^ Headerless table
1136 -> RSTParser m Blocks
1141 -> RSTParser m Blocks
1142+gridTable headerless = runIdentity <$>
1137−gridTable headerless = gridTableWith parseBlocks headerless
1143+ gridTableWith (Identity <$> parseBlocks) headerless
1138
1144
1139table :: PandocMonad m => RSTParser m Blocks
1145table :: PandocMonad m => RSTParser m Blocks
1140table = gridTable False <|> simpleTable False <|>
1146table = gridTable False <|> simpleTable False <|>