Move manyUntil to Text.Pandoc.Parsing and use it in Txt2Tags reader

This commit is contained in:
Alexander Krotov 2018-02-19 19:23:30 +03:00
parent 574104861f
commit 5a9d7d20dd
4 changed files with 20 additions and 17 deletions

View file

@ -40,6 +40,7 @@ module Text.Pandoc.Parsing ( takeWhileP,
anyLineNewline,
indentWith,
many1Till,
manyUntil,
notFollowedBy',
oneOfStrings,
oneOfStringsCI,
@ -325,6 +326,20 @@ many1Till p end = do
rest <- manyTill p end
return (first:rest)
-- | Like @manyTill@, but also returns the result of end parser.
manyUntil :: (Stream s m t)
=> ParserT s u m a
-> ParserT s u m b
-> ParserT s u m ([a], b)
manyUntil p end = scan
where scan =
(do e <- end
return ([], e)
) <|>
(do x <- p
(xs, e) <- scan
return (x:xs, e))
-- | A more general form of @notFollowedBy@. This one allows any
-- type of parser to be specified, and succeeds only if that parser fails.
-- It does not consume any input.

View file

@ -189,20 +189,6 @@ atStart p = do
guard $ museLastStrPos st /= Just pos
p
-- Like manyTill, but also returns result of end parser
manyUntil :: (Stream s m t)
=> ParserT s u m a
-> ParserT s u m b
-> ParserT s u m ([a], b)
manyUntil p end = scan
where scan =
(do e <- end
return ([], e)
) <|>
(do x <- p
(xs, e) <- scan
return (x:xs, e))
someUntil :: (Stream s m t)
=> ParserT s u m a
-> ParserT s u m b

View file

@ -529,8 +529,7 @@ image = try $ do
-- List taken from txt2tags source
let extensions = [".jpg", ".jpeg", ".gif", ".png", ".eps", ".bmp"]
char '['
path <- manyTill (noneOf "\n\t\r ") (try $ lookAhead (oneOfStrings extensions))
ext <- oneOfStrings extensions
(path, ext) <- manyUntil (noneOf "\n\t\r ") (oneOfStrings extensions)
char ']'
return $ B.image (path ++ ext) "" mempty

View file

@ -96,9 +96,12 @@ tests =
, "Autolink" =:
"http://www.google.com" =?>
para (link "http://www.google.com" "" (str "http://www.google.com"))
, "Image" =:
, "JPEG Image" =:
"[image.jpg]" =?>
para (image "image.jpg" "" mempty)
, "PNG Image" =:
"[image.png]" =?>
para (image "image.png" "" mempty)
, "Link" =:
"[title http://google.com]" =?>