Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Attribute = Attribute of string*string
- type Node =
- | Element of string * Node list
- | Text of string
- | Attributes of Attribute list
- with
- static member ToHTMLString node =
- match node with
- | Element (name, children) ->
- // Attributes nodes and child nodes are inserted at different places
- // in generated html so split 'em up
- let attributes, other = children |> List.partition (fun child ->
- match child with Attributes _ -> true | _ -> false
- )
- let attributesString =
- attributes
- |> List.map Node.ToHTMLString
- |> String.concat ""
- let bodyString =
- other
- |> List.map Node.ToHTMLString
- |> String.concat ""
- sprintf "<%s%s>%s</%s>" name attributesString bodyString name
- | Attributes list ->
- List.fold (fun str (Attribute (name,value)) ->
- sprintf "%s %s='%s'" str name value
- ) "" list
- | Text text ->
- text // Todo escape
- module Element =
- let element name children = Element (name,children)
- let Html = element "html"
- let Head = element "head"
- let Title text = element "title" [(Text text)]
- let Body = element "body"
- let P = element "p"
- [<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
- module Attribute =
- let attribute name value = Attribute (name,value)
- let Background = attribute "background"
- let Style = attribute "style"
- open Element
- open Attribute
- let page =
- Html [
- Head [
- Title "Page Title"
- ]
- Body [
- Attributes [Background "black"; Style "default"]
- P [Text "Some body text!!"]
- ]
- ]
- printfn "HTML: %s" (Node.ToHTMLString page)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement