RST writer: properly handle images with same alt text.

Previously we created duplicate references for these
in rendering RST.  Closes #6194.
This commit is contained in:
John MacFarlane 2020-04-24 16:54:52 -07:00
parent 3e52c402d0
commit f268ae3035
2 changed files with 32 additions and 7 deletions

View file

@ -42,6 +42,7 @@ data WriterState =
, stHasRawTeX :: Bool , stHasRawTeX :: Bool
, stOptions :: WriterOptions , stOptions :: WriterOptions
, stTopLevel :: Bool , stTopLevel :: Bool
, stImageId :: Int
} }
type RST = StateT WriterState type RST = StateT WriterState
@ -52,7 +53,7 @@ writeRST opts document = do
let st = WriterState { stNotes = [], stLinks = [], let st = WriterState { stNotes = [], stLinks = [],
stImages = [], stHasMath = False, stImages = [], stHasMath = False,
stHasRawTeX = False, stOptions = opts, stHasRawTeX = False, stOptions = opts,
stTopLevel = True } stTopLevel = True, stImageId = 1 }
evalStateT (pandocToRST document) st evalStateT (pandocToRST document) st
-- | Return RST representation of document. -- | Return RST representation of document.
@ -687,13 +688,23 @@ inlineToRST (Note contents) = do
registerImage :: PandocMonad m => Attr -> [Inline] -> Target -> Maybe Text -> RST m (Doc Text) registerImage :: PandocMonad m => Attr -> [Inline] -> Target -> Maybe Text -> RST m (Doc Text)
registerImage attr alt (src,tit) mbtarget = do registerImage attr alt (src,tit) mbtarget = do
pics <- gets stImages pics <- gets stImages
imgId <- gets stImageId
let getImageName = do
modify $ \st -> st{ stImageId = imgId + 1 }
return [Str ("image" <> tshow imgId)]
txt <- case lookup alt pics of txt <- case lookup alt pics of
Just (a,s,t,mbt) | (a,s,t,mbt) == (attr,src,tit,mbtarget) Just (a,s,t,mbt) ->
-> return alt if (a,s,t,mbt) == (attr,src,tit,mbtarget)
_ -> do then return alt
let alt' = if null alt || alt == [Str ""] else do
then [Str $ "image" <> tshow (length pics)] alt' <- getImageName
else alt modify $ \st -> st { stImages =
(alt', (attr,src,tit, mbtarget)):stImages st }
return alt'
Nothing -> do
alt' <- if null alt || alt == [Str ""]
then getImageName
else return alt
modify $ \st -> st { stImages = modify $ \st -> st { stImages =
(alt', (attr,src,tit, mbtarget)):stImages st } (alt', (attr,src,tit, mbtarget)):stImages st }
return alt' return alt'

14
test/command/6194.md Normal file
View file

@ -0,0 +1,14 @@
```
% pandoc -f markdown -t rst
image1: ![foo](x.png)
image2: ![foo](y.png)
^D
image1: |foo|
image2: |image1|
.. |foo| image:: x.png
.. |image1| image:: y.png
```