new function to extract multiple properties at once in CSS.hs

and use it in Textile reader
This commit is contained in:
mb21 2015-12-05 14:12:20 +01:00
parent 30644b291b
commit 2060f5fe83
2 changed files with 14 additions and 6 deletions

View file

@ -1,5 +1,6 @@
module Text.Pandoc.CSS ( foldOrElse, module Text.Pandoc.CSS ( foldOrElse
pickStyleAttrProps , pickStyleAttrProps
, pickStylesToKVs
) )
where where
@ -26,6 +27,16 @@ eitherToMaybe :: Either a b -> Maybe b
eitherToMaybe (Right x) = Just x eitherToMaybe (Right x) = Just x
eitherToMaybe _ = Nothing 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 :: [String] -> String -> Maybe String
pickStyleAttrProps lookupProps styleAttr = do pickStyleAttrProps lookupProps styleAttr = do
styles <- eitherToMaybe $ parse styleAttrParser "" styleAttr styles <- eitherToMaybe $ parse styleAttrParser "" styleAttr

View file

@ -537,11 +537,8 @@ image :: Parser [Char] ParserState Inlines
image = try $ do image = try $ do
char '!' >> notFollowedBy space char '!' >> notFollowedBy space
(ident, cls, kvs) <- attributes (ident, cls, kvs) <- attributes
let getAtt k styles = case pickStyleAttrProps [k] styles of
Just v -> [(k, v)]
Nothing -> []
let attr = case lookup "style" kvs of let attr = case lookup "style" kvs of
Just stls -> (ident, cls, getAtt "width" stls ++ getAtt "height" stls) Just stls -> (ident, cls, pickStylesToKVs ["width", "height"] stls)
Nothing -> (ident, cls, kvs) Nothing -> (ident, cls, kvs)
src <- manyTill anyChar' (lookAhead $ oneOf "!(") src <- manyTill anyChar' (lookAhead $ oneOf "!(")
alt <- option "" (try $ (char '(' >> manyTill anyChar' (char ')'))) alt <- option "" (try $ (char '(' >> manyTill anyChar' (char ')')))