git.iko.soy/typeable/pandoc
on master help sign in
RST reader: Support :target: on image substitutions.
731415aJohn MacFarlane
src/Text/Pandoc/Readers/RST.hs+26−26
@@ -39,6 +39,7 @@ import Data.List ( findIndex, intercalate, transpose, sort, deleteFirstsBy )
39import qualified Data.Map as M
39import qualified Data.Map as M
40import Text.Printf ( printf )
40import Text.Printf ( printf )
41import Data.Maybe ( catMaybes )
41import Data.Maybe ( catMaybes )
42+import Control.Applicative ((<$>))
42
43
43-- | Parse reStructuredText string and return Pandoc document.
44-- | Parse reStructuredText string and return Pandoc document.
44readRST :: ReaderOptions -- ^ Reader options
45readRST :: ReaderOptions -- ^ Reader options
@@ -95,8 +96,8 @@ parseRST = do
95 startPos <- getPosition
96 startPos <- getPosition
96 -- go through once just to get list of reference keys and notes
97 -- go through once just to get list of reference keys and notes
97 -- docMinusKeys is the raw document with blanks where the keys were...
98 -- docMinusKeys is the raw document with blanks where the keys were...
98− docMinusKeys <- manyTill (referenceKey <|> imageKey <|>
99+ docMinusKeys <- concat <$>
99− noteBlock <|> lineClump) eof >>= return . concat
100+ manyTill (referenceKey <|> noteBlock <|> lineClump) eof
100 setInput docMinusKeys
101 setInput docMinusKeys
101 setPosition startPos
102 setPosition startPos
102 st' <- getState
103 st' <- getState
@@ -252,18 +253,25 @@ plain = many1 inline >>= return . Plain . normalizeSpaces
252
253
253imageBlock :: Parser [Char] ParserState Block
254imageBlock :: Parser [Char] ParserState Block
254imageBlock = try $ do
255imageBlock = try $ do
255− string ".. image:: "
256+ string ".. "
257+ res <- imageDef [Str "image"]
258+ return $ Para [res]
259+
260+imageDef :: [Inline] -> Parser [Char] ParserState Inline
261+imageDef defaultAlt = try $ do
262+ string "image:: "
256− src <- manyTill anyChar newline
263+ src <- escapeURI . removeLeadingTrailingSpace <$> manyTill anyChar newline
257 fields <- try $ do indent <- lookAhead $ many (oneOf " /t")
264 fields <- try $ do indent <- lookAhead $ many (oneOf " /t")
258 many $ rawFieldListItem indent
265 many $ rawFieldListItem indent
259 optional blanklines
266 optional blanklines
260− let alt = maybe [Str "image"] (\x -> [Str $ removeTrailingSpace x])
267+ let alt = maybe defaultAlt (\x -> [Str $ removeTrailingSpace x])
261 $ lookup "alt" fields
268 $ lookup "alt" fields
262 let img = Image alt (src,"")
269 let img = Image alt (src,"")
263− return $ Para [case lookup "target" fields of
270+ return $ case lookup "target" fields of
264− Just t -> Link [img]
271+ Just t -> Link [img]
265− (escapeURI $ removeLeadingTrailingSpace t,"")
272+ (escapeURI $ removeLeadingTrailingSpace t,"")
266− Nothing -> img]
273+ Nothing -> img
274+
267
275
268--
276--
269-- header blocks
277-- header blocks
@@ -652,10 +660,7 @@ referenceName = quotedReferenceName <|>
652referenceKey :: Parser [Char] ParserState [Char]
660referenceKey :: Parser [Char] ParserState [Char]
653referenceKey = do
661referenceKey = do
654 startPos <- getPosition
662 startPos <- getPosition
655− (key, target) <- choice [anonymousKey, regularKey]
663+ choice [imageKey, anonymousKey, regularKey]
656− st <- getState
657− let oldkeys = stateKeys st
658− updateState $ \s -> s { stateKeys = M.insert key target oldkeys }
659 optional blanklines
664 optional blanklines
660 endPos <- getPosition
665 endPos <- getPosition
661 -- return enough blanks to replace key
666 -- return enough blanks to replace key
@@ -670,41 +675,36 @@ targetURI = do
670 blanklines
675 blanklines
671 return $ escapeURI $ removeLeadingTrailingSpace $ contents
676 return $ escapeURI $ removeLeadingTrailingSpace $ contents
672
677
673−imageKey :: Parser [Char] ParserState [Char]
678+imageKey :: Parser [Char] ParserState ()
674imageKey = try $ do
679imageKey = try $ do
675− startPos <- getPosition
676 string ".. |"
680 string ".. |"
677 (alt,ref) <- withRaw (manyTill inline (char '|'))
681 (alt,ref) <- withRaw (manyTill inline (char '|'))
678 skipSpaces
682 skipSpaces
679− string "image::"
683+ img <- imageDef alt
680− src <- targetURI
681− let img = Image alt (src,"")
682 let key = toKey $ init ref
684 let key = toKey $ init ref
683 updateState $ \s -> s{ stateSubstitutions = M.insert key img $ stateSubstitutions s }
685 updateState $ \s -> s{ stateSubstitutions = M.insert key img $ stateSubstitutions s }
684− optional blanklines
685− endPos <- getPosition
686− -- return enough blanks to replace key
687− return $ replicate (sourceLine endPos - sourceLine startPos) '\n'
688
686
689−anonymousKey :: Parser [Char] st (Key, Target)
687+anonymousKey :: Parser [Char] ParserState ()
690anonymousKey = try $ do
688anonymousKey = try $ do
691 oneOfStrings [".. __:", "__"]
689 oneOfStrings [".. __:", "__"]
692 src <- targetURI
690 src <- targetURI
693 pos <- getPosition
691 pos <- getPosition
694− return (toKey $ "_" ++ printf "%09d" (sourceLine pos), (src, ""))
692+ let key = toKey $ "_" ++ printf "%09d" (sourceLine pos)
693+ updateState $ \s -> s { stateKeys = M.insert key (src,"") $ stateKeys s }
695
694
696stripTicks :: String -> String
695stripTicks :: String -> String
697stripTicks = reverse . stripTick . reverse . stripTick
696stripTicks = reverse . stripTick . reverse . stripTick
698 where stripTick ('`':xs) = xs
697 where stripTick ('`':xs) = xs
699 stripTick xs = xs
698 stripTick xs = xs
700
699
701−regularKey :: Parser [Char] ParserState (Key, Target)
700+regularKey :: Parser [Char] ParserState ()
702regularKey = try $ do
701regularKey = try $ do
703 string ".. _"
702 string ".. _"
704 (_,ref) <- withRaw referenceName
703 (_,ref) <- withRaw referenceName
705 char ':'
704 char ':'
706 src <- targetURI
705 src <- targetURI
707− return (toKey $ stripTicks ref, (src, ""))
706+ let key = toKey $ stripTicks ref
707+ updateState $ \s -> s { stateKeys = M.insert key (src,"") $ stateKeys s }
708
708
709--
709--
710-- tables
710-- tables
tests/rst-reader.native+1
@@ -226,6 +226,7 @@ Pandoc (Meta {docTitle = [Str "Pandoc",Space,Str "Test",Space,Str "Suite",Str ":
226,Para [Image [Str "image"] ("lalune.jpg","")]
226,Para [Image [Str "image"] ("lalune.jpg","")]
227,Para [Image [Str "Voyage dans la Lune"] ("lalune.jpg","")]
227,Para [Image [Str "Voyage dans la Lune"] ("lalune.jpg","")]
228,Para [Str "Here",Space,Str "is",Space,Str "a",Space,Str "movie",Space,Image [Str "movie"] ("movie.jpg",""),Space,Str "icon",Str "."]
228,Para [Str "Here",Space,Str "is",Space,Str "a",Space,Str "movie",Space,Image [Str "movie"] ("movie.jpg",""),Space,Str "icon",Str "."]
229+,Para [Str "And",Space,Str "an",Space,Link [Image [Str "A movie"] ("movie.jpg","")] ("/url",""),Str "."]
229,Header 1 [Str "Comments"]
230,Header 1 [Str "Comments"]
230,Para [Str "First",Space,Str "paragraph"]
231,Para [Str "First",Space,Str "paragraph"]
231,Para [Str "Another",Space,Str "paragraph"]
232,Para [Str "Another",Space,Str "paragraph"]
tests/rst-reader.rst+6
@@ -416,6 +416,12 @@ Here is a movie |movie| icon.
416
416
417.. |movie| image:: movie.jpg
417.. |movie| image:: movie.jpg
418
418
419+And an |image with a link|.
420+
421+.. |image with a link| image:: movie.jpg
422+ :alt: A movie
423+ :target: /url
424+
419Comments
425Comments
420========
426========
421
427