git.iko.soy/typeable/pandoc
on master help sign in
RST reader: better handling of indirect roles.

Previously the parser failed on this kind of case .. role:: indirect(code) .. role:: py(indirect) :language: python :py:`hi` Now it currectly recognizes `:py:` as a code role. The previous test for this didn't work, because the name of the indirect role was the same as the language defined its parent, os it didn't really test for this behavior. Updated test.

commit1f00a5395f2c76d94f543775dc945183397a722e authorJohn MacFarlane <jgm@berkeley.edu> dateSat 15 Aug 2015, 10:22 (11 years ago) parent8c579a5
3 files changed+14−12
src/Text/Pandoc/Readers/RST.hs+11−9 tests/rst-reader.native+1−1 tests/rst-reader.rst+2−2
src/Text/Pandoc/Readers/RST.hs+11−9
@@ -614,20 +614,22 @@ directive' = do
614614 return mempty
615615
616616-- TODO:
617−-- - Silently ignores illegal fields
618617-- - Only supports :format: fields with a single format for :raw: roles,
619618-- change Text.Pandoc.Definition.Format to fix
620619addNewRole :: String -> [(String, String)] -> RSTParser Blocks
621620addNewRole roleString fields = do
622621 (role, parentRole) <- parseFromString inheritedRole roleString
623622 customRoles <- stateRstCustomRoles <$> getState
624− let (baseRole, baseFmt, baseAttr) =
625− maybe (parentRole, Nothing, nullAttr) id $
626− M.lookup parentRole customRoles
623+ let getBaseRole (r, f, a) roles =
624+ case M.lookup r roles of
625+ Just (r', f', a') -> getBaseRole (r', f', a') roles
626+ Nothing -> (r, f, a)
627+ (baseRole, baseFmt, baseAttr) =
628+ getBaseRole (parentRole, Nothing, nullAttr) customRoles
627629 fmt = if parentRole == "raw" then lookup "format" fields else baseFmt
628630 annotate :: [String] -> [String]
629631 annotate = maybe id (:) $
630− if parentRole == "code"
632+ if baseRole == "code"
631633 then lookup "language" fields
632634 else Nothing
633635 attr = let (ident, classes, keyValues) = baseAttr
@@ -636,12 +638,12 @@ addNewRole roleString fields = do
636638
637639 -- warn about syntax we ignore
638640 flip mapM_ fields $ \(key, _) -> case key of
639− "language" -> when (parentRole /= "code") $ addWarning Nothing $
641+ "language" -> when (baseRole /= "code") $ addWarning Nothing $
640642 "ignoring :language: field because the parent of role :" ++
641− role ++ ": is :" ++ parentRole ++ ": not :code:"
642− "format" -> when (parentRole /= "raw") $ addWarning Nothing $
643+ role ++ ": is :" ++ baseRole ++ ": not :code:"
644+ "format" -> when (baseRole /= "raw") $ addWarning Nothing $
643645 "ignoring :format: field because the parent of role :" ++
644− role ++ ": is :" ++ parentRole ++ ": not :raw:"
646+ role ++ ": is :" ++ baseRole ++ ": not :raw:"
645647 _ -> addWarning Nothing $ "ignoring unknown field :" ++ key ++
646648 ": in definition of role :" ++ role ++ ": in"
647649 when (parentRole == "raw" && countKeys "format" > 1) $
tests/rst-reader.native+1−1
@@ -325,7 +325,7 @@ Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "John",Spa
325325,Para [Str "And",Space,Str "some",Space,Str "inline",Space,Str "haskell",Space,Code ("",["haskell","sourceCode"],[]) "fmap id [1,2..10]",Str "."]
326326,Null
327327,Null
328−,Para [Str "Indirect",Space,Str "python",Space,Str "role",Space,Code ("",["python","indirect","sourceCode"],[]) "[x*x for x in [1,2,3,4,5]]",Str "."]
328+,Para [Str "Indirect",Space,Str "python",Space,Str "role",Space,Code ("",["py","python","indirect","sourceCode"],[]) "[x*x for x in [1,2,3,4,5]]",Str "."]
329329,Null
330330,Null
331331,Para [Str "Different",Space,Str "indirect",Space,Str "C",Space,Code ("",["c","different-indirect","sourceCode"],[]) "int x = 15;",Str "."]
tests/rst-reader.rst+2−2
@@ -611,10 +611,10 @@ And some inline haskell :haskell:`fmap id [1,2..10]`.
611611
612612.. role:: indirect(code)
613613
614−.. role:: python(indirect)
614+.. role:: py(indirect)
615615 :language: python
616616
617−Indirect python role :python:`[x*x for x in [1,2,3,4,5]]`.
617+Indirect python role :py:`[x*x for x in [1,2,3,4,5]]`.
618618
619619.. role:: different-indirect(code)
620620 :language: c