Removed Shared.compactify.

Changed signatures on Parsing.tableWith and Parsing.gridTableWith.
This commit is contained in:
John MacFarlane 2017-01-27 21:30:35 +01:00
parent 86b9a51ee3
commit 56f74cb0ab
3 changed files with 25 additions and 37 deletions

View file

@ -740,11 +740,11 @@ lineBlockLines = try $ do
-- | Parse a table using 'headerParser', 'rowParser',
-- 'lineParser', and 'footerParser'.
tableWith :: Stream s m Char
=> ParserT s ParserState m ([[Block]], [Alignment], [Int])
-> ([Int] -> ParserT s ParserState m [[Block]])
=> ParserT s ParserState m ([Blocks], [Alignment], [Int])
-> ([Int] -> ParserT s ParserState m [Blocks])
-> ParserT s ParserState m sep
-> ParserT s ParserState m end
-> ParserT s ParserState m Block
-> ParserT s ParserState m Blocks
tableWith headerParser rowParser lineParser footerParser = try $ do
(heads, aligns, indices) <- headerParser
lines' <- rowParser indices `sepEndBy1` lineParser
@ -753,7 +753,7 @@ tableWith headerParser rowParser lineParser footerParser = try $ do
let widths = if (indices == [])
then replicate (length aligns) 0.0
else widthsFromIndices numColumns indices
return $ Table [] aligns widths heads lines'
return $ B.table mempty (zip aligns widths) heads lines'
-- Calculate relative widths of table columns, based on indices
widthsFromIndices :: Int -- Number of columns on terminal
@ -787,9 +787,9 @@ widthsFromIndices numColumns' indices =
-- which may be grid, separated by blank lines, and
-- ending with a footer (dashed line followed by blank line).
gridTableWith :: Stream [Char] m Char
=> ParserT [Char] ParserState m [Block] -- ^ Block list parser
=> ParserT [Char] ParserState m Blocks -- ^ Block list parser
-> Bool -- ^ Headerless table
-> ParserT [Char] ParserState m Block
-> ParserT [Char] ParserState m Blocks
gridTableWith blocks headless =
tableWith (gridTableHeader headless blocks) (gridTableRow blocks)
(gridTableSep '-') gridTableFooter
@ -818,8 +818,8 @@ gridTableSep ch = try $ gridDashedLines ch >> return '\n'
-- | Parse header for a grid table.
gridTableHeader :: Stream [Char] m Char
=> Bool -- ^ Headerless table
-> ParserT [Char] ParserState m [Block]
-> ParserT [Char] ParserState m ([[Block]], [Alignment], [Int])
-> ParserT [Char] ParserState m Blocks
-> ParserT [Char] ParserState m ([Blocks], [Alignment], [Int])
gridTableHeader headless blocks = try $ do
optional blanklines
dashes <- gridDashedLines '-'
@ -850,9 +850,9 @@ gridTableRawLine indices = do
-- | Parse row of grid table.
gridTableRow :: Stream [Char] m Char
=> ParserT [Char] ParserState m [Block]
=> ParserT [Char] ParserState m Blocks
-> [Int]
-> ParserT [Char] ParserState m [[Block]]
-> ParserT [Char] ParserState m [Blocks]
gridTableRow blocks indices = do
colLines <- many1 (gridTableRawLine indices)
let cols = map ((++ "\n") . unlines . removeOneLeadingSpace) $
@ -867,8 +867,8 @@ removeOneLeadingSpace xs =
where startsWithSpace "" = True
startsWithSpace (y:_) = y == ' '
compactifyCell :: [Block] -> [Block]
compactifyCell bs = head $ compactify [bs]
compactifyCell :: Blocks -> Blocks
compactifyCell bs = head $ compactify' [bs]
-- | Parse footer for a grid table.
gridTableFooter :: Stream s m Char => ParserT s ParserState m [Char]

View file

@ -973,13 +973,13 @@ simpleTableRawLine indices = do
return (simpleTableSplitLine indices line)
-- Parse a table row and return a list of blocks (columns).
simpleTableRow :: PandocMonad m => [Int] -> RSTParser m [[Block]]
simpleTableRow :: PandocMonad m => [Int] -> RSTParser m [Blocks]
simpleTableRow indices = do
notFollowedBy' simpleTableFooter
firstLine <- simpleTableRawLine indices
colLines <- return [] -- TODO
let cols = map unlines . transpose $ firstLine : colLines
mapM (parseFromString (B.toList . mconcat <$> many plain)) cols
mapM (parseFromString (mconcat <$> many plain)) cols
simpleTableSplitLine :: [Int] -> String -> [String]
simpleTableSplitLine indices line =
@ -988,7 +988,7 @@ simpleTableSplitLine indices line =
simpleTableHeader :: PandocMonad m
=> Bool -- ^ Headerless table
-> RSTParser m ([[Block]], [Alignment], [Int])
-> RSTParser m ([Blocks], [Alignment], [Int])
simpleTableHeader headless = try $ do
optional blanklines
rawContent <- if headless
@ -1002,7 +1002,7 @@ simpleTableHeader headless = try $ do
let rawHeads = if headless
then replicate (length dashes) ""
else simpleTableSplitLine indices rawContent
heads <- mapM (parseFromString (B.toList . mconcat <$> many plain)) $
heads <- mapM (parseFromString (mconcat <$> many plain)) $
map trim rawHeads
return (heads, aligns, indices)
@ -1011,17 +1011,22 @@ simpleTable :: PandocMonad m
=> Bool -- ^ Headerless table
-> RSTParser m Blocks
simpleTable headless = do
Table c a _w h l <- tableWith (simpleTableHeader headless) simpleTableRow sep simpleTableFooter
tbl <- tableWith (simpleTableHeader headless) simpleTableRow
sep simpleTableFooter
-- Simple tables get 0s for relative column widths (i.e., use default)
return $ B.singleton $ Table c a (replicate (length a) 0) h l
case B.toList tbl of
[Table c a _w h l] -> return $ B.singleton $
Table c a (replicate (length a) 0) h l
_ -> do
warning "tableWith returned something unexpected"
return tbl -- TODO error?
where
sep = return () -- optional (simpleTableSep '-')
gridTable :: PandocMonad m
=> Bool -- ^ Headerless table
-> RSTParser m Blocks
gridTable headerless = B.singleton
<$> gridTableWith (B.toList <$> parseBlocks) headerless
gridTable headerless = gridTableWith parseBlocks headerless
table :: PandocMonad m => RSTParser m Blocks
table = gridTable False <|> simpleTable False <|>

View file

@ -59,7 +59,6 @@ module Text.Pandoc.Shared (
deNote,
stringify,
capitalize,
compactify,
compactify',
compactify'DL,
linesToPara,
@ -432,22 +431,6 @@ capitalize = walk go
go (Str s) = Str (T.unpack $ T.toUpper $ T.pack s)
go x = x
-- | Change final list item from @Para@ to @Plain@ if the list contains
-- no other @Para@ blocks.
compactify :: [[Block]] -- ^ List of list items (each a list of blocks)
-> [[Block]]
compactify [] = []
compactify items =
case (init items, last items) of
(_,[]) -> items
(others, final) ->
case last final of
Para a -> case (filter isPara $ concat items) of
-- if this is only Para, change to Plain
[_] -> others ++ [init final ++ [Plain a]]
_ -> items
_ -> items
-- | Change final list item from @Para@ to @Plain@ if the list contains
-- no other @Para@ blocks. Like compactify, but operates on @Blocks@ rather
-- than @[Block]@.