git.iko.soy/typeable/pandoc
on master help sign in
Org reader: allow HTML attribs on non-figure images

Images which are the only element in a paragraph can still be given HTML attributes, even if the image does not have a caption and is hence not a figure. The following will add set the `width` attribute of the image to `50%`: #+ATTR_HTML: :width 50% [[file:image.jpg]] Closes: #3222

commit7e5220b57c5a48fabe6e43ba270db812593d3463 authorAlbert Krewinkel <albert@zeitkraut.de> dateWed 9 Nov 2016, 22:49 (10 years ago) parent13bc573
2 files changed+14−6
src/Text/Pandoc/Readers/Org/Blocks.hs+8−6 tests/Tests/Readers/Org.hs+6
src/Text/Pandoc/Readers/Org/Blocks.hs+8−6
@@ -618,7 +618,8 @@ propertiesDrawer = try $ do
618618-- Figures
619619--
620620
621−-- | Figures (Image on a line by itself, preceded by name and/or caption)
621+-- | Figures or an image paragraph (i.e. an image on a line by itself). Only
622+-- images with a caption attribute are interpreted as figures.
622623figure :: OrgParser (F Blocks)
623624figure = try $ do
624625 figAttrs <- blockAttributes
@@ -626,23 +627,24 @@ figure = try $ do
626627 case cleanLinkString src of
627628 Nothing -> mzero
628629 Just imgSrc -> do
629− guard (not . isNothing . blockAttrCaption $ figAttrs)
630630 guard (isImageFilename imgSrc)
631− return $ figureBlock figAttrs imgSrc
631+ let isFigure = not . isNothing $ blockAttrCaption figAttrs
632+ return $ imageBlock isFigure figAttrs imgSrc
632633 where
633634 selfTarget :: OrgParser String
634635 selfTarget = try $ char '[' *> linkTarget <* char ']'
635636
636− figureBlock :: BlockAttributes -> String -> (F Blocks)
637− figureBlock figAttrs imgSrc =
637+ imageBlock :: Bool -> BlockAttributes -> String -> F Blocks
638+ imageBlock isFigure figAttrs imgSrc =
638639 let
639640 figName = fromMaybe mempty $ blockAttrName figAttrs
640641 figLabel = fromMaybe mempty $ blockAttrLabel figAttrs
641642 figCaption = fromMaybe mempty $ blockAttrCaption figAttrs
642643 figKeyVals = blockAttrKeyValues figAttrs
643644 attr = (figLabel, mempty, figKeyVals)
645+ figTitle = (if isFigure then withFigPrefix else id) figName
644646 in
645− B.para . B.imageWith attr imgSrc (withFigPrefix figName) <$> figCaption
647+ B.para . B.imageWith attr imgSrc figTitle <$> figCaption
646648
647649 withFigPrefix :: String -> String
648650 withFigPrefix cs =
tests/Tests/Readers/Org.hs+6
@@ -196,6 +196,12 @@ tests =
196196 "[[file:sunrise.jpg]]" =?>
197197 (para $ image "sunrise.jpg" "" "")
198198
199+ , "Image with html attributes" =:
200+ unlines [ "#+ATTR_HTML: :width 50%"
201+ , "[[file:guinea-pig.gif]]"
202+ ] =?>
203+ (para $ imageWith ("", [], [("width", "50%")]) "guinea-pig.gif" "" "")
204+
199205 , "Explicit link" =:
200206 "[[http://zeitlens.com/][pseudo-random /nonsense/]]" =?>
201207 (para $ link "http://zeitlens.com/" ""