for current code: {-# OPTIONSGHC -fno-warn-unused-do-bind #-} {-# LANGUAGE LambdaCase…
- User
for current code:
{-# OPTIONS_GHC -fno-warn-unused-do-bind #-} {-# LANGUAGE LambdaCase #-} {-# OPTIONS_GHC -Wno-unused-matches #-} {-# OPTIONS_GHC -Wno-unrecognised-pragmas #-} {-# HLINT ignore "Use newtype instead of data" #-}
module Assignment (markdownParser, convertADTHTML, getTime) where import Data.Time.Clock (getCurrentTime) import Data.Time.Format (defaultTimeLocale, formatTime) import Instances (ParseResult(..), Parser(..), parse)
import Parser hiding (unexpectedCharParser)
import Control.Monad (guard) import Control.Applicative (Alternative(..), optional,many) import Data.Char (isSpace) import Data.Maybe (fromMaybe) import Data.Functor (void, ($>))
-- Define the ADT data ADT = Document [Block] deriving (Show, Eq)
try :: Parser a -> Parser a try (Parser p) = Parser $ \input -> case p input of Error e -> Error e Result newInput a -> Result newInput a
choice :: [Parser a] -> Parser a choice = foldr (<|>) empty
endOfLineOrEOF :: Parser () endOfLineOrEOF = void eol <|> eof
lookAhead :: Parser a -> Parser a lookAhead (Parser p) = Parser $ \input -> case p input of Error e -> Error e Result _ x -> Result input x
many1 :: Alternative f => f a -> f [a] many1 p = (:) <$> p <*> many p
sepBy :: Parser a -> Parser sep -> Parser [a] sepBy p sep = sepBy1 p sep <|> pure []
data Block = Paragraph [Inline] | Heading Int [Inline] | BlockQuote [Block] | CodeBlock (Maybe String) String | OrderedList [ListItem] | FootnoteReference Int String | Table TableRow [TableRow] deriving (Show, Eq)
data ListItem = ListItem [Block] deriving (Show, Eq)
data TableRow = TableRow [[Inline]] deriving (Show, Eq)
data Inline = Plain String | Italic [Inline] | Bold [Inline] | Strikethrough [Inline] | Link [Inline] String | Image String String (Maybe String) | InlineCode String | Footnote Int deriving (Show, Eq)
-- Utility functions space1 :: Parser String space1 = some (satisfy (\c -> isSpace c && c /= '\n' && c /= '\r'))
-- | Parses an end-of-line sequence. -- Handles Unix (\n), Windows (\r\n), and old Mac (\r) line endings. eol :: Parser () eol = void (string "\r\n") <|> void (is '\n')
<|> void (is '\r')anyChar :: Parser Char anyChar = satisfy (const True)
anyCharButNewline :: Parser Char anyCharButNewline = satisfy (\c -> c /= '\n' && c /= '\r')
skipMany :: Parser a -> Parser () skipMany p = void (many p)
manyTill :: Parser a -> Parser end -> Parser [a] manyTill p end = scan where scan = (end Data.Functor.> scan)
sepBy1 :: Parser a -> Parser sep -> Parser [a] sepBy1 p sep = (:) <$> p <*> many (sep *> p)
-- Part A: Parsing Markdown
markdownParser :: Parser ADT markdownParser = do blocks <- sepEndBy1 blockParser (some eol) eof return $ Document blocks
blockParser :: Parser Block blockParser = choice [ try headingParser , try codeBlockParser , try blockQuoteParser , try orderedListParser , try footnoteReferenceParser , try tableParser , try imageBlockParser , paragraphParser ]
-- Adjusted headingParser to handle setext-style headings headingParser :: Parser Block headingParser = try atxHeadingParser <|> try setextHeadingParser
headingStartParser :: Parser () headingStartParser = lookAhead $ do optional inlineSpace (do hashes <- some (is '#') guard (length hashes <= 6) space1 return ()) <|> (do content <- inlineContentLine eol skipMany (satisfy isSpace) c <- satisfy (\c -> c == '=' || c == '-') cs <- many (satisfy (== c)) skipMany (satisfy isSpace) endOfLineOrEOF guard (length (c:cs) >= 2) return ())
atxHeadingParser :: Parser Block atxHeadingParser = do optional inlineSpace hashes <- some (is '#') -- Capture the number of
#guard (length hashes <= 6) -- Ensure valid heading level space1 -- Require at least one space content <- manyTill inlineElement (lookAhead eol <|> try eof) -- Parse heading content up to end of line or EOF return $ Heading (length hashes) contentsetextHeadingParser :: Parser Block setextHeadingParser = do content <- some inlineElement -- Ensure non-empty content eol -- Expect a newline skipMany (satisfy isSpace) -- Skip spaces c <- satisfy (\c -> c == '=' || c == '-') -- Check for '=' or '-' rest <- many (satisfy (\x -> x == c || isSpace x)) -- Capture the rest of the line guard (all (\x -> x == c || isSpace x) (c:rest)) -- Ensure separator line has only c and spaces endOfLineOrEOF -- Ensure the block ends correctly let level = if c == '=' then 1 else 2 -- Determine heading level return $ Heading level content
blockQuoteParser :: Parser Block blockQuoteParser = do blocks <- some blockQuoteBlock return $ BlockQuote blocks
blockQuoteBlock :: Parser Block blockQuoteBlock = do optional (satisfy isSpace) is '>' optional (is ' ') content <- manyTill anyChar (lookAhead eol <|> eof) _ <- optional eol parsedContent <- parseInlineContent content return $ Paragraph parsedContent
codeBlockParser :: Parser Block codeBlockParser = do optional inlineSpace string "
" lang <- optional (some (satisfy (/= '\n'))) eol code <- manyTill anyChar (try $ optional inlineSpace *> string "" *> endOfLineOrEOF) return $ CodeBlock lang codeorderedListParser :: Parser Block orderedListParser = do items <- some orderedListItemParser return $ OrderedList items
orderedListItemParser :: Parser ListItem orderedListItemParser = do optional inlineSpace numStr <- some digit is '.' space1 content <- manyTill anyCharButNewline (lookAhead eol <|> eof) _ <- optional eol subItems <- optional (try orderedListParser) parsedContent <- parseBlocks content let subBlocks = fromMaybe [] (subItems >>= \case (OrderedList items) -> Just (concatMap ((ListItem bs) -> bs) items); _ -> Nothing) return $ ListItem (parsedContent ++ subBlocks)
footnoteReferenceParser :: Parser Block footnoteReferenceParser = do optional inlineSpace is '[' is '^' numStr <- some digit is ']' is ':' skipMany (satisfy isSpace) -- Ignore spaces after colon content <- manyTill anyChar (lookAhead eol <|> eof) -- Capture the content let num = read numStr return $ FootnoteReference num (trim content)
trim :: String -> String trim = f . f where f = reverse . dropWhile isSpace
paragraphParser :: Parser Block paragraphParser = do notFollowedBy footnoteRefStartParser notFollowedBy tableStartParser -- Prevent parsing tables as paragraphs notFollowedBy headingStartParser -- Prevent headings being parsed as paragraphs Paragraph <$> inlineContentLine
sepByAtLeast2 :: Parser a -> Parser sep -> Parser [a] sepByAtLeast2 p sep = do a <- p sep b <- p rest <- many (sep *> p) return (a : b : rest)
tableStartParser :: Parser () tableStartParser = lookAhead $ do skipMany (satisfy isSpace) optional (is '|') cells <- sepByAtLeast2 (many (satisfy (\c -> c /= '|' && c /= '\n'))) (is '|') optional (is '|') eol skipMany (satisfy isSpace) optional (is '|') optional inlineSpace c <- satisfy (\c -> c == '-' || c == ':') cs <- many (satisfy (\c -> c == '-' || c == ':' || isSpace c)) optional (is '|') return ()
tableParser :: Parser Block tableParser = try Table header rows
separator :: Parser () separator = do skipMany (satisfy isSpace) optional (is '|') sepBy1 separatorCell (optional inlineSpace *> is '|' *> optional inlineSpace) optional (is '|') eol return ()
separatorCell :: Parser () separatorCell = do optional inlineSpace many1 (is '-') optional inlineSpace return ()
sepEndBy1 :: Parser a -> Parser sep -> Parser [a] sepEndBy1 p sep = do first <- p rest <- many (sep *> p) optional sep return (first : rest)
tableRowParser :: Parser TableRow tableRowParser = do skipMany (satisfy isSpace) optional (is '|') -- Use sepEndBy1 to handle trailing '|' cells <- sepEndBy1 cellParser (optional inlineSpace *> is '|' *> optional inlineSpace) optional (is '|') -- Optional trailing '|', do not treat as another cell eol return $ TableRow cells
cellParser :: Parser [Inline] cellParser = do optional inlineSpace content <- many (satisfy (\c -> c /= '|' && c /= '\n')) optional inlineSpace let trimmedContent = trim content guard (not (null trimmedContent)) case parse (inlineContent <* eof) trimmedContent of Result _ inlines -> return inlines _ -> return [Plain trimmedContent]
-- Inline parsing inlineContent :: Parser [Inline] inlineContent = many inlineElement
inlineContentLine :: Parser [Inline] inlineContentLine = some inlineElement <* lookAhead (eol <|> eof) -- Changed from 'manyTill' to 'some'
inlineElement :: Parser Inline inlineElement = choice [ try boldParser , try italicParser , try strikethroughParser , try imageParser , try linkParser , try inlineCodeParser , footnoteParser -- Moved footnoteParser before plainParser , plainParser ]
plainParser :: Parser Inline plainParser = do c <- satisfy (
notElem"_*~[!]^\n\r") rest <- many (satisfy (notElem"_*~[!]^\n\r")) return $ Plain (c:rest)italicParser :: Parser Inline italicParser = do is '' content <- manyTill inlineElement (is '') return $ Italic content
boldParser :: Parser Inline boldParser = do string "" content <- manyTill inlineElement (string "") return $ Bold content
strikethroughParser :: Parser Inline strikethroughParser = do string "
" content <- inlineContentTill (string "") return $ Strikethrough contentinlineCodeParser :: Parser Inline inlineCodeParser = do is '
' content <- many (satisfy (/= '')) is '`' return $ InlineCode contentfootnoteParser :: Parser Inline footnoteParser = do is '[' is '^' numStr <- some digit is ']' let num = read numStr return $ Footnote num
linkParser :: Parser Inline linkParser = do is '[' linkText <- inlineContentTill (is ']') is ']' is '(' url <- some (satisfy (\c -> not (isSpace c) && c /= ')')) _ <- optional (is ' ' *> is '"' > many (satisfy (/= '"')) < is '"') is ')' return $ Link linkText url
spaceNotNewline1 :: Parser () spaceNotNewline1 = do satisfy (\c -> isSpace c && c /= '\n' && c /= '\r') skipMany (satisfy (\c -> isSpace c && c /= '\n' && c /= '\r')) return ()
imageParser :: Parser Inline imageParser = do is '!' is '[' altText <- many (satisfy (/= ']')) is ']' is '(' url <- some (satisfy (\c -> not (isSpace c) && c /= ')')) spaceNotNewline1 is '"' caption <- many (satisfy (/= '"')) is '"' is ')' return $ Image altText url (Just caption)
inlineContentTill :: Parser a -> Parser [Inline] inlineContentTill end = manyTill inlineElement (try end)
parseInlineContent :: String -> Parser [Inline] parseInlineContent s = case parse (inlineContent <* eof) s of Result _ inlines -> return inlines _ -> empty
parseBlocks :: String -> Parser [Block] parseBlocks s = case parse markdownParser s of Result _ (Document blocks) -> return blocks _ -> return []
-- Convert Inline to Text inlineToText :: Inline -> String inlineToText (Plain text) = text inlineToText (Italic inlines) = concatMap inlineToText inlines inlineToText (Bold inlines) = concatMap inlineToText inlines inlineToText (Strikethrough inlines) = concatMap inlineToText inlines inlineToText (Link inlines _) = concatMap inlineToText inlines inlineToText (InlineCode text) = text inlineToText (Footnote n) = show n inlineToText _ = ""
-- Part B: Conversion between Markdown and HTML getTime :: IO String getTime = formatTime defaultTimeLocale "%Y-%m-%dT%H:%M:%S" <$> getCurrentTime
convertADTHTML :: ADT -> String convertADTHTML (Document blocks) = "\n" ++ "<html lang="en">\n\n" ++ "\n" ++ " <meta charset="UTF-8">\n" ++ " " ++ escapeHTML"Converted HTML" ++"\n" ++ "\n\n" ++ "\n" ++ concatMap convertBlockHTML blocks ++ "\n\n" ++ ""
convertBlockHTML :: Block -> String convertBlockHTML (Heading level inlines) = " <h" ++ show level ++ ">" ++ concatMap convertInlineHTML inlines ++ "</h" ++ show level ++ ">\n" convertBlockHTML (BlockQuote blocks) = " \n" ++ concatMap convertBlockHTMLIndented blocks ++ " \n" convertBlockHTML (CodeBlock mLang code) = let classAttr = maybe "" (\lang -> " class="language-" ++ lang ++ """) mLang escapedCode = escapeHTMLCode code in " <code" ++ classAttr ++ ">" ++ escapedCode ++ "\n" convertBlockHTML (FootnoteReference n text) = " <p id="fn" ++ show n ++ "">" ++ escapeHTML text ++ "\n" convertBlockHTML (OrderedList items) = " \n" ++ concatMap convertListItemHTML items ++ " \n" convertBlockHTML (Table header rows) = let numColumns = length (tableRowCells header) in " \n" ++ " \n \n" ++ concatMap (\cell -> " " ++ concatMap convertInlineHTML cell ++ "\n") (tableRowCells header) ++ " \n \n" ++ " \n" ++ concatMap (convertTableRowHTML numColumns) rows ++ " \n \n" convertBlockHTML (Paragraph [Image alt src mTitle]) = " <img src="" ++ escapeURL src ++ "" alt="" ++ escapeHTML alt ++ """ ++ maybe "" (\title -> " title="" ++ escapeHTML title ++ """) mTitle ++ ">\n" convertBlockHTML (Paragraph inlines) = " " ++ concatMap convertInlineHTML inlines ++ "\n"
convertBlockHTMLIndented :: Block -> String convertBlockHTMLIndented block = indent (convertBlockHTML block)
convertTableRowHTML :: Int -> TableRow -> String convertTableRowHTML numColumns (TableRow cells) = let cellsToRender = take numColumns cells in " \n" ++ concatMap (\cell -> " " ++ concatMap convertInlineHTML cell ++ "\n") cellsToRender ++ " \n"
tableRowCells :: TableRow -> [[Inline]] tableRowCells (TableRow cells) = cells
convertListItemHTML :: ListItem -> String convertListItemHTML (ListItem blocks) = " \n" ++ concatMap convertBlockHTMLIndented blocks ++ " \n"
convertInlineHTML :: Inline -> String convertInlineHTML (Plain text) = escapeHTML text convertInlineHTML (Italic inlines) = "" ++ concatMap convertInlineHTML inlines ++ "" convertInlineHTML (Bold inlines) = "" ++ concatMap convertInlineHTML inlines ++ "" convertInlineHTML (Strikethrough inlines) = "" ++ concatMap convertInlineHTML inlines ++ "" convertInlineHTML (Link inlines url) = "<a href="" ++ escapeURL url ++ "">" ++ concatMap convertInlineHTML inlines ++ "" convertInlineHTML (Image alt src mTitle) = "<img src="" ++ escapeURL src ++ "" alt="" ++ escapeHTML alt ++ """ ++ maybe "" (\title -> " title="" ++ escapeHTML title ++ """) mTitle ++ ">" convertInlineHTML (InlineCode text) = "" ++ escapeHTML text ++ "" convertInlineHTML (Footnote n) = "<a id="fn" ++ show n ++ "ref" href="#fn" ++ show n ++ "">" ++ show n ++ ""
escapeHTML :: String -> String escapeHTML = concatMap (\c -> case c of '&' -> "&" '<' -> "<" '>' -> ">" '"' -> """ '''-> "'" _ -> [c])
escapeHTMLCode :: String -> String escapeHTMLCode = concatMap (\c -> case c of '&' -> "&" '<' -> "<" '>' -> ">" _ -> [c])
escapeURL :: String -> String escapeURL = escapeHTML -- simplistic, should encode special URL characters
indent :: String -> String indent = unlines . map (" " ++) . lines
-- Added notFollowedBy function -- Correct implementation of notFollowedBy notFollowedBy :: Parser a -> Parser () notFollowedBy p = lookAhead (try p *> empty <|> pure ())
-- Defined footnoteRefStartParser footnoteRefStartParser :: Parser () footnoteRefStartParser = do optional inlineSpace is '[' is '^' some digit is ']' is ':' return ()
-- Added Image Block Parser imageBlockParser :: Parser Block imageBlockParser = do optional inlineSpace img <- imageParser optional inlineSpace eol <|> eof return $ Paragraph [img]
for some input:
- Item 1
- Sub Item 1
- Sub Item 2
- Sub Item 3
- Bolded Item 2
- Item 3
- Item 4
we should get :
instead we get :
find the reasin for that , and give a solution
- Item 1
- Assistant
The issue arises because the
orderedListItemParserusesparseBlocks, which in turn callsmarkdownParser. ThemarkdownParserexpects the entire input to be parsed, including an end-of-file (EOF) marker. This causes the parser to treat all subsequent list items as part of the first list item, resulting in a single<li>containing all the content instead of separate<li>elements for each list item.Reason for the Issue:
- Use of
markdownParserInsideorderedListItemParser:- The
orderedListItemParseris responsible for parsing individual list items. After parsing the content of a list item, it callsparseBlocksto parse any potential sub-items (e.g., nested lists). - However,
parseBlockscurrently callsmarkdownParser, which expects the entire input to be a complete Markdown document ending with an EOF. This leads to all subsequent list items being parsed as part of the first list item's content.
- The
Solution:
Modify
parseBlocksto Handle Partial Parsing:Instead of using
markdownParserwithinparseBlocks, which expects the entire input, we should redefineparseBlocksto parse a sequence of blocks without requiring an EOF. This allows the parser to correctly recognize and separate individual list items, including nested lists.Here's how you can implement the solution:
-
Redefine
parseBlocksWithout UsingmarkdownParser:Create a new parser that can parse multiple blocks without expecting the entire input to end. This ensures that each list item is treated separately.
parseBlocks :: String -> Parser [Block] parseBlocks s = case runParser (sepEndBy1 blockParser (some eol)) s of Result _ blocks -> return blocks _ -> return []Explanation:
sepEndBy1 blockParser (some eol): This parser will parse one or more blocks separated by one or more end-of-line characters.runParser: This function runs the parser on the given input strings.Result _ blocks: If parsing is successful, it extracts the parsed blocks.return blocks: Returns the parsed blocks without enforcing an EOF.
-
Update
orderedListItemParserto Use the NewparseBlocks:Ensure that the
orderedListItemParseruses the newly definedparseBlocksinstead of the original one that callsmarkdownParser.orderedListItemParser :: Parser ListItem orderedListItemParser = do optional inlineSpace numStr <- some digit is '.' space1 content <- manyTill anyCharButNewline (lookAhead eol <|> eof) _ <- optional eol subItems <- optional (try orderedListParser) parsedContent <- parseBlocks content let subBlocks = fromMaybe [] (subItems >>= \case (OrderedList items) -> Just (concatMap (\(ListItem bs) -> bs) items) _ -> Nothing) return $ ListItem (parsedContent ++ subBlocks)Explanation:
parsedContent <- parseBlocks content: Now uses the updatedparseBlocksthat doesn't enforce an EOF, allowing the parser to handle multiple list items correctly.- This ensures that each list item is parsed independently, and nested lists are appropriately associated with their parent list items.
-
Ensure Proper Indentation Handling for Nested Lists:
Make sure that the parser correctly handles indentation for nested lists. Typically, nested lists are indented by 4 spaces or a tab. Ensure your parser accounts for this indentation when parsing sub-items.
orderedListItemParser :: Parser ListItem orderedListItemParser = do optional inlineSpace numStr <- some digit is '.' space1 content <- manyTill anyCharButNewline (lookAhead eol <|> eof) _ <- optional eol subItems <- optional (try (indentedOrderedListParser)) parsedContent <- parseBlocks content let subBlocks = fromMaybe [] (subItems >>= \case (OrderedList items) -> Just (concatMap (\(ListItem bs) -> bs) items) _ -> Nothing) return $ ListItem (parsedContent ++ subBlocks)Additional Parser for Indented Lists:
Define an
indentedOrderedListParserto handle nested lists.indentedOrderedListParser :: Parser ADT indentedOrderedListParser = do indent spaces 4 $ orderedListParser return ()Note: You'll need to adjust this according to your specific parser combinator library and ensure that indentation is correctly recognized.
Final Adjusted Code Snippet:
Here’s the adjusted part of your code incorporating the solution:
-- Redefine parseBlocks without using markdownParser parseBlocks :: String -> Parser [Block] parseBlocks s = case runParser (sepEndBy1 blockParser (some eol)) s of Result _ blocks -> return blocks _ -> return [] -- Update orderedListItemParser to use the new parseBlocks orderedListItemParser :: Parser ListItem orderedListItemParser = do optional inlineSpace numStr <- some digit is '.' space1 content <- manyTill anyCharButNewline (lookAhead eol <|> eof) _ <- optional eol subItems <- optional (try orderedListParser) parsedContent <- parseBlocks content let subBlocks = fromMaybe [] (subItems >>= \case (OrderedList items) -> Just (concatMap (\(ListItem bs) -> bs) items) _ -> Nothing) return $ ListItem (parsedContent ++ subBlocks)Testing the Solution:
Given the input:
1. Item 1 1. Sub Item 1 2. Sub Item 2 3. Sub Item 3 2. **Bolded Item 2** 6. Item 3 7. Item 4After applying the above changes, your parser should correctly output:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Converted HTML</title> </head> <body> <ol> <li> <p>Item 1</p> <ol> <li>Sub Item 1</li> <li>Sub Item 2</li> <li>Sub Item 3</li> </ol> </li> <li><strong>Bolded Item 2</strong></li> <li>Item 3</li> <li>Item 4</li> </ol> </body> </html>Conclusion:
By adjusting the
parseBlocksfunction to handle partial parsing without enforcing an EOF and ensuring that nested lists are correctly associated with their parent list items, the parser will correctly recognize and render each list item separately in the resulting HTML. - Use of