git.iko.soy/typeable/pandoc
on master help sign in
Org reader: add support for ATTR_HTML attributes
16e2334Albert Krewinkel
src/Text/Pandoc/Readers/Org.hs+28−7
@@ -301,8 +301,9 @@ block = choice [ mempty <$ blanklines
301
301
302-- | Attributes that may be added to figures (like a name or caption).
302-- | Attributes that may be added to figures (like a name or caption).
303data BlockAttributes = BlockAttributes
303data BlockAttributes = BlockAttributes
304− { blockAttrName :: Maybe String
304+ { blockAttrName :: Maybe String
305− , blockAttrCaption :: Maybe (F Inlines)
305+ , blockAttrCaption :: Maybe (F Inlines)
306+ , blockAttrKeyValues :: [(String, String)]
306 }
307 }
307
308
308stringyMetaAttribute :: (String -> Bool) -> OrgParser (String, String)
309stringyMetaAttribute :: (String -> Bool) -> OrgParser (String, String)
@@ -318,21 +319,25 @@ blockAttributes :: OrgParser BlockAttributes
318blockAttributes = try $ do
319blockAttributes = try $ do
319 kv <- many (stringyMetaAttribute attrCheck)
320 kv <- many (stringyMetaAttribute attrCheck)
320 let caption = foldl' (appendValues "CAPTION") Nothing kv
321 let caption = foldl' (appendValues "CAPTION") Nothing kv
322+ let kvAttrs = foldl' (appendValues "ATTR_HTML") Nothing kv
321 let name = lookup "NAME" kv
323 let name = lookup "NAME" kv
322 caption' <- maybe (return Nothing)
324 caption' <- maybe (return Nothing)
323 (fmap Just . parseFromString parseInlines)
325 (fmap Just . parseFromString parseInlines)
324 caption
326 caption
327+ kvAttrs' <- parseFromString keyValues . (++ "\n") $ fromMaybe mempty kvAttrs
325 return $ BlockAttributes
328 return $ BlockAttributes
326 { blockAttrName = name
329 { blockAttrName = name
327 , blockAttrCaption = caption'
330 , blockAttrCaption = caption'
331+ , blockAttrKeyValues = kvAttrs'
328 }
332 }
329 where
333 where
330 attrCheck :: String -> Bool
334 attrCheck :: String -> Bool
331 attrCheck attr =
335 attrCheck attr =
332 case attr of
336 case attr of
333− "NAME" -> True
337+ "NAME" -> True
334− "CAPTION" -> True
338+ "CAPTION" -> True
339+ "ATTR_HTML" -> True
335− _ -> False
340+ _ -> False
336
341
337 appendValues :: String -> Maybe String -> (String, String) -> Maybe String
342 appendValues :: String -> Maybe String -> (String, String) -> Maybe String
338 appendValues attrName accValue (key, value) =
343 appendValues attrName accValue (key, value) =
@@ -342,6 +347,21 @@ blockAttributes = try $ do
342 Just acc -> Just $ acc ++ ' ':value
347 Just acc -> Just $ acc ++ ' ':value
343 Nothing -> Just value
348 Nothing -> Just value
344
349
350+keyValues :: OrgParser [(String, String)]
351+keyValues = try $
352+ manyTill ((,) <$> key <*> value) newline
353+ where
354+ key :: OrgParser String
355+ key = try $ skipSpaces *> char ':' *> many1 nonspaceChar
356+
357+ value :: OrgParser String
358+ value = skipSpaces *> manyTill anyChar endOfValue
359+
360+ endOfValue :: OrgParser ()
361+ endOfValue =
362+ lookAhead $ (() <$ try (many1 spaceChar <* key))
363+ <|> () <$ P.newline
364+
345
365
346--
366--
347-- Org Blocks (#+BEGIN_... / #+END_...)
367-- Org Blocks (#+BEGIN_... / #+END_...)
@@ -588,7 +608,6 @@ drawerEnd = try $
588-- Figures
608-- Figures
589--
609--
590
610
591−
592-- | Figures (Image on a line by itself, preceded by name and/or caption)
611-- | Figures (Image on a line by itself, preceded by name and/or caption)
593figure :: OrgParser (F Blocks)
612figure :: OrgParser (F Blocks)
594figure = try $ do
613figure = try $ do
@@ -598,7 +617,9 @@ figure = try $ do
598 guard (isImageFilename src)
617 guard (isImageFilename src)
599 let figName = fromMaybe mempty $ blockAttrName figAttrs
618 let figName = fromMaybe mempty $ blockAttrName figAttrs
600 let figCaption = fromMaybe mempty $ blockAttrCaption figAttrs
619 let figCaption = fromMaybe mempty $ blockAttrCaption figAttrs
620+ let figKeyVals = blockAttrKeyValues figAttrs
621+ let attr = (mempty, mempty, figKeyVals)
601− return $ (B.para . B.image src (withFigPrefix figName) <$> figCaption)
622+ return $ (B.para . B.imageWith attr src (withFigPrefix figName) <$> figCaption)
602 where
623 where
603 withFigPrefix cs =
624 withFigPrefix cs =
604 if "fig:" `isPrefixOf` cs
625 if "fig:" `isPrefixOf` cs
tests/Tests/Readers/Org.hs+11
@@ -667,6 +667,17 @@ tests =
667 para (image "the-red-queen.jpg" "fig:redqueen"
667 para (image "the-red-queen.jpg" "fig:redqueen"
668 "Used as a metapher in evolutionary biology.")
668 "Used as a metapher in evolutionary biology.")
669
669
670+ , "Figure with HTML attributes" =:
671+ unlines [ "#+CAPTION: mah brain just explodid"
672+ , "#+NAME: lambdacat"
673+ , "#+ATTR_HTML: :style color: blue :role button"
674+ , "[[lambdacat.jpg]]"
675+ ] =?>
676+ let kv = [("style", "color: blue"), ("role", "button")]
677+ name = "fig:lambdacat"
678+ caption = "mah brain just explodid"
679+ in para (imageWith (mempty, mempty, kv) "lambdacat.jpg" name caption)
680+
670 , "Footnote" =:
681 , "Footnote" =:
671 unlines [ "A footnote[1]"
682 unlines [ "A footnote[1]"
672 , ""
683 , ""