@@ -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 ((<$>))
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
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"]
260+imageDef :: [Inline] -> Parser [Char] ParserState Inline
261+imageDef defaultAlt = try $ do
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
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]
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
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
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 }
685− endPos <- getPosition
686− -- return enough blanks to replace key
687− return $ replicate (sourceLine endPos - sourceLine startPos) '\n'
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 }
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
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
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 }