@@ -39,7 +39,8 @@ module Text.Pandoc.ParserCombinators (
3939 enclosed,
4040 stringAnyCase,
4141 parseFromStr,
42− lineClump
42+ lineClump,
43+ charsInBalanced
4344 ) where
4445import Text.ParserCombinators.Parsec
4546import Data.Char ( toUpper, toLower )
@@ -122,3 +123,18 @@ lineClump = do
122123 blanks <- blanklines <|> (do{eof; return "\n"})
123124 return ((unlines lines) ++ blanks)
124125
126+-- | Parse a string of characters between an open character
127+-- and a close character, including text between balanced
128+-- pairs of open and close. For example,
129+-- @charsInBalanced '(' ')'@ will parse "(hello (there))"
130+-- and return "hello (there)".
131+charsInBalanced :: Char -> Char -> GenParser Char st String
132+charsInBalanced open close = try $ do
133+ char open
134+ raw <- manyTill ( (do res <- charsInBalanced open close
135+ return $ [open] ++ res ++ [close])
136+ <|> (do notFollowedBy' (blankline >> blanklines)
137+ count 1 anyChar))
138+ (char close)
139+ return $ concat raw
140+