git.iko.soy/typeable/pandoc
on bc3caaa help sign in
58 lines 1.9 kB history raw
1{-# LANGUAGE NoImplicitPrelude #-}
2{- |
3Module : Text.Pandoc.CSS
4Copyright : © 2006-2019 John MacFarlane <jgm@berkeley.edu>,
5 2015-2016 Mauro Bieg,
6 2015 Ophir Lifshitz <hangfromthefloor@gmail.com>
7License : GNU GPL, version 2 or above
9Maintainer : John MacFarlane <jgm@berkeley@edu>
10Stability : alpha
11Portability : portable
13Tools for working with CSS.
14-}
15module Text.Pandoc.CSS ( foldOrElse
16 , pickStyleAttrProps
17 , pickStylesToKVs
18 )
19where
21import Prelude
22import Text.Pandoc.Shared (trim)
23import Text.Parsec
24import Text.Parsec.String
26ruleParser :: Parser (String, String)
27ruleParser = do
28 p <- many1 (noneOf ":") <* char ':'
29 v <- many1 (noneOf ":;") <* optional (char ';') <* spaces
30 return (trim p, trim v)
32styleAttrParser :: Parser [(String, String)]
33styleAttrParser = many1 ruleParser
35orElse :: Eq a => a -> a -> a -> a
36orElse v x y = if v == x then y else x
38foldOrElse :: Eq a => a -> [a] -> a
39foldOrElse v xs = foldr (orElse v) v xs
41eitherToMaybe :: Either a b -> Maybe b
42eitherToMaybe (Right x) = Just x
43eitherToMaybe _ = Nothing
45-- | takes a list of keys/properties and a CSS string and
46-- returns the corresponding key-value-pairs.
47pickStylesToKVs :: [String] -> String -> [(String, String)]
48pickStylesToKVs props styleAttr =
49 case parse styleAttrParser "" styleAttr of
50 Left _ -> []
51 Right styles -> filter (\s -> fst s `elem` props) styles
53-- | takes a list of key/property synonyms and a CSS string and maybe
54-- returns the value of the first match (in order of the supplied list)
55pickStyleAttrProps :: [String] -> String -> Maybe String
56pickStyleAttrProps lookupProps styleAttr = do
57 styles <- eitherToMaybe $ parse styleAttrParser "" styleAttr
58 foldOrElse Nothing $ map (`lookup` styles) lookupProps