75 lines
2.0 KiB
Swift
75 lines
2.0 KiB
Swift
|
import Foundation
|
||
|
|
||
|
struct Section {
|
||
|
|
||
|
let metadata: Metadata
|
||
|
|
||
|
let inputFolder: URL
|
||
|
|
||
|
let elements: [SiteElement]
|
||
|
|
||
|
/// The path to get to the section from the root folder (no leading slash)
|
||
|
let path: String
|
||
|
|
||
|
var folderName: String {
|
||
|
inputFolder.lastPathComponent
|
||
|
}
|
||
|
|
||
|
var sortedItems: [SiteElement] {
|
||
|
guard metadata.sortByMostRecent else {
|
||
|
return elements.sorted { $0.sortIndex! < $1.sortIndex! }
|
||
|
}
|
||
|
return elements.sorted { $0.sortDate! > $1.sortDate! }
|
||
|
}
|
||
|
|
||
|
init?(folder: URL, path: String) throws {
|
||
|
self.path = path
|
||
|
guard let metadata = try Metadata(in: folder) else {
|
||
|
return nil
|
||
|
}
|
||
|
self.metadata = metadata
|
||
|
self.inputFolder = folder
|
||
|
let elements: [SiteElement] = try FileSystem.folders(in: folder)
|
||
|
.compactMap {
|
||
|
let sectionPath = "\(path)/\($0.lastPathComponent)"
|
||
|
if Page.Metadata.exists(in: $0) {
|
||
|
return try Page(folder: $0, path: sectionPath)
|
||
|
}
|
||
|
if Section.Metadata.exists(in: $0) {
|
||
|
return try Section(folder: $0, path: sectionPath)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
if metadata.sortByMostRecent {
|
||
|
self.elements = elements.sorted { $0.sortDate! > $1.sortDate! }
|
||
|
} else {
|
||
|
self.elements = elements.sorted { $0.sortIndex! < $1.sortIndex! }
|
||
|
}
|
||
|
#warning("Verify that all sort indices or sort dates are present")
|
||
|
print("Section \(folderName): \(elements.count) pages")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
extension Section: SiteElement {
|
||
|
|
||
|
var sortIndex: Int? {
|
||
|
metadata.sortIndex
|
||
|
}
|
||
|
|
||
|
var sortDate: Date? {
|
||
|
metadata.date
|
||
|
}
|
||
|
|
||
|
func cornerText(for language: String) -> String? {
|
||
|
localized(for: language).cornerText
|
||
|
}
|
||
|
|
||
|
func backLinkText(for language: String) -> String? {
|
||
|
localized(for: language).backLinkText
|
||
|
}
|
||
|
}
|
||
|
|
||
|
extension Section: LocalizedMetadataContainer {
|
||
|
|
||
|
}
|