diff options
author | John MacFarlane <fiddlosopher@gmail.com> | 2013-01-13 23:00:05 -0800 |
---|---|---|
committer | John MacFarlane <fiddlosopher@gmail.com> | 2013-01-13 23:00:05 -0800 |
commit | 45895b561f36afc0bdb2a04765f638e91a199635 (patch) | |
tree | 275f63d991c399ad1f90f767542ea1bd2daa6136 | |
parent | f191aa4a986d2f60a73df28d343c9848a5c38c64 (diff) | |
download | pandoc-45895b561f36afc0bdb2a04765f638e91a199635.tar.gz |
RST writer: Fixes bug with links with duplicate text.
We now (a) use anonymous links for links with inline URLs, and
(b) use an inline link instead of a reference link if the
reference link would require a label that has already been
used for a different link.
Closes #511.
-rw-r--r-- | src/Text/Pandoc/Writers/RST.hs | 16 | ||||
-rw-r--r-- | tests/writer.rst | 56 |
2 files changed, 38 insertions, 34 deletions
diff --git a/src/Text/Pandoc/Writers/RST.hs b/src/Text/Pandoc/Writers/RST.hs index 2ec2ef127..f3815011f 100644 --- a/src/Text/Pandoc/Writers/RST.hs +++ b/src/Text/Pandoc/Writers/RST.hs @@ -367,12 +367,16 @@ inlineToRST (Link txt (src, tit)) = do linktext <- inlineListToRST $ normalizeSpaces txt if useReferenceLinks then do refs <- get >>= return . stLinks - let refs' = if (txt, (src, tit)) `elem` refs - then refs - else (txt, (src, tit)):refs - modify $ \st -> st { stLinks = refs' } - return $ "`" <> linktext <> "`_" - else return $ "`" <> linktext <> " <" <> text src <> ">`_" + case lookup txt refs of + Just (src',tit') -> + if src == src' && tit == tit' + then return $ "`" <> linktext <> "`_" + else do -- duplicate label, use non-reference link + return $ "`" <> linktext <> " <" <> text src <> ">`__" + Nothing -> do + modify $ \st -> st { stLinks = (txt,(src,tit)):refs } + return $ "`" <> linktext <> "`_" + else return $ "`" <> linktext <> " <" <> text src <> ">`__" inlineToRST (Image alternate (source, tit)) = do label <- registerImage alternate (source,tit) Nothing return $ "|" <> label <> "|" diff --git a/tests/writer.rst b/tests/writer.rst index 399600ac0..41da5bc73 100644 --- a/tests/writer.rst +++ b/tests/writer.rst @@ -18,8 +18,8 @@ markdown test suite. Headers ======= -Level 2 with an `embedded link </url>`_ ---------------------------------------- +Level 2 with an `embedded link </url>`__ +---------------------------------------- Level 3 with *emphasis* ~~~~~~~~~~~~~~~~~~~~~~~ @@ -549,7 +549,7 @@ This is *emphasized*, and so *is this*. This is **strong**, and so **is this**. -An *`emphasized link </url>`_*. +An *`emphasized link </url>`__*. ***This is strong and em.*** @@ -584,7 +584,7 @@ Smart quotes, ellipses, dashes ‘He said, “I want to go.”’ Were you alive in the 70’s? Here is some quoted ‘``code``’ and a “`quoted -link <http://example.com/?foo=1&bar=2>`_”. +link <http://example.com/?foo=1&bar=2>`__”. Some dashes: one—two — three—four — five. @@ -690,42 +690,42 @@ Links Explicit -------- -Just a `URL </url/>`_. +Just a `URL </url/>`__. -`URL and title </url/>`_. +`URL and title </url/>`__. -`URL and title </url/>`_. +`URL and title </url/>`__. -`URL and title </url/>`_. +`URL and title </url/>`__. -`URL and title </url/>`_ +`URL and title </url/>`__ -`URL and title </url/>`_ +`URL and title </url/>`__ -`with\_underscore </url/with_underscore>`_ +`with\_underscore </url/with_underscore>`__ -`Email link <mailto:nobody@nowhere.net>`_ +`Email link <mailto:nobody@nowhere.net>`__ -`Empty <>`_. +`Empty <>`__. Reference --------- -Foo `bar </url/>`_. +Foo `bar </url/>`__. -Foo `bar </url/>`_. +Foo `bar </url/>`__. -Foo `bar </url/>`_. +Foo `bar </url/>`__. -With `embedded [brackets] </url/>`_. +With `embedded [brackets] </url/>`__. -`b </url/>`_ by itself should be a link. +`b </url/>`__ by itself should be a link. -Indented `once </url>`_. +Indented `once </url>`__. -Indented `twice </url>`_. +Indented `twice </url>`__. -Indented `thrice </url>`_. +Indented `thrice </url>`__. This should [not][] be a link. @@ -733,21 +733,21 @@ This should [not][] be a link. [not]: /url -Foo `bar </url/>`_. +Foo `bar </url/>`__. -Foo `biz </url/>`_. +Foo `biz </url/>`__. With ampersands --------------- Here’s a `link with an ampersand in the -URL <http://example.com/?foo=1&bar=2>`_. +URL <http://example.com/?foo=1&bar=2>`__. -Here’s a link with an amersand in the link text: `AT&T <http://att.com/>`_. +Here’s a link with an amersand in the link text: `AT&T <http://att.com/>`__. -Here’s an `inline link </script?foo=1&bar=2>`_. +Here’s an `inline link </script?foo=1&bar=2>`__. -Here’s an `inline link in pointy braces </script?foo=1&bar=2>`_. +Here’s an `inline link in pointy braces </script?foo=1&bar=2>`__. Autolinks --------- @@ -815,7 +815,7 @@ This paragraph should not be part of the note, as it is not indented. .. [3] This is *easier* to type. Inline notes may contain - `links <http://google.com>`_ and ``]`` verbatim characters, as well as + `links <http://google.com>`__ and ``]`` verbatim characters, as well as [bracketed text]. .. [4] |