Last active
July 4, 2016 13:58
-
-
Save lepoetemaudit/b43a7e5026d52c51cce1f245403275b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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