diff options
author | John MacFarlane <jgm@berkeley.edu> | 2020-06-17 09:57:56 -0700 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2020-06-17 09:57:56 -0700 |
commit | a8b3117e049f0a5821f1512f6c41e285b01efd25 (patch) | |
tree | a7580b3aa577fb68d5983a117acd56278f955eab /src/Text/Pandoc/Readers | |
parent | bf95282436436061341c80a41df585a33c849fec (diff) | |
download | pandoc-a8b3117e049f0a5821f1512f6c41e285b01efd25.tar.gz |
RST reader: pass arbitrary attributes through in code blocks.
Exceptions: name (which becomes the id), class (which becomes the
classes), and number-lines (which is treated specially to fit
with pandoc highlighting).
Closes #6465.
Diffstat (limited to 'src/Text/Pandoc/Readers')
-rw-r--r-- | src/Text/Pandoc/Readers/RST.hs | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/src/Text/Pandoc/Readers/RST.hs b/src/Text/Pandoc/Readers/RST.hs index 4acdc10c2..e558836a1 100644 --- a/src/Text/Pandoc/Readers/RST.hs +++ b/src/Text/Pandoc/Readers/RST.hs @@ -21,7 +21,7 @@ import Control.Monad.Identity (Identity (..)) import Data.Char (isHexDigit, isSpace, toUpper, isAlphaNum) import Data.List (deleteFirstsBy, elemIndex, nub, sort, transpose) import qualified Data.Map as M -import Data.Maybe (fromMaybe, maybeToList) +import Data.Maybe (fromMaybe, maybeToList, isJust) import Data.Sequence (ViewR (..), viewr) import Data.Text (Text) import qualified Data.Text as T @@ -487,10 +487,9 @@ includeDirective top fields body = do let contents' = T.unlines contentLines'' <> "\n" case lookup "code" fields of Just lang -> do - let numberLines = lookup "number-lines" fields let classes = maybe [] T.words (lookup "class" fields) let ident = maybe "" trimr $ lookup "name" fields - codeblock ident classes numberLines (trimr lang) contents' False + codeblock ident classes fields (trimr lang) contents' False Nothing -> case lookup "literal" fields of Just _ -> return $ B.rawBlock "rst" contents' Nothing -> do @@ -731,8 +730,8 @@ directive' = do "" -> stateRstHighlight def lang -> Just lang }) x | x == "code" || x == "code-block" || x == "sourcecode" -> - codeblock name classes - (lookup "number-lines" fields) (trim top) body True + codeblock name classes (map (second trimr) fields) + (trim top) body True "aafig" -> do let attribs = (name, ["aafig"], map (second trimr) fields) return $ B.codeBlockWith attribs $ stripTrailingNewlines body @@ -1007,21 +1006,22 @@ toChunks = dropWhile T.null then "\\begin{aligned}\n" <> s <> "\n\\end{aligned}" else s -codeblock :: Text -> [Text] -> Maybe Text -> Text -> Text -> Bool +codeblock :: Text -> [Text] -> [(Text, Text)] -> Text -> Text -> Bool -> RSTParser m Blocks -codeblock ident classes numberLines lang body rmTrailingNewlines = +codeblock ident classes fields lang body rmTrailingNewlines = do return $ B.codeBlockWith attribs $ stripTrailingNewlines' body where stripTrailingNewlines' = if rmTrailingNewlines then stripTrailingNewlines else id attribs = (ident, classes', kvs) classes' = lang - : maybe [] (const ["numberLines"]) numberLines + : ["numberLines" | isJust (lookup "number-lines" fields)] ++ classes - kvs = maybe [] (\n -> case trimr n of - "" -> [] - xs -> [("startFrom", xs)]) - numberLines + kvs = [(k,v) | (k,v) <- fields, k /= "number-lines", k /= "class", + k /= "id", k /= "name"] + ++ case lookup "number-lines" fields of + Just v | not (T.null v) -> [("startFrom", v)] + _ -> [] --- --- note block |