Skip to content

Instantly share code, notes, and snippets.

@clojj
Last active October 18, 2019 20:29
Show Gist options
  • Save clojj/43782a091cf9ccbab9ff013fcdfef533 to your computer and use it in GitHub Desktop.
Save clojj/43782a091cf9ccbab9ff013fcdfef533 to your computer and use it in GitHub Desktop.
Haskell literal Html
{-# LANGUAGE OverloadedStrings #-}
module Lib
( Html, Markup(..), html, eval
) where
eval :: Html -> String
eval (Html head body) = "<html>" ++ evalHead head ++ evalBody body ++ "</html>"
evalHead :: Head -> String
evalHead (Head resources) = "<head>" ++ concatMap evalResource resources ++ "</head>"
evalResource :: Resource -> String
evalResource resource = ""
evalBody :: Body -> String
evalBody (Body markups) = "<body>" ++ concatMap evalMarkup markups ++ "</body>"
evalMarkup :: Markup -> String
evalMarkup (Div attrs markup) = "<div" ++ concatMap evalAttr attrs ++ ">" ++ concatMap evalMarkup markup ++ "</div>"
evalMarkup (Text str) = str
evalAttr :: Attr -> String
evalAttr (name, value) = " " ++ name ++ "=\"" ++ value ++ "\""
-- model
data Html =
Html Head Body
data Head = Head [Resource]
data Resource = Resource
data Body = Body [Markup]
data Markup =
Div [Attr] [Markup]
| Text String
type Attr = (String, String)
html :: Html
html = Html
(Head [])
(Body [
Div [("class", "mystyle")]
[
Text "1"
, Text "2"
, Text "3"
]
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment