git.iko.soy/typeable/pandoc
on master help sign in
RST Reader: compute Attrs when role is defined

Move recursive role lookup from renderRole to addNewRole. The Attr value will be the same for every occurance of this role, so there's no reason to compute it every time. This allows simplifying the stateRstCustomRoles map considerably. We could go even further, and remove the fmt and attr arguments to renderRole, which are null except for custom roles.

commit689fb112bf925ce5394f88b48066be8abdc7fc34 authorDaniel Bergey <bergey@alum.mit.edu> dateThu 11 Dec 2014, 18:50 (12 years ago) parent4e04016
4 files changed+21−22
src/Text/Pandoc/Parsing.hs+2−3 src/Text/Pandoc/Readers/RST.hs+15−15 tests/Tests/Readers/RST.hs+1−1 tests/rst-reader.native+3−3
src/Text/Pandoc/Parsing.hs+2−3
@@ -921,10 +921,9 @@ data ParserState = ParserState
921921 stateHasChapters :: Bool, -- ^ True if \chapter encountered
922922 stateMacros :: [Macro], -- ^ List of macros defined so far
923923 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
925925 -- Triple represents: 1) Base role, 2) Optional format (only for :raw:
926− -- roles), 3) Source language annotation for code (could be used to
927− -- annotate role classes too).
926+ -- roles), 3) Additional classes (rest of Attr is unused)).
928927 stateCaption :: Maybe Inlines, -- ^ Caption in current environment
929928 stateInHtmlBlock :: Maybe String, -- ^ Tag type of HTML block being parsed
930929 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
625625addNewRole roleString fields = do
626626 (role, parentRole) <- parseFromString inheritedRole roleString
627627 customRoles <- stateRstCustomRoles <$> getState
628− baseRole <- case M.lookup parentRole customRoles of
629− Just (base, _, _) -> return base
630− Nothing -> return parentRole
631−
632− let fmt = if baseRole == "raw" then lookup "format" fields else Nothing
633− annotate = maybe (addClass role) (addLanguage role) $
628+ let (baseRole, baseFmt, baseAttr) =
629+ maybe (parentRole, Nothing, nullAttr) id $
630+ M.lookup parentRole customRoles
631+
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]
635+ annotate = maybe id (:) $
634636 if baseRole == "code"
635637 then lookup "language" fields
636638 else Nothing
639+ attr = let (ident, classes, keyValues) = baseAttr
640+ in (ident, nub . (role :) . annotate $ classes, keyValues)
637641
638642 updateState $ \s -> s {
639643 stateRstCustomRoles =
640− M.insert role (baseRole, fmt, (,) parentRole . annotate) customRoles
644+ M.insert role (baseRole, fmt, attr) customRoles
641645 }
642646
643647 return $ B.singleton Null
644648 where
645− addLanguage role lang (ident, classes, keyValues) =
646− (ident, nub ("sourceCode" : lang : role : classes), keyValues)
647649 inheritedRole =
648650 (,) <$> roleName <*> ((char '(' *> roleName <* char ')') <|> pure "span")
649651
@@ -1014,12 +1016,10 @@ renderRole contents fmt role attr = case role of
10141016 "span" -> return $ B.spanWith attr $ B.str contents
10151017 "raw" -> return $ B.rawInline (fromMaybe "" fmt) contents
10161018 custom -> do
1017− customRole <- stateRstCustomRoles <$> getState
1018− case M.lookup custom customRole of
1019− Just (_, newFmt, inherit) -> let
1020− fmtStr = fmt `mplus` newFmt
1021− (newRole, newAttr) = inherit attr
1022− in renderRole contents fmtStr newRole newAttr
1019+ customRoles <- stateRstCustomRoles <$> getState
1020+ case M.lookup custom customRoles of
1021+ Just (newRole, newFmt, newAttr) ->
1022+ renderRole contents newFmt newRole newAttr
10231023 Nothing -> do
10241024 pos <- getPosition
10251025 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" =:
9999 =?> para (codeWith ("", ["codeLike", "sourceCode"], []) "a")
100100 , "custom code role with language field"
101101 =: ".. role:: lhs(code)\n :language: haskell\n\n:lhs:`a`"
102− =?> para (codeWith ("", ["sourceCode", "haskell", "lhs"], []) "a")
102+ =?> para (codeWith ("", ["lhs", "haskell","sourceCode"], []) "a")
103103 , "custom role with unspecified parent role"
104104 =: ".. role:: classy\n\n:classy:`text`"
105105 =?> para (spanWith ("", ["classy"], []) "text")
tests/rst-reader.native+3−3
@@ -322,12 +322,12 @@ Pandoc (Meta {unMeta = fromList [("author",MetaList [MetaInlines [Str "John",Spa
322322,Null
323323,Para [Str "And",Space,Str "now",Space,Str "with",Space,RawInline (Format "html") "<b>inline</b> <span id=\"test\">HTML</span>",Str "."]
324324,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 "."]
326326,Null
327327,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 "."]
329329,Null
330330,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 "."]
332332,Header 2 ("literal-symbols",[],[]) [Str "Literal",Space,Str "symbols"]
333333,Para [Str "2*2",Space,Str "=",Space,Str "4*1"]]