@@ -29,20 +29,21 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2929Conversion from reStructuredText to 'Pandoc' document.
3030-}
3131module Text.Pandoc.Readers.RST (
32− readRST
32+ readRST,
33+ readRSTWithWarnings
3334 ) where
3435import Text.Pandoc.Definition
3536import Text.Pandoc.Builder (setMeta, fromList)
3637import Text.Pandoc.Shared
3738import Text.Pandoc.Parsing
3839import Text.Pandoc.Options
39−import Control.Monad ( when, liftM, guard, mzero, mplus )
40+import Control.Monad ( when, liftM, guard, mzero )
4041import Data.List ( findIndex, intersperse, intercalate,
41− transpose, sort, deleteFirstsBy, isSuffixOf )
42+ transpose, sort, deleteFirstsBy, isSuffixOf , nub, union)
4243import Data.Maybe (fromMaybe)
4344import qualified Data.Map as M
4445import Text.Printf ( printf )
45−import Control.Applicative ((<$>), (<$), (<*), (*>), (<*>))
46+import Control.Applicative ((<$>), (<$), (<*), (*>), (<*>), pure)
4647import Text.Pandoc.Builder (Inlines, Blocks, trimInlines, (<>))
4748import qualified Text.Pandoc.Builder as B
4849import Data.Monoid (mconcat, mempty)
@@ -55,6 +56,9 @@ readRST :: ReaderOptions -- ^ Reader options
5556 -> Pandoc
5657readRST opts s = (readWith parseRST) def{ stateOptions = opts } (s ++ "\n\n")
5758
59+readRSTWithWarnings :: ReaderOptions -> String -> (Pandoc, [String])
60+readRSTWithWarnings opts s = (readWithWarnings parseRST) def{ stateOptions = opts } (s ++ "\n\n")
61+
5862type RSTParser = Parser [Char] ParserState
5963
6064--
@@ -608,38 +612,62 @@ directive' = do
608612 "" -> block
609613 _ -> parseFromString parseBlocks body'
610614 return $ B.divWith attrs children
611− _ -> return mempty
615+ other -> do
616+ pos <- getPosition
617+ addWarning (Just pos) $ "ignoring unknown directive: " ++ other
618+ return mempty
612619
613620-- TODO:
614621-- - Silently ignores illegal fields
615−-- - Silently drops classes
616622-- - Only supports :format: fields with a single format for :raw: roles,
617623-- change Text.Pandoc.Definition.Format to fix
618624addNewRole :: String -> [(String, String)] -> RSTParser Blocks
619625addNewRole roleString fields = do
620626 (role, parentRole) <- parseFromString inheritedRole roleString
621627 customRoles <- stateRstCustomRoles <$> getState
622− baseRole <- case M.lookup parentRole customRoles of
623− Just (base, _, _) -> return base
624− Nothing -> return parentRole
625−
626− let fmt = if baseRole == "raw" then lookup "format" fields else Nothing
627− annotate = maybe id addLanguage $
628− if baseRole == "code"
628+ let (baseRole, baseFmt, baseAttr) =
629+ maybe (parentRole, Nothing, nullAttr) id $
630+ M.lookup parentRole customRoles
631+ fmt = if parentRole == "raw" then lookup "format" fields else baseFmt
632+ annotate :: [String] -> [String]
633+ annotate = maybe id (:) $
634+ if parentRole == "code"
629635 then lookup "language" fields
630636 else Nothing
637+ attr = let (ident, classes, keyValues) = baseAttr
638+ -- nub in case role name & language class are the same
639+ in (ident, nub . (role :) . annotate $ classes, keyValues)
640+
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"
631659
632660 updateState $ \s -> s {
633661 stateRstCustomRoles =
634− M.insert role (baseRole, fmt, (,) parentRole . annotate) customRoles
662+ M.insert role (baseRole, fmt, attr) customRoles
635663 }
636664
637665 return $ B.singleton Null
638666 where
639− addLanguage lang (ident, classes, keyValues) =
640− (ident, "sourceCode" : lang : classes, keyValues)
667+ countKeys k = length . filter (== k) . map fst $ fields
641668 inheritedRole =
642− (,) <$> roleNameEndingIn (char '(') <*> roleNameEndingIn (char ')')
669+ (,) <$> roleName <*> ((char '(' *> roleName <* char ')') <|> pure "span")
670+
643671
644672-- Can contain character codes as decimal numbers or
645673-- hexadecimal numbers, prefixed by 0x, x, \x, U+, u, or \u
@@ -999,21 +1027,23 @@ renderRole contents fmt role attr = case role of
9991027 "RFC" -> return $ rfcLink contents
10001028 "pep-reference" -> return $ pepLink contents
10011029 "PEP" -> return $ pepLink contents
1002− "literal" -> return $ B.str contents
1030+ "literal" -> return $ B.codeWith attr contents
10031031 "math" -> return $ B.math contents
10041032 "title-reference" -> titleRef contents
10051033 "title" -> titleRef contents
10061034 "t" -> titleRef contents
1007− "code" -> return $ B.codeWith attr contents
1035+ "code" -> return $ B.codeWith (addClass "sourceCode" attr) contents
1036+ "span" -> return $ B.spanWith attr $ B.str contents
10081037 "raw" -> return $ B.rawInline (fromMaybe "" fmt) contents
10091038 custom -> do
1010− customRole <- stateRstCustomRoles <$> getState
1011− case M.lookup custom customRole of
1012− Just (_, newFmt, inherit) -> let
1013− fmtStr = fmt `mplus` newFmt
1014− (newRole, newAttr) = inherit attr
1015− in renderRole contents fmtStr newRole newAttr
1016− Nothing -> return $ B.str contents -- Undefined role
1039+ customRoles <- stateRstCustomRoles <$> getState
1040+ case M.lookup custom customRoles of
1041+ Just (newRole, newFmt, newAttr) ->
1042+ renderRole contents newFmt newRole newAttr
1043+ Nothing -> do
1044+ pos <- getPosition
1045+ addWarning (Just pos) $ "ignoring unknown role :" ++ custom ++ ": in"
1046+ return $ B.str contents -- Undefined role
10171047 where
10181048 titleRef ref = return $ B.str ref -- FIXME: Not a sensible behaviour
10191049 rfcLink rfcNo = B.link rfcUrl ("RFC " ++ rfcNo) $ B.str ("RFC " ++ rfcNo)
@@ -1022,11 +1052,14 @@ renderRole contents fmt role attr = case role of
10221052 where padNo = replicate (4 - length pepNo) '0' ++ pepNo
10231053 pepUrl = "http://www.python.org/dev/peps/pep-" ++ padNo ++ "/"
10241054
1025−roleNameEndingIn :: RSTParser Char -> RSTParser String
1026−roleNameEndingIn end = many1Till (letter <|> char '-') end
1055+addClass :: String -> Attr -> Attr
1056+addClass c (ident, classes, keyValues) = (ident, union classes [c], keyValues)
1057+
1058+roleName :: RSTParser String
1059+roleName = many1 (letter <|> char '-')
10271060
10281061roleMarker :: RSTParser String
1029−roleMarker = char ':' *> roleNameEndingIn (char ':')
1062+roleMarker = char ':' *> roleName <* char ':'
10301063
10311064roleBefore :: RSTParser (String,String)
10321065roleBefore = try $ do