2022-08-16 10:39:05 +02:00
|
|
|
import Foundation
|
|
|
|
import Ink
|
|
|
|
|
|
|
|
struct LocalizedSiteTemplate {
|
|
|
|
|
|
|
|
let author: String
|
|
|
|
|
|
|
|
let factory: TemplateFactory
|
|
|
|
|
|
|
|
let topBar: PrefilledTopBarTemplate
|
|
|
|
|
|
|
|
// MARK: Site Elements
|
|
|
|
|
|
|
|
var backNavigation: BackNavigationTemplate {
|
|
|
|
factory.backNavigation
|
|
|
|
}
|
|
|
|
|
|
|
|
let pageHead: PageHeadGenerator
|
|
|
|
|
|
|
|
let overviewSection: OverviewSectionGenerator
|
|
|
|
|
|
|
|
let placeholder: String
|
|
|
|
|
|
|
|
private let fullDateFormatter: DateFormatter
|
|
|
|
|
|
|
|
private let month: DateFormatter
|
|
|
|
|
|
|
|
private let day: DateFormatter
|
|
|
|
|
|
|
|
var language: String {
|
|
|
|
topBar.language
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Pages
|
|
|
|
|
2022-08-16 12:27:13 +02:00
|
|
|
var page: PageTemplate {
|
|
|
|
factory.page
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
init(factory: TemplateFactory, language: String, site: Element) throws {
|
|
|
|
self.author = site.author
|
2022-08-16 10:39:05 +02:00
|
|
|
self.factory = factory
|
|
|
|
|
|
|
|
let df = DateFormatter()
|
|
|
|
df.dateStyle = .long
|
|
|
|
df.timeStyle = .none
|
|
|
|
df.locale = Locale(identifier: language)
|
|
|
|
self.fullDateFormatter = df
|
|
|
|
|
|
|
|
let df2 = DateFormatter()
|
|
|
|
df2.dateFormat = "MMMM"
|
|
|
|
df2.locale = Locale(identifier: language)
|
|
|
|
self.month = df2
|
|
|
|
|
|
|
|
let df3 = DateFormatter()
|
|
|
|
df3.dateFormat = "dd"
|
|
|
|
df3.locale = Locale(identifier: language)
|
|
|
|
self.day = df3
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
let metadata = site.localized(for: language)
|
|
|
|
|
2022-08-16 10:39:05 +02:00
|
|
|
let sections = site.elements.map {
|
|
|
|
PrefilledTopBarTemplate.SectionInfo(
|
|
|
|
name: $0.title(for: language),
|
|
|
|
url: "\($0.path)/\(language).html")
|
|
|
|
}
|
|
|
|
|
|
|
|
self.topBar = try .init(
|
|
|
|
template: factory.topBar,
|
|
|
|
language: language,
|
|
|
|
sections: sections,
|
2022-08-26 17:40:51 +02:00
|
|
|
topBarWebsiteTitle: site.topBarTitle)
|
2022-08-16 10:39:05 +02:00
|
|
|
self.pageHead = PageHeadGenerator(
|
2022-08-26 17:40:51 +02:00
|
|
|
factory: factory)
|
2022-08-16 10:39:05 +02:00
|
|
|
self.overviewSection = OverviewSectionGenerator(
|
2022-08-26 17:40:51 +02:00
|
|
|
factory: factory)
|
2022-08-16 10:39:05 +02:00
|
|
|
|
|
|
|
self.placeholder = factory.placeholder.generate([
|
|
|
|
.title: metadata.placeholderTitle,
|
|
|
|
.text: metadata.placeholderText])
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Content
|
|
|
|
|
|
|
|
func makeBackLink(text: String, language: String) -> String {
|
|
|
|
let content: [BackNavigationTemplate.Key : String] = [
|
|
|
|
.text: text,
|
|
|
|
.url: "../\(language).html"
|
|
|
|
]
|
|
|
|
return backNavigation.generate(content)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makePrevText(_ text: String) -> String {
|
|
|
|
"<span class=\"icon-back\"></span>\(text)"
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeNextText(_ text: String) -> String {
|
|
|
|
"\(text)<span class=\"icon-next\"></span>"
|
|
|
|
}
|
|
|
|
|
2022-08-26 17:40:51 +02:00
|
|
|
func makeDateString(start: Date?, end: Date?) -> String {
|
|
|
|
guard let start = start else {
|
|
|
|
return ""
|
|
|
|
}
|
2022-08-16 10:39:05 +02:00
|
|
|
guard let end = end else {
|
|
|
|
return fullDateFormatter.string(from: start)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch language {
|
|
|
|
case "de":
|
|
|
|
return makeGermanDateString(start: start, end: end)
|
|
|
|
case "en":
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
return makeEnglishDateString(start: start, end: end)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func makeGermanDateString(start: Date, end: Date) -> String {
|
|
|
|
guard Calendar.current.isDate(start, equalTo: end, toGranularity: .year) else {
|
|
|
|
return "\(fullDateFormatter.string(from: start)) - \(fullDateFormatter.string(from: end))"
|
|
|
|
}
|
|
|
|
|
|
|
|
let startDay = day.string(from: start)
|
|
|
|
guard Calendar.current.isDate(start, equalTo: end, toGranularity: .month) else {
|
|
|
|
let startMonth = month.string(from: start)
|
|
|
|
return "\(startDay). \(startMonth) - \(fullDateFormatter.string(from: end))"
|
|
|
|
}
|
|
|
|
return "\(startDay). - \(fullDateFormatter.string(from: end))"
|
|
|
|
}
|
|
|
|
|
|
|
|
private func makeEnglishDateString(start: Date, end: Date) -> String {
|
|
|
|
guard Calendar.current.isDate(start, equalTo: end, toGranularity: .year) else {
|
|
|
|
return "\(fullDateFormatter.string(from: start)) - \(fullDateFormatter.string(from: end))"
|
|
|
|
}
|
|
|
|
|
|
|
|
guard Calendar.current.isDate(start, equalTo: end, toGranularity: .month) else {
|
|
|
|
let startDay = day.string(from: start)
|
|
|
|
let startMonth = month.string(from: start)
|
|
|
|
return "\(startMonth) \(startDay) - \(fullDateFormatter.string(from: end))"
|
|
|
|
}
|
|
|
|
return fullDateFormatter.string(from: start)
|
|
|
|
.insert(" - \(day.string(from: end))", beforeLast: ",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|