git.iko.soy/typeable/pandoc
on master help sign in
RST reader: better handling of indirect roles.
1f00a53John MacFarlane
src/Text/Pandoc/Readers/RST.hs+11−9
@@ -614,20 +614,22 @@ directive' = do
614 return mempty
614 return mempty
615
615
616-- TODO:
616-- TODO:
617−-- - Silently ignores illegal fields
618-- - Only supports :format: fields with a single format for :raw: roles,
617-- - Only supports :format: fields with a single format for :raw: roles,
619-- change Text.Pandoc.Definition.Format to fix
618-- change Text.Pandoc.Definition.Format to fix
620addNewRole :: String -> [(String, String)] -> RSTParser Blocks
619addNewRole :: String -> [(String, String)] -> RSTParser Blocks
621addNewRole roleString fields = do
620addNewRole roleString fields = do
622 (role, parentRole) <- parseFromString inheritedRole roleString
621 (role, parentRole) <- parseFromString inheritedRole roleString
623 customRoles <- stateRstCustomRoles <$> getState
622 customRoles <- stateRstCustomRoles <$> getState
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)
624− let (baseRole, baseFmt, baseAttr) =
627+ (baseRole, baseFmt, baseAttr) =
625− maybe (parentRole, Nothing, nullAttr) id $
628+ getBaseRole (parentRole, Nothing, nullAttr) customRoles
626− M.lookup parentRole customRoles
627 fmt = if parentRole == "raw" then lookup "format" fields else baseFmt
629 fmt = if parentRole == "raw" then lookup "format" fields else baseFmt
628 annotate :: [String] -> [String]
630 annotate :: [String] -> [String]
629 annotate = maybe id (:) $
631 annotate = maybe id (:) $
630− if parentRole == "code"
632+ if baseRole == "code"
631 then lookup "language" fields
633 then lookup "language" fields
632 else Nothing
634 else Nothing
633 attr = let (ident, classes, keyValues) = baseAttr
635 attr = let (ident, classes, keyValues) = baseAttr
@@ -636,12 +638,12 @@ addNewRole roleString fields = do
636
638
637 -- warn about syntax we ignore
639 -- warn about syntax we ignore
638 flip mapM_ fields $ \(key, _) -> case key of
640 flip mapM_ fields $ \(key, _) -> case key of
639− "language" -> when (parentRole /= "code") $ addWarning Nothing $
641+ "language" -> when (baseRole /= "code") $ addWarning Nothing $
640 "ignoring :language: field because the parent of role :" ++
642 "ignoring :language: field because the parent of role :" ++
641− role ++ ": is :" ++ parentRole ++ ": not :code:"
643+ role ++ ": is :" ++ baseRole ++ ": not :code:"
642− "format" -> when (parentRole /= "raw") $ addWarning Nothing $
644+ "format" -> when (baseRole /= "raw") $ addWarning Nothing $
643 "ignoring :format: field because the parent of role :" ++
645 "ignoring :format: field because the parent of role :" ++
644− role ++ ": is :" ++ parentRole ++ ": not :raw:"
646+ role ++ ": is :" ++ baseRole ++ ": not :raw:"
645 _ -> addWarning Nothing $ "ignoring unknown field :" ++ key ++
647 _ -> addWarning Nothing $ "ignoring unknown field :" ++ key ++
646 ": in definition of role :" ++ role ++ ": in"
648 ": in definition of role :" ++ role ++ ": in"
647 when (parentRole == "raw" && countKeys "format" > 1) $
649 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
325,Para [Str "And",Space,Str "some",Space,Str "inline",Space,Str "haskell",Space,Code ("",["haskell","sourceCode"],[]) "fmap id [1,2..10]",Str "."]
325,Para [Str "And",Space,Str "some",Space,Str "inline",Space,Str "haskell",Space,Code ("",["haskell","sourceCode"],[]) "fmap id [1,2..10]",Str "."]
326,Null
326,Null
327,Null
327,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 "."]
329,Null
329,Null
330,Null
330,Null
331,Para [Str "Different",Space,Str "indirect",Space,Str "C",Space,Code ("",["c","different-indirect","sourceCode"],[]) "int x = 15;",Str "."]
331,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]`.
611
611
612.. role:: indirect(code)
612.. role:: indirect(code)
613
613
614−.. role:: python(indirect)
614+.. role:: py(indirect)
615 :language: python
615 :language: python
616
616
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]]`.
618
618
619.. role:: different-indirect(code)
619.. role:: different-indirect(code)
620 :language: c
620 :language: c