git.iko.soy/typeable/pandoc
on master help sign in
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

commit17e3efc785fa8b0680ec6d4ebaac1ea6bdb57e1a authorAlbert Krewinkel <albert@zeitkraut.de> dateFri 1 Jan 2021, 10:50 (6 years ago) parent23f964b
2 files changed+15−17
src/Text/Pandoc/Readers/Org/Blocks.hs+12−14 test/Tests/Readers/Org/Block/CodeBlock.hs+3−3
src/Text/Pandoc/Readers/Org/Blocks.hs+12−14
@@ -294,24 +294,22 @@ verseBlock blockType = try $ do
294294codeBlock :: PandocMonad m => BlockAttributes -> Text -> OrgParser m (F Blocks)
295295codeBlock blockAttrs blockType = do
296296 skipSpaces
297− (classes, kv) <- codeHeaderArgs <|> (mempty <$ ignHeaders)
298− content <- rawBlockContent blockType
299− resultsContent <- option mempty babelResultsBlock
300− let id' = fromMaybe mempty $ blockAttrName blockAttrs
301− let codeBlck = B.codeBlockWith ( id', classes, kv ) content
302− let labelledBlck = maybe (pure codeBlck)
303− (labelDiv codeBlck)
304− (blockAttrCaption blockAttrs)
297+ (classes, kv) <- codeHeaderArgs <|> (mempty <$ ignHeaders)
298+ content <- rawBlockContent blockType
299+ resultsContent <- option mempty babelResultsBlock
300+ let identifier = fromMaybe mempty $ blockAttrName blockAttrs
301+ let codeBlk = B.codeBlockWith (identifier, classes, kv) content
302+ let wrap = maybe pure addCaption (blockAttrCaption blockAttrs)
305303 return $
306− (if exportsCode kv then labelledBlck else mempty) <>
304+ (if exportsCode kv then wrap codeBlk else mempty) <>
307305 (if exportsResults kv then resultsContent else mempty)
308306 where
309− labelDiv :: Blocks -> F Inlines -> F Blocks
310− labelDiv blk value =
311− B.divWith nullAttr <$> (mappend <$> labelledBlock value <*> pure blk)
307+ addCaption :: F Inlines -> Blocks -> F Blocks
308+ addCaption caption blk = B.divWith ("", ["captioned-content"], [])
309+ <$> (mkCaptionBlock caption <> pure blk)
312310
313− labelledBlock :: F Inlines -> F Blocks
314− labelledBlock = fmap (B.plain . B.spanWith ("", ["label"], []))
311+ mkCaptionBlock :: F Inlines -> F Blocks
312+ mkCaptionBlock = fmap (B.divWith ("", ["caption"], []) . B.plain)
315313
316314 exportsResults :: [(Text, Text)] -> Bool
317315 exportsResults = maybe False (`elem` ["results", "both"]) . lookup "exports"
test/Tests/Readers/Org/Block/CodeBlock.hs+3−3
@@ -185,10 +185,10 @@ tests =
185185 , "#+end_src"
186186 ] =?>
187187 divWith
188− nullAttr
188+ ("", ["captioned-content"], [] )
189189 (mappend
190− (plain $ spanWith ("", ["label"], [])
191− (spcSep [ "Functor", "laws", "in", "Haskell" ]))
190+ (divWith ("", ["caption"], []) $
191+ plain (spcSep [ "Functor", "laws", "in", "Haskell" ]))
192192 (codeBlockWith ("functor-laws", ["haskell"], [])
193193 (T.unlines [ "fmap id = id"
194194 , "fmap (p . q) = (fmap p) . (fmap q)"