Generate first tag pages

This commit is contained in:
Christoph Hagen
2024-12-09 17:47:03 +01:00
parent 4f08526978
commit 8183bc4903
35 changed files with 719 additions and 1105 deletions

View File

@ -1,5 +1,10 @@
extension Content {
#warning("Get tag url prefix from settings")
func tagLink(_ tag: Tag, language: ContentLanguage) -> String {
"/tags/\(tag.localized(in: language).urlComponent).html"
}
func pageLink(_ page: Page, language: ContentLanguage) -> String {
// TODO: Record link to trace connections between pages
var prefix = settings.pages.pageUrlPrefix

View File

@ -1,63 +0,0 @@
import Foundation
extension Content {
func importOldContent() {
let importer = Importer()
do {
try importer.importContent()
} catch {
print(error)
return
}
for (_, file) in importer.files.sorted(by: { $0.key < $1.key }) {
storage.copyFile(at: file.url, fileId: file.name)
// TODO: Store alt text for image and videos
}
var missingPages: [String] = []
for (pageId, page) in importer.pages.sorted(by: { $0.key < $1.key }) {
storage.save(pageMetadata: page.page, for: pageId)
if FileManager.default.fileExists(atPath: page.deContentUrl.path()) {
storage.copyPageContent(from: page.deContentUrl, for: pageId, language: .german)
} else {
missingPages.append(pageId + " (DE)")
}
if FileManager.default.fileExists(atPath: page.enContentUrl.path()) {
storage.copyPageContent(from: page.enContentUrl, for: pageId, language: .english)
} else {
missingPages.append(pageId + " (EN)")
}
}
for (tagId, tag) in importer.tags {
storage.save(tagMetadata: tag, for: tagId)
}
for (postId, post) in importer.posts {
storage.save(post: post, for: postId)
}
let ignoredFiles = importer.ignoredFiles
.map { $0.path() }
.sorted()
print("Ignored files:")
for file in ignoredFiles {
print(file)
}
print("Missing pages:")
for page in missingPages {
print(page)
}
do {
try loadFromDisk()
} catch {
print("Failed to load from disk: \(error)")
}
}
}

View File

@ -49,7 +49,7 @@ extension Content {
let storage = Storage(baseFolder: URL(filePath: contentPath))
let settings = try storage.loadSettings()
let imageDescriptions = storage.loadFileDescriptions().reduce(into: [:]) { descriptions, description in
let imageDescriptions = try storage.loadFileDescriptions().reduce(into: [:]) { descriptions, description in
descriptions[description.fileId] = description
}
@ -84,6 +84,7 @@ extension Content {
let english = convert(post.english, images: images)
return Post(
content: self,
id: postId,
isDraft: post.isDraft,
createdDate: post.createdDate,
@ -129,6 +130,7 @@ extension Content {
pagesData.reduce(into: [:]) { pages, data in
let (pageId, page) = data
pages[pageId] = Page(
content: self,
id: pageId,
isDraft: page.isDraft,
createdDate: page.createdDate,

View File

@ -2,20 +2,20 @@ import Foundation
extension Content {
func saveToDisk() {
func saveToDisk() throws {
//print("Starting save")
for page in pages {
storage.save(pageMetadata: page.pageFile, for: page.id)
try storage.save(pageMetadata: page.pageFile, for: page.id)
}
for post in posts {
storage.save(post: post.postFile, for: post.id)
try storage.save(post: post.postFile, for: post.id)
}
for tag in tags {
storage.save(tagMetadata: tag.tagFile, for: tag.id)
try storage.save(tagMetadata: tag.tagFile, for: tag.id)
}
storage.save(settings: settings.file)
try storage.save(settings: settings.file)
let fileDescriptions: [FileDescriptions] = files.sorted().compactMap { file in
guard !file.englishDescription.isEmpty || !file.germanDescription.isEmpty else {
@ -27,22 +27,19 @@ extension Content {
english: file.englishDescription.nonEmpty)
}
storage.save(fileDescriptions: fileDescriptions)
try storage.save(fileDescriptions: fileDescriptions)
do {
try storage.deletePostFiles(notIn: posts.map { $0.id })
try storage.deletePageFiles(notIn: pages.map { $0.id })
try storage.deleteTagFiles(notIn: tags.map { $0.id })
try storage.deleteFiles(notIn: files.map { $0.id })
try storage.deleteFileResources(notIn: files.map { $0.id })
} catch {
print("Failed to remove unused files: \(error)")
}
// TODO: Remove all files that are no longer in use (they belong to deleted items)
//print("Finished save")
}
}
private extension Page {
var pageFile: PageFile {

View File

@ -36,13 +36,3 @@ final class LocalizedTag: ObservableObject {
self.originalUrl = originalUrl
}
}
extension LocalizedTag {
func data() -> FeedEntryData.Tag {
.init(
name: name,
url: "tags/\(urlComponent).html"
)
}
}

View File

@ -1,7 +1,9 @@
import Foundation
final class Page: ObservableObject {
unowned let content: Content
/**
The unique id of the entry
*/
@ -40,7 +42,8 @@ final class Page: ObservableObject {
@Published
var images: Set<String> = []
init(id: String,
init(content: Content,
id: String,
isDraft: Bool,
createdDate: Date,
startDate: Date,
@ -48,6 +51,7 @@ final class Page: ObservableObject {
german: LocalizedPage,
english: LocalizedPage,
tags: [Tag]) {
self.content = content
self.id = id
self.isDraft = isDraft
self.createdDate = createdDate
@ -65,6 +69,15 @@ final class Page: ObservableObject {
case .english: return english
}
}
func update(id newId: String) -> Bool {
guard content.storage.move(page: id, to: newId) else {
print("Failed to move file of page \(id)")
return false
}
id = newId
return true
}
}
extension Page: Identifiable {

View File

@ -2,6 +2,8 @@ import Foundation
final class Post: ObservableObject {
unowned let content: Content
@Published
var id: String
@ -33,7 +35,8 @@ final class Post: ObservableObject {
@Published
var linkedPage: Page?
init(id: String,
init(content: Content,
id: String,
isDraft: Bool,
createdDate: Date,
startDate: Date,
@ -42,6 +45,7 @@ final class Post: ObservableObject {
german: LocalizedPost,
english: LocalizedPost,
linkedPage: Page? = nil) {
self.content = content
self.id = id
self.isDraft = isDraft
self.createdDate = createdDate
@ -60,6 +64,17 @@ final class Post: ObservableObject {
case .german: return german
}
}
func update(id newId: String) -> Bool {
do {
try content.storage.move(post: id, to: newId)
} catch {
print("Failed to move file of post \(id)")
return false
}
id = newId
return true
}
}
extension Post: Identifiable {

View File

@ -37,18 +37,6 @@ final class Tag: ObservableObject {
}
}
extension Tag {
func data(in language: ContentLanguage) -> FeedEntryData.Tag {
switch language {
case .english:
return english.data()
case .german:
return german.data()
}
}
}
extension Tag: Identifiable {
}