git.iko.soy/typeable/pandoc
on master help sign in
Improve emailAddress in Text.Pandoc.Parsing.

Previously the parser would accept characters in domains that are illegal in domains, and this sometimes caused it to gobble bits of the following text. Closes #7398. Note that this change, by itself, caused some txt2tag reader tests to fail. txt2tags allows bare email addresses with a following form query. So, in addition to the change to emailAddress, we modify the txt2tags parser so it can still handle these cases.

commit0352f7845bfa2053797850c3639414978285b63e authorJohn MacFarlane <jgm@berkeley.edu> dateMon 21 Jun 2021, 22:35 (5 years ago) parent2ef2049
2 files changed+24−5
src/Text/Pandoc/Parsing.hs+3−4 src/Text/Pandoc/Readers/Txt2Tags.hs+21−1
src/Text/Pandoc/Parsing.hs+3−4
@@ -693,13 +693,12 @@ emailAddress = try $ toResult <$> mailbox <*> (char '@' *> domain)
693693 mailbox = intercalate "." <$> (emailWord `sepBy1'` dot)
694694 domain = intercalate "." <$> (subdomain `sepBy1'` dot)
695695 dot = char '.'
696− subdomain = many1 $ alphaNum <|> innerPunct
696+ subdomain = many1 $ alphaNum <|> innerPunct (=='-')
697697 -- this excludes some valid email addresses, since an
698698 -- email could contain e.g. '__', but gives better results
699699 -- for our purposes, when combined with markdown parsing:
700− innerPunct = try (satisfy (\c -> isEmailPunct c || c == '@')
701− <* notFollowedBy space
702− <* notFollowedBy (satisfy isPunctuation))
700+ innerPunct f = try (satisfy f
701+ <* notFollowedBy (satisfy (not . isAlphaNum)))
703702 -- technically an email address could begin with a symbol,
704703 -- but allowing this creates too many problems.
705704 -- See e.g. https://github.com/jgm/pandoc/issues/2940
src/Text/Pandoc/Readers/Txt2Tags.hs+21−1
@@ -478,9 +478,29 @@ macro = try $ do
478478-- raw URLs in text are automatically linked
479479url :: T2T Inlines
480480url = try $ do
481− (rawUrl, escapedUrl) <- try uri <|> emailAddress
481+ (rawUrl, escapedUrl) <- try uri <|> emailAddress'
482482 return $ B.link rawUrl "" (B.str escapedUrl)
483483
484+emailAddress' :: T2T (Text, Text)
485+emailAddress' = do
486+ (base, mailURI) <- emailAddress
487+ query <- option "" emailQuery
488+ return (base <> query, mailURI <> query)
489+
490+emailQuery :: T2T Text
491+emailQuery = do
492+ char '?'
493+ parts <- kv `sepBy1` (char '&')
494+ return $ "?" <> T.intercalate "&" parts
495+
496+kv :: T2T Text
497+kv = do
498+ k <- T.pack <$> many1 alphaNum
499+ char '='
500+ let vchar = alphaNum <|> try (oneOf "%._/~:,=$@&+-;*" <* lookAhead alphaNum)
501+ v <- T.pack <$> many1 vchar
502+ return (k <> "=" <> v)
503+
484504uri :: T2T (Text, Text)
485505uri = try $ do
486506 address <- t2tURI