RST reader: Consolidate super/subscript, math into interpretedRole.
This commit is contained in:
parent
9366d8681d
commit
e8260c27e1
2 changed files with 27 additions and 36 deletions
|
@ -854,9 +854,6 @@ inline = choice [ whitespace
|
|||
, emph
|
||||
, code
|
||||
, subst
|
||||
, superscript
|
||||
, subscript
|
||||
, math
|
||||
, interpretedRole
|
||||
, note
|
||||
, smart
|
||||
|
@ -907,42 +904,36 @@ strong :: RSTParser Inlines
|
|||
strong = B.strong . trimInlines . mconcat <$>
|
||||
enclosed (atStart $ string "**") (try $ string "**") inline
|
||||
|
||||
-- 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] -> RSTParser [Char]
|
||||
interpreted role = try $ do
|
||||
state <- getState
|
||||
if role == stateRstDefaultRole state
|
||||
then try markedInterpretedText <|> unmarkedInterpretedText
|
||||
else markedInterpretedText
|
||||
where
|
||||
markedInterpretedText = try (roleMarker *> unmarkedInterpretedText)
|
||||
<|> (unmarkedInterpretedText <* roleMarker)
|
||||
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
|
||||
-- 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
|
||||
interpretedRole :: RSTParser Inlines
|
||||
interpretedRole = try $ do
|
||||
(role, contents) <- roleBefore <|> roleAfter
|
||||
case role of
|
||||
"sup" -> return $ B.superscript $ B.str contents
|
||||
"sub" -> return $ B.subscript $ B.str contents
|
||||
"math" -> return $ B.math contents
|
||||
_ -> return $ B.str contents --unknown
|
||||
|
||||
roleMarker :: RSTParser String
|
||||
roleMarker = char ':' *> many1Till (letter <|> char '-') (char ':')
|
||||
|
||||
roleBefore :: RSTParser (String,String)
|
||||
roleBefore = try $ do
|
||||
role <- roleMarker
|
||||
contents <- unmarkedInterpretedText
|
||||
return (role,contents)
|
||||
|
||||
roleAfter :: RSTParser (String,String)
|
||||
roleAfter = try $ do
|
||||
contents <- unmarkedInterpretedText
|
||||
role <- roleMarker <|> (stateRstDefaultRole <$> getState)
|
||||
return (role,contents)
|
||||
|
||||
unmarkedInterpretedText :: RSTParser [Char]
|
||||
unmarkedInterpretedText = enclosed (atStart $ char '`') (char '`') anyChar
|
||||
|
||||
-- For unknown interpreted roles, we just ignore the role.
|
||||
interpretedRole :: RSTParser Inlines
|
||||
interpretedRole = try $ B.str <$>
|
||||
( (roleMarker *> unmarkedInterpretedText)
|
||||
<|> (unmarkedInterpretedText <* roleMarker) )
|
||||
where roleMarker = char ':' *> many1Till (letter <|> char '-') (char ':')
|
||||
|
||||
superscript :: RSTParser Inlines
|
||||
superscript = B.superscript . B.str <$> interpreted "sup"
|
||||
|
||||
subscript :: RSTParser Inlines
|
||||
subscript = B.subscript . B.str <$> interpreted "sub"
|
||||
|
||||
math :: RSTParser Inlines
|
||||
math = B.math <$> interpreted "math"
|
||||
|
||||
whitespace :: RSTParser Inlines
|
||||
whitespace = B.space <$ skipMany1 spaceChar <?> "whitespace"
|
||||
|
||||
|
|
|
@ -321,6 +321,6 @@ Pandoc (Meta {docTitle = [Str "Pandoc",Space,Str "Test",Space,Str "Suite",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-role",Space,Str "to",Space,Str "the",Space,Str "default",Space,Str "default."]
|
||||
,Para [Str "And",Space,Str "now",Space,Str "`some-invalid-string-3231231`",Space,Str "is",Space,Str "nonsense."]
|
||||
,Para [Str "And",Space,Str "now",Space,Str "some-invalid-string-3231231",Space,Str "is",Space,Str "nonsense."]
|
||||
,Header 2 [Str "Literal",Space,Str "symbols"]
|
||||
,Para [Str "2*2",Space,Str "=",Space,Str "4*1"]]
|
||||
|
|
Loading…
Reference in a new issue