From d283f9c864fc4a9ed8ffa4711dda9014b886149b Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Sat, 25 Jun 2016 10:56:37 -0700
Subject: [PATCH] Fixed RST links with no explicit link text.

The link

    `<foo>`_

should have `foo` as both its link text and its URL.

See RST spec at
<http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#embedded-uris-and-aliases>

"The reference text may also be omitted, in which case the URI will be
duplicated for use as the reference text. This is useful for relative
URIs where the address or file name is also the desired reference text:

See `<a_named_relative_link>`_ or `<an_anonymous_relative_link>`__
for details."

Closes Debian #828167 -- reported by Christian Heller.
---
 src/Text/Pandoc/Readers/RST.hs | 5 ++++-
 tests/rst-reader.native        | 1 +
 tests/rst-reader.rst           | 2 ++
 3 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/Text/Pandoc/Readers/RST.hs b/src/Text/Pandoc/Readers/RST.hs
index 296c55f32..60d69638b 100644
--- a/src/Text/Pandoc/Readers/RST.hs
+++ b/src/Text/Pandoc/Readers/RST.hs
@@ -1126,7 +1126,10 @@ explicitLink = try $ do
   skipSpaces
   string "`_"
   optional $ char '_' -- anonymous form
-  return $ B.link (escapeURI $ trim src) "" label'
+  let label'' = if label' == mempty
+                   then B.str src
+                   else label'
+  return $ B.link (escapeURI $ trim src) "" label''
 
 referenceLink :: RSTParser Inlines
 referenceLink = try $ do
diff --git a/tests/rst-reader.native b/tests/rst-reader.native
index 4752d76ff..d44fa5efb 100644
--- a/tests/rst-reader.native
+++ b/tests/rst-reader.native
@@ -211,6 +211,7 @@ Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "John",Spa
 ,Para [Str "Minus:",Space,Str "-"]
 ,Header 1 ("links",[],[]) [Str "Links"]
 ,Para [Str "Explicit:",Space,Str "a",Space,Link ("",[],[]) [Str "URL"] ("/url/",""),Str "."]
+,Para [Str "Explicit",Space,Str "with",Space,Str "no",Space,Str "label:",Space,Link ("",[],[]) [Str "foo"] ("foo",""),Str "."]
 ,Para [Str "Two",Space,Str "anonymous",Space,Str "links:",Space,Link ("",[],[]) [Str "the",Space,Str "first"] ("/url1/",""),Space,Str "and",Space,Link ("",[],[]) [Str "the",Space,Str "second"] ("/url2/","")]
 ,Para [Str "Reference",Space,Str "links:",Space,Link ("",[],[]) [Str "link1"] ("/url1/",""),Space,Str "and",Space,Link ("",[],[]) [Str "link2"] ("/url2/",""),Space,Str "and",Space,Link ("",[],[]) [Str "link1"] ("/url1/",""),Space,Str "again."]
 ,Para [Str "Here\8217s",Space,Str "a",Space,Link ("",[],[]) [Str "link",Space,Str "with",Space,Str "an",Space,Str "ampersand",Space,Str "in",Space,Str "the",Space,Str "URL"] ("http://example.com/?foo=1&bar=2",""),Str "."]
diff --git a/tests/rst-reader.rst b/tests/rst-reader.rst
index ff10abe24..450f2b939 100644
--- a/tests/rst-reader.rst
+++ b/tests/rst-reader.rst
@@ -378,6 +378,8 @@ Links
 
 Explicit:  a `URL </url/>`_.
 
+Explicit with no label:  `<foo>`_.
+
 Two anonymous links:  `the first`__ and `the second`__
 
 __ /url1/