@@ -35,6 +35,7 @@ import Data.List.Split (splitWhen)
3535import Data.List (foldl' )
3636import qualified Data.Map as M
3737import Data.Maybe (fromMaybe , isJust , isNothing )
38+ import Data.Either (partitionEithers )
3839import Data.Monoid (First (.. ))
3940import qualified Data.Set as Set
4041import Data.Text (Text )
@@ -57,7 +58,8 @@ import Text.Pandoc.Error
5758import Text.Pandoc.Logging
5859import Text.Pandoc.Options (
5960 Extension (Ext_epub_html_exts , Ext_empty_paragraphs , Ext_native_divs ,
60- Ext_native_spans , Ext_raw_html , Ext_line_blocks , Ext_raw_tex ),
61+ Ext_native_spans , Ext_raw_html , Ext_line_blocks , Ext_raw_tex ,
62+ Ext_native_figures ),
6163 ReaderOptions (readerExtensions , readerStripComments ),
6264 extensionEnabled )
6365import Text.Pandoc.Parsing hiding ((<|>) )
@@ -536,23 +538,40 @@ pPara = do
536538
537539pFigure :: PandocMonad m => TagParser m Blocks
538540pFigure = try $ do
539- TagOpen _ _ <- pSatisfy (matchTagOpen " figure" [] )
540- skipMany pBlank
541- let pImg = (\ x -> (Just x, Nothing )) <$>
542- (pInTag TagsOmittable " p" pImage <* skipMany pBlank)
543- pCapt = (\ x -> (Nothing , Just x)) <$> do
544- bs <- pInTags " figcaption" block
545- return $ blocksToInlines' $ B. toList bs
546- pSkip = (Nothing , Nothing ) <$ pSatisfy (not . matchTagClose " figure" )
547- res <- many (pImg <|> pCapt <|> pSkip)
548- let mbimg = msum $ map fst res
549- let mbcap = msum $ map snd res
550- TagClose _ <- pSatisfy (matchTagClose " figure" )
551- let caption = fromMaybe mempty mbcap
552- case B. toList <$> mbimg of
553- Just [Image attr _ (url, tit)] ->
554- return $ B. simpleFigureWith attr caption url tit
555- _ -> mzero
541+ has_native_figures <-
542+ extensionEnabled Ext_native_figures <$> getOption readerExtensions
543+ if has_native_figures
544+ then pNativeFigure
545+ else try $ do
546+ TagOpen _ _ <- pSatisfy (matchTagOpen " figure" [] )
547+ skipMany pBlank
548+ let pImg = (\ x -> (Just x, Nothing )) <$>
549+ (pInTag TagsOmittable " p" pImage <* skipMany pBlank)
550+ pCapt = (\ x -> (Nothing , Just x)) <$> do
551+ bs <- pInTags " figcaption" block
552+ return $ blocksToInlines' $ B. toList bs
553+ pSkip = (Nothing , Nothing ) <$ pSatisfy (not . matchTagClose " figure" )
554+ res <- many (pImg <|> pCapt <|> pSkip)
555+ -- Takes the first image and the first caption, if any, drop the rest.
556+ let mbimg = msum $ map fst res
557+ let mbcap = msum $ map snd res -- mbcap :: Maybe Inlines
558+ TagClose _ <- pSatisfy (matchTagClose " figure" )
559+ let caption = fromMaybe mempty mbcap
560+ -- only process one image
561+ case B. toList <$> mbimg of
562+ Just [Image attr _ (url, tit)] ->
563+ return $ B. simpleFigureWith attr caption url tit
564+ _ -> mzero
565+
566+ pNativeFigure :: PandocMonad m => TagParser m Blocks
567+ pNativeFigure = try $ do
568+ TagOpen tag attrList <- lookAhead $ pSatisfy (matchTagOpen " figure" [] )
569+ -- let (ident, classes, kvs) = toAttr attr
570+ contents <- pInTags tag (many $ Left <$> pInTags " figcaption" block <|> (Right <$> block))
571+
572+ let (captions, rest) = partitionEithers contents
573+ -- I should capture the caption
574+ return $ B. figureWith (toAttr attrList) (Caption Nothing (B. toList (mconcat captions))) $ mconcat rest
556575
557576pCodeBlock :: PandocMonad m => TagParser m Blocks
558577pCodeBlock = try $ do
0 commit comments