4 Commits

Author SHA1 Message Date
b98a8cc44f Added image tests 2025-05-23 14:55:58 -04:00
90c7a585d2 Added image parser 2025-05-23 14:04:37 -04:00
4a15330874 Updated showing of image 2025-05-23 14:04:21 -04:00
c14112d3e4 Updated special and escapable characters 2025-05-23 14:04:05 -04:00
2 changed files with 24 additions and 4 deletions

View File

@@ -58,7 +58,7 @@ instance Show MdToken where
show (Code code) = "<code>" ++ show code ++ "</code>" show (Code code) = "<code>" ++ show code ++ "</code>"
show (Codeblock code) = show code show (Codeblock code) = show code
show (Link txt url) = "<a href=\"" ++ getUrl url ++ "\">" ++ show txt ++ "</a>" show (Link txt url) = "<a href=\"" ++ getUrl url ++ "\">" ++ show txt ++ "</a>"
show (Image txt imgPath) = "<img src=" ++ getPath imgPath ++ ">" ++ show txt ++ "</img>" show (Image txt imgPath) = "<img src=\"" ++ getPath imgPath ++ "\"" ++ " alt=\"" ++ show txt ++ "\" />"
show (Bold token) = "<b>" ++ show token ++ "</b>" show (Bold token) = "<b>" ++ show token ++ "</b>"
show (Italic token) = "<i>" ++ show token ++ "</i>" show (Italic token) = "<i>" ++ show token ++ "</i>"
show (Strikethrough token) = "<s>" ++ show token ++ "</s>" show (Strikethrough token) = "<s>" ++ show token ++ "</s>"
@@ -88,9 +88,9 @@ leftmostLongestParse parser input =
Nothing -> (mempty, mempty) Nothing -> (mempty, mempty)
Just x -> x Just x -> x
specialChars = "\\#*_[\n`" specialChars = "\n\\`*_{}[]()<>#+|"
escapableChars = '~' : specialChars escapableChars = "-~!." ++ specialChars
-- Makes a parser greedy. Instead of returning all possible parses, only the longest one is returned. -- Makes a parser greedy. Instead of returning all possible parses, only the longest one is returned.
greedyParse :: ReadP a -> ReadP [a] greedyParse :: ReadP a -> ReadP [a]
@@ -194,6 +194,17 @@ parseSingleNewline = do
char '\n' char '\n'
return SingleNewline return SingleNewline
parseImage :: ReadP MdToken
parseImage = do
char '!'
char '['
altText <- many1 (parseEscapedChar <++ parseUnit)
char ']'
char '('
path <- many1 get
char ')'
return $ Image (Line altText) (ImgPath path)
-- Parse an escaped character -- Parse an escaped character
parseEscapedChar :: ReadP MdToken parseEscapedChar :: ReadP MdToken
parseEscapedChar = do parseEscapedChar = do
@@ -213,6 +224,7 @@ lineParsers =
parseSingleNewline, parseSingleNewline,
parseEscapedChar, parseEscapedChar,
parseCode, parseCode,
parseImage,
parseBold, parseBold,
parseItalic, parseItalic,
parseStrikethrough, parseStrikethrough,
@@ -225,6 +237,7 @@ listLineParsers =
[ parseLinebreak, [ parseLinebreak,
parseEscapedChar, parseEscapedChar,
parseCode, parseCode,
parseImage,
parseBold, parseBold,
parseItalic, parseItalic,
parseStrikethrough, parseStrikethrough,

View File

@@ -97,7 +97,13 @@ codeTests =
TestList TestList
[ check_equal "Code by itself" "<p><code>Hello world!</code></p>" (convert "`Hello world!`"), [ check_equal "Code by itself" "<p><code>Hello world!</code></p>" (convert "`Hello world!`"),
check_equal "Code in a paragraph" "<p>The following <code>text</code> is code</p>" (convert "The following `text` is code"), check_equal "Code in a paragraph" "<p>The following <code>text</code> is code</p>" (convert "The following `text` is code"),
check_equal "Code across paragraphs (shouldn't work" "<p>`Incomplete</p><p>Code`</p>" (convert "`Incomplete\n\nCode`") check_equal "Code across paragraphs (shouldn't work" "<p></p><p></p>" (convert "`Incomplete\n\nCode`") -- At the moment, this is just treated as a syntax error, so nothing is rendered.
]
imageTests =
TestList
[ check_equal "Image by itself" "<p><img src=\"img.png\" alt=\"Image 1\" /></p>" (convert "![Image 1](img.png)"),
check_equal "Image with text" "<p>This is an image <img src=\"img.png\" alt=\"Image 1\" /></p>" (convert "This is an image ![Image 1](img.png)")
] ]
integrationTests = integrationTests =
@@ -134,6 +140,7 @@ tests =
blockquoteTests, blockquoteTests,
unorderedListTests, unorderedListTests,
orderedListTests, orderedListTests,
imageTests,
codeTests, codeTests,
integrationTests integrationTests
] ]