Skip to content

Instantly share code, notes, and snippets.

@lepoetemaudit
Last active July 4, 2016 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lepoetemaudit/b43a7e5026d52c51cce1f245403275b0 to your computer and use it in GitHub Desktop.
Save lepoetemaudit/b43a7e5026d52c51cce1f245403275b0 to your computer and use it in GitHub Desktop.
type attribute = | Attribute of string * string
type node =
| Node of string * (attribute list) * (node list)
| Text of string
let element name attrs nodes =
Node (name, attrs, nodes)
let attr k v =
Attribute (k, v)
(* Elements *)
let div = element "div"
let html = element "html"
let p = element "p"
(* Attributes *)
let style = attr "style"
let text value = Text value
let render_attr attr =
match attr with
Attribute (key, value) -> key ^ "=\"" ^ value ^ "\""
let rec render_html node =
match node with
| Node (name, attrs, nodes) ->
"<" ^ name ^ (match attrs with | hd::[] -> " " | _ -> "") ^
((List.map render_attr attrs) |> String.concat " ") ^
">" ^
((List.map render_html nodes) |> String.concat " ") ^
"</" ^ name ^ ">"
| Text value -> value
let () =
html [] [
div [] [
p [(style "color: black")] [ text "Hello World!" ]
]
]
|> render_html
|> print_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment