From 490c2b543d51603663178c99aea88c1a1fdd2838 Mon Sep 17 00:00:00 2001 From: Sidharth Kapur Date: Sun, 1 May 2016 21:42:58 -0500 Subject: Add class option for code block in RST reader According to http://docutils.sourceforge.net/docs/ref/rst/directives.html#code, the code directive supports the ":class:" option. --- src/Text/Pandoc/Readers/RST.hs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Text/Pandoc/Readers/RST.hs b/src/Text/Pandoc/Readers/RST.hs index 7be0cd392..296c55f32 100644 --- a/src/Text/Pandoc/Readers/RST.hs +++ b/src/Text/Pandoc/Readers/RST.hs @@ -586,8 +586,9 @@ directive' = do case trim top of "" -> stateRstDefaultRole def role -> role }) - "code" -> codeblock (lookup "number-lines" fields) (trim top) body - "code-block" -> codeblock (lookup "number-lines" fields) (trim top) body + x | x == "code" || x == "code-block" -> + codeblock (words $ fromMaybe [] $ lookup "class" fields) + (lookup "number-lines" fields) (trim top) body "aafig" -> do let attribs = ("", ["aafig"], map (\(k,v) -> (k, trimr v)) fields) return $ B.codeBlockWith attribs $ stripTrailingNewlines body @@ -713,12 +714,13 @@ toChunks = dropWhile null . map (trim . unlines) . splitBy (all (`elem` (" \t" :: String))) . lines -codeblock :: Maybe String -> String -> String -> RSTParser Blocks -codeblock numberLines lang body = +codeblock :: [String] -> Maybe String -> String -> String -> RSTParser Blocks +codeblock classes numberLines lang body = return $ B.codeBlockWith attribs $ stripTrailingNewlines body - where attribs = ("", classes, kvs) - classes = "sourceCode" : lang + where attribs = ("", classes', kvs) + classes' = "sourceCode" : lang : maybe [] (\_ -> ["numberLines"]) numberLines + ++ classes kvs = case numberLines of Just "" -> [] Nothing -> [] -- cgit v1.2.3 From 72d1900b30d20e369df5f4201e224e8edf314386 Mon Sep 17 00:00:00 2001 From: Sidharth Kapur Date: Sun, 1 May 2016 22:23:33 -0500 Subject: Add test for RST code directive class --- tests/Tests/Readers/RST.hs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/Tests/Readers/RST.hs b/tests/Tests/Readers/RST.hs index ea85a5929..8fa2e4828 100644 --- a/tests/Tests/Readers/RST.hs +++ b/tests/Tests/Readers/RST.hs @@ -8,6 +8,7 @@ import Tests.Arbitrary() import Text.Pandoc.Builder import Text.Pandoc import Text.Pandoc.Error +import qualified Data.Sequence as Seq rst :: String -> Pandoc rst = handleError . readRST def{ readerStandalone = True } @@ -94,6 +95,20 @@ tests = [ "line block with blank line" =: ("A-1-B_2_C:3:D+4+E.5.F_\n\n" ++ ".. _A-1-B_2_C:3:D+4+E.5.F: https://example.com\n") =?> para (link "https://example.com" "" "A-1-B_2_C:3:D+4+E.5.F") + , "Code directive with class and number-lines" =: unlines + [ ".. code::python" + , " :number-lines: 34" + , " :class: class1 class2 class3" + , "" + , " def func(x):" + , " return y" + ] =?> + ( doc . Many . Seq.singleton $ + CodeBlock ( "" + , ["sourceCode", "python", "numberLines", "class1", "class2", "class3"] + , [ ("startFrom", "34") ] + ) "def func(x):\n return y" + ) , testGroup "literal / line / code blocks" [ "indented literal block" =: unlines [ "::" -- cgit v1.2.3 From d7bc8c06324a0ca0ce32ece35a5dc157dadc4187 Mon Sep 17 00:00:00 2001 From: Sidharth Kapur Date: Sun, 1 May 2016 22:32:26 -0500 Subject: Use `codeBlockWith` --- tests/Tests/Readers/RST.hs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Tests/Readers/RST.hs b/tests/Tests/Readers/RST.hs index 8fa2e4828..0fc8ecc32 100644 --- a/tests/Tests/Readers/RST.hs +++ b/tests/Tests/Readers/RST.hs @@ -8,7 +8,6 @@ import Tests.Arbitrary() import Text.Pandoc.Builder import Text.Pandoc import Text.Pandoc.Error -import qualified Data.Sequence as Seq rst :: String -> Pandoc rst = handleError . readRST def{ readerStandalone = True } @@ -103,11 +102,12 @@ tests = [ "line block with blank line" =: , " def func(x):" , " return y" ] =?> - ( doc . Many . Seq.singleton $ - CodeBlock ( "" - , ["sourceCode", "python", "numberLines", "class1", "class2", "class3"] - , [ ("startFrom", "34") ] - ) "def func(x):\n return y" + ( doc $ codeBlockWith + ( "" + , ["sourceCode", "python", "numberLines", "class1", "class2", "class3"] + , [ ("startFrom", "34") ] + ) + "def func(x):\n return y" ) , testGroup "literal / line / code blocks" [ "indented literal block" =: unlines -- cgit v1.2.3 From ff489a59f4158a95fb01d1164b5fd2b865586e73 Mon Sep 17 00:00:00 2001 From: Sidharth Kapur Date: Sun, 1 May 2016 22:36:19 -0500 Subject: Add one more test --- tests/Tests/Readers/RST.hs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/Tests/Readers/RST.hs b/tests/Tests/Readers/RST.hs index 0fc8ecc32..622f5e48b 100644 --- a/tests/Tests/Readers/RST.hs +++ b/tests/Tests/Readers/RST.hs @@ -109,6 +109,20 @@ tests = [ "line block with blank line" =: ) "def func(x):\n return y" ) + , "Code directive with number-lines, no line specified" =: unlines + [ ".. code::python" + , " :number-lines: " + , "" + , " def func(x):" + , " return y" + ] =?> + ( doc $ codeBlockWith + ( "" + , ["sourceCode", "python", "numberLines"] + , [ ("startFrom", "") ] + ) + "def func(x):\n return y" + ) , testGroup "literal / line / code blocks" [ "indented literal block" =: unlines [ "::" -- cgit v1.2.3