git.iko.soy/typeable/pandoc
on master help sign in
RST Reader: compute Attrs when role is defined
689fb11Daniel Bergey
src/Text/Pandoc/Parsing.hs+2−3
@@ -921,10 +921,9 @@ data ParserState = ParserState
921 stateHasChapters :: Bool, -- ^ True if \chapter encountered
921 stateHasChapters :: Bool, -- ^ True if \chapter encountered
922 stateMacros :: [Macro], -- ^ List of macros defined so far
922 stateMacros :: [Macro], -- ^ List of macros defined so far
923 stateRstDefaultRole :: String, -- ^ Current rST default interpreted text role
923 stateRstDefaultRole :: String, -- ^ Current rST default interpreted text role
924− stateRstCustomRoles :: M.Map String (String, Maybe String, Attr -> (String, Attr)), -- ^ Current rST custom text roles
924+ stateRstCustomRoles :: M.Map String (String, Maybe String, Attr), -- ^ Current rST custom text roles
925 -- Triple represents: 1) Base role, 2) Optional format (only for :raw:
925 -- Triple represents: 1) Base role, 2) Optional format (only for :raw:
926− -- roles), 3) Source language annotation for code (could be used to
926+ -- roles), 3) Additional classes (rest of Attr is unused)).
927− -- annotate role classes too).
928 stateCaption :: Maybe Inlines, -- ^ Caption in current environment
927 stateCaption :: Maybe Inlines, -- ^ Caption in current environment
929 stateInHtmlBlock :: Maybe String, -- ^ Tag type of HTML block being parsed
928 stateInHtmlBlock :: Maybe String, -- ^ Tag type of HTML block being parsed
930 stateMarkdownAttribute :: Bool, -- ^ True if in markdown=1 context
929 stateMarkdownAttribute :: Bool, -- ^ True if in markdown=1 context
src/Text/Pandoc/Readers/RST.hs+15−15
@@ -625,25 +625,27 @@ addNewRole :: String -> [(String, String)] -> RSTParser Blocks
625addNewRole roleString fields = do
625addNewRole roleString fields = do
626 (role, parentRole) <- parseFromString inheritedRole roleString
626 (role, parentRole) <- parseFromString inheritedRole roleString
627 customRoles <- stateRstCustomRoles <$> getState
627 customRoles <- stateRstCustomRoles <$> getState
628+ let (baseRole, baseFmt, baseAttr) =
629+ maybe (parentRole, Nothing, nullAttr) id $
628− baseRole <- case M.lookup parentRole customRoles of
630+ M.lookup parentRole customRoles
629− Just (base, _, _) -> return base
631+
630− Nothing -> return parentRole
631−
632− let fmt = if baseRole == "raw" then lookup "format" fields else Nothing
632+ let fmt = if parentRole == "raw" then lookup "format" fields else baseFmt
633+ -- nub in case role name & language class are the same
634+ annotate :: [String] -> [String]
633− annotate = maybe (addClass role) (addLanguage role) $
635+ annotate = maybe id (:) $
634 if baseRole == "code"
636 if baseRole == "code"
635 then lookup "language" fields
637 then lookup "language" fields
636 else Nothing
638 else Nothing
639+ attr = let (ident, classes, keyValues) = baseAttr
640+ in (ident, nub . (role :) . annotate $ classes, keyValues)
637
641
638 updateState $ \s -> s {
642 updateState $ \s -> s {
639 stateRstCustomRoles =
643 stateRstCustomRoles =
640− M.insert role (baseRole, fmt, (,) parentRole . annotate) customRoles
644+ M.insert role (baseRole, fmt, attr) customRoles
641 }
645 }
642
646
643 return $ B.singleton Null
647 return $ B.singleton Null
644 where
648 where
645− addLanguage role lang (ident, classes, keyValues) =
646− (ident, nub ("sourceCode" : lang : role : classes), keyValues)
647 inheritedRole =
649 inheritedRole =
648 (,) <$> roleName <*> ((char '(' *> roleName <* char ')') <|> pure "span")
650 (,) <$> roleName <*> ((char '(' *> roleName <* char ')') <|> pure "span")
649
651
@@ -1014,12 +1016,10 @@ renderRole contents fmt role attr = case role of
1014 "span" -> return $ B.spanWith attr $ B.str contents
1016 "span" -> return $ B.spanWith attr $ B.str contents
1015 "raw" -> return $ B.rawInline (fromMaybe "" fmt) contents
1017 "raw" -> return $ B.rawInline (fromMaybe "" fmt) contents
1016 custom -> do
1018 custom -> do
1017− customRole <- stateRstCustomRoles <$> getState
1019+ customRoles <- stateRstCustomRoles <$> getState
1018− case M.lookup custom customRole of
1020+ case M.lookup custom customRoles of
1019− Just (_, newFmt, inherit) -> let
1021+ Just (newRole, newFmt, newAttr) ->
1020− fmtStr = fmt `mplus` newFmt
1021− (newRole, newAttr) = inherit attr
1022− in renderRole contents fmtStr newRole newAttr
1022+ renderRole contents newFmt newRole newAttr
1023 Nothing -> do
1023 Nothing -> do
1024 pos <- getPosition
1024 pos <- getPosition
1025 addWarning (Just pos) $ "ignoring unknown role :" ++ custom ++ ": in "
1025 addWarning (Just pos) $ "ignoring unknown role :" ++ custom ++ ": in "
tests/Tests/Readers/RST.hs+1−1
@@ -99,7 +99,7 @@ tests = [ "line block with blank line" =:
99 =?> para (codeWith ("", ["codeLike", "sourceCode"], []) "a")
99 =?> para (codeWith ("", ["codeLike", "sourceCode"], []) "a")
100 , "custom code role with language field"
100 , "custom code role with language field"
101 =: ".. role:: lhs(code)\n :language: haskell\n\n:lhs:`a`"
101 =: ".. role:: lhs(code)\n :language: haskell\n\n:lhs:`a`"
102− =?> para (codeWith ("", ["sourceCode", "haskell", "lhs"], []) "a")
102+ =?> para (codeWith ("", ["lhs", "haskell","sourceCode"], []) "a")
103 , "custom role with unspecified parent role"
103 , "custom role with unspecified parent role"
104 =: ".. role:: classy\n\n:classy:`text`"
104 =: ".. role:: classy\n\n:classy:`text`"
105 =?> para (spanWith ("", ["classy"], []) "text")
105 =?> para (spanWith ("", ["classy"], []) "text")
tests/rst-reader.native+3−3
@@ -322,12 +322,12 @@ Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "John",Spa
322,Null
322,Null
323,Para [Str "And",Space,Str "now",Space,Str "with",Space,RawInline (Format "html") "<b>inline</b> <span id=\"test\">HTML</span>",Str "."]
323,Para [Str "And",Space,Str "now",Space,Str "with",Space,RawInline (Format "html") "<b>inline</b> <span id=\"test\">HTML</span>",Str "."]
324,Null
324,Null
325−,Para [Str "And",Space,Str "some",Space,Str "inline",Space,Str "haskell",Space,Code ("",["sourceCode","haskell"],[]) "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 ("",["sourceCode","python","indirect"],[]) "[x*x for x in [1,2,3,4,5]]",Str "."]
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 "."]
329,Null
329,Null
330,Null
330,Null
331−,Para [Str "Different",Space,Str "indirect",Space,Str "C",Space,Code ("",["sourceCode","c","different-indirect"],[]) "int x = 15;",Str "."]
331+,Para [Str "Different",Space,Str "indirect",Space,Str "C",Space,Code ("",["c","different-indirect","sourceCode"],[]) "int x = 15;",Str "."]
332,Header 2 ("literal-symbols",[],[]) [Str "Literal",Space,Str "symbols"]
332,Header 2 ("literal-symbols",[],[]) [Str "Literal",Space,Str "symbols"]
333,Para [Str "2*2",Space,Str "=",Space,Str "4*1"]]
333,Para [Str "2*2",Space,Str "=",Space,Str "4*1"]]