|
Packit |
dda32d |
module Text.Pandoc.CSS ( foldOrElse
|
|
Packit |
dda32d |
, pickStyleAttrProps
|
|
Packit |
dda32d |
, pickStylesToKVs
|
|
Packit |
dda32d |
)
|
|
Packit |
dda32d |
where
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
import Text.Pandoc.Shared (trim)
|
|
Packit |
dda32d |
import Text.Parsec
|
|
Packit |
dda32d |
import Text.Parsec.String
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
ruleParser :: Parser (String, String)
|
|
Packit |
dda32d |
ruleParser = do
|
|
Packit |
dda32d |
p <- many1 (noneOf ":") <* char ':'
|
|
Packit |
dda32d |
v <- many1 (noneOf ":;") <* optional (char ';') <* spaces
|
|
Packit |
dda32d |
return (trim p, trim v)
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
styleAttrParser :: Parser [(String, String)]
|
|
Packit |
dda32d |
styleAttrParser = many1 ruleParser
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
orElse :: Eq a => a -> a -> a -> a
|
|
Packit |
dda32d |
orElse v x y = if v == x then y else x
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
foldOrElse :: Eq a => a -> [a] -> a
|
|
Packit |
dda32d |
foldOrElse v xs = foldr (orElse v) v xs
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
eitherToMaybe :: Either a b -> Maybe b
|
|
Packit |
dda32d |
eitherToMaybe (Right x) = Just x
|
|
Packit |
dda32d |
eitherToMaybe _ = Nothing
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
-- | takes a list of keys/properties and a CSS string and
|
|
Packit |
dda32d |
-- returns the corresponding key-value-pairs.
|
|
Packit |
dda32d |
pickStylesToKVs :: [String] -> String -> [(String, String)]
|
|
Packit |
dda32d |
pickStylesToKVs props styleAttr =
|
|
Packit |
dda32d |
case parse styleAttrParser "" styleAttr of
|
|
Packit |
dda32d |
Left _ -> []
|
|
Packit |
dda32d |
Right styles -> filter (\s -> fst s `elem` props) styles
|
|
Packit |
dda32d |
|
|
Packit |
dda32d |
-- | takes a list of key/property synonyms and a CSS string and maybe
|
|
Packit |
dda32d |
-- returns the value of the first match (in order of the supplied list)
|
|
Packit |
dda32d |
pickStyleAttrProps :: [String] -> String -> Maybe String
|
|
Packit |
dda32d |
pickStyleAttrProps lookupProps styleAttr = do
|
|
Packit |
dda32d |
styles <- eitherToMaybe $ parse styleAttrParser "" styleAttr
|
|
Packit |
dda32d |
foldOrElse Nothing $ map (`lookup` styles) lookupProps
|