git.iko.soy/typeable/pandoc
on master help sign in
Org reader: support `ATTR_HTML` for special blocks

Special blocks (i.e. blocks with unrecognized names) can be prefixed with an `ATTR_HTML` block attribute. The attributes defined in that meta-directive are added to the `Div` which is used to represent the special block. Closes: #3182

commit4f06e6c4455b5b6be21416e9736a70f8d2b1ff1c authorAlbert Krewinkel <albert@zeitkraut.de> dateSun 30 Oct 2016, 20:23 (10 years ago) parent63bdc5d
2 files changed+31−9
src/Text/Pandoc/Readers/Org/Blocks.hs+22−9 tests/Tests/Readers/Org.hs+9
src/Text/Pandoc/Readers/Org/Blocks.hs+22−9
@@ -294,6 +294,17 @@ data BlockAttributes = BlockAttributes
294294 , blockAttrKeyValues :: [(String, String)]
295295 }
296296
297+-- | Convert BlockAttributes into pandoc Attr
298+attrFromBlockAttributes :: BlockAttributes -> Attr
299+attrFromBlockAttributes (BlockAttributes{..}) =
300+ let
301+ ident = fromMaybe mempty $ lookup "id" blockAttrKeyValues
302+ classes = case lookup "class" blockAttrKeyValues of
303+ Nothing -> []
304+ Just clsStr -> words clsStr
305+ kv = filter ((`notElem` ["id", "class"]) . fst) blockAttrKeyValues
306+ in (ident, classes, kv)
307+
297308stringyMetaAttribute :: (String -> Bool) -> OrgParser (String, String)
298309stringyMetaAttribute attrCheck = try $ do
299310 metaLineStart
@@ -364,23 +375,25 @@ orgBlock = try $ do
364375 blockAttrs <- blockAttributes
365376 blkType <- blockHeaderStart
366377 ($ blkType) $
367− case blkType of
378+ case (map toLower blkType) of
368379 "export" -> exportBlock
369380 "comment" -> rawBlockLines (const mempty)
370− "html" -> rawBlockLines (return . (B.rawBlock blkType))
371− "latex" -> rawBlockLines (return . (B.rawBlock blkType))
372− "ascii" -> rawBlockLines (return . (B.rawBlock blkType))
381+ "html" -> rawBlockLines (return . B.rawBlock (lowercase blkType))
382+ "latex" -> rawBlockLines (return . B.rawBlock (lowercase blkType))
383+ "ascii" -> rawBlockLines (return . B.rawBlock (lowercase blkType))
373384 "example" -> rawBlockLines (return . exampleCode)
374385 "quote" -> parseBlockLines (fmap B.blockQuote)
375386 "verse" -> verseBlock
376387 "src" -> codeBlock blockAttrs
377− _ -> parseBlockLines (fmap $ B.divWith (mempty, [blkType], mempty))
388+ _ -> parseBlockLines $
389+ let (ident, classes, kv) = attrFromBlockAttributes blockAttrs
390+ in fmap $ B.divWith (ident, classes ++ [blkType], kv)
378391 where
379392 blockHeaderStart :: OrgParser String
380− blockHeaderStart = try $ do
381− skipSpaces
382− blockType <- stringAnyCase "#+begin_" *> orgArgWord
383− return (map toLower blockType)
393+ blockHeaderStart = try $ skipSpaces *> stringAnyCase "#+begin_" *> orgArgWord
394+
395+ lowercase :: String -> String
396+ lowercase = map toLower
384397
385398rawBlockLines :: (String -> F Blocks) -> String -> OrgParser (F Blocks)
386399rawBlockLines f blockType = (ignHeaders *> (f <$> rawBlockContent blockType))
tests/Tests/Readers/Org.hs+9
@@ -1625,6 +1625,15 @@ tests =
16251625 ] =?>
16261626 rawBlock "html" "\n<span>boring</span>\n\n"
16271627
1628+ , "Accept `ATTR_HTML` attributes for generic block" =:
1629+ unlines [ "#+ATTR_HTML: :title hello, world :id test :class fun code"
1630+ , "#+BEGIN_TEST"
1631+ , "nonsense"
1632+ , "#+END_TEST"
1633+ ] =?>
1634+ let attr = ("test", ["fun", "code", "TEST"], [("title", "hello, world")])
1635+ in divWith attr (para "nonsense")
1636+
16281637 , "Non-letter chars in source block parameters" =:
16291638 unlines [ "#+BEGIN_SRC C :tangle xxxx.c :city Zürich"
16301639 , "code body"