@@ -57,6 +57,7 @@ import qualified Text.Pandoc.Builder as B
5757import Text.Pandoc.Options
5858import Text.Pandoc.Parsing
5959import Text.Pandoc.Readers.HTML ( htmlTag, isBlockTag )
60+import Text.Pandoc.Shared (trim)
6061import Text.Pandoc.Readers.LaTeX ( rawLaTeXInline, rawLaTeXBlock )
6162import Text.HTML.TagSoup (parseTags, innerText, fromAttrib, Tag(..))
6263import Text.HTML.TagSoup.Match
@@ -325,33 +326,30 @@ para = B.para . trimInlines . mconcat <$> many1 inline
325326-- Tables
326327
327328-- | A table cell spans until a pipe |
328−tableCell :: Parser [Char] ParserState Blocks
329−tableCell = do
330− c <- many1 (noneOf "|\n")
331− content <- trimInlines . mconcat <$> parseFromString (many1 inline) c
329+tableCell :: Bool -> Parser [Char] ParserState Blocks
330+tableCell headerCell = try $ do
331+ char '|'
332+ when headerCell $ () <$ string "_."
333+ notFollowedBy blankline
334+ raw <- trim <$>
335+ many (noneOf "|\n" <|> try (char '\n' <* notFollowedBy blankline))
336+ content <- mconcat <$> parseFromString (many inline) raw
332337 return $ B.plain content
333338
334339-- | A table row is made of many table cells
335340tableRow :: Parser [Char] ParserState [Blocks]
336−tableRow = try $ ( char '|' *>
337− (endBy1 tableCell (optional blankline *> char '|')) <* newline)
338−
339−-- | Many table rows
340−tableRows :: Parser [Char] ParserState [[Blocks]]
341−tableRows = many1 tableRow
341+tableRow = many1 (tableCell False) <* char '|' <* newline
342342
343−-- | Table headers are made of cells separated by a tag "|_."
344−tableHeaders :: Parser [Char] ParserState [Blocks]
345−tableHeaders = let separator = (try $ string "|_.") in
346− try $ ( separator *> (sepBy1 tableCell separator) <* char '|' <* newline )
343+tableHeader :: Parser [Char] ParserState [Blocks]
344+tableHeader = many1 (tableCell True) <* char '|' <* newline
347345
348346-- | A table with an optional header. Current implementation can
349347-- handle tables with and without header, but will parse cells
350348-- alignment attributes as content.
351349table :: Parser [Char] ParserState Blocks
352350table = try $ do
353− headers <- option mempty tableHeaders
354− rows <- tableRows
351+ headers <- option mempty $ tableHeader
352+ rows <- many1 tableRow
355353 blanklines
356354 let nbOfCols = max (length headers) (length $ head rows)
357355 return $ B.table mempty