diff --git a/WebsiteGenerator/Content/Element.swift b/WebsiteGenerator/Content/Element.swift index a179ff0..e9f8880 100644 --- a/WebsiteGenerator/Content/Element.swift +++ b/WebsiteGenerator/Content/Element.swift @@ -372,7 +372,10 @@ extension Element { } static func rootPaths(for input: Set?, path: String) -> Set { - input.unwrapped { Set($0.map { relativeToRoot(filePath: $0, folder: path) }) } ?? [] + guard let input = input else { + return [] + } + return Set(input.map { relativeToRoot(filePath: $0, folder: path) }) } func relativePathToFileWithPath(_ filePath: String) -> String { diff --git a/WebsiteGenerator/Files/FileSystem.swift b/WebsiteGenerator/Files/FileSystem.swift index 997813b..da38c6c 100644 --- a/WebsiteGenerator/Files/FileSystem.swift +++ b/WebsiteGenerator/Files/FileSystem.swift @@ -386,7 +386,7 @@ final class FileSystem { let sourceUrl = input.appendingPathComponent(cleanPath) let destinationUrl = output.appendingPathComponent(cleanPath) guard sourceUrl.exists else { - if !externalFiles.contains(file) { + if !isExternal(file: file) { log.add(error: "Missing required file", source: cleanPath) } continue @@ -403,7 +403,7 @@ final class FileSystem { } } for (file, source) in expectedFiles { - guard !externalFiles.contains(file) else { + guard !isExternal(file: file) else { continue } let cleanPath = cleanRelativeURL(file) @@ -438,6 +438,42 @@ final class FileSystem { return result.joined(separator: "/") } + /** + Check if a file is marked as external. + + Also checks for sub-paths of the file, e.g if the folder `docs` is marked as external, + then files like `docs/index.html` are also found to be external. + - Note: All paths are either relative to root (no leading slash) or absolute paths of the domain (leading slash) + */ + func isExternal(file: String) -> Bool { + // Deconstruct file path + var path = "" + for part in file.components(separatedBy: "/") { + guard part != "" else { + continue + } + if path == "" { + path = part + } else { + path += "/" + part + } + if externalFiles.contains(path) { + return true + } + } + return false + } + + func printExternalFiles() { + guard !externalFiles.isEmpty else { + return + } + print("\(externalFiles.count) external files needed:") + for file in externalFiles.sorted() { + print(" " + file) + } + } + // MARK: Pages func isEmpty(page: String) {