~alcinnz/haskell-stylist

d7ce78638b82ba58283b9c197ace8be368f6e5dc — Adrian Cochrane 3 years ago cbb7662
XML Conduit Stylist functions upstream.
1 files changed, 16 insertions(+), 10 deletions(-)

M xml-conduit-stylist/src/Data/HTML2CSS.hs
M xml-conduit-stylist/src/Data/HTML2CSS.hs => xml-conduit-stylist/src/Data/HTML2CSS.hs +16 -10
@@ 2,7 2,8 @@
-- | Bindings from `xml-conduit` to `haskell-stylist`.
module Data.HTML2CSS(
        html2css, cssPriorityAgent, cssPriorityUser, cssPriorityAuthor, -- parsing
        preorder, el2styletree, els2stylist, el2stylist, stylize, stylize', stylizeEl -- application
        preorder, el2styletree, els2stylist, el2stylist, stylize, stylize', stylizeEl, -- application
        inlinePseudos, stylizeNoPseudos, stylizeElNoPseudos
    ) where

import qualified Data.List as L


@@ 65,15 66,6 @@ strContent [] = ""

---- Styling

preorder :: (Maybe b -> Maybe b -> a -> b) -> StyleTree a -> StyleTree b
preorder cb self = head $ preorder' cb Nothing Nothing [self]
preorder' :: (Maybe b -> Maybe b -> a -> b) ->
        Maybe b -> Maybe b -> [StyleTree a] -> [StyleTree b]
preorder' cb parent previous (self:sibs) = let self' = cb parent previous $ style self
        in StyleTree self' (preorder' cb (Just self') Nothing $ children self) :
            preorder' cb parent (Just self') sibs
preorder' _ _ _ [] = []

el2styletree el = StyleTree (Left el) $ mapMaybe node2styletree $ XML.elementNodes el
node2styletree (XML.NodeElement el) = Just $ el2styletree el
node2styletree (XML.NodeContent txt) = Just $ StyleTree (Right [("content", [String txt])]) []


@@ 110,3 102,17 @@ stylize' stylesheet parent _ el = ("", base) : [
        overrides = concat [fst $ parseProperties' $ tokenize $ Txt.pack val
            | Attribute "style" _ val <- attributes el]
stylizeEl stylesheet = stylize stylesheet . el2stylist

inlinePseudos :: PropertyParser s => StyleTree [(Txt.Text, VarParser s)] -> StyleTree s
inlinePseudos (StyleTree self childs) = StyleTree {
        style = fromMaybe temp $ innerParser <$> lookup "" self,
        children = pseudo "before" ++ map inlinePseudos childs ++ pseudo "after"
    } where
        pseudo n
            | Just style <- innerParser <$> lookup n self,
                Just style' <- longhand style style "::" [Ident n] = [StyleTree style' []]
            | Just style <- innerParser <$> lookup n self = [StyleTree style []]
            | otherwise = []

stylizeNoPseudos css = inlinePseudos . stylize css
stylizeElNoPseudos css = inlinePseudos . stylizeEl css