git.iko.soy/typeable/pandoc
on 3645607 help sign in
56 lines 1.8 kB history raw
1{- |
2Module : Text.Pandoc.CSS
3Copyright : © 2006-2021 John MacFarlane <jgm@berkeley.edu>,
4 2015-2016 Mauro Bieg,
5 2015 Ophir Lifshitz <hangfromthefloor@gmail.com>
6License : GNU GPL, version 2 or above
8Maintainer : John MacFarlane <jgm@berkeley@edu>
9Stability : alpha
10Portability : portable
12Tools for working with CSS.
13-}
14module Text.Pandoc.CSS
15 ( cssAttributes
16 , pickStyleAttrProps
17 , pickStylesToKVs
18 )
19where
21import Data.Maybe (mapMaybe, listToMaybe)
22import Data.Text (Text, pack)
23import Text.Pandoc.Shared (trim)
24import Text.Parsec
25import Text.Parsec.Text
27ruleParser :: Parser (Text, Text)
28ruleParser = do
29 p <- many1 (noneOf ":") <* char ':'
30 v <- many1 (noneOf ":;") <* optional (char ';') <* spaces
31 return (trim $ pack p, trim $ pack v)
33styleAttrParser :: Parser [(Text, Text)]
34styleAttrParser = many1 ruleParser
36-- | Parses a style string, returning the CSS attributes.
37-- Returns an empty list on failure.
38cssAttributes :: Text -> [(Text, Text)]
39cssAttributes styleString =
40 -- Use Data.Either.fromRight once GHC 8.0 is no longer supported
41 case parse styleAttrParser "" styleString of
42 Left _ -> []
43 Right x -> x
45-- | takes a list of keys/properties and a CSS string and
46-- returns the corresponding key-value-pairs.
47pickStylesToKVs :: [Text] -> Text -> [(Text, Text)]
48pickStylesToKVs props styleAttr =
49 filter (\s -> fst s `elem` props) $ cssAttributes styleAttr
51-- | takes a list of key/property synonyms and a CSS string and maybe
52-- returns the value of the first match (in order of the supplied list)
53pickStyleAttrProps :: [Text] -> Text -> Maybe Text
54pickStyleAttrProps lookupProps styleAttr = do
55 styles <- either (const Nothing) Just $ parse styleAttrParser "" styleAttr
56 listToMaybe $ mapMaybe (`lookup` styles) lookupProps