Compare commits
5 Commits
9a128407cc
...
d1b0ce6b10
Author | SHA1 | Date | |
---|---|---|---|
d1b0ce6b10 | |||
b6f51c33c7 | |||
9ffbb7365c | |||
71aacdd26a | |||
873795e267 |
@@ -59,9 +59,6 @@ instance Show MdToken where
|
|||||||
|
|
||||||
-- ---------------
|
-- ---------------
|
||||||
-- Helpers
|
-- Helpers
|
||||||
mustBeHash :: ReadP Char
|
|
||||||
mustBeHash = satisfy (\x -> x == '#')
|
|
||||||
|
|
||||||
leftmostLongest :: (Foldable t) => [(a, t b)] -> (a, t b)
|
leftmostLongest :: (Foldable t) => [(a, t b)] -> (a, t b)
|
||||||
leftmostLongest xs =
|
leftmostLongest xs =
|
||||||
let lastElem = (last xs)
|
let lastElem = (last xs)
|
||||||
@@ -115,8 +112,8 @@ parseItalic :: ReadP MdToken
|
|||||||
parseItalic = do
|
parseItalic = do
|
||||||
text <-
|
text <-
|
||||||
choice
|
choice
|
||||||
[ (between (string "_") (string "_") (munch1 (/= '_'))),
|
[ between (string "_") (string "_") (munch1 (/= '_')),
|
||||||
(between (string "*") (string "*") (munch1 (/= '*')))
|
between (string "*") (string "*") (munch1 (/= '*'))
|
||||||
]
|
]
|
||||||
let parsedText = fst $ leftmostLongestParse parseLine text
|
let parsedText = fst $ leftmostLongestParse parseLine text
|
||||||
return (Italic parsedText)
|
return (Italic parsedText)
|
||||||
@@ -143,7 +140,6 @@ parseString = do
|
|||||||
|
|
||||||
lineParsers :: [ReadP MdToken]
|
lineParsers :: [ReadP MdToken]
|
||||||
lineParsers = [parseLinebreak, parseSingleNewline, parseBold, parseItalic, parseString] -- A 'line' doesn't include a 'header'
|
lineParsers = [parseLinebreak, parseSingleNewline, parseBold, parseItalic, parseString] -- A 'line' doesn't include a 'header'
|
||||||
--lineParsers = [parseSingleNewline, parseString]
|
|
||||||
|
|
||||||
-- List of all parsers
|
-- List of all parsers
|
||||||
allParsers :: [ReadP MdToken]
|
allParsers :: [ReadP MdToken]
|
||||||
@@ -158,9 +154,7 @@ parseLine :: ReadP MdToken
|
|||||||
parseLine = do
|
parseLine = do
|
||||||
skipSpaces
|
skipSpaces
|
||||||
-- Fail if we have reached the end of the document.
|
-- Fail if we have reached the end of the document.
|
||||||
remaining <- look
|
parsed <- manyTill parseLineToken eof
|
||||||
when (null remaining) pfail
|
|
||||||
parsed <- parseMany parseLineToken
|
|
||||||
return (Line parsed)
|
return (Line parsed)
|
||||||
|
|
||||||
-- Parse a paragraph, which is a 'Line' (can span multiple actual lines), separated by double-newlines.
|
-- Parse a paragraph, which is a 'Line' (can span multiple actual lines), separated by double-newlines.
|
||||||
@@ -172,7 +166,7 @@ parsePara = do
|
|||||||
-- string "\n\n" <|> (eof >> return "") -- Consume the next double-newline or EOF.
|
-- string "\n\n" <|> (eof >> return "") -- Consume the next double-newline or EOF.
|
||||||
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 (parseHeader <|> parseLine) text -- Parse either a line or a header.
|
let parsedText = fst $ leftmostLongestParse parseLine text -- Parse either a line or a header.
|
||||||
-- If the paragraph is a header, return a Header token. Otheriwse return a Para token.
|
-- If the paragraph is a header, return a Header token. Otheriwse return a Para token.
|
||||||
case parsedText of
|
case parsedText of
|
||||||
Header level token -> return (Header level token)
|
Header level token -> return (Header level token)
|
||||||
@@ -180,4 +174,6 @@ parsePara = do
|
|||||||
|
|
||||||
-- Parse a document, which is multiple paragraphs.
|
-- Parse a document, which is multiple paragraphs.
|
||||||
parseDocument :: ReadP MdToken
|
parseDocument :: ReadP MdToken
|
||||||
parseDocument = (many1 parsePara) >>= (\res -> return (Document (res)))
|
parseDocument = do
|
||||||
|
res <- manyTill (parseHeader <++ parsePara) (eof)
|
||||||
|
return (Document res)
|
||||||
|
Reference in New Issue
Block a user