diff --git a/Sources/Generator/Generators/MarkdownProcessor.swift b/Sources/Generator/Generators/PageContentGenerator.swift similarity index 99% rename from Sources/Generator/Generators/MarkdownProcessor.swift rename to Sources/Generator/Generators/PageContentGenerator.swift index 3711a1e..c56dc46 100644 --- a/Sources/Generator/Generators/MarkdownProcessor.swift +++ b/Sources/Generator/Generators/PageContentGenerator.swift @@ -8,8 +8,11 @@ struct PageContentGenerator { private let swift = SyntaxHighlighter(format: HTMLOutputFormat()) - init(factory: TemplateFactory) { + private let siteRoot: Element + + init(factory: TemplateFactory, siteRoot: Element) { self.factory = factory + self.siteRoot = siteRoot } func generate(page: Element, language: String, content: String) -> (content: String, includesCode: Bool) { diff --git a/Sources/Generator/Generators/PageGenerator.swift b/Sources/Generator/Generators/PageGenerator.swift index 0ff1720..f01fff1 100644 --- a/Sources/Generator/Generators/PageGenerator.swift +++ b/Sources/Generator/Generators/PageGenerator.swift @@ -5,8 +5,11 @@ struct PageGenerator { private let factory: LocalizedSiteTemplate - init(factory: LocalizedSiteTemplate) { + private let contentGenerator: PageContentGenerator + + init(factory: LocalizedSiteTemplate, siteRoot: Element) { self.factory = factory + self.contentGenerator = PageContentGenerator(factory: factory.factory, siteRoot: siteRoot) } func generate(page: Element, language: String, previousPage: Element?, nextPage: Element?) { @@ -74,12 +77,10 @@ struct PageGenerator { private func makeContent(page: Element, metadata: Element.LocalizedMetadata, language: String, path: String) -> (content: String, includesCode: Bool, isEmpty: Bool) { 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) + let (content, includesCode) = contentGenerator.generate(page: page, language: language, content: raw) return (content, includesCode, false) } else { - let (content, includesCode) = PageContentGenerator(factory: factory.factory) - .generate(page: page, language: language, content: metadata.placeholderText) + let (content, includesCode) = contentGenerator.generate(page: page, language: language, content: metadata.placeholderText) let placeholder = factory.factory.makePlaceholder(title: metadata.placeholderTitle, text: content) return (placeholder, includesCode, true) } diff --git a/Sources/Generator/Generators/SiteGenerator.swift b/Sources/Generator/Generators/SiteGenerator.swift index baabd1f..7f81271 100644 --- a/Sources/Generator/Generators/SiteGenerator.swift +++ b/Sources/Generator/Generators/SiteGenerator.swift @@ -26,7 +26,7 @@ struct SiteGenerator { // Generate sections let overviewGenerator = OverviewPageGenerator(factory: template) - let pageGenerator = PageGenerator(factory: template) + let pageGenerator = PageGenerator(factory: template, siteRoot: site) var elementsToProcess: [LinkedElement] = [(nil, site, nil)] while let (previous, element, next) = elementsToProcess.popLast() { diff --git a/Sources/Generator/run.swift b/Sources/Generator/run.swift index f232fd9..d6d9d49 100644 --- a/Sources/Generator/run.swift +++ b/Sources/Generator/run.swift @@ -3,7 +3,6 @@ import ArgumentParser let log = ValidationLog() var files: FileSystem! -var siteRoot: Element! @main struct CHGenerator: ParsableCommand { @@ -57,8 +56,7 @@ private func generate(configPath: String) throws { // 2. Scan site elements - siteRoot = try loadSiteData(in: configuration.contentDirectory) - guard siteRoot != nil else { + guard let siteRoot = try loadSiteData(in: configuration.contentDirectory) else { return } let siteGenerator = try SiteGenerator()