git.iko.soy/typeable/pandoc
on master help sign in
RST reader: improve support for custom roles
dc3ea98Daniel Bergey
src/Text/Pandoc/Readers/RST.hs+14−11
@@ -39,11 +39,11 @@ import Text.Pandoc.Parsing
39import Text.Pandoc.Options
39import Text.Pandoc.Options
40import Control.Monad ( when, liftM, guard, mzero, mplus )
40import Control.Monad ( when, liftM, guard, mzero, mplus )
41import Data.List ( findIndex, intersperse, intercalate,
41import Data.List ( findIndex, intersperse, intercalate,
42− transpose, sort, deleteFirstsBy, isSuffixOf )
42+ transpose, sort, deleteFirstsBy, isSuffixOf , nub, union)
43import Data.Maybe (fromMaybe)
43import Data.Maybe (fromMaybe)
44import qualified Data.Map as M
44import qualified Data.Map as M
45import Text.Printf ( printf )
45import Text.Printf ( printf )
46−import Control.Applicative ((<$>), (<$), (<*), (*>), (<*>))
46+import Control.Applicative ((<$>), (<$), (<*), (*>), (<*>), pure)
47import Text.Pandoc.Builder (Inlines, Blocks, trimInlines, (<>))
47import Text.Pandoc.Builder (Inlines, Blocks, trimInlines, (<>))
48import qualified Text.Pandoc.Builder as B
48import qualified Text.Pandoc.Builder as B
49import Data.Monoid (mconcat, mempty)
49import Data.Monoid (mconcat, mempty)
@@ -619,7 +619,6 @@ directive' = do
619
619
620-- TODO:
620-- TODO:
621-- - Silently ignores illegal fields
621-- - Silently ignores illegal fields
622−-- - Silently drops classes
623-- - Only supports :format: fields with a single format for :raw: roles,
622-- - Only supports :format: fields with a single format for :raw: roles,
624-- change Text.Pandoc.Definition.Format to fix
623-- change Text.Pandoc.Definition.Format to fix
625addNewRole :: String -> [(String, String)] -> RSTParser Blocks
624addNewRole :: String -> [(String, String)] -> RSTParser Blocks
@@ -631,7 +630,7 @@ addNewRole roleString fields = do
631 Nothing -> return parentRole
630 Nothing -> return parentRole
632
631
633 let fmt = if baseRole == "raw" then lookup "format" fields else Nothing
632 let fmt = if baseRole == "raw" then lookup "format" fields else Nothing
634− annotate = maybe id addLanguage $
633+ annotate = maybe (addClass role) (addLanguage role) $
635 if baseRole == "code"
634 if baseRole == "code"
636 then lookup "language" fields
635 then lookup "language" fields
637 else Nothing
636 else Nothing
@@ -643,10 +642,10 @@ addNewRole roleString fields = do
643
642
644 return $ B.singleton Null
643 return $ B.singleton Null
645 where
644 where
646− addLanguage lang (ident, classes, keyValues) =
645+ addLanguage role lang (ident, classes, keyValues) =
647− (ident, "sourceCode" : lang : classes, keyValues)
646+ (ident, nub ("sourceCode" : lang : role : classes), keyValues)
648 inheritedRole =
647 inheritedRole =
649− (,) <$> roleNameEndingIn (char '(') <*> roleNameEndingIn (char ')')
648+ (,) <$> roleName <*> ((char '(' *> roleName <* char ')') <|> pure "span")
650
649
651-- Can contain character codes as decimal numbers or
650-- Can contain character codes as decimal numbers or
652-- hexadecimal numbers, prefixed by 0x, x, \x, U+, u, or \u
651-- hexadecimal numbers, prefixed by 0x, x, \x, U+, u, or \u
@@ -1011,7 +1010,8 @@ renderRole contents fmt role attr = case role of
1011 "title-reference" -> titleRef contents
1010 "title-reference" -> titleRef contents
1012 "title" -> titleRef contents
1011 "title" -> titleRef contents
1013 "t" -> titleRef contents
1012 "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
1015 "raw" -> return $ B.rawInline (fromMaybe "" fmt) contents
1015 "raw" -> return $ B.rawInline (fromMaybe "" fmt) contents
1016 custom -> do
1016 custom -> do
1017 customRole <- stateRstCustomRoles <$> getState
1017 customRole <- stateRstCustomRoles <$> getState
@@ -1032,11 +1032,14 @@ renderRole contents fmt role attr = case role of
1032 where padNo = replicate (4 - length pepNo) '0' ++ pepNo
1032 where padNo = replicate (4 - length pepNo) '0' ++ pepNo
1033 pepUrl = "http://www.python.org/dev/peps/pep-" ++ padNo ++ "/"
1033 pepUrl = "http://www.python.org/dev/peps/pep-" ++ padNo ++ "/"
1034
1034
1035+addClass :: String -> Attr -> Attr
1036+addClass c (ident, classes, keyValues) = (ident, union classes [c], keyValues)
1037+
1035−roleNameEndingIn :: RSTParser Char -> RSTParser String
1038+roleName :: RSTParser String
1036−roleNameEndingIn end = many1Till (letter <|> char '-') end
1039+roleName = many1 (letter <|> char '-')
1037
1040
1038roleMarker :: RSTParser String
1041roleMarker :: RSTParser String
1039−roleMarker = char ':' *> roleNameEndingIn (char ':')
1042+roleMarker = char ':' *> roleName <* char ':'
1040
1043
1041roleBefore :: RSTParser (String,String)
1044roleBefore :: RSTParser (String,String)
1042roleBefore = try $ do
1045roleBefore = try $ do