git.iko.soy/typeable/pandoc
on master help sign in
parse RST class directives
74c1b54Daniel Bergey
src/Text/Pandoc/Readers/RST.hs+8−2
@@ -47,7 +47,7 @@ import Text.Pandoc.Builder (Inlines, Blocks, trimInlines, (<>))
47import qualified Text.Pandoc.Builder as B
47import qualified Text.Pandoc.Builder as B
48import Data.Monoid (mconcat, mempty)
48import Data.Monoid (mconcat, mempty)
49import Data.Sequence (viewr, ViewR(..))
49import Data.Sequence (viewr, ViewR(..))
50−import Data.Char (toLower, isHexDigit)
50+import Data.Char (toLower, isHexDigit, isSpace)
51
51
52-- | Parse reStructuredText string and return Pandoc document.
52-- | Parse reStructuredText string and return Pandoc document.
53readRST :: ReaderOptions -- ^ Reader options
53readRST :: ReaderOptions -- ^ Reader options
@@ -521,7 +521,6 @@ directive = try $ do
521-- TODO: line-block, parsed-literal, table, csv-table, list-table
521-- TODO: line-block, parsed-literal, table, csv-table, list-table
522-- date
522-- date
523-- include
523-- include
524−-- class
525-- title
524-- title
526directive' :: RSTParser Blocks
525directive' :: RSTParser Blocks
527directive' = do
526directive' = do
@@ -602,6 +601,13 @@ directive' = do
602 Just t -> B.link (escapeURI $ trim t) ""
601 Just t -> B.link (escapeURI $ trim t) ""
603 $ B.image src "" alt
602 $ B.image src "" alt
604 Nothing -> B.image src "" alt
603 Nothing -> B.image src "" alt
604+ "class" -> do
605+ let attrs = ("", (splitBy isSpace $ trim top), map (\(k,v) -> (k, trimr v)) fields)
606+ -- directive content or the first immediately following element
607+ children <- case body of
608+ "" -> block
609+ _ -> parseFromString parseBlocks body'
610+ return $ B.divWith attrs children
605 _ -> return mempty
611 _ -> return mempty
606
612
607-- TODO:
613-- TODO:
tests/Tests/Readers/RST.hs+6
@@ -83,4 +83,10 @@ tests = [ "line block with blank line" =:
83 =?> codeBlock "> quoted\n> block" <> para "Ordinary paragraph"
83 =?> codeBlock "> quoted\n> block" <> para "Ordinary paragraph"
84 , "quoted literal block using | (not a line block)" =: "::\n\n| quoted\n| block\n\nOrdinary paragraph"
84 , "quoted literal block using | (not a line block)" =: "::\n\n| quoted\n| block\n\nOrdinary paragraph"
85 =?> codeBlock "| quoted\n| block" <> para "Ordinary paragraph"
85 =?> codeBlock "| quoted\n| block" <> para "Ordinary paragraph"
86+ , "class directive with single paragraph" =: ".. class:: special\n\nThis is a \"special\" paragraph."
87+ =?> divWith ("", ["special"], []) (para "This is a \"special\" paragraph.")
88+ , "class directive with two paragraphs" =: ".. class:: exceptional remarkable\n\n First paragraph.\n\n Second paragraph."
89+ =?> divWith ("", ["exceptional", "remarkable"], []) (para "First paragraph." <> para "Second paragraph.")
90+ , "class directive around literal block" =: ".. class:: classy\n\n::\n\n a\n b"
91+ =?> divWith ("", ["classy"], []) (codeBlock "a\nb")
86 ]
92 ]