git.iko.soy/typeable/pandoc
on master help sign in
RST writer: properly handle images with same alt text.

Previously we created duplicate references for these in rendering RST. Closes #6194.

commitf268ae30350e3d94e57e58e5a543d3292d204b83 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 24 Apr 2020, 16:54 (7 years ago) parent3e52c40
2 files changed+32−7
src/Text/Pandoc/Writers/RST.hs+18−7 test/command/6194.md+14
src/Text/Pandoc/Writers/RST.hs+18−7
@@ -42,6 +42,7 @@ data WriterState =
4242 , stHasRawTeX :: Bool
4343 , stOptions :: WriterOptions
4444 , stTopLevel :: Bool
45+ , stImageId :: Int
4546 }
4647
4748type RST = StateT WriterState
@@ -52,7 +53,7 @@ writeRST opts document = do
5253 let st = WriterState { stNotes = [], stLinks = [],
5354 stImages = [], stHasMath = False,
5455 stHasRawTeX = False, stOptions = opts,
55− stTopLevel = True }
56+ stTopLevel = True, stImageId = 1 }
5657 evalStateT (pandocToRST document) st
5758
5859-- | Return RST representation of document.
@@ -687,13 +688,23 @@ inlineToRST (Note contents) = do
687688registerImage :: PandocMonad m => Attr -> [Inline] -> Target -> Maybe Text -> RST m (Doc Text)
688689registerImage attr alt (src,tit) mbtarget = do
689690 pics <- gets stImages
691+ imgId <- gets stImageId
692+ let getImageName = do
693+ modify $ \st -> st{ stImageId = imgId + 1 }
694+ return [Str ("image" <> tshow imgId)]
690695 txt <- case lookup alt pics of
691− Just (a,s,t,mbt) | (a,s,t,mbt) == (attr,src,tit,mbtarget)
692− -> return alt
693− _ -> do
694− let alt' = if null alt || alt == [Str ""]
695− then [Str $ "image" <> tshow (length pics)]
696− else alt
696+ Just (a,s,t,mbt) ->
697+ if (a,s,t,mbt) == (attr,src,tit,mbtarget)
698+ then return alt
699+ else do
700+ alt' <- getImageName
701+ modify $ \st -> st { stImages =
702+ (alt', (attr,src,tit, mbtarget)):stImages st }
703+ return alt'
704+ Nothing -> do
705+ alt' <- if null alt || alt == [Str ""]
706+ then getImageName
707+ else return alt
697708 modify $ \st -> st { stImages =
698709 (alt', (attr,src,tit, mbtarget)):stImages st }
699710 return alt'
test/command/6194.mdnew file+14
@@ -0,0 +1,14 @@
1+```
2+% pandoc -f markdown -t rst
3+image1: ![foo](x.png)
4+
5+image2: ![foo](y.png)
6+^D
7+image1: |foo|
8+
9+image2: |image1|
10+
11+.. |foo| image:: x.png
12+.. |image1| image:: y.png
13+```
14+