diff --git a/Sources/Generator/Files/Configuration.swift b/Sources/Generator/Files/Configuration.swift index 73214eb..d481735 100644 --- a/Sources/Generator/Files/Configuration.swift +++ b/Sources/Generator/Files/Configuration.swift @@ -56,4 +56,13 @@ struct Configuration: Codable { var outputDirectory: URL { .init(fileURLWithPath: outputPath) } + + func printOverview() { + print(" Source folder: \(contentDirectory.path)") + print(" Output folder: \(outputDirectory.path)") + print(" Page width: \(pageImageWidth)") + print(" Minify JavaScript: \(minifyCSSandJS)") + print(" Minify CSS: \(minifyCSSandJS)") + print(" Create markdown files: \(createMdFilesIfMissing)") + } } diff --git a/Sources/Generator/Files/FileSystem.swift b/Sources/Generator/Files/FileSystem.swift index 2cfa5fb..6bf0ce8 100644 --- a/Sources/Generator/Files/FileSystem.swift +++ b/Sources/Generator/Files/FileSystem.swift @@ -14,6 +14,8 @@ final class FileSystem { private let images: ImageGenerator + private let configuration: Configuration + private var tempFile: URL { input.appendingPathComponent(FileSystem.tempFileName) } @@ -68,11 +70,12 @@ final class FileSystem { */ private var generatedPages: Set = [] - init(in input: URL, to output: URL) { + init(in input: URL, to output: URL, configuration: Configuration) { self.input = input self.output = output self.images = .init(input: input, output: output) self.generatorInfoFolder = input.appendingPathComponent("run") + self.configuration = configuration } func urlInOutputFolder(_ path: String) -> URL { @@ -102,6 +105,10 @@ final class FileSystem { } } + func contentOfMdFile(atPath path: String, source: String) -> String? { + contentOfOptionalFile(atPath: path, source: source, createEmptyFileIfMissing: configuration.createMdFilesIfMissing) + } + func contentOfOptionalFile(atPath path: String, source: String, createEmptyFileIfMissing: Bool = false) -> String? { let url = input.appendingPathComponent(path) guard exists(url) else { @@ -148,6 +155,10 @@ final class FileSystem { images.requireMultiVersionImage(source: source, destination: destination, requiredBy: path, width: width, desiredHeight: desiredHeight) } + func requireFullSizeMultiVersionImage(source: String, destination: String, requiredBy path: String) -> NSSize { + images.requireMultiVersionImage(source: source, destination: destination, requiredBy: path, width: configuration.pageImageWidth, desiredHeight: nil) + } + func createImages() { images.createImages() } diff --git a/Sources/Generator/Generators/MarkdownProcessor.swift b/Sources/Generator/Generators/MarkdownProcessor.swift index e329dee..3711a1e 100644 --- a/Sources/Generator/Generators/MarkdownProcessor.swift +++ b/Sources/Generator/Generators/MarkdownProcessor.swift @@ -120,11 +120,10 @@ struct PageContentGenerator { private func handleImage(page: Element, file: String, rightTitle: String?, leftTitle: String?) -> String { let imagePath = page.pathRelativeToRootForContainedInputFile(file) - let size = files.requireMultiVersionImage( + let size = files.requireFullSizeMultiVersionImage( source: imagePath, destination: imagePath, - requiredBy: page.path, - width: configuration.pageImageWidth) + requiredBy: page.path) let content: [PageImageTemplate.Key : String] = [ .image: file.dropAfterLast("."), diff --git a/Sources/Generator/Generators/PageGenerator.swift b/Sources/Generator/Generators/PageGenerator.swift index 48278f5..0ff1720 100644 --- a/Sources/Generator/Generators/PageGenerator.swift +++ b/Sources/Generator/Generators/PageGenerator.swift @@ -73,9 +73,7 @@ struct PageGenerator { } private func makeContent(page: Element, metadata: Element.LocalizedMetadata, language: String, path: String) -> (content: String, includesCode: Bool, isEmpty: Bool) { - let create = configuration.createMdFilesIfMissing - if let raw = files.contentOfOptionalFile(atPath: path, source: page.path, createEmptyFileIfMissing: create)? - .trimmed.nonEmpty { + if let raw = files.contentOfMdFile(atPath: path, source: page.path)?.trimmed.nonEmpty { let (content, includesCode) = PageContentGenerator(factory: factory.factory) .generate(page: page, language: language, content: raw) return (content, includesCode, false) diff --git a/Sources/Generator/run.swift b/Sources/Generator/run.swift index ebf19e1..f232fd9 100644 --- a/Sources/Generator/run.swift +++ b/Sources/Generator/run.swift @@ -1,7 +1,6 @@ import Foundation import ArgumentParser -var configuration: Configuration! let log = ValidationLog() var files: FileSystem! var siteRoot: Element! @@ -28,14 +27,34 @@ private func loadSiteData(in folder: URL) throws -> Element? { return root } -private func generate(configPath: String) throws { +private func loadConfiguration(at configPath: String) -> Configuration? { + print("--- CONFIGURATION ----------------------------------") + print("") + print(" Configuration file: \(configPath)") let configUrl = URL(fileURLWithPath: configPath) - let data = try Data(contentsOf: configUrl) - configuration = try JSONDecoder().decode(from: data) + let config: Configuration + do { + let data = try Data(contentsOf: configUrl) + config = try JSONDecoder().decode(from: data) + } catch { + print(" Configuration error: \(error)") + return nil + } + config.printOverview() + print(" ") + return config +} + +private func generate(configPath: String) throws { + guard let configuration = loadConfiguration(at: configPath) else { + return + } files = .init( in: configuration.contentDirectory, - to: configuration.outputDirectory) + to: configuration.outputDirectory, + configuration: configuration) + // 2. Scan site elements siteRoot = try loadSiteData(in: configuration.contentDirectory)