Working on parsing single newlines

master
Aadhavan Srinivasan 5 days ago
parent f916267d29
commit a18d03e4ac

@ -21,6 +21,7 @@ data MdToken
| Header HeaderLevel MdToken
| Para MdToken
| Line [MdToken]
| SingleNewline -- A single newline is rendered as a space.
| Linebreak
| HorizontalRule
| Blockquote MdToken
@ -42,6 +43,7 @@ instance Show MdToken where
show (Para token) = "<p>" ++ show token ++ "</p>"
show (Line tokens) = concat (map show tokens)
show Linebreak = "<br>"
show SingleNewline = " "
show HorizontalRule = "---------"
show (Blockquote token) = "BLOCK" ++ show token
show (UnordList tokens) = "UNORD" ++ concat (map show tokens)
@ -87,12 +89,11 @@ lineToList (Line tokens) = tokens
parseHeader :: ReadP MdToken
parseHeader = do
skipSpaces
headers <- many1 mustBeHash
headers <- munch1 (== '#')
when
((length headers) > 6)
pfail
_ <- string " "
-- text <- manyTill (get) ((string "\n") <|> (eof >> return ""))-- Parse until EOL or EOF
skipSpaces
text <- munch1 (/= '\n')
Text.ParserCombinators.ReadP.optional (char '\n')
let parsedText = fst $ leftmostLongestParse parseLine text
@ -128,6 +129,11 @@ parseLinebreak = do
char '\n'
return Linebreak
parseSingleNewline :: ReadP MdToken
parseSingleNewline = do
char '\n'
return SingleNewline
-- Parse a regular string as a Unit.
parseString :: ReadP MdToken
parseString = do
@ -136,7 +142,7 @@ parseString = do
return (Unit (firstChar : text))
lineParsers :: [ReadP MdToken]
lineParsers = [parseLinebreak, parseBold, parseItalic, parseString] -- A 'line' doesn't include a 'header'
lineParsers = [parseLinebreak, parseSingleNewline, parseBold, parseItalic, parseString] -- A 'line' doesn't include a 'header'
-- List of all parsers
allParsers :: [ReadP MdToken]
@ -154,7 +160,6 @@ parseLine = do
remaining <- look
when (null remaining) pfail
parsed <- parseMany parseLineToken
-- traceM $ show parsed
return (Line parsed)
-- Parse a paragraph, which is a 'Line' (can span multiple actual lines), separated by double-newlines.

@ -9,9 +9,9 @@ check_equal desc expected actual = TestCase (assertEqual desc expected actual)
convert :: String -> String
convert md = show . fst $ leftmostLongestParse parseDocument md
headerTests = TestList
[
check_equal "Should convert H1 heading" "<h1>Hello</h1>" (convert "# Hello"),
headerTests =
TestList
[ check_equal "Should convert H1 heading" "<h1>Hello</h1>" (convert "# Hello"),
check_equal "Should convert H2 heading" "<h2>Hello</h2>" (convert "## Hello"),
check_equal "Should convert H3 heading" "<h3>Hello</h3>" (convert "### Hello"),
check_equal "Should convert H4 heading" "<h4>Hello</h4>" (convert "#### Hello"),
@ -19,24 +19,24 @@ headerTests = TestList
check_equal "Should convert H6 heading" "<h6>Hello</h6>" (convert "###### Hello")
]
boldTests = TestList
[
check_equal "Should convert bold" "<p><b>Hello</b></p>" (convert "__Hello__"),
boldTests =
TestList
[ check_equal "Should convert bold" "<p><b>Hello</b></p>" (convert "__Hello__"),
check_equal "Should convert italic" "<p><i>Hello</i></p>" (convert "_Hello_"),
check_equal "Should convert bold and italic in a sentence" "<p>It <i>is</i> a <b>wonderful</b> day</p>" (convert "It _is_ a __wonderful__ day")
]
integrationTests = TestList
[
check_equal "Integration 1" "<h1>Sample Markdown</h1><p>This is some basic, sample markdown.</p><h2><b>Second</b> <i>Heading</i></h2>" (convert "# Sample Markdown\n\n This is some basic, sample markdown.\n\n ## __Second__ _Heading_"),
integrationTests =
TestList
[ check_equal "Integration 1" "<h1>Sample Markdown</h1><p>This is some basic, sample markdown.</p><h2><b>Second</b> <i>Heading</i></h2>" (convert "# Sample Markdown\n\n This is some basic, sample markdown.\n\n ## __Second__ _Heading_"),
check_equal "Integration 2" "<p><b>Hello</b> <i>World</i></p>" (convert "__Hello__\n_World_"),
check_equal "Integration 3" "<h1>Hello</h1><p>World</p>" (convert "# Hello\nWorld")
check_equal "Integration 3" "<h1>Hello</h1><p>World</p>" (convert "# Hello\nWorld"),
check_equal "Integration 4" "<h1>Hello</h1>" (convert "# Hello\n")
]
tests = TestList
[
headerTests,
tests =
TestList
[ headerTests,
boldTests,
integrationTests
]

Loading…
Cancel
Save