git.iko.soy/typeable/pandoc
on master help sign in
RST reader: change treatment of `number-lines` directives. (#5207)

Directives of this type without numeric inputs should not have a `startFrom` attribute; with a blank value, the writers can produce extra whitespace.

commit35971495abbc15342f0035040f964ed4dd3b0c5d authorBrian Leung <bkleung89@gmail.com> dateThu 10 Jan 2019, 07:19 (8 years ago) parent4830125
5 files changed+29−17
pandoc.cabal+1 src/Text/Pandoc/Readers/RST.hs+15−15 test/Tests/Readers/RST.hs+2−2 test/command/5182.md+6 test/command/5182.txt+5
pandoc.cabal+1
@@ -189,6 +189,7 @@ extra-source-files:
189189 test/command/*.md
190190 test/command/3533-rst-csv-tables.csv
191191 test/command/3880.txt
192+ test/command/5182.txt
192193 test/command/abbrevs
193194 test/command/SVG_logo-without-xml-declaration.svg
194195 test/command/SVG_logo.svg
src/Text/Pandoc/Readers/RST.hs+15−15
@@ -41,7 +41,7 @@ import Data.Char (isHexDigit, isSpace, toLower, toUpper, isAlphaNum)
4141import Data.List (deleteFirstsBy, elemIndex, intercalate, isInfixOf, isSuffixOf,
4242 nub, sort, transpose)
4343import qualified Data.Map as M
44−import Data.Maybe (fromMaybe, isJust)
44+import Data.Maybe (fromMaybe)
4545import Data.Sequence (ViewR (..), viewr)
4646import Data.Text (Text)
4747import qualified Data.Text as T
@@ -503,12 +503,9 @@ includeDirective top fields body = do
503503 case lookup "code" fields of
504504 Just lang -> do
505505 let numberLines = lookup "number-lines" fields
506− let classes = trimr lang : ["numberLines" | isJust numberLines] ++
507− maybe [] words (lookup "class" fields)
508− let kvs = maybe [] (\n -> [("startFrom", trimr n)]) numberLines
506+ let classes = maybe [] words (lookup "class" fields)
509507 let ident = maybe "" trimr $ lookup "name" fields
510− let attribs = (ident, classes, kvs)
511− return $ B.codeBlockWith attribs contents'
508+ codeblock ident classes numberLines (trimr lang) contents' False
512509 Nothing -> case lookup "literal" fields of
513510 Just _ -> return $ B.rawBlock "rst" contents'
514511 Nothing -> do
@@ -739,7 +736,7 @@ directive' = do
739736 role -> role })
740737 x | x == "code" || x == "code-block" || x == "sourcecode" ->
741738 codeblock name classes
742− (lookup "number-lines" fields) (trim top) body
739+ (lookup "number-lines" fields) (trim top) body True
743740 "aafig" -> do
744741 let attribs = (name, ["aafig"], map (second trimr) fields)
745742 return $ B.codeBlockWith attribs $ stripTrailingNewlines body
@@ -990,18 +987,21 @@ toChunks = dropWhile null
990987 then "\\begin{aligned}\n" ++ s ++ "\n\\end{aligned}"
991988 else s
992989
993−codeblock :: String -> [String] -> Maybe String -> String -> String
990+codeblock :: String -> [String] -> Maybe String -> String -> String -> Bool
994991 -> RSTParser m Blocks
995−codeblock ident classes numberLines lang body =
996− return $ B.codeBlockWith attribs $ stripTrailingNewlines body
997− where attribs = (ident, classes', kvs)
992+codeblock ident classes numberLines lang body rmTrailingNewlines =
993+ return $ B.codeBlockWith attribs $ stripTrailingNewlines' body
994+ where stripTrailingNewlines' = if rmTrailingNewlines
995+ then stripTrailingNewlines
996+ else id
997+ attribs = (ident, classes', kvs)
998998 classes' = lang
999999 : maybe [] (const ["numberLines"]) numberLines
10001000 ++ classes
1001− kvs = case numberLines of
1002− Just "" -> []
1003− Nothing -> []
1004− Just n -> [("startFrom",trim n)]
1001+ kvs = maybe [] (\n -> case trimr n of
1002+ [] -> []
1003+ xs -> [("startFrom", xs)])
1004+ numberLines
10051005
10061006---
10071007--- note block
test/Tests/Readers/RST.hs+2−2
@@ -112,7 +112,7 @@ tests = [ "line block with blank line" =:
112112 "def func(x):\n return y")
113113 , "Code directive with number-lines, no line specified" =: T.unlines
114114 [ ".. code::python"
115− , " :number-lines: "
115+ , " :number-lines:"
116116 , ""
117117 , " def func(x):"
118118 , " return y"
@@ -120,7 +120,7 @@ tests = [ "line block with blank line" =:
120120 doc (codeBlockWith
121121 ( ""
122122 , ["python", "numberLines"]
123− , [ ("startFrom", "") ]
123+ , []
124124 )
125125 "def func(x):\n return y")
126126 , testGroup "literal / line / code blocks"
test/command/5182.mdnew file+6
@@ -0,0 +1,6 @@
1+```
2+pandoc -f rst -t native
3+.. include:: command/5182.txt
4+^D
5+[CodeBlock ("",["python","numberLines"],[]) "def func(x):\n return y"]
6+```
test/command/5182.txtnew file+5
@@ -0,0 +1,5 @@
1+.. code::python
2+ :number-lines:
3+
4+ def func(x):
5+ return y