git.iko.soy/typeable/pandoc
on master help sign in
RST: warn about ignored fields in role directives
commitea157cf23fa3bdd60c86599a0791d9492d6bd0bb authorDaniel Bergey <bergey@alum.mit.edu> dateThu 11 Dec 2014, 19:21 (12 years ago) parent689fb11
1 file changed+26−6
src/Text/Pandoc/Readers/RST.hs+26−6
src/Text/Pandoc/Readers/RST.hs+26−6
@@ -37,7 +37,7 @@ import Text.Pandoc.Builder (setMeta, fromList)
3737import Text.Pandoc.Shared
3838import Text.Pandoc.Parsing
3939import Text.Pandoc.Options
40−import Control.Monad ( when, liftM, guard, mzero, mplus )
40+import Control.Monad ( when, liftM, guard, mzero )
4141import Data.List ( findIndex, intersperse, intercalate,
4242 transpose, sort, deleteFirstsBy, isSuffixOf , nub, union)
4343import Data.Maybe (fromMaybe)
@@ -628,17 +628,35 @@ addNewRole roleString fields = do
628628 let (baseRole, baseFmt, baseAttr) =
629629 maybe (parentRole, Nothing, nullAttr) id $
630630 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
631+ fmt = if parentRole == "raw" then lookup "format" fields else baseFmt
634632 annotate :: [String] -> [String]
635633 annotate = maybe id (:) $
636− if baseRole == "code"
634+ if parentRole == "code"
637635 then lookup "language" fields
638636 else Nothing
639637 attr = let (ident, classes, keyValues) = baseAttr
638+ -- nub in case role name & language class are the same
640639 in (ident, nub . (role :) . annotate $ classes, keyValues)
641640
641+ -- warn about syntax we ignore
642+ flip mapM_ fields $ \(key, _) -> case key of
643+ "language" -> when (parentRole /= "code") $ addWarning Nothing $
644+ "ignoring :language: field because the parent of role :" ++
645+ role ++ ": is :" ++ parentRole ++ ": not :code:"
646+ "format" -> when (parentRole /= "raw") $ addWarning Nothing $
647+ "ignoring :format: field because the parent of role :" ++
648+ role ++ ": is :" ++ parentRole ++ ": not :raw:"
649+ _ -> addWarning Nothing $ "ignoring unknown field :" ++ key ++
650+ ": in definition of role :" ++ role ++ ": in"
651+ when (parentRole == "raw" && countKeys "format" > 1) $
652+ addWarning Nothing $
653+ "ignoring :format: fields after the first in the definition of role :"
654+ ++ role ++": in"
655+ when (parentRole == "code" && countKeys "language" > 1) $
656+ addWarning Nothing $
657+ "ignoring :language: fields after the first in the definition of role :"
658+ ++ role ++": in"
659+
642660 updateState $ \s -> s {
643661 stateRstCustomRoles =
644662 M.insert role (baseRole, fmt, attr) customRoles
@@ -646,9 +664,11 @@ addNewRole roleString fields = do
646664
647665 return $ B.singleton Null
648666 where
667+ countKeys k = length . filter (== k) . map fst $ fields
649668 inheritedRole =
650669 (,) <$> roleName <*> ((char '(' *> roleName <* char ')') <|> pure "span")
651670
671+
652672-- Can contain character codes as decimal numbers or
653673-- hexadecimal numbers, prefixed by 0x, x, \x, U+, u, or \u
654674-- or as XML-style hexadecimal character entities, e.g. &#x1a2b;
@@ -1022,7 +1042,7 @@ renderRole contents fmt role attr = case role of
10221042 renderRole contents newFmt newRole newAttr
10231043 Nothing -> do
10241044 pos <- getPosition
1025− addWarning (Just pos) $ "ignoring unknown role :" ++ custom ++ ": in "
1045+ addWarning (Just pos) $ "ignoring unknown role :" ++ custom ++ ": in"
10261046 return $ B.str contents -- Undefined role
10271047 where
10281048 titleRef ref = return $ B.str ref -- FIXME: Not a sensible behaviour