mirror of
https://github.com/jgm/pandoc.git
synced 2026-08-26 10:16:32 +00:00
Org reader: restructure output of captioned code blocks
The Div wrapper of code blocks with captions now has the class "captioned-content". The caption itself is added as a Plain block inside a Div of class "caption". This makes it easier to write filters which match on captioned code blocks. Existing filters will need to be updated. Closes: #6977
This commit is contained in:
@@ -294,24 +294,22 @@ verseBlock blockType = try $ do
|
||||
codeBlock :: PandocMonad m => BlockAttributes -> Text -> OrgParser m (F Blocks)
|
||||
codeBlock blockAttrs blockType = do
|
||||
skipSpaces
|
||||
(classes, kv) <- codeHeaderArgs <|> (mempty <$ ignHeaders)
|
||||
content <- rawBlockContent blockType
|
||||
resultsContent <- option mempty babelResultsBlock
|
||||
let id' = fromMaybe mempty $ blockAttrName blockAttrs
|
||||
let codeBlck = B.codeBlockWith ( id', classes, kv ) content
|
||||
let labelledBlck = maybe (pure codeBlck)
|
||||
(labelDiv codeBlck)
|
||||
(blockAttrCaption blockAttrs)
|
||||
(classes, kv) <- codeHeaderArgs <|> (mempty <$ ignHeaders)
|
||||
content <- rawBlockContent blockType
|
||||
resultsContent <- option mempty babelResultsBlock
|
||||
let identifier = fromMaybe mempty $ blockAttrName blockAttrs
|
||||
let codeBlk = B.codeBlockWith (identifier, classes, kv) content
|
||||
let wrap = maybe pure addCaption (blockAttrCaption blockAttrs)
|
||||
return $
|
||||
(if exportsCode kv then labelledBlck else mempty) <>
|
||||
(if exportsCode kv then wrap codeBlk else mempty) <>
|
||||
(if exportsResults kv then resultsContent else mempty)
|
||||
where
|
||||
labelDiv :: Blocks -> F Inlines -> F Blocks
|
||||
labelDiv blk value =
|
||||
B.divWith nullAttr <$> (mappend <$> labelledBlock value <*> pure blk)
|
||||
addCaption :: F Inlines -> Blocks -> F Blocks
|
||||
addCaption caption blk = B.divWith ("", ["captioned-content"], [])
|
||||
<$> (mkCaptionBlock caption <> pure blk)
|
||||
|
||||
labelledBlock :: F Inlines -> F Blocks
|
||||
labelledBlock = fmap (B.plain . B.spanWith ("", ["label"], []))
|
||||
mkCaptionBlock :: F Inlines -> F Blocks
|
||||
mkCaptionBlock = fmap (B.divWith ("", ["caption"], []) . B.plain)
|
||||
|
||||
exportsResults :: [(Text, Text)] -> Bool
|
||||
exportsResults = maybe False (`elem` ["results", "both"]) . lookup "exports"
|
||||
|
||||
@@ -185,10 +185,10 @@ tests =
|
||||
, "#+end_src"
|
||||
] =?>
|
||||
divWith
|
||||
nullAttr
|
||||
("", ["captioned-content"], [] )
|
||||
(mappend
|
||||
(plain $ spanWith ("", ["label"], [])
|
||||
(spcSep [ "Functor", "laws", "in", "Haskell" ]))
|
||||
(divWith ("", ["caption"], []) $
|
||||
plain (spcSep [ "Functor", "laws", "in", "Haskell" ]))
|
||||
(codeBlockWith ("functor-laws", ["haskell"], [])
|
||||
(T.unlines [ "fmap id = id"
|
||||
, "fmap (p . q) = (fmap p) . (fmap q)"
|
||||
|
||||
Reference in New Issue
Block a user