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 31 additions and 29 deletions

View File

@@ -58,7 +58,7 @@ instance Show MdToken where
show (Code code) = "<code>" ++ show code ++ "</code>"
show (Codeblock code) = show code
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 (Italic token) = "<i>" ++ show token ++ "</i>"
show (Strikethrough token) = "<s>" ++ show token ++ "</s>"
@@ -88,9 +88,9 @@ leftmostLongestParse parser input =
Nothing -> (mempty, mempty)
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.
greedyParse :: ReadP a -> ReadP [a]
@@ -120,16 +120,6 @@ fallthroughParser :: [ReadP a] -> ReadP a
fallthroughParser [x] = x
fallthroughParser (x : xs) = x <++ fallthroughParser xs
myMany :: (Monoid a) => ReadP a -> ReadP [a]
myMany p = do
remaining <- look
case remaining of
[] -> return []
_ -> return [] +++ myMany1 p
myMany1 :: (Monoid a) => ReadP a -> ReadP [a]
myMany1 p = liftM2 (:) p (myMany p)
-- ---------------
-- Parse a markdown header, denoted by 1-6 #'s followed by some text, followed by EOL.
@@ -153,25 +143,19 @@ parseBold = parseBoldWith "**" <|> parseBoldWith "__"
where
parseBoldWith delim = do
string delim
inside <- myMany1 parseLineToken
inside <- greedyParse1 parseLineToken
string delim
return (Bold (Line inside))
-- Parse italic text
parseItalic :: ReadP MdToken
parseItalic = parseItalicWith '*' <|> parseItalicWith '_'
parseItalic = parseItalicWith "*" <|> parseItalicWith "_"
where
parseItalicWith delim = do
exactlyOnce delim
inside <- myMany1 parseLineToken
exactlyOnce delim
string delim
inside <- greedyParse1 parseLineToken
string delim
return (Italic (Line inside))
exactlyOnce ch = do
char ch
remaining <- look
case remaining of
[] -> return ch
x : xs -> if x == ch then pfail else return ch
-- Parse strikethrough text
parseStrikethrough :: ReadP MdToken
@@ -210,6 +194,17 @@ parseSingleNewline = do
char '\n'
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
parseEscapedChar :: ReadP MdToken
parseEscapedChar = do
@@ -220,8 +215,7 @@ parseEscapedChar = do
-- Parse a character as a Unit.
parseUnit :: ReadP MdToken
parseUnit = do
-- text <- satisfy (`notElem` specialChars)
text <- get
text <- satisfy (`notElem` specialChars)
return (Unit [text])
lineParsers :: [ReadP MdToken]
@@ -230,6 +224,7 @@ lineParsers =
parseSingleNewline,
parseEscapedChar,
parseCode,
parseImage,
parseBold,
parseItalic,
parseStrikethrough,
@@ -242,6 +237,7 @@ listLineParsers =
[ parseLinebreak,
parseEscapedChar,
parseCode,
parseImage,
parseBold,
parseItalic,
parseStrikethrough,
@@ -266,7 +262,7 @@ parseLine :: ReadP MdToken
parseLine = do
skipSpaces
-- Fail if we have reached the end of the document.
parsed <- myMany1 parseLineToken
parsed <- manyTill parseLineToken eof
return (Line parsed)
-- Parse a paragraph, which is a 'Line' (can span multiple actual lines), separated by double-newlines.

View File

@@ -22,7 +22,6 @@ headerTests =
boldTests =
TestList
[ check_equal "Should convert bold" "<p><b>Hello</b></p>" (convert "__Hello__"),
check_equal " Should not convert incomplete bold" "<p>**Hello</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"),
check_equal "Should convert nested bold and italic" "<p><b>Bold then <i>Italic</i></b></p>" (convert "**Bold then *Italic***"),
@@ -98,7 +97,13 @@ codeTests =
TestList
[ 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 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 =
@@ -135,6 +140,7 @@ tests =
blockquoteTests,
unorderedListTests,
orderedListTests,
imageTests,
codeTests,
integrationTests
]