git.iko.soy/typeable/pandoc
on master help sign in
Markdown reader: distinguish autolinks in the AST.

With this change, autolinks are parsed as Links with the `uri` class. (The same is true for bare links, if the `autolink_bare_uris` extension is enabled.) Email autolinks are parsed as Links with the `email` class. This allows the distinction to be represented in the URI. Formerly the `uri` class was added to autolinks by the HTML writer, but it had to guess what was an autolink and could not distinguish `[http://example.com](http://example.com)` from `<http://example.com>`. It also incorrectly recognized `[pandoc](pandoc)` as an autolink. Now the HTML writer simply passes through the `uri` attribute if it is present, but does not add anything. The Textile writer has been modified so that the `uri` class is not explicitly added for autolinks, even if it is present. Closes #4913.

commit136bf901aa088eaf4e5c996c71e0a36c171f1587 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 19 Sep 2018, 14:49 (8 years ago) parentdba5c8d
14 files changed+73−38
src/Text/Pandoc/Readers/Markdown.hs+7−5 src/Text/Pandoc/Writers/HTML.hs+2−5 src/Text/Pandoc/Writers/Textile.hs+3−3 test/Tests/Readers/Markdown.hs+5−3 test/command/3716.md+1−1 test/command/4913.md+34 test/markdown-reader-more.native+4−4 test/testsuite.native+4−4 test/writer.docbook4+3−3 test/writer.docbook5+3−3 test/writer.html4+1−1 test/writer.html5+1−1 test/writer.native+4−4 test/writer.textile+1−1
src/Text/Pandoc/Readers/Markdown.hs+7−5
@@ -1,6 +1,7 @@
11{-# LANGUAGE NoImplicitPrelude #-}
22{-# LANGUAGE RelaxedPolyRec #-}
33{-# LANGUAGE ScopedTypeVariables #-}
4+{-# LANGUAGE TupleSections #-}
45
56{-
67Copyright (C) 2006-2018 John MacFarlane <jgm@berkeley.edu>
@@ -1879,23 +1880,24 @@ bareURL :: PandocMonad m => MarkdownParser m (F Inlines)
18791880bareURL = try $ do
18801881 guardEnabled Ext_autolink_bare_uris
18811882 getState >>= guard . stateAllowLinks
1882− (orig, src) <- uri <|> emailAddress
1883+ (cls, (orig, src)) <- (("uri",) <$> uri) <|> (("email",) <$> emailAddress)
18831884 notFollowedBy $ try $ spaces >> htmlTag (~== TagClose "a")
1884− return $ return $ B.link src "" (B.str orig)
1885+ return $ return $ B.linkWith ("",[cls],[]) src "" (B.str orig)
18851886
18861887autoLink :: PandocMonad m => MarkdownParser m (F Inlines)
18871888autoLink = try $ do
18881889 getState >>= guard . stateAllowLinks
18891890 char '<'
1890− (orig, src) <- uri <|> emailAddress
1891+ (cls, (orig, src)) <- (("uri",) <$> uri) <|> (("email",) <$> emailAddress)
18911892 -- in rare cases, something may remain after the uri parser
18921893 -- is finished, because the uri parser tries to avoid parsing
18931894 -- final punctuation. for example: in `<http://hi---there>`,
18941895 -- the URI parser will stop before the dashes.
18951896 extra <- fromEntities <$> manyTill nonspaceChar (char '>')
1896− attr <- option nullAttr $ try $
1897+ attr <- option ("", [cls], []) $ try $
18971898 guardEnabled Ext_link_attributes >> attributes
1898− return $ return $ B.linkWith attr (src ++ escapeURI extra) "" (B.str $ orig ++ extra)
1899+ return $ return $ B.linkWith attr (src ++ escapeURI extra) ""
1900+ (B.str $ orig ++ extra)
18991901
19001902image :: PandocMonad m => MarkdownParser m (F Inlines)
19011903image = try $ do
src/Text/Pandoc/Writers/HTML.hs+2−5
@@ -56,7 +56,7 @@ import Data.String (fromString)
5656import Data.Text (Text)
5757import qualified Data.Text.Lazy as TL
5858import Network.HTTP (urlEncode)
59−import Network.URI (URI (..), parseURIReference, unEscapeString)
59+import Network.URI (URI (..), parseURIReference)
6060import Numeric (showHex)
6161import Text.Blaze.Internal (customLeaf, customParent, MarkupM(Empty))
6262#if MIN_VERSION_blaze_markup(0,6,3)
@@ -1084,10 +1084,7 @@ inlineToHtml opts inline = do
10841084 in '#' : prefix ++ xs
10851085 _ -> s
10861086 let link = H.a ! A.href (toValue s') $ linkText
1087− let attr = if txt == [Str (unEscapeString s)]
1088− then (ident, "uri" : classes, kvs)
1089− else (ident, classes, kvs)
1090− link' <- addAttrs opts attr link
1087+ link' <- addAttrs opts (ident, classes, kvs) link
10911088 return $ if null tit
10921089 then link'
10931090 else link' ! A.title (toValue tit)
src/Text/Pandoc/Writers/Textile.hs+3−3
@@ -463,15 +463,15 @@ inlineToTextile _ SoftBreak = return " "
463463inlineToTextile _ Space = return " "
464464
465465inlineToTextile opts (Link (_, cls, _) txt (src, _)) = do
466− let classes = if null cls
467− then ""
468− else "(" ++ unwords cls ++ ")"
469466 label <- case txt of
470467 [Code _ s]
471468 | s == src -> return "$"
472469 [Str s]
473470 | s == src -> return "$"
474471 _ -> inlineListToTextile opts txt
472+ let classes = if null cls || cls == ["uri"] && label == "$"
473+ then ""
474+ else "(" ++ unwords cls ++ ")"
475475 return $ "\"" ++ classes ++ label ++ "\":" ++ src
476476
477477inlineToTextile opts (Image attr@(_, cls, _) alt (source, tit)) = do
test/Tests/Readers/Markdown.hs+5−3
@@ -39,7 +39,7 @@ testBareLink (inp, ils) =
3939 (unpack inp) (inp, doc $ para ils)
4040
4141autolink :: String -> Inlines
42−autolink = autolinkWith nullAttr
42+autolink = autolinkWith ("",["uri"],[])
4343
4444autolinkWith :: Attr -> String -> Inlines
4545autolinkWith attr s = linkWith attr s "" (str s)
@@ -72,10 +72,12 @@ bareLinkTests =
7272 , ("http://en.wikipedia.org/wiki/Sprite_(computer_graphics)",
7373 autolink "http://en.wikipedia.org/wiki/Sprite_(computer_graphics)")
7474 , ("http://en.wikipedia.org/wiki/Sprite_[computer_graphics]",
75− link "http://en.wikipedia.org/wiki/Sprite_%5Bcomputer_graphics%5D" ""
75+ linkWith ("",["uri"],[])
76+ "http://en.wikipedia.org/wiki/Sprite_%5Bcomputer_graphics%5D" ""
7677 (str "http://en.wikipedia.org/wiki/Sprite_[computer_graphics]"))
7778 , ("http://en.wikipedia.org/wiki/Sprite_{computer_graphics}",
78− link "http://en.wikipedia.org/wiki/Sprite_%7Bcomputer_graphics%7D" ""
79+ linkWith ("",["uri"],[])
80+ "http://en.wikipedia.org/wiki/Sprite_%7Bcomputer_graphics%7D" ""
7981 (str "http://en.wikipedia.org/wiki/Sprite_{computer_graphics}"))
8082 , ("http://example.com/Notification_Center-GitHub-20101108-140050.jpg",
8183 autolink "http://example.com/Notification_Center-GitHub-20101108-140050.jpg")
test/command/3716.md+1−1
@@ -2,5 +2,5 @@
22% pandoc
33<http://example.com>{.foo}
44^D
5−<p><a href="http://example.com" class="uri foo">http://example.com</a></p>
5+<p><a href="http://example.com" class="foo">http://example.com</a></p>
66```
test/command/4913.mdnew file+34
@@ -0,0 +1,34 @@
1+```
2+% pandoc -f markdown -t html
3+[https://pandoc.org](https://pandoc.org)
4+^D
5+<p><a href="https://pandoc.org">https://pandoc.org</a></p>
6+```
7+
8+```
9+% pandoc -f markdown -t markdown
10+[https://pandoc.org](https://pandoc.org)
11+^D
12+<https://pandoc.org>
13+```
14+
15+```
16+% pandoc -f markdown -t html
17+<https://pandoc.org>
18+^D
19+<p><a href="https://pandoc.org" class="uri">https://pandoc.org</a></p>
20+```
21+
22+```
23+% pandoc -f markdown -t html
24+<https://pandoc.org>{.foo}
25+^D
26+<p><a href="https://pandoc.org" class="foo">https://pandoc.org</a></p>
27+```
28+
29+```
30+% pandoc -f markdown -t html
31+<me@example.com>
32+^D
33+<p><a href="mailto:me@example.com" class="email">me@example.com</a></p>
34+```
test/markdown-reader-more.native+4−4
@@ -45,9 +45,9 @@ Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "Author",S
4545,Para [Str "`hi"]
4646,Para [Str "there`"]
4747,Header 2 ("multilingual-urls",[],[]) [Str "Multilingual",Space,Str "URLs"]
48−,Para [Link ("",[],[]) [Str "http://\27979.com?\27979=\27979"] ("http://\27979.com?\27979=\27979","")]
48+,Para [Link ("",["uri"],[]) [Str "http://\27979.com?\27979=\27979"] ("http://\27979.com?\27979=\27979","")]
4949,Para [Link ("",[],[]) [Str "foo"] ("/bar/\27979?x=\27979","title")]
50−,Para [Link ("",[],[]) [Str "\27979@foo.\27979.baz"] ("mailto:\27979@foo.\27979.baz","")]
50+,Para [Link ("",["email"],[]) [Str "\27979@foo.\27979.baz"] ("mailto:\27979@foo.\27979.baz","")]
5151,Header 2 ("numbered-examples",[],[]) [Str "Numbered",Space,Str "examples"]
5252,OrderedList (1,Example,TwoParens)
5353 [[Plain [Str "First",Space,Str "example."]]
@@ -176,8 +176,8 @@ Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "Author",S
176176 ,[]]]
177177,Header 2 ("entities-in-links-and-titles",[],[]) [Str "Entities",Space,Str "in",Space,Str "links",Space,Str "and",Space,Str "titles"]
178178,Para [Link ("",[],[]) [Str "link"] ("/\252rl","\246\246!")]
179−,Para [Link ("",[],[]) [Str "http://g\246\246gle.com"] ("http://g\246\246gle.com","")]
180−,Para [Link ("",[],[]) [Str "me@ex\228mple.com"] ("mailto:me@ex\228mple.com","")]
179+,Para [Link ("",["uri"],[]) [Str "http://g\246\246gle.com"] ("http://g\246\246gle.com","")]
180+,Para [Link ("",["email"],[]) [Str "me@ex\228mple.com"] ("mailto:me@ex\228mple.com","")]
181181,Para [Link ("",[],[]) [Str "foobar"] ("/\252rl","\246\246!")]
182182,Header 2 ("parentheses-in-urls",[],[]) [Str "Parentheses",Space,Str "in",Space,Str "URLs"]
183183,Para [Link ("",[],[]) [Str "link"] ("/hi(there)","")]
test/testsuite.native+4−4
@@ -384,14 +384,14 @@ Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "John",Spa
384384,Para [Str "Here\8217s",Space,Str "an",Space,Link ("",[],[]) [Str "inline",Space,Str "link"] ("/script?foo=1&bar=2",""),Str "."]
385385,Para [Str "Here\8217s",Space,Str "an",Space,Link ("",[],[]) [Str "inline",Space,Str "link",Space,Str "in",Space,Str "pointy",Space,Str "braces"] ("/script?foo=1&bar=2",""),Str "."]
386386,Header 2 ("autolinks",[],[]) [Str "Autolinks"]
387−,Para [Str "With",Space,Str "an",Space,Str "ampersand:",Space,Link ("",[],[]) [Str "http://example.com/?foo=1&bar=2"] ("http://example.com/?foo=1&bar=2","")]
387+,Para [Str "With",Space,Str "an",Space,Str "ampersand:",Space,Link ("",["uri"],[]) [Str "http://example.com/?foo=1&bar=2"] ("http://example.com/?foo=1&bar=2","")]
388388,BulletList
389389 [[Plain [Str "In",Space,Str "a",Space,Str "list?"]]
390− ,[Plain [Link ("",[],[]) [Str "http://example.com/"] ("http://example.com/","")]]
390+ ,[Plain [Link ("",["uri"],[]) [Str "http://example.com/"] ("http://example.com/","")]]
391391 ,[Plain [Str "It",Space,Str "should."]]]
392−,Para [Str "An",Space,Str "e-mail",Space,Str "address:",Space,Link ("",[],[]) [Str "nobody@nowhere.net"] ("mailto:nobody@nowhere.net","")]
392+,Para [Str "An",Space,Str "e-mail",Space,Str "address:",Space,Link ("",["email"],[]) [Str "nobody@nowhere.net"] ("mailto:nobody@nowhere.net","")]
393393,BlockQuote
394− [Para [Str "Blockquoted:",Space,Link ("",[],[]) [Str "http://example.com/"] ("http://example.com/","")]]
394+ [Para [Str "Blockquoted:",Space,Link ("",["uri"],[]) [Str "http://example.com/"] ("http://example.com/","")]]
395395,Para [Str "Auto-links",Space,Str "should",Space,Str "not",Space,Str "occur",Space,Str "here:",Space,Code ("",[],[]) "<http://example.com/>"]
396396,CodeBlock ("",[],[]) "or here: <http://example.com/>"
397397,HorizontalRule
test/writer.docbook4+3−3
@@ -1298,7 +1298,7 @@ These should not be escaped: \$ \\ \&gt; \[ \{
12981298 <title>Autolinks</title>
12991299 <para>
13001300 With an ampersand:
1301− <ulink url="http://example.com/?foo=1&amp;bar=2">http://example.com/?foo=1&amp;bar=2</ulink>
1301+ <ulink url="http://example.com/?foo=1&amp;bar=2" role="uri">http://example.com/?foo=1&amp;bar=2</ulink>
13021302 </para>
13031303 <itemizedlist spacing="compact">
13041304 <listitem>
@@ -1308,7 +1308,7 @@ These should not be escaped: \$ \\ \&gt; \[ \{
13081308 </listitem>
13091309 <listitem>
13101310 <para>
1311− <ulink url="http://example.com/">http://example.com/</ulink>
1311+ <ulink url="http://example.com/" role="uri">http://example.com/</ulink>
13121312 </para>
13131313 </listitem>
13141314 <listitem>
@@ -1323,7 +1323,7 @@ These should not be escaped: \$ \\ \&gt; \[ \{
13231323 <blockquote>
13241324 <para>
13251325 Blockquoted:
1326− <ulink url="http://example.com/">http://example.com/</ulink>
1326+ <ulink url="http://example.com/" role="uri">http://example.com/</ulink>
13271327 </para>
13281328 </blockquote>
13291329 <para>
test/writer.docbook5+3−3
@@ -1273,7 +1273,7 @@ These should not be escaped: \$ \\ \&gt; \[ \{
12731273 <title>Autolinks</title>
12741274 <para>
12751275 With an ampersand:
1276− <link xlink:href="http://example.com/?foo=1&amp;bar=2">http://example.com/?foo=1&amp;bar=2</link>
1276+ <link xlink:href="http://example.com/?foo=1&amp;bar=2" role="uri">http://example.com/?foo=1&amp;bar=2</link>
12771277 </para>
12781278 <itemizedlist spacing="compact">
12791279 <listitem>
@@ -1283,7 +1283,7 @@ These should not be escaped: \$ \\ \&gt; \[ \{
12831283 </listitem>
12841284 <listitem>
12851285 <para>
1286− <link xlink:href="http://example.com/">http://example.com/</link>
1286+ <link xlink:href="http://example.com/" role="uri">http://example.com/</link>
12871287 </para>
12881288 </listitem>
12891289 <listitem>
@@ -1298,7 +1298,7 @@ These should not be escaped: \$ \\ \&gt; \[ \{
12981298 <blockquote>
12991299 <para>
13001300 Blockquoted:
1301− <link xlink:href="http://example.com/">http://example.com/</link>
1301+ <link xlink:href="http://example.com/" role="uri">http://example.com/</link>
13021302 </para>
13031303 </blockquote>
13041304 <para>
test/writer.html4+1−1
@@ -508,7 +508,7 @@ Blah
508508<li><a href="http://example.com/" class="uri">http://example.com/</a></li>
509509<li>It should.</li>
510510</ul>
511−<p>An e-mail address: <a href="mailto:nobody@nowhere.net">nobody@nowhere.net</a></p>
511+<p>An e-mail address: <a href="mailto:nobody@nowhere.net" class="email">nobody@nowhere.net</a></p>
512512<blockquote>
513513<p>Blockquoted: <a href="http://example.com/" class="uri">http://example.com/</a></p>
514514</blockquote>
test/writer.html5+1−1
@@ -511,7 +511,7 @@ Blah
511511<li><a href="http://example.com/" class="uri">http://example.com/</a></li>
512512<li>It should.</li>
513513</ul>
514−<p>An e-mail address: <a href="mailto:nobody@nowhere.net">nobody@nowhere.net</a></p>
514+<p>An e-mail address: <a href="mailto:nobody@nowhere.net" class="email">nobody@nowhere.net</a></p>
515515<blockquote>
516516<p>Blockquoted: <a href="http://example.com/" class="uri">http://example.com/</a></p>
517517</blockquote>
test/writer.native+4−4
@@ -384,14 +384,14 @@ Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "John",Spa
384384,Para [Str "Here\8217s",Space,Str "an",Space,Link ("",[],[]) [Str "inline",Space,Str "link"] ("/script?foo=1&bar=2",""),Str "."]
385385,Para [Str "Here\8217s",Space,Str "an",Space,Link ("",[],[]) [Str "inline",Space,Str "link",Space,Str "in",Space,Str "pointy",Space,Str "braces"] ("/script?foo=1&bar=2",""),Str "."]
386386,Header 2 ("autolinks",[],[]) [Str "Autolinks"]
387−,Para [Str "With",Space,Str "an",Space,Str "ampersand:",Space,Link ("",[],[]) [Str "http://example.com/?foo=1&bar=2"] ("http://example.com/?foo=1&bar=2","")]
387+,Para [Str "With",Space,Str "an",Space,Str "ampersand:",Space,Link ("",["uri"],[]) [Str "http://example.com/?foo=1&bar=2"] ("http://example.com/?foo=1&bar=2","")]
388388,BulletList
389389 [[Plain [Str "In",Space,Str "a",Space,Str "list?"]]
390− ,[Plain [Link ("",[],[]) [Str "http://example.com/"] ("http://example.com/","")]]
390+ ,[Plain [Link ("",["uri"],[]) [Str "http://example.com/"] ("http://example.com/","")]]
391391 ,[Plain [Str "It",Space,Str "should."]]]
392−,Para [Str "An",Space,Str "e-mail",Space,Str "address:",Space,Link ("",[],[]) [Str "nobody@nowhere.net"] ("mailto:nobody@nowhere.net","")]
392+,Para [Str "An",Space,Str "e-mail",Space,Str "address:",Space,Link ("",["email"],[]) [Str "nobody@nowhere.net"] ("mailto:nobody@nowhere.net","")]
393393,BlockQuote
394− [Para [Str "Blockquoted:",Space,Link ("",[],[]) [Str "http://example.com/"] ("http://example.com/","")]]
394+ [Para [Str "Blockquoted:",Space,Link ("",["uri"],[]) [Str "http://example.com/"] ("http://example.com/","")]]
395395,Para [Str "Auto-links",Space,Str "should",Space,Str "not",Space,Str "occur",Space,Str "here:",Space,Code ("",[],[]) "<http://example.com/>"]
396396,CodeBlock ("",[],[]) "or here: <http://example.com/>"
397397,HorizontalRule
test/writer.textile+1−1
@@ -660,7 +660,7 @@ With an ampersand: "$":http://example.com/?foo=1&bar=2
660660* "$":http://example.com/
661661* It should.
662662
663−An e&#45;mail address: "nobody&#64;nowhere.net":mailto:nobody@nowhere.net
663+An e&#45;mail address: "(email)nobody&#64;nowhere.net":mailto:nobody@nowhere.net
664664
665665bq. Blockquoted: "$":http://example.com/
666666