git.iko.soy/typeable/pandoc
on master help sign in
Modified markdown reader to disallow links within links. (Resolves Issue #35.)

+ Replaced inlinesInBalanced with inlinesInBalancedBrackets, which instead of hard-coding the inline parser takes an inline parser as a parameter. + Modified reference and inlineNote to use inlinesInBalancedBrackets. + Removed unneeded inlineString function. + Added inlineNonLink parser, which is now used in the definition of reference. + Added inlineParsers list and redefined inline and inlineNonLink parsers in terms of it. + Added failIfLink parser. git-svn-id: https://pandoc.googlecode.com/svn/trunk@1155 788f1e2b-df1e-0410-8736-df70ead52e1b

commitee6f06ec05a405795aa80135a7eb3a012fe7ea27 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateMon 24 Dec 2007, 04:22 (19 years ago) parent97992e6
1 file changed+29−22
Text/Pandoc/Readers/Markdown.hs+29−22
Text/Pandoc/Readers/Markdown.hs+29−22
@@ -89,23 +89,20 @@ failUnlessSmart = do
8989 state <- getState
9090 if stateSmart state then return () else fail "Smart typography feature"
9191
92−-- | Parse an inline Str element with a given content.
93−inlineString str = try $ do
94− (Str res) <- inline
95− if res == str then return res else fail $ "unexpected Str content"
96−
97−-- | Parse a sequence of inline elements between a string
98−-- @opener@ and a string @closer@, including inlines
99−-- between balanced pairs of @opener@ and a @closer@.
100−inlinesInBalanced :: String -> String -> GenParser Char ParserState [Inline]
101−inlinesInBalanced opener closer = try $ do
102− string opener
103− result <- manyTill ( (do lookAhead (inlineString opener)
104− -- because it might be a link...
105− bal <- inlinesInBalanced opener closer
106− return $ [Str opener] ++ bal ++ [Str closer])
107− <|> (count 1 inline))
108− (try (string closer))
92+-- | Parse a sequence of inline elements between square brackets,
93+-- including inlines between balanced pairs of square brackets.
94+inlinesInBalancedBrackets :: GenParser Char ParserState Inline
95+ -> GenParser Char ParserState [Inline]
96+inlinesInBalancedBrackets parser = try $ do
97+ char '['
98+ result <- manyTill ( (do lookAhead $ try $ do (Str res) <- parser
99+ if res == "["
100+ then return ()
101+ else pzero
102+ bal <- inlinesInBalancedBrackets parser
103+ return $ [Str "["] ++ bal ++ [Str "]"])
104+ <|> (count 1 parser))
105+ (char ']')
109106 return $ concat result
110107
111108--
@@ -638,7 +635,9 @@ table = failIfStrict >> (simpleTable <|> multilineTable) <?> "table"
638635-- inline
639636--
640637
641−inline = choice [ str
638+inline = choice inlineParsers <?> "inline"
639+
640+inlineParsers = [ str
642641 , smartPunctuation
643642 , whitespace
644643 , endline
@@ -659,7 +658,14 @@ inline = choice [ str
659658 , rawLaTeXInline'
660659 , escapedChar
661660 , symbol
662− , ltSign ] <?> "inline"
661+ , ltSign ]
662+
663+inlineNonLink = (choice $
664+ map (\parser -> try (parser >>= failIfLink)) inlineParsers)
665+ <?> "inline (non-link)"
666+
667+failIfLink (Link _ _) = pzero
668+failIfLink elt = return elt
663669
664670escapedChar = do
665671 char '\\'
@@ -820,8 +826,9 @@ endline = try $ do
820826--
821827
822828-- a reference label for a link
823−reference = notFollowedBy' (string "[^") >> -- footnote reference
824− inlinesInBalanced "[" "]" >>= (return . normalizeSpaces)
829+reference = do notFollowedBy' (string "[^") -- footnote reference
830+ result <- inlinesInBalancedBrackets inlineNonLink
831+ return $ normalizeSpaces result
825832
826833-- source for a link, with optional title
827834source = try $ do
@@ -887,7 +894,7 @@ note = try $ do
887894inlineNote = try $ do
888895 failIfStrict
889896 char '^'
890− contents <- inlinesInBalanced "[" "]"
897+ contents <- inlinesInBalancedBrackets inline
891898 return $ Note [Para contents]
892899
893900rawLaTeXInline' = failIfStrict >> rawLaTeXInline