git.iko.soy/typeable/pandoc
on master help sign in
RST reader: Folded image block handling into directive.
commit1948c55914c4fa61a3590c1572d3ad025c19075f authorJohn MacFarlane <jgm@berkeley.edu> dateSat 29 Sep 2012, 19:22 (14 years ago) parent720a7ba
1 file changed+36−41
src/Text/Pandoc/Readers/RST.hs+36−41
src/Text/Pandoc/Readers/RST.hs+36−41
@@ -134,7 +134,6 @@ block = choice [ codeBlock
134134 , rawBlock
135135 , blockQuote
136136 , fieldList
137− , imageBlock
138137 , figureBlock
139138 , directive
140139 , header
@@ -241,31 +240,6 @@ para = try $ do
241240plain :: RSTParser Blocks
242241plain = B.plain . trimInlines . mconcat <$> many1 inline
243242
244−--
245−-- image block
246−--
247−
248−imageBlock :: RSTParser Blocks
249−imageBlock = try $ do
250− string ".. "
251− res <- imageDef (B.str "image")
252− return $ B.para res
253−
254−imageDef :: Inlines -> RSTParser Inlines
255−imageDef defaultAlt = try $ do
256− string "image:: "
257− src <- escapeURI . trim <$> manyTill anyChar newline
258− fields <- try $ do indent <- lookAhead $ many (oneOf " /t")
259− many $ rawFieldListItem indent
260− optional blanklines
261− let alt = maybe defaultAlt (\x -> B.str $ trimr x)
262− $ lookup "alt" fields
263− let img = B.image src "" alt
264− return $ case lookup "target" fields of
265− Just t -> B.link (escapeURI $ trim t)
266− "" img
267− Nothing -> img
268−
269243--
270244-- header blocks
271245--
@@ -376,10 +350,9 @@ extractCaption = try $ do
376350
377351lhsCodeBlock :: RSTParser Blocks
378352lhsCodeBlock = try $ do
353+ getPosition >>= guard . (==1) . sourceColumn
379354 guardEnabled Ext_literate_haskell
380355 optional codeBlockStart
381− pos <- getPosition
382− when (sourceColumn pos /= 1) $ fail "Not in first column"
383356 lns <- many1 birdTrackLine
384357 -- if (as is normal) there is always a space after >, drop it
385358 let lns' = if all (\ln -> null ln || take 1 ln == " ") lns
@@ -521,7 +494,12 @@ bulletList = B.bulletList . compactify' <$> many1 (listItem bulletListStart)
521494
522495directive :: RSTParser Blocks
523496directive = try $ do
497+ getPosition >>= guard . (==1) . sourceColumn
524498 string ".."
499+ directive'
500+
501+directive' :: RSTParser Blocks
502+directive' = do
525503 lookAhead (char '\n') <|> spaceChar
526504 skipMany spaceChar
527505 label <- option "" $ try
@@ -538,7 +516,7 @@ directive = try $ do
538516 body <- option "" indentedBlock
539517 let body' = body ++ "\n\n"
540518 case label of
541− "" -> return mempty -- comment
519+ "" -> return mempty -- comment
542520 "container" -> parseFromString parseBlocks body'
543521 "compound" -> parseFromString parseBlocks body'
544522 "pull-quote" -> B.blockQuote <$> parseFromString parseBlocks body'
@@ -555,6 +533,14 @@ directive = try $ do
555533 "code-block" -> codeblock (lookup "number-lines" fields) (trim top) body
556534 "math" -> return $ B.para $ mconcat $ map B.displayMath
557535 $ toChunks $ top ++ "\n\n" ++ body
536+ "image" -> do
537+ let src = escapeURI $ trim top
538+ let alt = B.str $ maybe "image" trim $ lookup "alt" fields
539+ return $ B.para
540+ $ case lookup "target" fields of
541+ Just t -> B.link (escapeURI $ trim t) ""
542+ $ B.image src "" alt
543+ Nothing -> B.image src "" alt
558544 _ -> return mempty
559545
560546-- divide string by blanklines
@@ -645,7 +631,7 @@ referenceName = quotedReferenceName <|>
645631referenceKey :: RSTParser [Char]
646632referenceKey = do
647633 startPos <- getPosition
648− choice [imageKey, anonymousKey, regularKey]
634+ choice [substKey, anonymousKey, regularKey]
649635 optional blanklines
650636 endPos <- getPosition
651637 -- return enough blanks to replace key
@@ -660,14 +646,24 @@ targetURI = do
660646 blanklines
661647 return $ escapeURI $ trim $ contents
662648
663−imageKey :: RSTParser ()
664−imageKey = try $ do
665− string ".. |"
666− (alt,ref) <- withRaw (trimInlines . mconcat <$> manyTill inline (char '|'))
667− skipSpaces
668− img <- imageDef alt
669− let key = toKey $ init ref
670− updateState $ \s -> s{ stateSubstitutions = M.insert key img $ stateSubstitutions s }
649+substKey :: RSTParser ()
650+substKey = try $ do
651+ getPosition >>= guard . (==1) . sourceColumn
652+ string ".."
653+ skipMany1 spaceChar
654+ (alt,ref) <- withRaw $ trimInlines . mconcat
655+ <$> enclosed (char '|') (char '|') inline
656+ res <- B.toList <$> directive'
657+ il <- case res of
658+ -- use alt unless :alt: attribute on image:
659+ [Para [Image [Str "image"] (src,tit)]] ->
660+ return $ B.image src tit alt
661+ [Para [Link [Image [Str "image"] (src,tit)] (src',tit')]] ->
662+ return $ B.link src' tit' (B.image src tit alt)
663+ [Para ils] -> return $ B.fromList ils
664+ _ -> mzero
665+ let key = toKey $ stripFirstAndLast ref
666+ updateState $ \s -> s{ stateSubstitutions = M.insert key il $ stateSubstitutions s }
671667
672668anonymousKey :: RSTParser ()
673669anonymousKey = try $ do
@@ -955,11 +951,10 @@ autoLink = autoURI <|> autoEmail
955951-- For now, we assume that all substitution references are for images.
956952image :: RSTParser Inlines
957953image = try $ do
958− char '|'
959− (_,ref) <- withRaw (manyTill inline (char '|'))
954+ (_,ref) <- withRaw $ enclosed (char '|') (char '|') inline
960955 state <- getState
961956 let substTable = stateSubstitutions state
962− case M.lookup (toKey $ init ref) substTable of
957+ case M.lookup (toKey $ stripFirstAndLast ref) substTable of
963958 Nothing -> fail "no corresponding key"
964959 Just target -> return target
965960