Skip to content

Instantly share code, notes, and snippets.

@LnL7 LnL7/html.hs
Created Jul 28, 2012

Embed
What would you like to do?
module One where
class HTML a where
oneToHTML :: a -> String
manyToHTML :: [a] -> String
data Attribute = Attribute {
key :: String,
value :: String
} deriving (Show, Eq)
instance HTML Attribute where
oneToHTML (Attribute {key=key, value=val}) = key ++ "=\"" ++ val ++ "\""
manyToHTML (attr : attrs) = " " ++ oneToHTML attr ++ manyToHTML attrs
manyToHTML [] = ""
data Tag = Tag {
name :: String,
attributes :: [Attribute],
children :: Maybe [Tag]
} deriving (Show, Eq)
instance HTML Tag where
oneToHTML (Tag {name=name, attributes=attrs, children=childs})
| childs == Nothing = startTag
| True = startTag ++ (manyToHTML childs) ++ endTag
where
startTag = "<" ++ name ++ (manyToHTML $ extract attrs) ++ ">"
endTag = "<" ++ name ++ "/>"
extract (Just tags) = tags
extract (Nothing) = []
manyToHTML (tag : tags) = oneToHTML tag ++ manyToHTML tags
manyToHTML [] = ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.