Full page content, fixes, cleaner settings
This commit is contained in:
@ -1,56 +1,47 @@
|
||||
extension Content {
|
||||
|
||||
#warning("Get tag url prefix from settings")
|
||||
func tagLink(_ tag: Tag, language: ContentLanguage) -> String {
|
||||
"/tags/\(tag.localized(in: language).urlComponent).html"
|
||||
private func makeCleanAbsolutePath(_ path: String) -> String {
|
||||
("/" + path).replacingOccurrences(of: "//", with: "/")
|
||||
}
|
||||
|
||||
func pageLink(_ page: Page, language: ContentLanguage) -> String {
|
||||
private func pathPrefix(for file: FileResource) -> String {
|
||||
switch file.type {
|
||||
case .image: return settings.paths.imagesOutputFolderPath
|
||||
case .video: return settings.paths.videosOutputFolderPath
|
||||
default: return settings.paths.filesOutputFolderPath
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Paths to items
|
||||
|
||||
func absoluteUrlPrefixForTag(_ tag: Tag, language: ContentLanguage) -> String {
|
||||
makeCleanAbsolutePath(settings.paths.tagsOutputFolderPath + "/" + tag.localized(in: language).urlComponent)
|
||||
}
|
||||
|
||||
func absoluteUrlToTag(_ tag: Tag, language: ContentLanguage) -> String {
|
||||
absoluteUrlPrefixForTag(tag, language: language) + ".html"
|
||||
}
|
||||
|
||||
func absoluteUrlToPage(_ page: Page, language: ContentLanguage) -> String {
|
||||
// TODO: Record link to trace connections between pages
|
||||
var prefix = settings.pages.pageUrlPrefix
|
||||
if !prefix.hasPrefix("/") {
|
||||
prefix = "/" + prefix
|
||||
}
|
||||
if !prefix.hasSuffix("/") {
|
||||
prefix.append("/")
|
||||
}
|
||||
|
||||
return prefix + page.localized(in: language).urlString
|
||||
makeCleanAbsolutePath(settings.pages.pageUrlPrefix + "/" + page.localized(in: language).urlString)
|
||||
}
|
||||
|
||||
/**
|
||||
Get the url path to a file in the output folder.
|
||||
The result is an absolute path from the output folder for use in HTML.
|
||||
*/
|
||||
func absoluteUrlToFile(_ file: FileResource) -> String {
|
||||
let path = pathPrefix(for: file) + "/" + file.id
|
||||
return makeCleanAbsolutePath(path)
|
||||
}
|
||||
|
||||
// MARK: Find items by id
|
||||
|
||||
func page(_ pageId: String) -> Page? {
|
||||
pages.first { $0.id == pageId }
|
||||
}
|
||||
|
||||
func pageLink(pageId: String, language: ContentLanguage) -> String? {
|
||||
guard let page = pages.first(where: { $0.id == pageId }) else {
|
||||
// TODO: Note missing link
|
||||
print("Missing page \(pageId) linked")
|
||||
return nil
|
||||
}
|
||||
return pageLink(page, language: language)
|
||||
}
|
||||
|
||||
func pathToFile(_ fileId: String) -> String? {
|
||||
guard let file = file(id: fileId) else {
|
||||
return nil
|
||||
}
|
||||
switch file.type {
|
||||
case .image: return pathToImage(file)
|
||||
case .video: return pathToVideo(file)
|
||||
default: return pathToFile(file)
|
||||
}
|
||||
}
|
||||
|
||||
func pathToFile(_ file: FileResource) -> String {
|
||||
#warning("Add files path to settings")
|
||||
return "/files/\(file.id)"
|
||||
}
|
||||
|
||||
func pathToImage(_ image: FileResource) -> String {
|
||||
return "/images/\(image.id)"
|
||||
}
|
||||
|
||||
func image(_ imageId: String) -> FileResource? {
|
||||
files.first { $0.id == imageId && $0.type.isImage }
|
||||
}
|
||||
@ -59,19 +50,11 @@ extension Content {
|
||||
files.first { $0.id == videoId && $0.type.isVideo }
|
||||
}
|
||||
|
||||
func pathToVideo(_ videoId: String) -> String? {
|
||||
guard let video = video(videoId) else {
|
||||
return nil
|
||||
}
|
||||
return pathToVideo(video)
|
||||
}
|
||||
|
||||
func pathToVideo(_ video: FileResource) -> String {
|
||||
"/videos/\(video.id)"
|
||||
}
|
||||
|
||||
|
||||
func file(id: String) -> FileResource? {
|
||||
files.first { $0.id == id }
|
||||
}
|
||||
|
||||
func tag(_ tagId: String) -> Tag? {
|
||||
tags.first { $0.id == tagId }
|
||||
}
|
||||
}
|
||||
|
@ -38,14 +38,6 @@ extension Content {
|
||||
linkPreviewDescription: page.linkPreviewDescription)
|
||||
}
|
||||
|
||||
private func convert(_ settings: LocalizedSettingsFile) -> LocalizedSettings {
|
||||
.init(navigationBarIconDescription: settings.navigationBarIconDescription,
|
||||
posts: .init(
|
||||
title: settings.posts.feedTitle,
|
||||
description: settings.posts.feedDescription,
|
||||
feedUrlPrefix: settings.posts.feedUrlPrefix))
|
||||
}
|
||||
|
||||
func loadFromDisk() throws {
|
||||
let storage = Storage(baseFolder: URL(filePath: contentPath))
|
||||
|
||||
@ -118,26 +110,23 @@ extension Content {
|
||||
|
||||
private func makeSettings(_ settings: SettingsFile, tags: [String : Tag]) -> Settings {
|
||||
|
||||
let navigationBar = NavigationBarSettings(
|
||||
iconPath: settings.navigationBar.navigationIconPath,
|
||||
tags: settings.navigationBar.navigationTags.map { tags[$0]! })
|
||||
let navigationTags = settings.navigationTags.map { tags[$0]! }
|
||||
|
||||
let posts = PostSettings(
|
||||
postsPerPage: settings.posts.postsPerPage,
|
||||
contentWidth: settings.posts.contentWidth)
|
||||
|
||||
let pages = PageSettings(
|
||||
pageUrlPrefix: settings.pages.pageUrlPrefix,
|
||||
contentWidth: settings.pages.contentWidth,
|
||||
largeImageWidth: settings.pages.largeImageWidth)
|
||||
let pages = PageSettings(file: settings.pages)
|
||||
|
||||
let paths = PathSettings(file: settings.paths)
|
||||
|
||||
return Settings(
|
||||
outputDirectoryPath: settings.outputDirectoryPath,
|
||||
navigationBar: navigationBar,
|
||||
paths: paths,
|
||||
navigationTags: navigationTags,
|
||||
posts: posts,
|
||||
pages: pages,
|
||||
german: convert(settings.german),
|
||||
english: convert(settings.english))
|
||||
german: .init(file: settings.german),
|
||||
english: .init(file: settings.english))
|
||||
}
|
||||
|
||||
private func loadPages(_ pagesData: [String : PageFile], tags: [String : Tag], images: [String : FileResource]) -> [String : Page] {
|
||||
|
@ -124,20 +124,12 @@ private extension LocalizedTag {
|
||||
}
|
||||
}
|
||||
|
||||
private extension NavigationBarSettings {
|
||||
|
||||
var file: NavigationBarSettingsFile {
|
||||
.init(navigationIconPath: iconPath,
|
||||
navigationTags: tags.map { $0.id })
|
||||
}
|
||||
}
|
||||
|
||||
extension Settings {
|
||||
|
||||
var file: SettingsFile {
|
||||
.init(
|
||||
outputDirectoryPath: outputDirectoryPath,
|
||||
navigationBar: navigationBar.file,
|
||||
paths: paths.file,
|
||||
navigationTags: navigationTags.map { $0.id },
|
||||
posts: posts.file,
|
||||
pages: pages.file,
|
||||
german: german.file,
|
||||
@ -145,6 +137,18 @@ extension Settings {
|
||||
}
|
||||
}
|
||||
|
||||
private extension PathSettings {
|
||||
|
||||
var file: PathSettingsFile {
|
||||
.init(outputDirectoryPath: outputDirectoryPath,
|
||||
pagesOutputFolderPath: pagesOutputFolderPath,
|
||||
imagesOutputFolderPath: imagesOutputFolderPath,
|
||||
filesOutputFolderPath: filesOutputFolderPath,
|
||||
videosOutputFolderPath: videosOutputFolderPath,
|
||||
tagsOutputFolderPath: tagsOutputFolderPath)
|
||||
}
|
||||
}
|
||||
|
||||
private extension PostSettings {
|
||||
|
||||
var file: PostSettingsFile {
|
||||
@ -158,15 +162,9 @@ private extension PageSettings {
|
||||
var file: PageSettingsFile {
|
||||
.init(pageUrlPrefix: pageUrlPrefix,
|
||||
contentWidth: contentWidth,
|
||||
largeImageWidth: largeImageWidth)
|
||||
}
|
||||
}
|
||||
|
||||
private extension LocalizedSettings {
|
||||
|
||||
var file: LocalizedSettingsFile {
|
||||
.init(navigationBarIconDescription: navigationBarIconDescription,
|
||||
posts: posts.file)
|
||||
largeImageWidth: largeImageWidth,
|
||||
pageLinkImageSize: pageLinkImageSize,
|
||||
javascriptFilesPath: javascriptFilesPath)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,4 +16,10 @@ final class LocalizedPostSettings: ObservableObject {
|
||||
self.description = description
|
||||
self.feedUrlPrefix = feedUrlPrefix
|
||||
}
|
||||
|
||||
init(file: LocalizedPostSettingsFile) {
|
||||
self.title = file.feedTitle
|
||||
self.description = file.feedDescription
|
||||
self.feedUrlPrefix = file.feedUrlPrefix
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +0,0 @@
|
||||
import Foundation
|
||||
|
||||
final class LocalizedSettings: ObservableObject {
|
||||
|
||||
@Published
|
||||
var navigationBarIconDescription: String
|
||||
|
||||
@Published
|
||||
var posts: LocalizedPostSettings
|
||||
|
||||
init(navigationBarIconDescription: String, posts: LocalizedPostSettings) {
|
||||
self.navigationBarIconDescription = navigationBarIconDescription
|
||||
self.posts = posts
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
import Foundation
|
||||
|
||||
final class NavigationBarSettings: ObservableObject {
|
||||
|
||||
/// The path to the main icon in the navigation bar
|
||||
@Published
|
||||
var iconPath: String
|
||||
|
||||
/// The tags to show in the navigation bar
|
||||
@Published
|
||||
var tags: [Tag]
|
||||
|
||||
init(iconPath: String, tags: [Tag]) {
|
||||
self.iconPath = iconPath
|
||||
self.tags = tags
|
||||
}
|
||||
}
|
@ -13,9 +13,17 @@ final class PageSettings: ObservableObject {
|
||||
@Published
|
||||
var largeImageWidth: Int
|
||||
|
||||
init(pageUrlPrefix: String, contentWidth: Int, largeImageWidth: Int) {
|
||||
self.pageUrlPrefix = pageUrlPrefix
|
||||
self.contentWidth = contentWidth
|
||||
self.largeImageWidth = largeImageWidth
|
||||
@Published
|
||||
var pageLinkImageSize: Int
|
||||
|
||||
@Published
|
||||
var javascriptFilesPath: String
|
||||
|
||||
init(file: PageSettingsFile) {
|
||||
self.pageUrlPrefix = file.pageUrlPrefix
|
||||
self.contentWidth = file.contentWidth
|
||||
self.largeImageWidth = file.largeImageWidth
|
||||
self.pageLinkImageSize = file.pageLinkImageSize
|
||||
self.javascriptFilesPath = file.javascriptFilesPath
|
||||
}
|
||||
}
|
||||
|
31
CHDataManagement/Model/Settings/PathSettings.swift
Normal file
31
CHDataManagement/Model/Settings/PathSettings.swift
Normal file
@ -0,0 +1,31 @@
|
||||
import Foundation
|
||||
|
||||
final class PathSettings: ObservableObject {
|
||||
|
||||
@Published
|
||||
var outputDirectoryPath: String
|
||||
|
||||
@Published
|
||||
var pagesOutputFolderPath: String
|
||||
|
||||
@Published
|
||||
var imagesOutputFolderPath: String
|
||||
|
||||
@Published
|
||||
var filesOutputFolderPath: String
|
||||
|
||||
@Published
|
||||
var videosOutputFolderPath: String
|
||||
|
||||
@Published
|
||||
var tagsOutputFolderPath: String
|
||||
|
||||
init(file: PathSettingsFile) {
|
||||
self.outputDirectoryPath = file.outputDirectoryPath
|
||||
self.pagesOutputFolderPath = file.pagesOutputFolderPath
|
||||
self.imagesOutputFolderPath = file.imagesOutputFolderPath
|
||||
self.filesOutputFolderPath = file.filesOutputFolderPath
|
||||
self.videosOutputFolderPath = file.videosOutputFolderPath
|
||||
self.tagsOutputFolderPath = file.tagsOutputFolderPath
|
||||
}
|
||||
}
|
@ -14,4 +14,9 @@ final class PostSettings: ObservableObject {
|
||||
self.postsPerPage = postsPerPage
|
||||
self.contentWidth = contentWidth
|
||||
}
|
||||
|
||||
init(file: PostSettingsFile) {
|
||||
self.postsPerPage = file.postsPerPage
|
||||
self.contentWidth = file.contentWidth
|
||||
}
|
||||
}
|
||||
|
@ -3,10 +3,11 @@ import Foundation
|
||||
final class Settings: ObservableObject {
|
||||
|
||||
@Published
|
||||
var outputDirectoryPath: String
|
||||
var paths: PathSettings
|
||||
|
||||
/// The tags to show in the navigation bar
|
||||
@Published
|
||||
var navigationBar: NavigationBarSettings
|
||||
var navigationTags: [Tag]
|
||||
|
||||
@Published
|
||||
var posts: PostSettings
|
||||
@ -15,24 +16,28 @@ final class Settings: ObservableObject {
|
||||
var pages: PageSettings
|
||||
|
||||
@Published
|
||||
var german: LocalizedSettings
|
||||
var german: LocalizedPostSettings
|
||||
|
||||
@Published
|
||||
var english: LocalizedSettings
|
||||
var english: LocalizedPostSettings
|
||||
|
||||
init(outputDirectoryPath: String, navigationBar: NavigationBarSettings, posts: PostSettings, pages: PageSettings, german: LocalizedSettings, english: LocalizedSettings) {
|
||||
self.outputDirectoryPath = outputDirectoryPath
|
||||
self.navigationBar = navigationBar
|
||||
init(paths: PathSettings, navigationTags: [Tag], posts: PostSettings, pages: PageSettings, german: LocalizedPostSettings, english: LocalizedPostSettings) {
|
||||
self.paths = paths
|
||||
self.navigationTags = navigationTags
|
||||
self.posts = posts
|
||||
self.pages = pages
|
||||
self.german = german
|
||||
self.english = english
|
||||
}
|
||||
|
||||
func localized(in language: ContentLanguage) -> LocalizedSettings {
|
||||
func localized(in language: ContentLanguage) -> LocalizedPostSettings {
|
||||
switch language {
|
||||
case .english: return english
|
||||
case .german: return german
|
||||
}
|
||||
}
|
||||
|
||||
var outputDirectory: URL {
|
||||
URL(fileURLWithPath: paths.outputDirectoryPath)
|
||||
}
|
||||
}
|
||||
|
@ -103,4 +103,14 @@ enum FileType {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var isSvg: Bool {
|
||||
guard case .image(let imageFileType) = self else {
|
||||
return false
|
||||
}
|
||||
guard case .svg = imageFileType else {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user