git.iko.soy/typeable/pandoc
on master help sign in
Markdown reader: Added inlinesInBalanced parser combinator to

unify treatment of embedded brackets in links and inline footnotes. Note that the solution adopted here causes one of John Gruber's markdown tests to fail: [with_underscore](/url/with_underscore) Here the whole phrase "underscore](/url/with" is treated as emphasized. The previous version of the markdown reader handled this the way Gruber's script handles it, but it ran into trouble on the following: [link with verbatim `]`](/url) where the inner ] was treated as the end of the reference link label. I don't see any good way to handle both cases in the framework of pandoc, so I choose to require an escape in the first example: [with\_underscore](/url/with_underscore) git-svn-id: https://pandoc.googlecode.com/svn/trunk@729 788f1e2b-df1e-0410-8736-df70ead52e1b

commit7e1370aa874195761aae6e504c66f7111f9a2ccc authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateSun 15 Jul 2007, 23:53 (19 years ago) parent8e71c4c
1 file changed+24−29
src/Text/Pandoc/Readers/Markdown.hs+24−29
src/Text/Pandoc/Readers/Markdown.hs+24−29
@@ -128,6 +128,24 @@ failUnlessSmart = do
128128 state <- getState
129129 if stateSmart state then return () else fail "Smart typography feature"
130130
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+
131149--
132150-- document structure
133151--
@@ -915,22 +933,9 @@ endline = try (do
915933-- links
916934--
917935
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−
925936-- a reference label for a link
926−reference = try $ do
927− raw <- rawLabel
928− oldInput <- getInput
929− setInput raw
930− label <- many inline
931− setInput oldInput
932− return (normalizeSpaces label)
933−
937+reference = inlinesInBalanced [labelStart] [labelEnd] >>= (return . normalizeSpaces)
938+
934939-- source for a link, with optional title
935940source = try $ do
936941 char srcStart
@@ -948,10 +953,9 @@ titleWith startChar endChar = try (do
948953 then fail "title must be separated by space and on same or next line"
949954 else return ()
950955 char startChar
951− tit <- manyTill anyChar (try (do
952− char endChar
953− skipSpaces
954− notFollowedBy (noneOf ")\n")))
956+ tit <- manyTill anyChar (try (do char endChar
957+ skipSpaces
958+ notFollowedBy (noneOf ")\n")))
955959 return $ decodeEntities tit)
956960
957961title = choice [ titleWith '(' ')',
@@ -1011,19 +1015,10 @@ note = try $ do
10111015 Nothing -> fail "note not found"
10121016 Just contents -> return (Note contents)
10131017
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−
10231018inlineNote = try $ do
10241019 failIfStrict
10251020 char noteStart
1026− contents <- inlinesInBrackets
1021+ contents <- inlinesInBalanced "[" "]"
10271022 return (Note [Para contents])
10281023
10291024rawLaTeXInline' = do