git.iko.soy/typeable/pandoc
on master help sign in
RST reader: Support :target: on image substitutions.
commit731415a4e598adaac894865f72b935a742e569ec authorJohn MacFarlane <jgm@berkeley.edu> dateThu 27 Sep 2012, 15:40 (14 years ago) parent1be27ff
3 files changed+33−26
src/Text/Pandoc/Readers/RST.hs+26−26 tests/rst-reader.native+1 tests/rst-reader.rst+6
src/Text/Pandoc/Readers/RST.hs+26−26
@@ -39,6 +39,7 @@ import Data.List ( findIndex, intercalate, transpose, sort, deleteFirstsBy )
3939import qualified Data.Map as M
4040import Text.Printf ( printf )
4141import Data.Maybe ( catMaybes )
42+import Control.Applicative ((<$>))
4243
4344-- | Parse reStructuredText string and return Pandoc document.
4445readRST :: ReaderOptions -- ^ Reader options
@@ -95,8 +96,8 @@ parseRST = do
9596 startPos <- getPosition
9697 -- go through once just to get list of reference keys and notes
9798 -- docMinusKeys is the raw document with blanks where the keys were...
98− docMinusKeys <- manyTill (referenceKey <|> imageKey <|>
99− noteBlock <|> lineClump) eof >>= return . concat
99+ docMinusKeys <- concat <$>
100+ manyTill (referenceKey <|> noteBlock <|> lineClump) eof
100101 setInput docMinusKeys
101102 setPosition startPos
102103 st' <- getState
@@ -252,18 +253,25 @@ plain = many1 inline >>= return . Plain . normalizeSpaces
252253
253254imageBlock :: Parser [Char] ParserState Block
254255imageBlock = try $ do
255− string ".. image:: "
256− src <- manyTill anyChar newline
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:: "
263+ src <- escapeURI . removeLeadingTrailingSpace <$> manyTill anyChar newline
257264 fields <- try $ do indent <- lookAhead $ many (oneOf " /t")
258265 many $ rawFieldListItem indent
259266 optional blanklines
260− let alt = maybe [Str "image"] (\x -> [Str $ removeTrailingSpace x])
267+ let alt = maybe defaultAlt (\x -> [Str $ removeTrailingSpace x])
261268 $ lookup "alt" fields
262269 let img = Image alt (src,"")
263− return $ Para [case lookup "target" fields of
264− Just t -> Link [img]
265− (escapeURI $ removeLeadingTrailingSpace t,"")
266− Nothing -> img]
270+ return $ case lookup "target" fields of
271+ Just t -> Link [img]
272+ (escapeURI $ removeLeadingTrailingSpace t,"")
273+ Nothing -> img
274+
267275
268276--
269277-- header blocks
@@ -652,10 +660,7 @@ referenceName = quotedReferenceName <|>
652660referenceKey :: Parser [Char] ParserState [Char]
653661referenceKey = do
654662 startPos <- getPosition
655− (key, target) <- choice [anonymousKey, regularKey]
656− st <- getState
657− let oldkeys = stateKeys st
658− updateState $ \s -> s { stateKeys = M.insert key target oldkeys }
663+ choice [imageKey, anonymousKey, regularKey]
659664 optional blanklines
660665 endPos <- getPosition
661666 -- return enough blanks to replace key
@@ -670,41 +675,36 @@ targetURI = do
670675 blanklines
671676 return $ escapeURI $ removeLeadingTrailingSpace $ contents
672677
673−imageKey :: Parser [Char] ParserState [Char]
678+imageKey :: Parser [Char] ParserState ()
674679imageKey = try $ do
675− startPos <- getPosition
676680 string ".. |"
677681 (alt,ref) <- withRaw (manyTill inline (char '|'))
678682 skipSpaces
679− string "image::"
680− src <- targetURI
681− let img = Image alt (src,"")
683+ img <- imageDef alt
682684 let key = toKey $ init ref
683685 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'
688686
689−anonymousKey :: Parser [Char] st (Key, Target)
687+anonymousKey :: Parser [Char] ParserState ()
690688anonymousKey = try $ do
691689 oneOfStrings [".. __:", "__"]
692690 src <- targetURI
693691 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 }
695694
696695stripTicks :: String -> String
697696stripTicks = reverse . stripTick . reverse . stripTick
698697 where stripTick ('`':xs) = xs
699698 stripTick xs = xs
700699
701−regularKey :: Parser [Char] ParserState (Key, Target)
700+regularKey :: Parser [Char] ParserState ()
702701regularKey = try $ do
703702 string ".. _"
704703 (_,ref) <- withRaw referenceName
705704 char ':'
706705 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 }
708708
709709--
710710-- tables
tests/rst-reader.native+1
@@ -226,6 +226,7 @@ Pandoc (Meta {docTitle = [Str "Pandoc",Space,Str "Test",Space,Str "Suite",Str ":
226226,Para [Image [Str "image"] ("lalune.jpg","")]
227227,Para [Image [Str "Voyage dans la Lune"] ("lalune.jpg","")]
228228,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 "."]
229230,Header 1 [Str "Comments"]
230231,Para [Str "First",Space,Str "paragraph"]
231232,Para [Str "Another",Space,Str "paragraph"]
tests/rst-reader.rst+6
@@ -416,6 +416,12 @@ Here is a movie |movie| icon.
416416
417417.. |movie| image:: movie.jpg
418418
419+And an |image with a link|.
420+
421+.. |image with a link| image:: movie.jpg
422+ :alt: A movie
423+ :target: /url
424+
419425Comments
420426========
421427