diff options
Diffstat (limited to 'src/Text/Pandoc/CSS.hs')
-rw-r--r-- | src/Text/Pandoc/CSS.hs | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/src/Text/Pandoc/CSS.hs b/src/Text/Pandoc/CSS.hs index 9d0c84243..f479ed9d0 100644 --- a/src/Text/Pandoc/CSS.hs +++ b/src/Text/Pandoc/CSS.hs @@ -1,5 +1,6 @@ -module Text.Pandoc.CSS ( foldOrElse, - pickStyleAttrProps +module Text.Pandoc.CSS ( foldOrElse + , pickStyleAttrProps + , pickStylesToKVs ) where @@ -10,13 +11,11 @@ import Text.Parsec.String ruleParser :: Parser (String, String) ruleParser = do p <- many1 (noneOf ":") <* char ':' - v <- many1 (noneOf ":;") <* char ';' <* spaces + v <- many1 (noneOf ":;") <* (optional $ char ';') <* spaces return (trim p, trim v) styleAttrParser :: Parser [(String, String)] -styleAttrParser = do - p <- many1 ruleParser - return p +styleAttrParser = many1 ruleParser orElse :: Eq a => a -> a -> a -> a orElse v x y = if v == x then y else x @@ -28,6 +27,16 @@ eitherToMaybe :: Either a b -> Maybe b eitherToMaybe (Right x) = Just x eitherToMaybe _ = Nothing +-- | takes a list of keys/properties and a CSS string and +-- returns the corresponding key-value-pairs. +pickStylesToKVs :: [String] -> String -> [(String, String)] +pickStylesToKVs props styleAttr = + case parse styleAttrParser "" styleAttr of + Left _ -> [] + Right styles -> filter (\s -> fst s `elem` props) styles + +-- | takes a list of key/property synonyms and a CSS string and maybe +-- returns the value of the first match (in order of the supplied list) pickStyleAttrProps :: [String] -> String -> Maybe String pickStyleAttrProps lookupProps styleAttr = do styles <- eitherToMaybe $ parse styleAttrParser "" styleAttr |