git.iko.soy/typeable/pandoc
on master help sign in
RST reader: Consolidate super/subscript, math into interpretedRole.
e8260c2John MacFarlane
src/Text/Pandoc/Readers/RST.hs+26−35
@@ -854,9 +854,6 @@ inline = choice [ whitespace
854 , emph
854 , emph
855 , code
855 , code
856 , subst
856 , subst
857− , superscript
858− , subscript
859− , math
860 , interpretedRole
857 , interpretedRole
861 , note
858 , note
862 , smart
859 , smart
@@ -907,42 +904,36 @@ strong :: RSTParser Inlines
907strong = B.strong . trimInlines . mconcat <$>
904strong = B.strong . trimInlines . mconcat <$>
908 enclosed (atStart $ string "**") (try $ string "**") inline
905 enclosed (atStart $ string "**") (try $ string "**") inline
909
906
910−-- Parses inline interpreted text which is required to have the given role.
911−-- This decision is based on the role marker (if present),
912−-- and the current default interpreted text role.
913−interpreted :: [Char] -> RSTParser [Char]
914−interpreted role = try $ do
915− state <- getState
916− if role == stateRstDefaultRole state
917− then try markedInterpretedText <|> unmarkedInterpretedText
918− else markedInterpretedText
919− where
920− markedInterpretedText = try (roleMarker *> unmarkedInterpretedText)
921− <|> (unmarkedInterpretedText <* roleMarker)
922− roleMarker = string $ ":" ++ role ++ ":"
923− -- Note, this doesn't precisely implement the complex rule in
907+-- Note, this doesn't precisely implement the complex rule in
924− -- http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#inline-markup-recognition-rules
908+-- http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#inline-markup-recognition-rules
925− -- but it should be good enough for most purposes
909+-- but it should be good enough for most purposes
910+interpretedRole :: RSTParser Inlines
911+interpretedRole = try $ do
912+ (role, contents) <- roleBefore <|> roleAfter
913+ case role of
914+ "sup" -> return $ B.superscript $ B.str contents
915+ "sub" -> return $ B.subscript $ B.str contents
916+ "math" -> return $ B.math contents
917+ _ -> return $ B.str contents --unknown
918+
919+roleMarker :: RSTParser String
920+roleMarker = char ':' *> many1Till (letter <|> char '-') (char ':')
921+
922+roleBefore :: RSTParser (String,String)
923+roleBefore = try $ do
924+ role <- roleMarker
925+ contents <- unmarkedInterpretedText
926+ return (role,contents)
927+
928+roleAfter :: RSTParser (String,String)
929+roleAfter = try $ do
930+ contents <- unmarkedInterpretedText
931+ role <- roleMarker <|> (stateRstDefaultRole <$> getState)
932+ return (role,contents)
926
933
927unmarkedInterpretedText :: RSTParser [Char]
934unmarkedInterpretedText :: RSTParser [Char]
928unmarkedInterpretedText = enclosed (atStart $ char '`') (char '`') anyChar
935unmarkedInterpretedText = enclosed (atStart $ char '`') (char '`') anyChar
929
936
930−-- For unknown interpreted roles, we just ignore the role.
931−interpretedRole :: RSTParser Inlines
932−interpretedRole = try $ B.str <$>
933− ( (roleMarker *> unmarkedInterpretedText)
934− <|> (unmarkedInterpretedText <* roleMarker) )
935− where roleMarker = char ':' *> many1Till (letter <|> char '-') (char ':')
936−
937−superscript :: RSTParser Inlines
938−superscript = B.superscript . B.str <$> interpreted "sup"
939−
940−subscript :: RSTParser Inlines
941−subscript = B.subscript . B.str <$> interpreted "sub"
942−
943−math :: RSTParser Inlines
944−math = B.math <$> interpreted "math"
945−
946whitespace :: RSTParser Inlines
937whitespace :: RSTParser Inlines
947whitespace = B.space <$ skipMany1 spaceChar <?> "whitespace"
938whitespace = B.space <$ skipMany1 spaceChar <?> "whitespace"
948
939
tests/rst-reader.native+1−1
@@ -321,6 +321,6 @@ Pandoc (Meta {docTitle = [Str "Pandoc",Space,Str "Test",Space,Str "Suite",Str ":
321,Para [Math DisplayMath "\\alpha = beta",Math DisplayMath "E = mc^2"]
321,Para [Math DisplayMath "\\alpha = beta",Math DisplayMath "E = mc^2"]
322,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 "."]
322,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 "."]
323,Para [Str "Reset",Space,Str "default-role",Space,Str "to",Space,Str "the",Space,Str "default",Space,Str "default."]
323,Para [Str "Reset",Space,Str "default-role",Space,Str "to",Space,Str "the",Space,Str "default",Space,Str "default."]
324−,Para [Str "And",Space,Str "now",Space,Str "`some-invalid-string-3231231`",Space,Str "is",Space,Str "nonsense."]
324+,Para [Str "And",Space,Str "now",Space,Str "some-invalid-string-3231231",Space,Str "is",Space,Str "nonsense."]
325,Header 2 [Str "Literal",Space,Str "symbols"]
325,Header 2 [Str "Literal",Space,Str "symbols"]
326,Para [Str "2*2",Space,Str "=",Space,Str "4*1"]]
326,Para [Str "2*2",Space,Str "=",Space,Str "4*1"]]