Reorganize saving, generate feed
This commit is contained in:
@ -1,79 +0,0 @@
|
||||
import Foundation
|
||||
|
||||
struct FeedNavigationLink {
|
||||
|
||||
let text: String
|
||||
|
||||
let url: String
|
||||
}
|
||||
|
||||
struct Feed {
|
||||
|
||||
private let navigationIconPath = "/assets/icons/ch.svg"
|
||||
|
||||
let language: ContentLanguage
|
||||
|
||||
let title: String
|
||||
|
||||
let description: String
|
||||
|
||||
let iconDescription: String
|
||||
|
||||
let navigationItems: [FeedNavigationLink]
|
||||
|
||||
let posts: [FeedEntryData]
|
||||
|
||||
var content: String {
|
||||
#warning("TODO: Split feed into multiple pages")
|
||||
var result = ""
|
||||
result += "<!DOCTYPE html><html lang=\"\(language.rawValue)\">"
|
||||
let head = PageHead(
|
||||
title: title,
|
||||
description: description)
|
||||
result += head.content
|
||||
result += "<body>"
|
||||
addNavbar(to: &result)
|
||||
result += "<div class=\"content\"><div style=\"height: 70px;\"></div>"
|
||||
for post in posts {
|
||||
FeedEntry(data: post)
|
||||
.addContent(to: &result)
|
||||
}
|
||||
|
||||
addSwiperInits(to: &result)
|
||||
result += "</div></body></html>" // Close content
|
||||
return result
|
||||
}
|
||||
|
||||
#warning("TODO: Set correct navigation links and texts")
|
||||
private func addNavbar(to result: inout String) {
|
||||
result += "<nav class=\"navbar\"><div class=\"navbar-fade\"></div><div class=\"nav-center\">"
|
||||
let middleIndex = navigationItems.count / 2
|
||||
let leftNavigationItems = navigationItems[..<middleIndex]
|
||||
let rightNavigationItems = navigationItems[middleIndex...]
|
||||
|
||||
for item in leftNavigationItems {
|
||||
result += "<a class=\"nav-animate\" href=\"\(item.url)\">\(item.text)</a>"
|
||||
}
|
||||
|
||||
result += "<a id=\"nav-image\" href=\"/\">"
|
||||
result += "<img class=\"navbar-icon\" src=\"\(navigationIconPath)\" alt=\"\(iconDescription)\">"
|
||||
|
||||
for item in rightNavigationItems {
|
||||
result += "<a class=\"nav-animate\" href=\"\(item.url)\">\(item.text)</a>"
|
||||
}
|
||||
result += "</div></nav>" // Close nav-center, navbar
|
||||
}
|
||||
|
||||
private func addSwiperInits(to result: inout String) {
|
||||
if posts.contains(where: { $0.images.count > 1 }) {
|
||||
result += "<script src=\"/assets/swiper/swiper.min.js\"></script><script>"
|
||||
for post in posts {
|
||||
guard post.images.count > 1 else {
|
||||
continue
|
||||
}
|
||||
result += ImageGallery.swiperInit(id: post.entryId)
|
||||
}
|
||||
result += "</script>"
|
||||
}
|
||||
}
|
||||
}
|
36
CHDataManagement/Pages/GenericPage.swift
Normal file
36
CHDataManagement/Pages/GenericPage.swift
Normal file
@ -0,0 +1,36 @@
|
||||
import Foundation
|
||||
|
||||
struct GenericPage {
|
||||
|
||||
let language: ContentLanguage
|
||||
|
||||
let title: String
|
||||
|
||||
let description: String
|
||||
|
||||
let data: NavigationBarData
|
||||
|
||||
let additionalHeaders: String
|
||||
|
||||
let insertedContent: (inout String) -> Void
|
||||
|
||||
init(language: ContentLanguage, title: String, description: String, data: NavigationBarData, additionalHeaders: String, insertedContent: @escaping (inout String) -> Void) {
|
||||
self.language = language
|
||||
self.title = title
|
||||
self.description = description
|
||||
self.data = data
|
||||
self.additionalHeaders = additionalHeaders
|
||||
self.insertedContent = insertedContent
|
||||
}
|
||||
var content: String {
|
||||
var result = ""
|
||||
result += "<!DOCTYPE html><html lang=\"\(language.rawValue)\">"
|
||||
result += PageHead(title: title, description: description, additionalHeaders: additionalHeaders).content
|
||||
result += "<body>"
|
||||
result += NavigationBar(data: data).content
|
||||
result += "<div class=\"content\"><div style=\"height: 70px;\"></div>"
|
||||
insertedContent(&result)
|
||||
result += "</div></body></html>" // Close content
|
||||
return result
|
||||
}
|
||||
}
|
58
CHDataManagement/Pages/PageInFeed.swift
Normal file
58
CHDataManagement/Pages/PageInFeed.swift
Normal file
@ -0,0 +1,58 @@
|
||||
import Foundation
|
||||
|
||||
struct PageInFeed {
|
||||
|
||||
private let swiperStyleSheetPath = "/assets/swiper/swiper-bundle.min.css"
|
||||
|
||||
private let swiperJsPath = "/assets/swiper/swiper-bundle.min.js"
|
||||
|
||||
let language: ContentLanguage
|
||||
|
||||
let title: String
|
||||
|
||||
let description: String
|
||||
|
||||
let navigationBarData: NavigationBarData
|
||||
|
||||
let pageNumber: Int
|
||||
|
||||
let totalPages: Int
|
||||
|
||||
let posts: [FeedEntryData]
|
||||
|
||||
private var swiperHeader: String {
|
||||
"<link rel='stylesheet' href='\(swiperStyleSheetPath)' />"
|
||||
}
|
||||
|
||||
private var swiperIsNeeded: Bool {
|
||||
posts.contains(where: { $0.images.count > 1 })
|
||||
}
|
||||
|
||||
private var headers: String {
|
||||
swiperIsNeeded ? swiperHeader : ""
|
||||
}
|
||||
|
||||
var content: String {
|
||||
GenericPage(language: language, title: title, description: description, data: navigationBarData, additionalHeaders: headers) { content in
|
||||
for post in posts {
|
||||
FeedEntry(data: post)
|
||||
.addContent(to: &content)
|
||||
}
|
||||
content += PostFeedPageNavigation(currentPage: pageNumber, numberOfPages: totalPages, language: language).content
|
||||
if swiperIsNeeded {
|
||||
addSwiperInits(to: &content)
|
||||
}
|
||||
}.content
|
||||
}
|
||||
|
||||
private func addSwiperInits(to result: inout String) {
|
||||
result += "<script src='\(swiperJsPath)'></script><script>"
|
||||
for post in posts {
|
||||
guard post.images.count > 1 else {
|
||||
continue
|
||||
}
|
||||
result += ImageGallery.swiperInit(id: post.entryId)
|
||||
}
|
||||
result += "</script>"
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user