git.iko.soy/typeable/pandoc
on master help sign in
RST reader include: handle negative values for start-, end-line.
d595702John MacFarlane
src/Text/Pandoc/Readers/RST.hs+30−23
@@ -405,28 +405,25 @@ blockQuote = do
405From RST docs:
405From RST docs:
406The following options are recognized:
406The following options are recognized:
407
407
408−start-line : integer
408+[x] start-line : integer
409−Only the content starting from this line will be included. (As usual in Python, the first line has index 0 and negative values count from the end.)
410−end-line : integer
409+[x] end-line : integer
411−Only the content up to (but excluding) this line will be included.
412−start-after : text to find in the external data file
410+[ ] start-after : text to find in the external data file
413−Only the content after the first occurrence of the specified text will be included.
411+ Only the content after the first occurrence of the specified text will be included.
414−end-before : text to find in the external data file
412+[ ] end-before : text to find in the external data file
415−Only the content before the first occurrence of the specified text (but after any after text) will be included.
413+ Only the content before the first occurrence of the specified text (but after any after text) will be included.
414+ Combining start/end-line and start-after/end-before is possible. The text markers will be searched in the specified lines (further limiting the included content).
416−literal : flag (empty)
415+[ ] literal : flag (empty)
417−The entire included text is inserted into the document as a single literal block.
416+ The entire included text is inserted into the document as a single literal block.
418−code : formal language (optional)
417+[ ] code : formal language (optional)
419−The argument and the content of the included file are passed to the code directive (useful for program listings). (New in Docutils 0.9)
418+ The argument and the content of the included file are passed to the code directive (useful for program listings). (New in Docutils 0.9)
420−number-lines : [start line number]
419+[ ] number-lines : [start line number]
421−Precede every code line with a line number. The optional argument is the number of the first line (defaut 1). Works only with code or literal. (New in Docutils 0.9)
420+ Precede every code line with a line number. The optional argument is the number of the first line (defaut 1). Works only with code or literal. (New in Docutils 0.9)
422−encoding : name of text encoding
421+[ ] encoding : name of text encoding
423The text encoding of the external data file. Defaults to the document's input_encoding.
422The text encoding of the external data file. Defaults to the document's input_encoding.
424−tab-width : integer
423+[ ] tab-width : integer
425Number of spaces for hard tab expansion. A negative value prevents expansion of hard tabs. Defaults to the tab_width configuration setting.
424Number of spaces for hard tab expansion. A negative value prevents expansion of hard tabs. Defaults to the tab_width configuration setting.
426−With code or literal the common options :class: and :name: are recognized as well.
425+[ ] class (common option) - with code or literal
427−
426+[ ] name (common option) - with code or literal
428−Combining start/end-line and start-after/end-before is possible. The text markers will be searched in the specified lines (further limiting the included content).
429−
430-}
427-}
431
428
432include :: PandocMonad m => RSTParser m Blocks
429include :: PandocMonad m => RSTParser m Blocks
@@ -451,9 +448,19 @@ include = try $ do
451 Left _e -> do
448 Left _e -> do
452 lift $ warning $ "Could not read include file " ++ f ++ "."
449 lift $ warning $ "Could not read include file " ++ f ++ "."
453 return ""
450 return ""
451+ let contentLines = lines contents
452+ let numLines = length contentLines
453+ let startLine' = case startLine of
454+ Nothing -> 1
455+ Just x | x >= 0 -> x
456+ | otherwise -> numLines + x -- negative from end
457+ let endLine' = case endLine of
458+ Nothing -> numLines + 1
459+ Just x | x >= 0 -> x
460+ | otherwise -> numLines + x -- negative from end
454− let contents' = unlines $ maybe id (drop . (\x -> x - 1)) startLine
461+ let contents' = unlines $ drop (startLine' - 1)
455− $ maybe id (take . (\x -> x - 1)) endLine
462+ $ take (endLine' - 1)
456− $ lines contents
463+ $ contentLines
457 setPosition $ newPos f 1 1
464 setPosition $ newPos f 1 1
458 setInput contents'
465 setInput contents'
459 bs <- optional blanklines >> (mconcat <$> many block)
466 bs <- optional blanklines >> (mconcat <$> many block)