aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2012-03-25 11:19:57 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2012-03-25 11:19:57 -0700
commita2e5de1d1180c196f6b4bdfdfcadc80911e2b7c6 (patch)
treeb53fcae960d48c8984e1cc41039a5a9369fa8611
parent3c4e1ff063100cbdf27f911fcbedbb842adf2af4 (diff)
parent4a1ba8ce49ffc3c2a154721dc18fc48896f97f59 (diff)
downloadpandoc-a2e5de1d1180c196f6b4bdfdfcadc80911e2b7c6.tar.gz
Merge pull request #456 from gmaslov/master
Implement the RST default-role directive
-rw-r--r--src/Text/Pandoc/Parsing.hs6
-rw-r--r--src/Text/Pandoc/Readers/RST.hs37
-rw-r--r--tests/rst-reader.native10
-rw-r--r--tests/rst-reader.rst28
4 files changed, 75 insertions, 6 deletions
diff --git a/src/Text/Pandoc/Parsing.hs b/src/Text/Pandoc/Parsing.hs
index 725621ce2..22a8d4d50 100644
--- a/src/Text/Pandoc/Parsing.hs
+++ b/src/Text/Pandoc/Parsing.hs
@@ -652,7 +652,8 @@ data ParserState = ParserState
stateExamples :: M.Map String Int, -- ^ Map from example labels to numbers
stateHasChapters :: Bool, -- ^ True if \chapter encountered
stateApplyMacros :: Bool, -- ^ Apply LaTeX macros?
- stateMacros :: [Macro] -- ^ List of macros defined so far
+ stateMacros :: [Macro], -- ^ List of macros defined so far
+ stateRstDefaultRole :: String -- ^ Current rST default interpreted text role
}
deriving Show
@@ -682,7 +683,8 @@ defaultParserState =
stateExamples = M.empty,
stateHasChapters = False,
stateApplyMacros = True,
- stateMacros = []}
+ stateMacros = [],
+ stateRstDefaultRole = "title-reference"}
data HeaderType
= SingleHeader Char -- ^ Single line of characters underneath
diff --git a/src/Text/Pandoc/Readers/RST.hs b/src/Text/Pandoc/Readers/RST.hs
index 30d9aae44..35fe5d768 100644
--- a/src/Text/Pandoc/Readers/RST.hs
+++ b/src/Text/Pandoc/Readers/RST.hs
@@ -129,6 +129,7 @@ block = choice [ codeBlock
, imageBlock
, customCodeBlock
, mathBlock
+ , defaultRoleBlock
, unknownDirective
, header
, hrule
@@ -533,6 +534,24 @@ bulletList = many1 (listItem bulletListStart) >>=
return . BulletList . compactify
--
+-- default-role block
+--
+
+defaultRoleBlock :: GenParser Char ParserState Block
+defaultRoleBlock = try $ do
+ string ".. default-role::"
+ -- doesn't enforce any restrictions on the role name; embedded spaces shouldn't be allowed, for one
+ role <- manyTill anyChar newline >>= return . removeLeadingTrailingSpace
+ updateState $ \s -> s { stateRstDefaultRole =
+ if null role
+ then stateRstDefaultRole defaultParserState
+ else role
+ }
+ -- skip body of the directive if it exists
+ many $ blanklines <|> (spaceChar >> manyTill anyChar newline)
+ return Null
+
+--
-- unknown directive (e.g. comment)
--
@@ -805,13 +824,25 @@ strong :: GenParser Char ParserState Inline
strong = enclosed (string "**") (try $ string "**") inline >>=
return . Strong . normalizeSpaces
-interpreted :: [Char] -> GenParser Char st [Char]
+-- Parses inline interpreted text which is required to have the given role.
+-- This decision is based on the role marker (if present),
+-- and the current default interpreted text role.
+interpreted :: [Char] -> GenParser Char ParserState [Char]
interpreted role = try $ do
+ state <- getState
+ if role == stateRstDefaultRole state
+ then try markedInterpretedText <|> unmarkedInterpretedText
+ else markedInterpretedText
+ where
+ markedInterpretedText = try (roleMarker >> unmarkedInterpretedText)
+ <|> (unmarkedInterpretedText >>= (\txt -> roleMarker >> return txt))
+ roleMarker = string $ ":" ++ role ++ ":"
-- Note, this doesn't precisely implement the complex rule in
-- http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#inline-markup-recognition-rules
-- but it should be good enough for most purposes
- result <- enclosed (string $ ":" ++ role ++ ":`") (char '`') anyChar
- return result
+ unmarkedInterpretedText = do
+ result <- enclosed (char '`') (char '`') anyChar
+ return result
superscript :: GenParser Char ParserState Inline
superscript = interpreted "sup" >>= \x -> return (Superscript [Str x])
diff --git a/tests/rst-reader.native b/tests/rst-reader.native
index e0eb4d438..bf794c849 100644
--- a/tests/rst-reader.native
+++ b/tests/rst-reader.native
@@ -312,4 +312,12 @@ Pandoc (Meta {docTitle = [Str "Pandoc",Space,Str "Test",Space,Str "Suite",Str ":
,Para [Math DisplayMath "E = mc^2"]
,Para [Math DisplayMath "E = mc^2",Math DisplayMath "\\alpha = \\beta"]
,Para [Math DisplayMath "E &= mc^2\\\\\nF &= \\pi E",Math DisplayMath "F &= \\gamma \\alpha^2"]
-,Para [Str "All",Space,Str "done",Str "."]]
+,Para [Str "All",Space,Str "done",Str "."]
+,Header 1 [Str "Default",Str "-",Str "Role"]
+,Para [Str "Try",Space,Str "changing",Space,Str "the",Space,Str "default",Space,Str "role",Space,Str "to",Space,Str "a",Space,Str "few",Space,Str "different",Space,Str "things",Str "."]
+,Header 2 [Str "Doesn",Str "\8217",Str "t",Space,Str "Break",Space,Str "Title",Space,Str "Parsing"]
+,Para [Str "Inline",Space,Str "math",Str ":",Space,Math InlineMath "E=mc^2",Space,Str "or",Space,Math InlineMath "E=mc^2",Space,Str "or",Space,Math InlineMath "E=mc^2",Str ".",Space,Str "Other",Space,Str "roles",Str ":",Space,Superscript [Str "super"],Str ",",Space,Subscript [Str "sub"],Str "."]
+,Para [Math DisplayMath "\\alpha = beta",Math DisplayMath "E = mc^2"]
+,Para [Str "Some",Space,Superscript [Str "of"],Space,Str "these",Space,Superscript [Str "words"],Space,Str "are",Space,Str "in",Space,Superscript [Str "superscript"],Str "."]
+,Para [Str "Reset",Space,Str "default",Str "-",Str "role",Space,Str "to",Space,Str "the",Space,Str "default",Space,Str "default",Str "."]
+,Para [Str "And",Space,Str "now",Space,Str "`",Str "some",Str "-",Str "invalid",Str "-",Str "string",Str "-",Str "3231231",Str "`",Space,Str "is",Space,Str "nonsense",Str "."]]
diff --git a/tests/rst-reader.rst b/tests/rst-reader.rst
index cfebd2054..abe6d4f69 100644
--- a/tests/rst-reader.rst
+++ b/tests/rst-reader.rst
@@ -565,3 +565,31 @@ display math:
All done.
+Default-Role
+============
+
+Try changing the default role to a few different things.
+
+.. default-role:: math
+
+Doesn't Break Title Parsing
+---------------------------
+
+Inline math: `E=mc^2` or :math:`E=mc^2` or `E=mc^2`:math:.
+Other roles: :sup:`super`, `sub`:sub:.
+
+.. math::
+ \alpha = beta
+
+ E = mc^2
+
+.. default-role:: sup
+
+Some `of` these :sup:`words` are in `superscript`:sup:.
+
+Reset default-role to the default default.
+
+.. default-role::
+
+And now `some-invalid-string-3231231` is nonsense.
+