trying to figure out how the hell to make this work for new lines but now the header seems to include the follow on line

This commit is contained in:
Pagwin 2025-11-13 17:01:08 -05:00
parent f3e9c4c8b2
commit bc0475fde4
No known key found for this signature in database
GPG key ID: 81137023740CA260

View file

@ -12,7 +12,7 @@ import Data.Text (Text)
import qualified Data.Text as T import qualified Data.Text as T
import Data.Void (Void) import Data.Void (Void)
import IR import IR
import Text.Megaparsec (Parsec, anySingle, anySingleBut, between, choice, count, eof, manyTill, notFollowedBy, satisfy, skipSome, try) import Text.Megaparsec (Parsec, anySingle, anySingleBut, between, choice, count, eof, manyTill, notFollowedBy, satisfy, skipSome, try, (<?>))
import qualified Text.Megaparsec as MP import qualified Text.Megaparsec as MP
import Text.Megaparsec.Char (alphaNumChar, char, digitChar, string) import Text.Megaparsec.Char (alphaNumChar, char, digitChar, string)
@ -50,16 +50,16 @@ document = Doc <$> many element <* eof
element :: Parser Element element :: Parser Element
element = element =
choice choice
[ try headingBlock, [ try headingBlock <?> "Element Heading",
try fencedCodeBlock, try fencedCodeBlock <?> "Fenced Code Block",
try indentedCodeBlock, try indentedCodeBlock <?> "Indented Code Block",
try blockquoteBlock, try blockquoteBlock <?> "BlockQuote",
try unorderedListBlock, try unorderedListBlock <?> "Unordered List",
try orderedListBlock, try orderedListBlock <?> "Ordered List",
try horizontalRuleBlock, try horizontalRuleBlock <?> "Horizontal Rule",
try htmlBlock, try htmlBlock <?> "HTML Block",
try blankLines, -- Consume blank lines but don't add to AST try blankLines <?> "Blank Lines", -- Consume blank lines but don't add to AST
paragraphBlock paragraphBlock <?> "Paragarph"
] ]
-- Blank lines (consumed but not stored) -- Blank lines (consumed but not stored)
@ -77,11 +77,11 @@ blankLine = do
-- Heading Block -- Heading Block
headingBlock :: Parser Element headingBlock :: Parser Element
headingBlock = do headingBlock = do
hashes <- some (char '#') hashes <- some (char '#') <?> "Heading Hashes"
let level = length hashes let level = length hashes
guard (level <= 6) guard (level <= 6) <?> "Higher than level 6"
many (char ' ' <|> char '\t') many (char ' ' <|> char '\t') <?> "Pre-Text Whitespace"
content <- manyTill inlineElement (try lineEnding) content <- manyTill (inlineElement <?> "Header Text") (try lineEnding <?> "Header Ending")
pure $ Heading $ H level content pure $ Heading $ H level content
-- Fenced Code Block -- Fenced Code Block
@ -246,15 +246,15 @@ paragraphBlock = do
inlineElement :: Parser InlineText inlineElement :: Parser InlineText
inlineElement = inlineElement =
choice choice
[ try strong, [ try strong <?> "Inline Strong Text",
try emphasis, try emphasis <?> "Inline Italic Text",
try crossedText, try crossedText <?> "Inline Crossed Text",
try codeSpan, try codeSpan <?> "Inline Code",
try image, try image <?> "Inline Image",
try link, try link <?> "Inline Link",
try htmlInline, try htmlInline <?> "Inline HTML",
try escapedChar, try escapedChar <?> "Escaped Character",
plainText plainText <?> "Inline Plain Text"
] ]
-- Strong (Bold) -- Strong (Bold)
@ -314,8 +314,8 @@ inlineElementNo c =
plainTextNo :: [Char] -> Parser InlineText plainTextNo :: [Char] -> Parser InlineText
plainTextNo disallow = do plainTextNo disallow = do
firstChar <- noneOf disallow firstChar <- noneOf disallow <?> "Plain Text Initial Disallow"
remChars <- some $ plainTextCharNo disallow <* notFollowedBy lineEnding remChars <- manyTill (plainTextCharNo disallow) lineEnding <?> "Remaining Characters"
pure $ Text $ T.map wspHandler $ T.pack $ firstChar : remChars pure $ Text $ T.map wspHandler $ T.pack $ firstChar : remChars
where where
wspHandler '\n' = ' ' wspHandler '\n' = ' '
@ -422,7 +422,7 @@ escapedChar = do
-- Plain Text -- Plain Text
plainText :: Parser InlineText plainText :: Parser InlineText
plainText = plainTextNo [] plainText = plainTextNo [] <?> "Baseline Plain Text"
plainTextBaseDisallow :: [Char] plainTextBaseDisallow :: [Char]
plainTextBaseDisallow = "[~`_*<" plainTextBaseDisallow = "[~`_*<"
@ -441,7 +441,7 @@ plainTextNoBracket = plainTextNo "[]"
-- Helper Parsers -- Helper Parsers
lineEnding :: Parser () lineEnding :: Parser ()
lineEnding = void $ count 2 (try (string "\r\n") <|> try (string "\n") <|> string "\r") lineEnding = void (try $ count 2 (try (string "\r\n") <|> try (string "\n") <|> string "\r")) <|> eof
wsParser :: Parser () wsParser :: Parser ()
wsParser = void $ some (char ' ' <|> char '\t') wsParser = void $ some (char ' ' <|> char '\t')