git.iko.soy/typeable/pandoc
on master help sign in
RST reader: Improved grid tables.

+ Table cells can now contain multiple block elements, such as lists or paragraphs. + Table parser is now forgiving of spaces at ends of lines. + Added test cases. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1852 788f1e2b-df1e-0410-8736-df70ead52e1b

commit76ab88807e2a6e2161343f8b0a3d01a8a22e9d99 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateSat 27 Feb 2010, 20:39 (17 years ago) parentddcde4d
3 files changed+73−21
src/Text/Pandoc/Readers/RST.hs+34−20 tests/rst-reader.native+22−1 tests/rst-reader.rst+17
src/Text/Pandoc/Readers/RST.hs+34−20
@@ -33,7 +33,7 @@ module Text.Pandoc.Readers.RST (
3333import Text.Pandoc.Definition
3434import Text.Pandoc.Shared
3535import Text.ParserCombinators.Parsec
36−import Control.Monad ( when, unless )
36+import Control.Monad ( when, unless, liftM )
3737import Data.List ( findIndex, delete, intercalate, transpose )
3838
3939-- | Parse reStructuredText string and return Pandoc document.
@@ -598,29 +598,30 @@ regularKey = try $ do
598598-- Grid tables TODO:
599599-- - column spans
600600
601−dashedLine :: Char -> Char
602− -> GenParser Char st (Int, Int)
603−dashedLine ch sch = do
601+dashedLine :: Char -> GenParser Char st (Int, Int)
602+dashedLine ch = do
604603 dashes <- many1 (char ch)
605− sp <- many (char sch)
604+ sp <- many (char ' ')
606605 return (length dashes, length $ dashes ++ sp)
607606
608607simpleDashedLines :: Char -> GenParser Char st [(Int,Int)]
609−simpleDashedLines ch = try $ many1 (dashedLine ch sch)
610− where
611− sch = ' '
608+simpleDashedLines ch = try $ many1 (dashedLine ch)
609+
610+gridPart :: Char -> GenParser Char st (Int, Int)
611+gridPart ch = do
612+ dashes <- many1 (char ch)
613+ char '+'
614+ return (length dashes, length dashes + 1)
612615
613616gridDashedLines :: Char -> GenParser Char st [(Int,Int)]
614−gridDashedLines ch = try $ char sch >> many1 (dashedLine ch sch)
615− where
616− sch = '+'
617+gridDashedLines ch = try $ char '+' >> many1 (gridPart ch) >>~ blankline
617618
618619-- Parse a table row separator
619620simpleTableSep :: Char -> GenParser Char ParserState Char
620621simpleTableSep ch = try $ simpleDashedLines ch >> newline
621622
622623gridTableSep :: Char -> GenParser Char ParserState Char
623−gridTableSep ch = try $ gridDashedLines ch >> newline
624+gridTableSep ch = try $ gridDashedLines ch >> return '\n'
624625
625626-- Parse a table footer
626627simpleTableFooter :: GenParser Char ParserState [Char]
@@ -639,7 +640,7 @@ gridTableRawLine :: [Int] -> GenParser Char ParserState [String]
639640gridTableRawLine indices = do
640641 char '|'
641642 line <- many1Till anyChar newline
642− return (gridTableSplitLine indices line)
643+ return (gridTableSplitLine indices $ removeTrailingSpace line)
643644
644645-- Parse a table row and return a list of blocks (columns).
645646simpleTableRow :: [Int] -> GenParser Char ParserState [[Block]]
@@ -654,8 +655,12 @@ gridTableRow :: [Int]
654655 -> GenParser Char ParserState [[Block]]
655656gridTableRow indices = do
656657 colLines <- many1 (gridTableRawLine indices)
657− let cols = map unlines $ transpose colLines
658− mapM (parseFromString (many plain)) cols
658+ let cols = map ((++ "\n") . unlines . removeOneLeadingSpace) $
659+ transpose colLines
660+ mapM (liftM compactifyCell . parseFromString (many block)) cols
661+
662+compactifyCell :: [Block] -> [Block]
663+compactifyCell bs = head $ compactify [bs]
659664
660665simpleTableSplitLine :: [Int] -> String -> [String]
661666simpleTableSplitLine indices line =
@@ -664,9 +669,19 @@ simpleTableSplitLine indices line =
664669
665670gridTableSplitLine :: [Int] -> String -> [String]
666671gridTableSplitLine indices line =
667− map removeLeadingTrailingSpace
668− $ map (takeWhile (/= '|')) -- strip trailing '|' off each column
669− $ tail $ splitByIndices (init indices) line
672+ map removeFinalBar $ tail $ splitByIndices (init indices) line
673+
674+removeFinalBar :: String -> String
675+removeFinalBar = reverse . dropWhile (=='|') . dropWhile (`elem` " \t") .
676+ reverse
677+
678+removeOneLeadingSpace :: [String] -> [String]
679+removeOneLeadingSpace xs =
680+ if all startsWithSpace xs
681+ then map (drop 1) xs
682+ else xs
683+ where startsWithSpace "" = True
684+ startsWithSpace (y:_) = y == ' '
670685
671686-- Calculate relative widths of table columns, based on indices
672687widthsFromIndices :: Int -- Number of columns on terminal
@@ -710,11 +725,10 @@ simpleTableHeader headless = try $ do
710725 return (rawHeads, aligns, indices)
711726
712727gridTableHeader :: Bool -- ^ Headerless table
713− -> GenParser Char ParserState ([String], [Alignment], [Int])
728+ -> GenParser Char ParserState ([String], [Alignment], [Int])
714729gridTableHeader headless = try $ do
715730 optional blanklines
716731 dashes <- gridDashedLines '-'
717− newline
718732 rawContent <- if headless
719733 then return $ repeat ""
720734 else many1
tests/rst-reader.native+22−1
@@ -287,5 +287,26 @@ Pandoc (Meta [Str "Pandoc",Space,Str "Test",Space,Str "Suite",Str ":",Space,Str
287287 , [ Plain [Str "c",Space,Str "c",Space,Str "2"] ] ],
288288 [ [ Plain [Str "r2",Space,Str "d"] ]
289289 , [ Plain [Str "e"] ]
290− , [ Plain [Str "f"] ] ] ] ]
290+ , [ Plain [Str "f"] ] ] ]
291+, Para [Str "Spaces",Space,Str "at",Space,Str "ends",Space,Str "of",Space,Str "lines"]
292+, Table [] [AlignDefault,AlignDefault,AlignDefault] [0.2375,0.15,0.1625]
293+ [ []
294+ , []
295+ , [] ] [
296+ [ [ Plain [Str "r1",Space,Str "a",Space,Str "r1",Space,Str "bis"] ]
297+ , [ Plain [Str "b",Space,Str "b",Space,Str "2"] ]
298+ , [ Plain [Str "c",Space,Str "c",Space,Str "2"] ] ],
299+ [ [ Plain [Str "r2",Space,Str "d"] ]
300+ , [ Plain [Str "e"] ]
301+ , [ Plain [Str "f"] ] ] ]
302+, Para [Str "Multiple",Space,Str "blocks",Space,Str "in",Space,Str "a",Space,Str "cell"]
303+, Table [] [AlignDefault,AlignDefault,AlignDefault] [0.2375,0.15,0.1625]
304+ [ []
305+ , []
306+ , [] ] [
307+ [ [ Para [Str "r1",Space,Str "a"]
308+ , Para [Str "r1",Space,Str "bis"] ], [ BulletList
309+ [ [ Plain [Str "b"] ]
310+ , [ Plain [Str "b",Space,Str "2"] ]
311+ , [ Plain [Str "b",Space,Str "2"] ] ] ], [ Plain [Str "c",Space,Str "c",Space,Str "2",Space,Str "c",Space,Str "2"] ] ] ] ]
291312
tests/rst-reader.rst+17
@@ -491,3 +491,20 @@ Headless
491491+------------------+-----------+------------+
492492| r2 d | e | f |
493493+------------------+-----------+------------+
494+
495+Spaces at ends of lines
496+
497++------------------+-----------+------------+
498+| r1 a | b | c |
499+| r1 bis | b 2 | c 2 |
500++------------------+-----------+------------+
501+| r2 d | e | f |
502++------------------+-----------+------------+
503+
504+Multiple blocks in a cell
505+
506++------------------+-----------+------------+
507+| r1 a | - b | c |
508+| | - b 2 | c 2 |
509+| r1 bis | - b 2 | c 2 |
510++------------------+-----------+------------+