@@ -46,7 +46,7 @@ type Refs = [([Inline], Target)]
4646data WriterState =
4747 WriterState { stNotes :: [[Block]]
4848 , stLinks :: Refs
49− , stImages :: Refs
49+ , stImages :: [([Inline], (String, String, Maybe String))]
5050 , stHasMath :: Bool
5151 , stOptions :: WriterOptions
5252 }
@@ -111,18 +111,23 @@ noteToRST :: Int -> [Block] -> State WriterState Doc
111111noteToRST num note = do
112112 contents <- blockListToRST note
113113 let marker = ".. [" <> text (show num) <> "]"
114− return $ marker $$ nest 3 contents
114+ return $ nowrap $ marker $$ nest 3 contents
115115
116116-- | Return RST representation of picture reference table.
117−pictRefsToRST :: Refs -> State WriterState Doc
117+pictRefsToRST :: [([Inline], (String, String, Maybe String))]
118+ -> State WriterState Doc
118119pictRefsToRST refs = mapM pictToRST refs >>= return . vcat
119120
120121-- | Return RST representation of a picture substitution reference.
121−pictToRST :: ([Inline], (String, String))
122+pictToRST :: ([Inline], (String, String,Maybe String))
122123 -> State WriterState Doc
123−pictToRST (label, (src, _)) = do
124+pictToRST (label, (src, _, mbtarget)) = do
124125 label' <- inlineListToRST label
125− return $ ".. |" <> label' <> "| image:: " <> text src
126+ return $ nowrap
127+ $ ".. |" <> label' <> "| image:: " <> text src
128+ $$ case mbtarget of
129+ Nothing -> empty
130+ Just t -> " :target: " <> text t
126131
127132-- | Escape special characters for RST.
128133escapeString :: String -> String
@@ -346,6 +351,9 @@ inlineToRST (Link [Code _ str] (src, _)) | src == str ||
346351 src == "mailto:" ++ str = do
347352 let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
348353 return $ text srcSuffix
354+inlineToRST (Link [Image alt (imgsrc,imgtit)] (src, _tit)) = do
355+ label <- registerImage alt (imgsrc,imgtit) (Just src)
356+ return $ "|" <> label <> "|"
349357inlineToRST (Link txt (src, tit)) = do
350358 useReferenceLinks <- get >>= return . writerReferenceLinks . stOptions
351359 linktext <- inlineListToRST $ normalizeSpaces txt
@@ -358,17 +366,7 @@ inlineToRST (Link txt (src, tit)) = do
358366 return $ "`" <> linktext <> "`_"
359367 else return $ "`" <> linktext <> " <" <> text src <> ">`_"
360368inlineToRST (Image alternate (source, tit)) = do
361− pics <- get >>= return . stImages
362− let labelsUsed = map fst pics
363− let txt = if null alternate || alternate == [Str ""] ||
364− alternate `elem` labelsUsed
365− then [Str $ "image" ++ show (length pics)]
366− else alternate
367− let pics' = if (txt, (source, tit)) `elem` pics
368− then pics
369− else (txt, (source, tit)):pics
370− modify $ \st -> st { stImages = pics' }
371− label <- inlineListToRST txt
369+ label <- registerImage alternate (source,tit) Nothing
372370 return $ "|" <> label <> "|"
373371inlineToRST (Note contents) = do
374372 -- add to notes in state
@@ -376,3 +374,16 @@ inlineToRST (Note contents) = do
376374 modify $ \st -> st { stNotes = contents:notes }
377375 let ref = show $ (length notes) + 1
378376 return $ " [" <> text ref <> "]_"
377+
378+registerImage :: [Inline] -> Target -> Maybe String -> State WriterState Doc
379+registerImage alt (src,tit) mbtarget = do
380+ pics <- get >>= return . stImages
381+ txt <- case lookup alt pics of
382+ Just (s,t,mbt) | (s,t,mbt) == (src,tit,mbtarget) -> return alt
383+ _ | null alt || alt == [Str ""] -> return
384+ [Str $ "image" ++ show (length pics)]
385+ | otherwise -> do
386+ modify $ \st -> st { stImages =
387+ (alt, (src,tit, mbtarget)):stImages st }
388+ return alt
389+ inlineListToRST txt