@ -86,23 +86,21 @@ lineToList (Line tokens) = tokens
-- Parse a markdown header, denoted by 1-6 #'s followed by some text, followed by EOL.
parseHeader :: ReadP MdToken
parseHeader = do
traceM " Reached parseHeader "
skipSpaces
headers <- many1 mustBeHash
when
( ( length headers ) > 6 )
pfail
_ <- string " "
text <- munch1 ( \ x -> x /= ' \ n ' ) -- Parse until EOL
-- text <- manyTill (get) ((string "\n") <|> (eof >> return ""))-- Parse until EOL or EOF
text <- munch1 ( /= ' \ n ' )
Text . ParserCombinators . ReadP . optional ( char ' \ n ' )
let parsedText = fst $ leftmostLongestParse parseLine text
traceM ( show parsedText )
traceM ( show ( length headers ) )
return ( Header ( length headers ) parsedText )
-- Parse bold text
parseBold :: ReadP MdToken
parseBold = do
traceM " Reached parseBold "
text <-
choice
[ between ( string " __ " ) ( string " __ " ) ( many1 ( lookaheadParse ( /= " __ " ) ) ) ,
@ -114,7 +112,6 @@ parseBold = do
-- Parse italic text
parseItalic :: ReadP MdToken
parseItalic = do
traceM " Reached parseItalic "
text <-
choice
[ ( between ( string " _ " ) ( string " _ " ) ( munch1 ( /= '_' ) ) ) ,
@ -126,7 +123,6 @@ parseItalic = do
-- Parse a linebreak character
parseLinebreak :: ReadP MdToken
parseLinebreak = do
traceM " Reached parseLinebreak "
char ' '
many1 ( char ' ' )
char ' \ n '
@ -135,7 +131,6 @@ parseLinebreak = do
-- Parse a regular string as a Unit.
parseString :: ReadP MdToken
parseString = do
traceM " Reached parseString "
firstChar <- get -- Must parse at least one character here
text <- munch ( \ x -> not ( elem x " #*_[ \ n " ) )
return ( Unit ( firstChar : text ) )
@ -154,7 +149,6 @@ parseLineToken = choice lineParsers
-- Parse a line, consisting of one or more tokens.
parseLine :: ReadP MdToken
parseLine = do
traceM " Reached parseLine "
skipSpaces
-- Fail if we have reached the end of the document.
remaining <- look
@ -167,14 +161,12 @@ parseLine = do
-- As a weird special case, a 'Paragraph' can also be a 'Header'.
parsePara :: ReadP MdToken
parsePara = do
traceM " Reached parsePara "
parseMany ( char ' \ n ' )
-- text <- many1 (lookaheadParse (\x -> ((length x) < 2) || (take 2 x) /= "\n\n")) -- Parse until a double-newline.
-- string "\n\n" <|> (eof >> return "") -- Consume the next double-newline or EOF.
text <- ( manyTill get ( ( string " \ n \ n " ) <|> ( eof >> return " " ) ) )
when ( null text ) pfail
let parsedText = fst $ leftmostLongestParse ( parseHeader <|> parseLine ) text -- Parse either a line or a header.
traceM ( show parsedText )
-- If the paragraph is a header, return a Header token. Otheriwse return a Para token.
case parsedText of
Header level token -> return ( Header level token )