git.iko.soy/typeable/pandoc
on master help sign in
RST reader: Consolidate super/subscript, math into interpretedRole.
commite8260c27e18577b9af3d293b7cb9565b6dc9af6b authorJohn MacFarlane <fiddlosopher@gmail.com> dateSun 30 Sep 2012, 20:28 (14 years ago) parent9366d86
2 files changed+27−36
src/Text/Pandoc/Readers/RST.hs+26−35 tests/rst-reader.native+1−1
src/Text/Pandoc/Readers/RST.hs+26−35
@@ -854,9 +854,6 @@ inline = choice [ whitespace
854854 , emph
855855 , code
856856 , subst
857− , superscript
858− , subscript
859− , math
860857 , interpretedRole
861858 , note
862859 , smart
@@ -907,42 +904,36 @@ strong :: RSTParser Inlines
907904strong = B.strong . trimInlines . mconcat <$>
908905 enclosed (atStart $ string "**") (try $ string "**") inline
909906
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
924− -- http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#inline-markup-recognition-rules
925− -- but it should be good enough for most purposes
907+-- Note, this doesn't precisely implement the complex rule in
908+-- http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#inline-markup-recognition-rules
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)
926933
927934unmarkedInterpretedText :: RSTParser [Char]
928935unmarkedInterpretedText = enclosed (atStart $ char '`') (char '`') anyChar
929936
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−
946937whitespace :: RSTParser Inlines
947938whitespace = B.space <$ skipMany1 spaceChar <?> "whitespace"
948939
tests/rst-reader.native+1−1
@@ -321,6 +321,6 @@ Pandoc (Meta {docTitle = [Str "Pandoc",Space,Str "Test",Space,Str "Suite",Str ":
321321,Para [Math DisplayMath "\\alpha = beta",Math DisplayMath "E = mc^2"]
322322,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 "."]
323323,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."]
325325,Header 2 [Str "Literal",Space,Str "symbols"]
326326,Para [Str "2*2",Space,Str "=",Space,Str "4*1"]]