git.iko.soy/typeable/pandoc
on master help sign in
Markdown reader: Added inlinesInBalanced parser combinator to
7e1370afiddlosopher
src/Text/Pandoc/Readers/Markdown.hs+24−29
@@ -128,6 +128,24 @@ failUnlessSmart = do
128 state <- getState
128 state <- getState
129 if stateSmart state then return () else fail "Smart typography feature"
129 if stateSmart state then return () else fail "Smart typography feature"
130
130
131+-- | Parse a sequence of inline elements between a string
132+-- @opener@ and a string @closer@, including inlines
133+-- between balanced pairs of @opener@ and a @closer@.
134+inlinesInBalanced :: String -> String -> GenParser Char ParserState [Inline]
135+inlinesInBalanced opener closer = try $ do
136+ let nonOpenerSymbol = try $ do -- succeeds if next inline would be Str opener
137+ res <- inline -- fails if next inline merely begins with opener
138+ if res == Str opener
139+ then pzero
140+ else return ' '
141+ try (string opener)
142+ result <- manyTill ( (do notFollowedBy nonOpenerSymbol
143+ bal <- inlinesInBalanced opener closer
144+ return $ [Str opener] ++ bal ++ [Str closer])
145+ <|> (count 1 inline))
146+ (try (string closer))
147+ return $ concat result
148+
131--
149--
132-- document structure
150-- document structure
133--
151--
@@ -915,22 +933,9 @@ endline = try (do
915-- links
933-- links
916--
934--
917
935
918−rawLabel = try $ do
919− char labelStart
920− -- allow for embedded brackets:
921− raw <- manyTill (do{res <- rawLabel; return ("[" ++ res ++ "]")} <|>
922− count 1 anyChar) (char labelEnd)
923− return $ concat raw
924−
925-- a reference label for a link
936-- a reference label for a link
926−reference = try $ do
937+reference = inlinesInBalanced [labelStart] [labelEnd] >>= (return . normalizeSpaces)
927− raw <- rawLabel
938+
928− oldInput <- getInput
929− setInput raw
930− label <- many inline
931− setInput oldInput
932− return (normalizeSpaces label)
933−
934-- source for a link, with optional title
939-- source for a link, with optional title
935source = try $ do
940source = try $ do
936 char srcStart
941 char srcStart
@@ -948,10 +953,9 @@ titleWith startChar endChar = try (do
948 then fail "title must be separated by space and on same or next line"
953 then fail "title must be separated by space and on same or next line"
949 else return ()
954 else return ()
950 char startChar
955 char startChar
951− tit <- manyTill anyChar (try (do
956+ tit <- manyTill anyChar (try (do char endChar
952− char endChar
953− skipSpaces
957+ skipSpaces
954− notFollowedBy (noneOf ")\n")))
958+ notFollowedBy (noneOf ")\n")))
955 return $ decodeEntities tit)
959 return $ decodeEntities tit)
956
960
957title = choice [ titleWith '(' ')',
961title = choice [ titleWith '(' ')',
@@ -1011,19 +1015,10 @@ note = try $ do
1011 Nothing -> fail "note not found"
1015 Nothing -> fail "note not found"
1012 Just contents -> return (Note contents)
1016 Just contents -> return (Note contents)
1013
1017
1014−inlinesInBrackets = try $ do
1015− char '['
1016− results <- many $ count 1 (choice [link, referenceLink, image]) <|>
1017− try (do{res <- inlinesInBrackets; return
1018− ([Str "["] ++ res ++ [Str "]"])}) <|>
1019− (do{notFollowedBy (char ']'); count 1 inline})
1020− char ']'
1021− return $ concat results
1022−
1023inlineNote = try $ do
1018inlineNote = try $ do
1024 failIfStrict
1019 failIfStrict
1025 char noteStart
1020 char noteStart
1026− contents <- inlinesInBrackets
1021+ contents <- inlinesInBalanced "[" "]"
1027 return (Note [Para contents])
1022 return (Note [Para contents])
1028
1023
1029rawLaTeXInline' = do
1024rawLaTeXInline' = do