Skip to content

Instantly share code, notes, and snippets.

@douglashill
Created July 9, 2014 16:57
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 douglashill/9f754263840651bd4781 to your computer and use it in GitHub Desktop.
Save douglashill/9f754263840651bd4781 to your computer and use it in GitHub Desktop.
Incomplete, but a promising start. I like either types.
// HTMLWriter.swift
// Douglas Hill, June 2014
// HTML syntax documentation: http://www.w3.org/TR/html-markup/syntax.html
enum HTMLContent {
case Element(HTMLElement)
case Text(String)
}
struct HTMLElement {
var name: String
var attributes: Dictionary<String, String>
var contents: HTMLContent[]
}
extension HTMLElement: Printable {
var description: String {
var attributesString = attributes.count >= 1 ? " " : ""
for (key, value) in attributes {
attributesString += "\(key)=\"\(value)\""
}
var contentsString = ""
for item in contents {
switch item {
case .Element(let element): contentsString += element.description
case .Text(let text): contentsString += text
}
}
return "<\(name)\(attributesString)>\(contentsString)</\(name)>"
}
}
let title = HTMLElement(name: "h1", attributes: ["id": "main-title"], contents: [.Text("The "), .Element(HTMLElement(name: "em", attributes: [:], contents: [.Text("Title")]))])
println("\(title)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment