git.iko.soy/typeable/pandoc
on 4eff607 help sign in
43 lines 1.4 kB history raw
1module Text.Pandoc.CSS ( foldOrElse
2 , pickStyleAttrProps
3 , pickStylesToKVs
4 )
5where
7import Text.Pandoc.Shared (trim)
8import Text.Parsec
9import Text.Parsec.String
11ruleParser :: Parser (String, String)
12ruleParser = do
13 p <- many1 (noneOf ":") <* char ':'
14 v <- many1 (noneOf ":;") <* (optional $ char ';') <* spaces
15 return (trim p, trim v)
17styleAttrParser :: Parser [(String, String)]
18styleAttrParser = many1 ruleParser
20orElse :: Eq a => a -> a -> a -> a
21orElse v x y = if v == x then y else x
23foldOrElse :: Eq a => a -> [a] -> a
24foldOrElse v xs = foldr (orElse v) v xs
26eitherToMaybe :: Either a b -> Maybe b
27eitherToMaybe (Right x) = Just x
28eitherToMaybe _ = Nothing
30-- | takes a list of keys/properties and a CSS string and
31-- returns the corresponding key-value-pairs.
32pickStylesToKVs :: [String] -> String -> [(String, String)]
33pickStylesToKVs props styleAttr =
34 case parse styleAttrParser "" styleAttr of
35 Left _ -> []
36 Right styles -> filter (\s -> fst s `elem` props) styles
38-- | takes a list of key/property synonyms and a CSS string and maybe
39-- returns the value of the first match (in order of the supplied list)
40pickStyleAttrProps :: [String] -> String -> Maybe String
41pickStyleAttrProps lookupProps styleAttr = do
42 styles <- eitherToMaybe $ parse styleAttrParser "" styleAttr
43 foldOrElse Nothing $ map (flip lookup styles) lookupProps