git.iko.soy/typeable/pandoc
on master help sign in
Add highlight directive to the rST reader (#6140)
commit29c2670da2a267094148f3edacaed5fc258bcdd1 authorLucas Escot <flupe@users.noreply.github.com> dateThu 13 Feb 2020, 19:27 (7 years ago) parent3a181f0
4 files changed+41−4
src/Text/Pandoc/Parsing.hs+2 src/Text/Pandoc/Readers/RST.hs+11−4 test/rst-reader.native+4 test/rst-reader.rst+24
src/Text/Pandoc/Parsing.hs+2
@@ -1138,6 +1138,7 @@ data ParserState = ParserState
11381138 stateExamples :: M.Map Text Int, -- ^ Map from example labels to numbers
11391139 stateMacros :: M.Map Text Macro, -- ^ Table of macros defined so far
11401140 stateRstDefaultRole :: Text, -- ^ Current rST default interpreted text role
1141+ stateRstHighlight :: Maybe Text, -- ^ Current rST literal block language
11411142 stateRstCustomRoles :: M.Map Text (Text, Maybe Text, Attr), -- ^ Current rST custom text roles
11421143 -- Triple represents: 1) Base role, 2) Optional format (only for :raw:
11431144 -- roles), 3) Additional classes (rest of Attr is unused)).
@@ -1248,6 +1249,7 @@ defaultParserState =
12481249 stateExamples = M.empty,
12491250 stateMacros = M.empty,
12501251 stateRstDefaultRole = "title-reference",
1252+ stateRstHighlight = Nothing,
12511253 stateRstCustomRoles = M.empty,
12521254 stateCaption = Nothing,
12531255 stateInHtmlBlock = Nothing,
src/Text/Pandoc/Readers/RST.hs+11−4
@@ -23,7 +23,7 @@ import Control.Monad.Identity (Identity (..))
2323import Data.Char (isHexDigit, isSpace, toUpper, isAlphaNum)
2424import Data.List (deleteFirstsBy, elemIndex, nub, sort, transpose)
2525import qualified Data.Map as M
26−import Data.Maybe (fromMaybe)
26+import Data.Maybe (fromMaybe, maybeToList)
2727import Data.Sequence (ViewR (..), viewr)
2828import Data.Text (Text)
2929import qualified Data.Text as T
@@ -390,11 +390,13 @@ quotedBlock = try $ do
390390codeBlockStart :: Monad m => ParserT Text st m Char
391391codeBlockStart = string "::" >> blankline >> blankline
392392
393−codeBlock :: (HasReaderOptions st, Monad m) => ParserT Text st m Blocks
393+codeBlock :: Monad m => ParserT Text ParserState m Blocks
394394codeBlock = try $ codeBlockStart >> codeBlockBody
395395
396−codeBlockBody :: (HasReaderOptions st, Monad m) => ParserT Text st m Blocks
397−codeBlockBody = try $ B.codeBlock . stripTrailingNewlines <$>
396+codeBlockBody :: Monad m => ParserT Text ParserState m Blocks
397+codeBlockBody = do
398+ lang <- stateRstHighlight <$> getState
399+ try $ B.codeBlockWith ("", maybeToList lang, []) . stripTrailingNewlines <$>
398400 (indentedBlock <|> quotedBlock)
399401
400402lhsCodeBlock :: Monad m => RSTParser m Blocks
@@ -716,6 +718,11 @@ directive' = do
716718 case trim top of
717719 "" -> stateRstDefaultRole def
718720 role -> role })
721+ "highlight" -> mempty <$ updateState (\s ->
722+ s { stateRstHighlight =
723+ case trim top of
724+ "" -> stateRstHighlight def
725+ lang -> Just lang })
719726 x | x == "code" || x == "code-block" || x == "sourcecode" ->
720727 codeblock name classes
721728 (lookup "number-lines" fields) (trim top) body True
test/rst-reader.native+4
@@ -39,6 +39,10 @@ Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "John",Spa
3939,CodeBlock ("",[],[]) "this block is indented by two tabs\n\nThese should not be escaped: \\$ \\\\ \\> \\[ \\{"
4040,Para [Str "And:"]
4141,CodeBlock ("",["python"],[]) "def my_function(x):\n return x + 1"
42+,Para [Str "If",Space,Str "we",Space,Str "use",Space,Str "the",Space,Str "highlight",Space,Str "directive,",Space,Str "we",Space,Str "can",Space,Str "specify",Space,Str "a",Space,Str "default",Space,Str "language",SoftBreak,Str "for",Space,Str "literate",Space,Str "blocks."]
43+,CodeBlock ("",["haskell"],[]) "-- this code is in haskell\ndata Tree = Leaf | Node Tree Tree"
44+,CodeBlock ("",["haskell"],[]) "-- this code is in haskell too\ndata Nat = Zero | Succ Nat"
45+,CodeBlock ("",["javascript"],[]) "-- this code is in javascript\nlet f = (x, y) => x + y"
4246,Header 1 ("lists",[],[]) [Str "Lists"]
4347,Header 2 ("unordered",[],[]) [Str "Unordered"]
4448,Para [Str "Asterisks",Space,Str "tight:"]
test/rst-reader.rst+24
@@ -107,6 +107,30 @@ And:
107107 def my_function(x):
108108 return x + 1
109109
110+If we use the highlight directive, we can specify a default language
111+for literate blocks.
112+
113+.. highlight:: haskell
114+
115+::
116+
117+ -- this code is in haskell
118+ data Tree = Leaf | Node Tree Tree
119+
120+::
121+
122+ -- this code is in haskell too
123+ data Nat = Zero | Succ Nat
124+
125+.. highlight:: javascript
126+
127+::
128+
129+ -- this code is in javascript
130+ let f = (x, y) => x + y
131+
132+.. highlight::
133+
110134Lists
111135=====
112136