Skip to content

Instantly share code, notes, and snippets.

@LnL7
Created July 28, 2012 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LnL7/3193958 to your computer and use it in GitHub Desktop.
Save LnL7/3193958 to your computer and use it in GitHub Desktop.
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