ChWebsiteApp/CHDataManagement/Model/Content+Generation.swift
2024-12-13 11:26:34 +01:00

61 lines
1.9 KiB
Swift

extension Content {
private func makeCleanAbsolutePath(_ path: String) -> String {
("/" + path).replacingOccurrences(of: "//", with: "/")
}
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
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 image(_ imageId: String) -> FileResource? {
files.first { $0.id == imageId && $0.type.isImage }
}
func video(_ videoId: String) -> FileResource? {
files.first { $0.id == videoId && $0.type.isVideo }
}
func file(id: String) -> FileResource? {
files.first { $0.id == id }
}
func tag(_ tagId: String) -> Tag? {
tags.first { $0.id == tagId }
}
}