From f268ae30350e3d94e57e58e5a543d3292d204b83 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 24 Apr 2020 16:54:52 -0700 Subject: RST writer: properly handle images with same alt text. Previously we created duplicate references for these in rendering RST. Closes #6194. --- src/Text/Pandoc/Writers/RST.hs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/Text/Pandoc/Writers/RST.hs b/src/Text/Pandoc/Writers/RST.hs index a390cc6cf..67603728b 100644 --- a/src/Text/Pandoc/Writers/RST.hs +++ b/src/Text/Pandoc/Writers/RST.hs @@ -42,6 +42,7 @@ data WriterState = , stHasRawTeX :: Bool , stOptions :: WriterOptions , stTopLevel :: Bool + , stImageId :: Int } type RST = StateT WriterState @@ -52,7 +53,7 @@ writeRST opts document = do let st = WriterState { stNotes = [], stLinks = [], stImages = [], stHasMath = False, stHasRawTeX = False, stOptions = opts, - stTopLevel = True } + stTopLevel = True, stImageId = 1 } evalStateT (pandocToRST document) st -- | 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 attr alt (src,tit) mbtarget = do 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 - Just (a,s,t,mbt) | (a,s,t,mbt) == (attr,src,tit,mbtarget) - -> return alt - _ -> do - let alt' = if null alt || alt == [Str ""] - then [Str $ "image" <> tshow (length pics)] - else alt + Just (a,s,t,mbt) -> + if (a,s,t,mbt) == (attr,src,tit,mbtarget) + then return alt + else do + alt' <- getImageName + 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 = (alt', (attr,src,tit, mbtarget)):stImages st } return alt' -- cgit v1.2.3