git.iko.soy/typeable/pandoc
on master help sign in
RST reader: improve support for custom roles

- Add "sourceCode" to classes for :code: role, and anything inheriting from it. - Add the name of the custom role to classes if the Inline constructor supports Attr. - If the custom role directive does not specify a parent role, inherit from the :span: role. This differs somewhat from the rst2xml.py behavior. If a custom role inherits from another custom role, Pandoc will attach both roles' names as classes. rst2xml.py will only use the class of the directly invoked role (though in the case of inheriting from a :code: role with a :language: defined, it will also provide the inherited language as a class).

commitdc3ea9840e301b5d18760050ec2dc50bdb4de509 authorDaniel Bergey <bergey@alum.mit.edu> dateThu 11 Dec 2014, 16:12 (12 years ago) parentdba066a
1 file changed+14−11
src/Text/Pandoc/Readers/RST.hs+14−11
src/Text/Pandoc/Readers/RST.hs+14−11
@@ -39,11 +39,11 @@ import Text.Pandoc.Parsing
3939import Text.Pandoc.Options
4040import Control.Monad ( when, liftM, guard, mzero, mplus )
4141import Data.List ( findIndex, intersperse, intercalate,
42− transpose, sort, deleteFirstsBy, isSuffixOf )
42+ transpose, sort, deleteFirstsBy, isSuffixOf , nub, union)
4343import Data.Maybe (fromMaybe)
4444import qualified Data.Map as M
4545import Text.Printf ( printf )
46−import Control.Applicative ((<$>), (<$), (<*), (*>), (<*>))
46+import Control.Applicative ((<$>), (<$), (<*), (*>), (<*>), pure)
4747import Text.Pandoc.Builder (Inlines, Blocks, trimInlines, (<>))
4848import qualified Text.Pandoc.Builder as B
4949import Data.Monoid (mconcat, mempty)
@@ -619,7 +619,6 @@ directive' = do
619619
620620-- TODO:
621621-- - Silently ignores illegal fields
622−-- - Silently drops classes
623622-- - Only supports :format: fields with a single format for :raw: roles,
624623-- change Text.Pandoc.Definition.Format to fix
625624addNewRole :: String -> [(String, String)] -> RSTParser Blocks
@@ -631,7 +630,7 @@ addNewRole roleString fields = do
631630 Nothing -> return parentRole
632631
633632 let fmt = if baseRole == "raw" then lookup "format" fields else Nothing
634− annotate = maybe id addLanguage $
633+ annotate = maybe (addClass role) (addLanguage role) $
635634 if baseRole == "code"
636635 then lookup "language" fields
637636 else Nothing
@@ -643,10 +642,10 @@ addNewRole roleString fields = do
643642
644643 return $ B.singleton Null
645644 where
646− addLanguage lang (ident, classes, keyValues) =
647− (ident, "sourceCode" : lang : classes, keyValues)
645+ addLanguage role lang (ident, classes, keyValues) =
646+ (ident, nub ("sourceCode" : lang : role : classes), keyValues)
648647 inheritedRole =
649− (,) <$> roleNameEndingIn (char '(') <*> roleNameEndingIn (char ')')
648+ (,) <$> roleName <*> ((char '(' *> roleName <* char ')') <|> pure "span")
650649
651650-- Can contain character codes as decimal numbers or
652651-- hexadecimal numbers, prefixed by 0x, x, \x, U+, u, or \u
@@ -1011,7 +1010,8 @@ renderRole contents fmt role attr = case role of
10111010 "title-reference" -> titleRef contents
10121011 "title" -> titleRef contents
10131012 "t" -> titleRef contents
1014− "code" -> return $ B.codeWith (union attr ["code"]) contents
1013+ "code" -> return $ B.codeWith (addClass "sourceCode" attr) contents
1014+ "span" -> return $ B.spanWith attr $ B.str contents
10151015 "raw" -> return $ B.rawInline (fromMaybe "" fmt) contents
10161016 custom -> do
10171017 customRole <- stateRstCustomRoles <$> getState
@@ -1032,11 +1032,14 @@ renderRole contents fmt role attr = case role of
10321032 where padNo = replicate (4 - length pepNo) '0' ++ pepNo
10331033 pepUrl = "http://www.python.org/dev/peps/pep-" ++ padNo ++ "/"
10341034
1035−roleNameEndingIn :: RSTParser Char -> RSTParser String
1036−roleNameEndingIn end = many1Till (letter <|> char '-') end
1035+addClass :: String -> Attr -> Attr
1036+addClass c (ident, classes, keyValues) = (ident, union classes [c], keyValues)
1037+
1038+roleName :: RSTParser String
1039+roleName = many1 (letter <|> char '-')
10371040
10381041roleMarker :: RSTParser String
1039−roleMarker = char ':' *> roleNameEndingIn (char ':')
1042+roleMarker = char ':' *> roleName <* char ':'
10401043
10411044roleBefore :: RSTParser (String,String)
10421045roleBefore = try $ do