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.
This commit is contained in:
Brian Leung 2019-01-10 07:19:26 +01:00 committed by John MacFarlane
parent 483012552a
commit 35971495ab
5 changed files with 29 additions and 17 deletions

View file

@ -189,6 +189,7 @@ extra-source-files:
test/command/*.md
test/command/3533-rst-csv-tables.csv
test/command/3880.txt
test/command/5182.txt
test/command/abbrevs
test/command/SVG_logo-without-xml-declaration.svg
test/command/SVG_logo.svg

View file

@ -41,7 +41,7 @@ import Data.Char (isHexDigit, isSpace, toLower, toUpper, isAlphaNum)
import Data.List (deleteFirstsBy, elemIndex, intercalate, isInfixOf, isSuffixOf,
nub, sort, transpose)
import qualified Data.Map as M
import Data.Maybe (fromMaybe, isJust)
import Data.Maybe (fromMaybe)
import Data.Sequence (ViewR (..), viewr)
import Data.Text (Text)
import qualified Data.Text as T
@ -503,12 +503,9 @@ includeDirective top fields body = do
case lookup "code" fields of
Just lang -> do
let numberLines = lookup "number-lines" fields
let classes = trimr lang : ["numberLines" | isJust numberLines] ++
maybe [] words (lookup "class" fields)
let kvs = maybe [] (\n -> [("startFrom", trimr n)]) numberLines
let classes = maybe [] words (lookup "class" fields)
let ident = maybe "" trimr $ lookup "name" fields
let attribs = (ident, classes, kvs)
return $ B.codeBlockWith attribs contents'
codeblock ident classes numberLines (trimr lang) contents' False
Nothing -> case lookup "literal" fields of
Just _ -> return $ B.rawBlock "rst" contents'
Nothing -> do
@ -739,7 +736,7 @@ directive' = do
role -> role })
x | x == "code" || x == "code-block" || x == "sourcecode" ->
codeblock name classes
(lookup "number-lines" fields) (trim top) body
(lookup "number-lines" fields) (trim top) body True
"aafig" -> do
let attribs = (name, ["aafig"], map (second trimr) fields)
return $ B.codeBlockWith attribs $ stripTrailingNewlines body
@ -990,18 +987,21 @@ toChunks = dropWhile null
then "\\begin{aligned}\n" ++ s ++ "\n\\end{aligned}"
else s
codeblock :: String -> [String] -> Maybe String -> String -> String
codeblock :: String -> [String] -> Maybe String -> String -> String -> Bool
-> RSTParser m Blocks
codeblock ident classes numberLines lang body =
return $ B.codeBlockWith attribs $ stripTrailingNewlines body
where attribs = (ident, classes', kvs)
codeblock ident classes numberLines lang body rmTrailingNewlines =
return $ B.codeBlockWith attribs $ stripTrailingNewlines' body
where stripTrailingNewlines' = if rmTrailingNewlines
then stripTrailingNewlines
else id
attribs = (ident, classes', kvs)
classes' = lang
: maybe [] (const ["numberLines"]) numberLines
++ classes
kvs = case numberLines of
Just "" -> []
Nothing -> []
Just n -> [("startFrom",trim n)]
kvs = maybe [] (\n -> case trimr n of
[] -> []
xs -> [("startFrom", xs)])
numberLines
---
--- note block

View file

@ -112,7 +112,7 @@ tests = [ "line block with blank line" =:
"def func(x):\n return y")
, "Code directive with number-lines, no line specified" =: T.unlines
[ ".. code::python"
, " :number-lines: "
, " :number-lines:"
, ""
, " def func(x):"
, " return y"
@ -120,7 +120,7 @@ tests = [ "line block with blank line" =:
doc (codeBlockWith
( ""
, ["python", "numberLines"]
, [ ("startFrom", "") ]
, []
)
"def func(x):\n return y")
, testGroup "literal / line / code blocks"

6
test/command/5182.md Normal file
View file

@ -0,0 +1,6 @@
```
pandoc -f rst -t native
.. include:: command/5182.txt
^D
[CodeBlock ("",["python","numberLines"],[]) "def func(x):\n return y"]
```

5
test/command/5182.txt Normal file
View file

@ -0,0 +1,5 @@
.. code::python
:number-lines:
def func(x):
return y