@@ -46,7 +46,7 @@ type Refs = [([Inline], Target)]
46data WriterState =
46data WriterState =
47 WriterState { stNotes :: [[Block]]
47 WriterState { stNotes :: [[Block]]
48 , stLinks :: Refs
48 , stLinks :: Refs
49− , stImages :: Refs
49+ , stImages :: [([Inline], (String, String, Maybe String))]
50 , stHasMath :: Bool
50 , stHasMath :: Bool
51 , stOptions :: WriterOptions
51 , stOptions :: WriterOptions
@@ -111,18 +111,23 @@ noteToRST :: Int -> [Block] -> State WriterState Doc
111noteToRST num note = do
111noteToRST num note = do
112 contents <- blockListToRST note
112 contents <- blockListToRST note
113 let marker = ".. [" <> text (show num) <> "]"
113 let marker = ".. [" <> text (show num) <> "]"
114− return $ marker $$ nest 3 contents
114+ return $ nowrap $ marker $$ nest 3 contents
116-- | Return RST representation of picture reference table.
116-- | Return RST representation of picture reference table.
117+pictRefsToRST :: [([Inline], (String, String, Maybe String))]
117−pictRefsToRST :: Refs -> State WriterState Doc
118+ -> State WriterState Doc
118pictRefsToRST refs = mapM pictToRST refs >>= return . vcat
119pictRefsToRST refs = mapM pictToRST refs >>= return . vcat
120-- | Return RST representation of a picture substitution reference.
121-- | Return RST representation of a picture substitution reference.
121−pictToRST :: ([Inline], (String, String))
122+pictToRST :: ([Inline], (String, String,Maybe String))
122 -> State WriterState Doc
123 -> State WriterState Doc
123−pictToRST (label, (src, _)) = do
124+pictToRST (label, (src, _, mbtarget)) = do
124 label' <- inlineListToRST label
125 label' <- inlineListToRST label
125− return $ ".. |" <> label' <> "| image:: " <> text src
127+ $ ".. |" <> label' <> "| image:: " <> text src
130+ Just t -> " :target: " <> text t
127-- | Escape special characters for RST.
132-- | Escape special characters for RST.
128escapeString :: String -> String
133escapeString :: String -> String
@@ -346,6 +351,9 @@ inlineToRST (Link [Code _ str] (src, _)) | src == str ||
346 src == "mailto:" ++ str = do
351 src == "mailto:" ++ str = do
347 let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
352 let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
348 return $ text srcSuffix
353 return $ text srcSuffix
354+inlineToRST (Link [Image alt (imgsrc,imgtit)] (src, _tit)) = do
355+ label <- registerImage alt (imgsrc,imgtit) (Just src)
356+ return $ "|" <> label <> "|"
349inlineToRST (Link txt (src, tit)) = do
357inlineToRST (Link txt (src, tit)) = do
350 useReferenceLinks <- get >>= return . writerReferenceLinks . stOptions
358 useReferenceLinks <- get >>= return . writerReferenceLinks . stOptions
351 linktext <- inlineListToRST $ normalizeSpaces txt
359 linktext <- inlineListToRST $ normalizeSpaces txt
@@ -358,17 +366,7 @@ inlineToRST (Link txt (src, tit)) = do
358 return $ "`" <> linktext <> "`_"
366 return $ "`" <> linktext <> "`_"
359 else return $ "`" <> linktext <> " <" <> text src <> ">`_"
367 else return $ "`" <> linktext <> " <" <> text src <> ">`_"
360inlineToRST (Image alternate (source, tit)) = do
368inlineToRST (Image alternate (source, tit)) = do
361− pics <- get >>= return . stImages
369+ label <- registerImage alternate (source,tit) Nothing
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)]
367− let pics' = if (txt, (source, tit)) `elem` pics
369− else (txt, (source, tit)):pics
370− modify $ \st -> st { stImages = pics' }
371− label <- inlineListToRST txt
372 return $ "|" <> label <> "|"
370 return $ "|" <> label <> "|"
373inlineToRST (Note contents) = do
371inlineToRST (Note contents) = do
374 -- add to notes in state
372 -- add to notes in state
@@ -376,3 +374,16 @@ inlineToRST (Note contents) = do
376 modify $ \st -> st { stNotes = contents:notes }
374 modify $ \st -> st { stNotes = contents:notes }
377 let ref = show $ (length notes) + 1
375 let ref = show $ (length notes) + 1
378 return $ " [" <> text ref <> "]_"
376 return $ " [" <> text ref <> "]_"
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)]
386+ modify $ \st -> st { stImages =
387+ (alt, (src,tit, mbtarget)):stImages st }