~alcinnz/haskell-stylist

e45e40ba46103ccef5b5d3fe040e95376c2291e7 — Adrian Cochrane 4 years ago 45584b8
XML Conduit Stylist Refactor: convert XML Conduit tree to Haskell Stylist Element StyleTree.
1 files changed, 29 insertions(+), 8 deletions(-)

M xml-conduit-stylist/src/Data/HTML2CSS.hs
M xml-conduit-stylist/src/Data/HTML2CSS.hs => xml-conduit-stylist/src/Data/HTML2CSS.hs +29 -8
@@ 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 [] _ _ = []