git.iko.soy/typeable/pandoc
on master help sign in
RST reader: Folded image block handling into directive.
1948c55John MacFarlane
src/Text/Pandoc/Readers/RST.hs+36−41
@@ -134,7 +134,6 @@ block = choice [ codeBlock
134 , rawBlock
134 , rawBlock
135 , blockQuote
135 , blockQuote
136 , fieldList
136 , fieldList
137− , imageBlock
138 , figureBlock
137 , figureBlock
139 , directive
138 , directive
140 , header
139 , header
@@ -241,31 +240,6 @@ para = try $ do
241plain :: RSTParser Blocks
240plain :: RSTParser Blocks
242plain = B.plain . trimInlines . mconcat <$> many1 inline
241plain = B.plain . trimInlines . mconcat <$> many1 inline
243
242
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−
269--
243--
270-- header blocks
244-- header blocks
271--
245--
@@ -376,10 +350,9 @@ extractCaption = try $ do
376
350
377lhsCodeBlock :: RSTParser Blocks
351lhsCodeBlock :: RSTParser Blocks
378lhsCodeBlock = try $ do
352lhsCodeBlock = try $ do
353+ getPosition >>= guard . (==1) . sourceColumn
379 guardEnabled Ext_literate_haskell
354 guardEnabled Ext_literate_haskell
380 optional codeBlockStart
355 optional codeBlockStart
381− pos <- getPosition
382− when (sourceColumn pos /= 1) $ fail "Not in first column"
383 lns <- many1 birdTrackLine
356 lns <- many1 birdTrackLine
384 -- if (as is normal) there is always a space after >, drop it
357 -- if (as is normal) there is always a space after >, drop it
385 let lns' = if all (\ln -> null ln || take 1 ln == " ") lns
358 let lns' = if all (\ln -> null ln || take 1 ln == " ") lns
@@ -521,7 +494,12 @@ bulletList = B.bulletList . compactify' <$> many1 (listItem bulletListStart)
521
494
522directive :: RSTParser Blocks
495directive :: RSTParser Blocks
523directive = try $ do
496directive = try $ do
497+ getPosition >>= guard . (==1) . sourceColumn
524 string ".."
498 string ".."
499+ directive'
500+
501+directive' :: RSTParser Blocks
502+directive' = do
525 lookAhead (char '\n') <|> spaceChar
503 lookAhead (char '\n') <|> spaceChar
526 skipMany spaceChar
504 skipMany spaceChar
527 label <- option "" $ try
505 label <- option "" $ try
@@ -538,7 +516,7 @@ directive = try $ do
538 body <- option "" indentedBlock
516 body <- option "" indentedBlock
539 let body' = body ++ "\n\n"
517 let body' = body ++ "\n\n"
540 case label of
518 case label of
541− "" -> return mempty -- comment
519+ "" -> return mempty -- comment
542 "container" -> parseFromString parseBlocks body'
520 "container" -> parseFromString parseBlocks body'
543 "compound" -> parseFromString parseBlocks body'
521 "compound" -> parseFromString parseBlocks body'
544 "pull-quote" -> B.blockQuote <$> parseFromString parseBlocks body'
522 "pull-quote" -> B.blockQuote <$> parseFromString parseBlocks body'
@@ -555,6 +533,14 @@ directive = try $ do
555 "code-block" -> codeblock (lookup "number-lines" fields) (trim top) body
533 "code-block" -> codeblock (lookup "number-lines" fields) (trim top) body
556 "math" -> return $ B.para $ mconcat $ map B.displayMath
534 "math" -> return $ B.para $ mconcat $ map B.displayMath
557 $ toChunks $ top ++ "\n\n" ++ body
535 $ 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
558 _ -> return mempty
544 _ -> return mempty
559
545
560-- divide string by blanklines
546-- divide string by blanklines
@@ -645,7 +631,7 @@ referenceName = quotedReferenceName <|>
645referenceKey :: RSTParser [Char]
631referenceKey :: RSTParser [Char]
646referenceKey = do
632referenceKey = do
647 startPos <- getPosition
633 startPos <- getPosition
648− choice [imageKey, anonymousKey, regularKey]
634+ choice [substKey, anonymousKey, regularKey]
649 optional blanklines
635 optional blanklines
650 endPos <- getPosition
636 endPos <- getPosition
651 -- return enough blanks to replace key
637 -- return enough blanks to replace key
@@ -660,14 +646,24 @@ targetURI = do
660 blanklines
646 blanklines
661 return $ escapeURI $ trim $ contents
647 return $ escapeURI $ trim $ contents
662
648
663−imageKey :: RSTParser ()
649+substKey :: RSTParser ()
664−imageKey = try $ do
650+substKey = try $ do
651+ getPosition >>= guard . (==1) . sourceColumn
665− string ".. |"
652+ string ".."
653+ skipMany1 spaceChar
666− (alt,ref) <- withRaw (trimInlines . mconcat <$> manyTill inline (char '|'))
654+ (alt,ref) <- withRaw $ trimInlines . mconcat
667− skipSpaces
655+ <$> enclosed (char '|') (char '|') inline
668− img <- imageDef alt
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
669− let key = toKey $ init ref
665+ let key = toKey $ stripFirstAndLast ref
670− updateState $ \s -> s{ stateSubstitutions = M.insert key img $ stateSubstitutions s }
666+ updateState $ \s -> s{ stateSubstitutions = M.insert key il $ stateSubstitutions s }
671
667
672anonymousKey :: RSTParser ()
668anonymousKey :: RSTParser ()
673anonymousKey = try $ do
669anonymousKey = try $ do
@@ -955,11 +951,10 @@ autoLink = autoURI <|> autoEmail
955-- For now, we assume that all substitution references are for images.
951-- For now, we assume that all substitution references are for images.
956image :: RSTParser Inlines
952image :: RSTParser Inlines
957image = try $ do
953image = try $ do
958− char '|'
959− (_,ref) <- withRaw (manyTill inline (char '|'))
954+ (_,ref) <- withRaw $ enclosed (char '|') (char '|') inline
960 state <- getState
955 state <- getState
961 let substTable = stateSubstitutions state
956 let substTable = stateSubstitutions state
962− case M.lookup (toKey $ init ref) substTable of
957+ case M.lookup (toKey $ stripFirstAndLast ref) substTable of
963 Nothing -> fail "no corresponding key"
958 Nothing -> fail "no corresponding key"
964 Just target -> return target
959 Just target -> return target
965
960