//
// Created by Lennart Stolz on 03.07.15.
// Copyright (c) 2016 Lennart Stolz. All rights reserved.
//
import Foundation
/// Function to create the lstolz.com webiste.
func createWebsite(withFactory factory: HTMLFactory ) throws {
// This is the part where I should be polite.
let greeting = factory.element(tag: "h1", content: "Hello Visitor" )
print (greeting)
let subline = factory.element(tag: "h2" , content: "Welcome to my website")
print (subline)
// Not sure if I'm an attention-seeker or just a self-publicist.
let about = factory.element(tag: "h2" , content: "About me")
print (about)
let firstName = "Lennart"
let lastName = "Stolz"
let aboutContent = factory.element(tag: "p", content: "I'm \ (firstName) \ (lastName)")
print (aboutContent)
// I'm just getting older and better. (Neal Schon)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd.MM.y"
guard let dateOfBirth = dateFormatter. date(from: "20.03.1989") else {
throw HTMLFactory.Errors .invalidDate
}
let now = Date()
let calendar = Calendar.current
guard let age = calendar.dateComponents ([.year], from: dateOfBirth, to: now). year else {
throw HTMLFactory.Errors .invalidDate
}
print (factory.element(tag: "p" , content: "I'm \( age) years old."))
// Stop here before getting a shaggy dog story.
}
/// A tiny factory to provide html elements.
class HTMLFactory {
/**
Method to create an html element with opening and closing tag for a given content.
- Parameters:
- tag: The html tag which should be used for this element.
- content: The text which should be displayed within the html element.
- Returns: The aggregated html element.
*/
func element(tag: String, content: String ) -> String {
return "<\(tag )>\(content )</\(tag )>"
}
enum Errors: Error {
case invalidDate
}
}
let factory = HTMLFactory ()
try createWebsite(withFactory: factory)