Added token and parser for list checkbox
This commit is contained in:
@@ -40,6 +40,7 @@ data MdToken
|
|||||||
| Blockquote [MdToken]
|
| Blockquote [MdToken]
|
||||||
| UnordList [MdToken]
|
| UnordList [MdToken]
|
||||||
| OrdList [MdToken]
|
| OrdList [MdToken]
|
||||||
|
| Checkbox Bool
|
||||||
| Code MdToken
|
| Code MdToken
|
||||||
| Table [[MdToken]]
|
| Table [[MdToken]]
|
||||||
| Codeblock MdToken
|
| Codeblock MdToken
|
||||||
@@ -66,6 +67,7 @@ instance Show MdToken where
|
|||||||
show (Blockquote tokens) = "<blockquote>" ++ concatMap show tokens ++ "</blockquote>"
|
show (Blockquote tokens) = "<blockquote>" ++ concatMap show tokens ++ "</blockquote>"
|
||||||
show (UnordList tokens) = "<ul>" ++ concatMap (prepend "<li>" . append "</li>" . show) tokens ++ "</ul>"
|
show (UnordList tokens) = "<ul>" ++ concatMap (prepend "<li>" . append "</li>" . show) tokens ++ "</ul>"
|
||||||
show (OrdList tokens) = "<ol>" ++ concatMap (prepend "<li>" . append "</li>" . show) tokens ++ "</ol>"
|
show (OrdList tokens) = "<ol>" ++ concatMap (prepend "<li>" . append "</li>" . show) tokens ++ "</ol>"
|
||||||
|
show (Checkbox isChecked) = "<input type=\"checkbox\"" ++ (if isChecked then " checked=\"\"" else "") ++ " />"
|
||||||
show (Code code) = "<code>" ++ strip (show code) ++ "</code>"
|
show (Code code) = "<code>" ++ strip (show code) ++ "</code>"
|
||||||
show (Table (thead : tokenGrid)) = "<table><thead><tr>" ++ concatMap (\x -> "<th>" ++ rstrip (show x) ++ "</th>") thead ++ "</tr></thead>" ++ "<tbody>" ++ concatMap (\x -> "<tr>" ++ concatMap (\y -> "<td>" ++ rstrip (show y) ++ "</td>") x ++ "</tr>") tokenGrid ++ "</tbody></table>"
|
show (Table (thead : tokenGrid)) = "<table><thead><tr>" ++ concatMap (\x -> "<th>" ++ rstrip (show x) ++ "</th>") thead ++ "</tr></thead>" ++ "<tbody>" ++ concatMap (\x -> "<tr>" ++ concatMap (\y -> "<td>" ++ rstrip (show y) ++ "</td>") x ++ "</tr>") tokenGrid ++ "</tbody></table>"
|
||||||
show (Codeblock code) = "<pre><code>" ++ show code ++ "</code></pre>"
|
show (Codeblock code) = "<pre><code>" ++ show code ++ "</code></pre>"
|
||||||
@@ -425,6 +427,15 @@ parseBlockquote = do
|
|||||||
let parsedQuotedLines = leftmostLongestParse (some (parseBlockquote <|> parsePara)) (init $ unlines quotedLines) -- unlines joins the lines together with a newline, and adds a trailing newline. init removes the trailing newline.
|
let parsedQuotedLines = leftmostLongestParse (some (parseBlockquote <|> parsePara)) (init $ unlines quotedLines) -- unlines joins the lines together with a newline, and adds a trailing newline. init removes the trailing newline.
|
||||||
return (Blockquote parsedQuotedLines)
|
return (Blockquote parsedQuotedLines)
|
||||||
|
|
||||||
|
-- Parse a checkbox
|
||||||
|
parseCheckbox :: Parser MdToken
|
||||||
|
parseCheckbox = do
|
||||||
|
char '['
|
||||||
|
inside <- char ' ' <|> char 'x'
|
||||||
|
char ']'
|
||||||
|
space
|
||||||
|
return (if inside == 'x' then Checkbox True else Checkbox False)
|
||||||
|
|
||||||
-- Parse a nested list item.
|
-- Parse a nested list item.
|
||||||
parseListNested :: Parser MdToken
|
parseListNested :: Parser MdToken
|
||||||
parseListNested = do
|
parseListNested = do
|
||||||
@@ -460,9 +471,12 @@ parseOListLineItem = do
|
|||||||
parseListLineItemCommon :: Parser MdToken
|
parseListLineItemCommon :: Parser MdToken
|
||||||
parseListLineItemCommon = do
|
parseListLineItemCommon = do
|
||||||
space
|
space
|
||||||
|
checkbox <- optional $ try parseCheckbox
|
||||||
restOfLine <- manyTill parseListLineToken (void (char '\n') <|> eof)
|
restOfLine <- manyTill parseListLineToken (void (char '\n') <|> eof)
|
||||||
nestedList <- try parseListNested <|> return (Unit "")
|
nestedList <- try parseListNested <|> return (Unit "")
|
||||||
return $ Line [Line restOfLine, nestedList]
|
case checkbox of
|
||||||
|
Just box -> return $ Line [box, Line restOfLine, nestedList]
|
||||||
|
Nothing -> return $ Line [Line restOfLine, nestedList]
|
||||||
|
|
||||||
-- Parse an unordered list paragraph item.
|
-- Parse an unordered list paragraph item.
|
||||||
parseUListParaItem :: Parser MdToken
|
parseUListParaItem :: Parser MdToken
|
||||||
|
Reference in New Issue
Block a user