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 `_ ---------------------------------------- +Level 2 with an `embedded link `__ +---------------------------------------- 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 `_*. +An *`emphasized link `__*. ***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 `_”. +link `__”. Some dashes: one—two — three—four — five. @@ -690,42 +690,42 @@ Links Explicit -------- -Just a `URL `_. +Just a `URL `__. -`URL and title `_. +`URL and title `__. -`URL and title `_. +`URL and title `__. -`URL and title `_. +`URL and title `__. -`URL and title `_ +`URL and title `__ -`URL and title `_ +`URL and title `__ -`with\_underscore `_ +`with\_underscore `__ -`Email link `_ +`Email link `__ -`Empty <>`_. +`Empty <>`__. Reference --------- -Foo `bar `_. +Foo `bar `__. -Foo `bar `_. +Foo `bar `__. -Foo `bar `_. +Foo `bar `__. -With `embedded [brackets] `_. +With `embedded [brackets] `__. -`b `_ by itself should be a link. +`b `__ by itself should be a link. -Indented `once `_. +Indented `once `__. -Indented `twice `_. +Indented `twice `__. -Indented `thrice `_. +Indented `thrice `__. This should [not][] be a link. @@ -733,21 +733,21 @@ This should [not][] be a link. [not]: /url -Foo `bar `_. +Foo `bar `__. -Foo `biz `_. +Foo `biz `__. With ampersands --------------- Here’s a `link with an ampersand in the -URL `_. +URL `__. -Here’s a link with an amersand in the link text: `AT&T `_. +Here’s a link with an amersand in the link text: `AT&T `__. -Here’s an `inline link `_. +Here’s an `inline link `__. -Here’s an `inline link in pointy braces `_. +Here’s an `inline link in pointy braces `__. 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 `_ and ``]`` verbatim characters, as well as + `links `__ and ``]`` verbatim characters, as well as [bracketed text]. .. [4]