Created
July 9, 2014 16:57
-
-
Save douglashill/9f754263840651bd4781 to your computer and use it in GitHub Desktop.
Incomplete, but a promising start. I like either types.
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
// 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