diff options
-rw-r--r-- | src/Text/Pandoc/Readers/RST.hs | 14 | ||||
-rw-r--r-- | tests/Tests/Readers/RST.hs | 29 |
2 files changed, 37 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 -> [] diff --git a/tests/Tests/Readers/RST.hs b/tests/Tests/Readers/RST.hs index ea85a5929..622f5e48b 100644 --- a/tests/Tests/Readers/RST.hs +++ b/tests/Tests/Readers/RST.hs @@ -94,6 +94,35 @@ 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 $ codeBlockWith + ( "" + , ["sourceCode", "python", "numberLines", "class1", "class2", "class3"] + , [ ("startFrom", "34") ] + ) + "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 [ "::" |