From ce72d6aba83981dee3deb012373bd11e7432142a Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Tue, 24 Jul 2012 07:13:49 -0700 Subject: Slight improvement to performance for pipe tables. Still, pipe tables are a huge performance drag. One benchmark: With pipe tables, 1.25 sec (including this fix). without pipe tables, 1.05 sec. --- src/Text/Pandoc/Readers/Markdown.hs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs index 1786c7f45..8806416a7 100644 --- a/src/Text/Pandoc/Readers/Markdown.hs +++ b/src/Text/Pandoc/Readers/Markdown.hs @@ -913,17 +913,18 @@ pipeTable headless = tableWith (pipeTableHeader headless) -- | Parse header for an pipe table. pipeTableHeader :: Bool -- ^ Headerless table -> Parser [Char] ParserState ([[Block]], [Alignment], [Int]) -pipeTableHeader headless = try $ do - optional blanklines - heads <- if headless - then return $ repeat [] - else pipeTableRow - aligns <- nonindentSpaces >> optional (char '|') >> - pipeTableHeaderPart `sepBy1` sepPipe - optional (char '|') - newline - let cols = length aligns - return (take cols heads, aligns, []) +pipeTableHeader headless = do + scanForPipe + try $ do + heads <- if headless + then return $ repeat [] + else pipeTableRow + aligns <- nonindentSpaces >> optional (char '|') >> + pipeTableHeaderPart `sepBy1` sepPipe + optional (char '|') + newline + let cols = length aligns + return (take cols heads, aligns, []) sepPipe :: Parser [Char] ParserState () sepPipe = try $ char '|' >> notFollowedBy blankline @@ -955,6 +956,10 @@ pipeTableHeaderPart = do (Nothing,Just _) -> AlignRight (Just _,Just _) -> AlignCenter +-- Succeed only if current line contains a pipe. +scanForPipe :: Parser [Char] st () +scanForPipe = lookAhead (manyTill (satisfy (/='\n')) (char '|')) >> return () + table :: Parser [Char] ParserState Block table = multilineTable False <|> simpleTable True <|> simpleTable False <|> multilineTable True <|> -- cgit v1.2.3