git.iko.soy/typeable/pandoc
on master help sign in
Changed uri parser so it doesn't include trailing punctuation.

So, in RST, 'http://google.com.' should be parsed as a link to 'http://google.com' followed by a period. The parser is smart enough to recognize balanced parentheses, as often occur in wikipedia links: 'http://foo.bar/baz_(bam)'. Also added ()s to RST specialChars, so '(http://google.com)' will be parsed as a link in parens. Added test cases. Resolves Issue #291.

commit6beba76f61faa41c4351fa4eb54161c060315a23 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 18 Mar 2011, 11:27 (16 years ago) parentd1304e8
4 files changed+29−5
src/Tests/Readers/RST.hs+8 src/Text/Pandoc/Parsing.hs+19−3 src/Text/Pandoc/Readers/RST.hs+1−1 tests/rst-reader.native+1−1
src/Tests/Readers/RST.hs+8
@@ -45,5 +45,13 @@ tests = [ "field list" =:
4545 , (str "Parameter i", [para "integer"])
4646 , (str "Final", [para "item on two lines"])
4747 ])
48+ , "URLs with following punctuation" =:
49+ ("http://google.com, http://yahoo.com; http://foo.bar.baz.\n" ++
50+ "http://foo.bar/baz_(bam) (http://foo.bar)") =?>
51+ para (link "http://google.com" "" "http://google.com" +++ ", " +++
52+ link "http://yahoo.com" "" "http://yahoo.com" +++ "; " +++
53+ link "http://foo.bar.baz" "" "http://foo.bar.baz" +++ ". " +++
54+ link "http://foo.bar/baz_(bam)" "" "http://foo.bar/baz_(bam)"
55+ +++ " (" +++ link "http://foo.bar" "" "http://foo.bar" +++ ")")
4856 ]
4957
src/Text/Pandoc/Parsing.hs+19−3
@@ -78,7 +78,7 @@ import Text.Pandoc.Generic
7878import qualified Text.Pandoc.UTF8 as UTF8 (putStrLn)
7979import Text.ParserCombinators.Parsec
8080import Text.Pandoc.CharacterReferences ( characterReference )
81−import Data.Char ( toLower, toUpper, ord, isAscii, isAlphaNum, isDigit )
81+import Data.Char ( toLower, toUpper, ord, isAscii, isAlphaNum, isDigit, isPunctuation )
8282import Data.List ( intercalate, transpose )
8383import Network.URI ( parseURI, URI (..), isAllowedInURI )
8484import Control.Monad ( join, liftM, guard )
@@ -264,8 +264,24 @@ uri = try $ do
264264 let protocols = [ "http:", "https:", "ftp:", "file:", "mailto:",
265265 "news:", "telnet:" ]
266266 lookAhead $ oneOfStrings protocols
267− -- scan non-ascii characters and ascii characters allowed in a URI
268− str <- many1 $ satisfy (\c -> not (isAscii c) || isAllowedInURI c)
267+ -- Scan non-ascii characters and ascii characters allowed in a URI.
268+ -- We allow punctuation except when followed by a space, since
269+ -- we don't want the trailing '.' in 'http://google.com.'
270+ let innerPunct = try $ satisfy isPunctuation >>~
271+ notFollowedBy (newline <|> spaceChar)
272+ let uriChar = innerPunct <|>
273+ satisfy (\c -> not (isPunctuation c) &&
274+ (not (isAscii c) || isAllowedInURI c))
275+ -- We want to allow
276+ -- http://en.wikipedia.org/wiki/State_of_emergency_(disambiguation)
277+ -- as a URL, while NOT picking up the closing paren in
278+ -- (http://wikipedia.org)
279+ -- So we include balanced parens in the URL.
280+ let inParens = try $ do char '('
281+ res <- many uriChar
282+ char ')'
283+ return $ '(' : res ++ ")"
284+ str <- liftM concat $ many1 $ inParens <|> count 1 (innerPunct <|> uriChar)
269285 -- now see if they amount to an absolute URI
270286 case parseURI (escapeURI str) of
271287 Just uri' -> if uriScheme uri' `elem` protocols
src/Text/Pandoc/Readers/RST.hs+1−1
@@ -58,7 +58,7 @@ underlineChars = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
5858
5959-- treat these as potentially non-text when parsing inline:
6060specialChars :: [Char]
61−specialChars = "\\`|*_<>$:[-.\"'\8216\8217\8220\8221"
61+specialChars = "\\`|*_<>$:[]()-.\"'\8216\8217\8220\8221"
6262
6363--
6464-- parsing documents
tests/rst-reader.native+1−1
@@ -222,7 +222,7 @@ Pandoc (Meta {docTitle = [Str "Pandoc",Space,Str "Test",Space,Str "Suite",Str ":
222222,Para [Str "But",Space,Str "not",Space,Str "here",Str ":"]
223223,CodeBlock ("",[],[]) "http://example.com/"
224224,Header 1 [Str "Images"]
225−,Para [Str "From",Space,Quoted DoubleQuote [Str "Voyage",Space,Str "dans",Space,Str "la",Space,Str "Lune"],Space,Str "by",Space,Str "Georges",Space,Str "Melies",Space,Str "(1902)",Str ":"]
225+,Para [Str "From",Space,Quoted DoubleQuote [Str "Voyage",Space,Str "dans",Space,Str "la",Space,Str "Lune"],Space,Str "by",Space,Str "Georges",Space,Str "Melies",Space,Str "(",Str "1902",Str ")",Str ":"]
226226,Plain [Image [Str "image"] ("lalune.jpg","")]
227227,Plain [Image [Str "Voyage dans la Lune"] ("lalune.jpg","")]
228228,Para [Str "Here",Space,Str "is",Space,Str "a",Space,Str "movie",Space,Image [Str "movie"] ("movie.jpg",""),Space,Str "icon",Str "."]