git.iko.soy/typeable/pandoc
on master help sign in
Add parsing support for the rST default-role directive.
commit618dc294f93f1865dcdfbfda21003745b4d6b389 authorGreg Maslov <gmaslov@bootis.org> dateSat 24 Mar 2012, 21:30 (15 years ago) parent3c4e1ff
4 files changed+59−6
src/Text/Pandoc/Parsing.hs+4−2 src/Text/Pandoc/Readers/RST.hs+31−3 tests/rst-reader.native+6−1 tests/rst-reader.rst+18
src/Text/Pandoc/Parsing.hs+4−2
@@ -652,7 +652,8 @@ data ParserState = ParserState
652652 stateExamples :: M.Map String Int, -- ^ Map from example labels to numbers
653653 stateHasChapters :: Bool, -- ^ True if \chapter encountered
654654 stateApplyMacros :: Bool, -- ^ Apply LaTeX macros?
655− stateMacros :: [Macro] -- ^ List of macros defined so far
655+ stateMacros :: [Macro], -- ^ List of macros defined so far
656+ stateRstDefaultRole :: String -- ^ Current rST default interpreted text role
656657 }
657658 deriving Show
658659
@@ -682,7 +683,8 @@ defaultParserState =
682683 stateExamples = M.empty,
683684 stateHasChapters = False,
684685 stateApplyMacros = True,
685− stateMacros = []}
686+ stateMacros = [],
687+ stateRstDefaultRole = "title-reference"}
686688
687689data HeaderType
688690 = SingleHeader Char -- ^ Single line of characters underneath
src/Text/Pandoc/Readers/RST.hs+31−3
@@ -129,6 +129,7 @@ block = choice [ codeBlock
129129 , imageBlock
130130 , customCodeBlock
131131 , mathBlock
132+ , defaultRoleBlock
132133 , unknownDirective
133134 , header
134135 , hrule
@@ -532,6 +533,21 @@ bulletList :: GenParser Char ParserState Block
532533bulletList = many1 (listItem bulletListStart) >>=
533534 return . BulletList . compactify
534535
536+--
537+-- default-role block
538+--
539+
540+defaultRoleBlock :: GenParser Char ParserState Block
541+defaultRoleBlock = try $ do
542+ string ".. default-role:: "
543+ role <- manyTill anyChar newline >>= return . removeLeadingTrailingSpace
544+ updateState $ \s -> s { stateRstDefaultRole =
545+ if null role
546+ then stateRstDefaultRole defaultParserState
547+ else role
548+ }
549+ return Null
550+
535551--
536552-- unknown directive (e.g. comment)
537553--
@@ -805,13 +821,25 @@ strong :: GenParser Char ParserState Inline
805821strong = enclosed (string "**") (try $ string "**") inline >>=
806822 return . Strong . normalizeSpaces
807823
808−interpreted :: [Char] -> GenParser Char st [Char]
824+-- Parses inline interpreted text which is required to have the given role.
825+-- This decision is based on the role marker (if present),
826+-- and the current default interpreted text role.
827+interpreted :: [Char] -> GenParser Char ParserState [Char]
809828interpreted role = try $ do
829+ state <- getState
830+ if role == stateRstDefaultRole state
831+ then try markedInterpretedText <|> unmarkedInterpretedText
832+ else markedInterpretedText
833+ where
834+ markedInterpretedText = try (roleMarker >> unmarkedInterpretedText)
835+ <|> (unmarkedInterpretedText >>= (\txt -> roleMarker >> return txt))
836+ roleMarker = string $ ":" ++ role ++ ":"
810837 -- Note, this doesn't precisely implement the complex rule in
811838 -- http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#inline-markup-recognition-rules
812839 -- but it should be good enough for most purposes
813− result <- enclosed (string $ ":" ++ role ++ ":`") (char '`') anyChar
814− return result
840+ unmarkedInterpretedText = do
841+ result <- enclosed (char '`') (char '`') anyChar
842+ return result
815843
816844superscript :: GenParser Char ParserState Inline
817845superscript = interpreted "sup" >>= \x -> return (Superscript [Str x])
tests/rst-reader.native+6−1
@@ -312,4 +312,9 @@ Pandoc (Meta {docTitle = [Str "Pandoc",Space,Str "Test",Space,Str "Suite",Str ":
312312,Para [Math DisplayMath "E = mc^2"]
313313,Para [Math DisplayMath "E = mc^2",Math DisplayMath "\\alpha = \\beta"]
314314,Para [Math DisplayMath "E &= mc^2\\\\\nF &= \\pi E",Math DisplayMath "F &= \\gamma \\alpha^2"]
315−,Para [Str "All",Space,Str "done",Str "."]]
315+,Para [Str "All",Space,Str "done",Str "."]
316+,Header 1 [Str "Default",Str "-",Str "Role"]
317+,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 "."]
318+,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 "."]
319+,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 "."]
320+,Para [Str "Reset",Space,Str "default",Str "-",Str "role",Space,Str "to",Space,Str "the",Space,Str "default",Space,Str "default",Str "."]]
tests/rst-reader.rst+18
@@ -565,3 +565,21 @@ display math:
565565
566566All done.
567567
568+Default-Role
569+============
570+
571+Try changing the default role to a few different things.
572+
573+.. default-role:: math
574+
575+Inline math: `E=mc^2` or :math:`E=mc^2` or `E=mc^2`:math:.
576+Other roles: :sup:`super`, `sub`:sub:.
577+
578+.. default-role:: sup
579+
580+Some `of` these :sup:`words` are in `superscript`:sup:.
581+
582+Reset default-role to the default default.
583+
584+.. default-role::
585+