From e45e40ba46103ccef5b5d3fe040e95376c2291e7 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Fri, 24 Apr 2020 20:33:58 +1200 Subject: [PATCH] XML Conduit Stylist Refactor: convert XML Conduit tree to Haskell Stylist Element StyleTree. --- xml-conduit-stylist/src/Data/HTML2CSS.hs | 37 +++++++++++++++++++----- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/xml-conduit-stylist/src/Data/HTML2CSS.hs b/xml-conduit-stylist/src/Data/HTML2CSS.hs index 2629635..8f5ffca 100644 --- a/xml-conduit-stylist/src/Data/HTML2CSS.hs +++ b/xml-conduit-stylist/src/Data/HTML2CSS.hs @@ -14,7 +14,8 @@ import Data.Maybe (fromMaybe) import qualified Text.XML as XML import Data.CSS.Syntax.StyleSheet import Data.CSS.Style -import Data.CSS.Syntax.Tokens (tokenize) +import Data.CSS.StyleTree +import Data.CSS.Syntax.Tokens import Data.CSS.Preprocessor.Conditions import qualified Data.CSS.Preprocessor.Conditions.Expr as Query import Network.URI @@ -115,10 +116,30 @@ elToStylish (XML.Element (XML.Name name ns _) attrs _) parent previous = parent = parent, previous = previous } -addPsuedoclasses el psuedoclasses - | (Attribute "" "" value : attrs) <- attributes el = el { - attributes = Attribute "" "" (psuedoclasses ++ value) : attrs - } - | otherwise = el { - attributes = Attribute "" "" psuedoclasses : attributes el - } + +el2stylist el = nodes2stylist [XML.NodeElement el] Nothing Nothing +nodes2stylist (XML.NodeElement (XML.Element (XML.Name name ns _) attrs childs):nodes) parent previous = + let el = ElementNode { + name = name, + namespace = fromMaybe "" ns, + attributes = L.sort [ + Attribute name (fromMaybe "" ns) (Txt.unpack value) + | (XML.Name name ns _, value) <- M.toList attrs + ], + parent = parent, + previous = previous + } in StyleTree { + style = el, + children = nodes2stylist childs (Just el) Nothing + } : nodes2stylist nodes parent previous +nodes2stylist (XML.NodeContent txt:nodes) parent previous = StyleTree { + style = ElementNode { + name = "", namespace = "", + attributes = [Attribute "style" "" $ Txt.unpack $ + Txt.append "content: " $ serialize [String txt]], + parent = parent, previous = previous + }, + children = [] + } : nodes2stylist nodes parent previous +nodes2stylist (_:nodes) parent previous = nodes2stylist nodes parent previous +nodes2stylist [] _ _ = [] -- 2.30.2