git.iko.soy/typeable/pandoc
on master help sign in
parse RST class directives

The class directive accepts one or more class names, and creates a Div value with those classes. If the directive has an indented body, the body is parsed as the children of the Div. If not, the first block folowing the directive is made a child of the Div. This differs from the behavior of rst2xml, which does not create a Div element. Instead, the specified classes are applied to each child of the directive. However, most Pandoc Block constructors to not take an Attr argument, so we can't duplicate this behavior.

commit74c1b547c247bbe2adab0fc6857cd78eaef918c3 authorDaniel Bergey <bergey@alum.mit.edu> dateMon 1 Dec 2014, 18:18 (12 years ago) parent2cdfa5e
2 files changed+14−2
src/Text/Pandoc/Readers/RST.hs+8−2 tests/Tests/Readers/RST.hs+6
src/Text/Pandoc/Readers/RST.hs+8−2
@@ -47,7 +47,7 @@ import Text.Pandoc.Builder (Inlines, Blocks, trimInlines, (<>))
4747import qualified Text.Pandoc.Builder as B
4848import Data.Monoid (mconcat, mempty)
4949import Data.Sequence (viewr, ViewR(..))
50−import Data.Char (toLower, isHexDigit)
50+import Data.Char (toLower, isHexDigit, isSpace)
5151
5252-- | Parse reStructuredText string and return Pandoc document.
5353readRST :: ReaderOptions -- ^ Reader options
@@ -521,7 +521,6 @@ directive = try $ do
521521-- TODO: line-block, parsed-literal, table, csv-table, list-table
522522-- date
523523-- include
524−-- class
525524-- title
526525directive' :: RSTParser Blocks
527526directive' = do
@@ -602,6 +601,13 @@ directive' = do
602601 Just t -> B.link (escapeURI $ trim t) ""
603602 $ B.image src "" alt
604603 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
605611 _ -> return mempty
606612
607613-- TODO:
tests/Tests/Readers/RST.hs+6
@@ -83,4 +83,10 @@ tests = [ "line block with blank line" =:
8383 =?> codeBlock "> quoted\n> block" <> para "Ordinary paragraph"
8484 , "quoted literal block using | (not a line block)" =: "::\n\n| quoted\n| block\n\nOrdinary paragraph"
8585 =?> 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")
8692 ]