Compare commits
4 Commits
1fcce32ef6
...
master
Author | SHA1 | Date | |
---|---|---|---|
1d9ac86a2a | |||
0320402957 | |||
8696a185a7 | |||
da38ac226f |
@@ -15,7 +15,7 @@ readLinesHelper xs = do
|
|||||||
readLinesHelper xs'
|
readLinesHelper xs'
|
||||||
|
|
||||||
readLines :: IO [String]
|
readLines :: IO [String]
|
||||||
readLines = fmap reverse $ readLinesHelper []
|
readLines = reverse <$> readLinesHelper []
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
|
@@ -37,6 +37,7 @@ data MdToken
|
|||||||
| Codeblock String
|
| Codeblock String
|
||||||
| Link MdToken URL
|
| Link MdToken URL
|
||||||
| Image MdToken ImgPath
|
| Image MdToken ImgPath
|
||||||
|
| Figure MdToken ImgPath
|
||||||
| Bold MdToken
|
| Bold MdToken
|
||||||
| Italic MdToken
|
| Italic MdToken
|
||||||
| Strikethrough MdToken
|
| Strikethrough MdToken
|
||||||
@@ -59,6 +60,7 @@ instance Show MdToken where
|
|||||||
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 ++ "\"" ++ " alt=\"" ++ show txt ++ "\" />"
|
show (Image txt imgPath) = "<img src=\"" ++ getPath imgPath ++ "\"" ++ " alt=\"" ++ show txt ++ "\" />"
|
||||||
|
show (Figure txt imgPath) = "<figure><img src=\"" ++ getPath imgPath ++ "\" alt=\"" ++ show txt ++ "\"/><figcaption aria-hidden=\"true\">" ++ show txt ++ "</figcaption></figure>"
|
||||||
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>"
|
||||||
@@ -192,7 +194,10 @@ parseLinebreak = do
|
|||||||
parseSingleNewline :: ReadP MdToken
|
parseSingleNewline :: ReadP MdToken
|
||||||
parseSingleNewline = do
|
parseSingleNewline = do
|
||||||
char '\n'
|
char '\n'
|
||||||
return SingleNewline
|
remaining <- look
|
||||||
|
case remaining of
|
||||||
|
[] -> return $ Unit ""
|
||||||
|
_ -> return SingleNewline
|
||||||
|
|
||||||
parseImage :: ReadP MdToken
|
parseImage :: ReadP MdToken
|
||||||
parseImage = do
|
parseImage = do
|
||||||
@@ -205,6 +210,13 @@ parseImage = do
|
|||||||
char ')'
|
char ')'
|
||||||
return $ Image (Line altText) (ImgPath path)
|
return $ Image (Line altText) (ImgPath path)
|
||||||
|
|
||||||
|
parseFigure = do
|
||||||
|
img <- parseImage
|
||||||
|
void (string "\n\n") <++ eof
|
||||||
|
case img of
|
||||||
|
Image text path -> return $ Figure text path
|
||||||
|
_ -> return img
|
||||||
|
|
||||||
-- Parse an escaped character
|
-- Parse an escaped character
|
||||||
parseEscapedChar :: ReadP MdToken
|
parseEscapedChar :: ReadP MdToken
|
||||||
parseEscapedChar = do
|
parseEscapedChar = do
|
||||||
@@ -274,6 +286,7 @@ parsePara = do
|
|||||||
text <- manyTill get (string "\n\n" <|> (eof >> return ""))
|
text <- manyTill get (string "\n\n" <|> (eof >> return ""))
|
||||||
when (null text) pfail
|
when (null text) pfail
|
||||||
let parsedText = fst $ leftmostLongestParse parseLine text -- Parse a line
|
let parsedText = fst $ leftmostLongestParse parseLine text -- Parse a line
|
||||||
|
parseMany (char '\n')
|
||||||
return (Para parsedText)
|
return (Para parsedText)
|
||||||
|
|
||||||
-- Parse a line starting with '>', return the line except for the '>'.
|
-- Parse a line starting with '>', return the line except for the '>'.
|
||||||
@@ -394,12 +407,17 @@ parseOrderedList = do
|
|||||||
void (char '\n') <++ eof
|
void (char '\n') <++ eof
|
||||||
return $ OrdList (firstLine : lineItems)
|
return $ OrdList (firstLine : lineItems)
|
||||||
|
|
||||||
|
parseHorizontalRule :: ReadP MdToken
|
||||||
|
parseHorizontalRule = string "---" *> (void (string "\n\n") <++ eof) *> return HorizontalRule
|
||||||
|
|
||||||
documentParsers :: [ReadP MdToken]
|
documentParsers :: [ReadP MdToken]
|
||||||
documentParsers =
|
documentParsers =
|
||||||
[ parseHeader,
|
[ parseHorizontalRule,
|
||||||
|
parseHeader,
|
||||||
parseBlockquote,
|
parseBlockquote,
|
||||||
parseUnorderedList,
|
parseUnorderedList,
|
||||||
parseOrderedList,
|
parseOrderedList,
|
||||||
|
parseFigure,
|
||||||
parsePara
|
parsePara
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@@ -102,10 +102,18 @@ codeTests =
|
|||||||
|
|
||||||
imageTests =
|
imageTests =
|
||||||
TestList
|
TestList
|
||||||
[ check_equal "Image by itself" "<p><img src=\"img.png\" alt=\"Image 1\" /></p>" (convert ""),
|
[ check_equal "Image with text" "<p>This is an image <img src=\"img.png\" alt=\"Image 1\" /></p>" (convert "This is an image ")
|
||||||
check_equal "Image with text" "<p>This is an image <img src=\"img.png\" alt=\"Image 1\" /></p>" (convert "This is an image ")
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
figureTests =
|
||||||
|
TestList
|
||||||
|
[ check_equal "Image by itself" "<figure><img src=\"img.png\" alt=\"Image 1\"/><figcaption aria-hidden=\"true\">Image 1</figcaption></figure>" (convert "")
|
||||||
|
]
|
||||||
|
|
||||||
|
horizontalRuleTests =
|
||||||
|
TestList
|
||||||
|
[check_equal "Horizontal Rule" "<p>a</p><hr><p>b</p>" (convert "a\n\n---\n\nb")]
|
||||||
|
|
||||||
integrationTests =
|
integrationTests =
|
||||||
TestList
|
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 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_"),
|
||||||
@@ -141,7 +149,9 @@ tests =
|
|||||||
unorderedListTests,
|
unorderedListTests,
|
||||||
orderedListTests,
|
orderedListTests,
|
||||||
imageTests,
|
imageTests,
|
||||||
|
figureTests,
|
||||||
codeTests,
|
codeTests,
|
||||||
|
horizontalRuleTests,
|
||||||
integrationTests
|
integrationTests
|
||||||
]
|
]
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user