From 39152c00345154231a4a1fa162e8414c121f35af Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Tue, 20 May 2025 16:45:07 -0400 Subject: [PATCH] Factor out common code for UList and OList parsing into a separate function. Refactored UList function; wrote OList function. --- src/MdToHTML.hs | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/MdToHTML.hs b/src/MdToHTML.hs index 77ea2b1..e168c6d 100644 --- a/src/MdToHTML.hs +++ b/src/MdToHTML.hs @@ -287,13 +287,8 @@ parseBlockquote = do return (Blockquote parsedQuotedLines) -- Parse a nested list item. -parseUListNested :: ReadP MdToken -parseUListNested = do - -- firstChar <- string " " <++ string "\t" - -- skipSpaces - -- restOfLine <- manyTill get (void (char '\n') <++ eof) - -- let restOfLineParsed = fst $ leftmostLongestParse parseLine restOfLine - -- return restOfLineParsed +parseListNested :: ReadP MdToken +parseListNested = do let firstCharParser = string " " <++ string "\t" let restOfLineParser = manyTill get (void (char '\n') <++ eof) lines <- greedyParse1 (firstCharParser *> restOfLineParser) @@ -308,7 +303,7 @@ parseUListLineItem = do skipSpaces restOfLine <- many1 parseListLineToken void (char '\n') <++ eof - nestedList <- parseUListNested <++ return (Unit "") + nestedList <- parseListNested <++ return (Unit "") return $ Line [Line restOfLine, nestedList] -- restOfLine <- manyTill get (void (char '\n') <++ eof) @@ -322,11 +317,27 @@ parseUListLineItem = do parseUListParaItem :: ReadP MdToken parseUListParaItem = do firstLine <- parseUListLineItem + res <- parseListParaItemCommon + return $ Document (Para firstLine : res) -- I only wrap this in a document because I want some way of converting [MdToken] to MdToken, without any overhead. There is no other reason to wrap it in a Document. + +-- Parse an unordered list paragraph item. +parseOListParaItem :: ReadP MdToken +parseOListParaItem = do + firstLine <- parseOListLineItem + res <- parseListParaItemCommon + return $ Document (Para firstLine : res) -- I only wrap this in a document because I want some way of converting [MdToken] to MdToken, without any overhead. There is no other reason to wrap it in a Document. + +-- Common code for parsing list paragraph items. +-- A list paragraph item is defined as a line item, followed by an empty line, followed by one or more +-- lines indented by a space or tab. +-- A list paragraph item can also be a blockquote. +parseListParaItemCommon :: ReadP [MdToken] +parseListParaItemCommon = do char '\n' lines <- greedyParse1 ((string " " <|> string "\t") *> parseTillEol) let res = fst $ leftmostLongestParse (greedyParse1 parsePara) (init $ unlines lines) char '\n' - return $ Document (Para firstLine : res) -- I only wrap this in a document because I want some way of converting [MdToken] to MdToken, without any overhead. There is no other reason to wrap it in a Document. + return res -- I only wrap this in a document because I want some way of converting [MdToken] to MdToken, without any overhead. There is no other reason to wrap it in a Document. -- Parse an unordered list item, which can be a line item or another list. parseUListItem :: ReadP MdToken