Compare commits
No commits in common. "deb7e6187e2269c6a44c78488e85c98f116314d7" and "a8b328efcead27928f9e9075462170a04c2aa2ce" have entirely different histories.
deb7e6187e
...
a8b328efce
@ -289,23 +289,16 @@ struct Element {
|
||||
self.readElements(in: folder, source: path, log: log)
|
||||
}
|
||||
|
||||
func getExternalPageMap(language: String) -> [String : String] {
|
||||
var result = [String : String]()
|
||||
if let ext = getExternalLink(for: language) {
|
||||
result[id] = ext
|
||||
} else {
|
||||
result[id] = path + Element.htmlPagePathAddition(for: language)
|
||||
}
|
||||
elements.forEach { element in
|
||||
element.getExternalPageMap(language: language).forEach { key, value in
|
||||
result[key] = value
|
||||
func getContainedIds(log: MetadataInfoLogger) -> [String : String] {
|
||||
elements.reduce(into: [id : path]) { dict, element in
|
||||
element.getContainedIds(log: log).forEach { id, path in
|
||||
if let existing = dict[id] {
|
||||
log.error("Conflicting id with \(existing)", source: path)
|
||||
} else {
|
||||
dict[id] = path
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private func getExternalLink(for language: String) -> String? {
|
||||
languages.first { $0.language == language }?.externalUrl
|
||||
}
|
||||
}
|
||||
|
||||
@ -388,9 +381,6 @@ extension Element {
|
||||
- Returns: The relative url from a localized page of the element to the target file.
|
||||
*/
|
||||
func relativePathToOtherSiteElement(file: String) -> String {
|
||||
guard !file.hasPrefix("/") else {
|
||||
return file
|
||||
}
|
||||
// Note: The element `path` is missing the last component
|
||||
// i.e. travel/alps instead of travel/alps/en.html
|
||||
let ownParts = path.components(separatedBy: "/")
|
||||
|
@ -47,12 +47,13 @@ struct PageContentGenerator {
|
||||
let file = markdown.between("(", and: ")")
|
||||
if file.hasPrefix("page:") {
|
||||
let pageId = file.replacingOccurrences(of: "page:", with: "")
|
||||
guard let pagePath = results.getPagePath(for: pageId, source: page.path, language: language) else {
|
||||
guard let pagePath = results.getPagePath(for: pageId, source: page.path) else {
|
||||
// Remove link since the page can't be found
|
||||
return markdown.between("[", and: "]")
|
||||
}
|
||||
let fullPath = pagePath + Element.htmlPagePathAddition(for: language)
|
||||
// Adjust file path to get the page url
|
||||
let url = page.relativePathToOtherSiteElement(file: pagePath)
|
||||
let url = page.relativePathToOtherSiteElement(file: fullPath)
|
||||
return html.replacingOccurrences(of: file, with: url)
|
||||
}
|
||||
|
||||
@ -243,7 +244,7 @@ struct PageContentGenerator {
|
||||
private func handlePageLink(page: Element, language: String, pageId: String) -> String {
|
||||
guard let linkedPage = siteRoot.find(pageId) else {
|
||||
// Checking the page path will add it to the missing pages
|
||||
_ = results.getPagePath(for: pageId, source: page.path, language: language)
|
||||
_ = results.getPagePath(for: pageId, source: page.path)
|
||||
// Remove link since the page can't be found
|
||||
return ""
|
||||
}
|
||||
|
@ -1,75 +0,0 @@
|
||||
import Foundation
|
||||
|
||||
func checkDependencies() -> Bool {
|
||||
print("--- DEPENDENCIES -----------------------------------")
|
||||
print(" ")
|
||||
defer { print(" ") }
|
||||
return checkImageOptimAvailability() && checkMagickAvailability() && checkCwebpAvailability() && checkAvifAvailability()
|
||||
}
|
||||
|
||||
private func checkImageOptimAvailability() -> Bool {
|
||||
do {
|
||||
let output = try safeShell("imageoptim --version")
|
||||
let version = output.components(separatedBy: ".").compactMap { Int($0.trimmed) }
|
||||
if version.count > 1 {
|
||||
print(" ImageOptim: \(version.map { "\($0)" }.joined(separator: "."))")
|
||||
} else {
|
||||
print(" ImageOptim: Not found")
|
||||
return false
|
||||
}
|
||||
} catch {
|
||||
print(" ImageOptim: Failed to get version (\(error))")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private func checkMagickAvailability() -> Bool {
|
||||
do {
|
||||
let output = try safeShell("magick --version")
|
||||
guard let version = output.components(separatedBy: "ImageMagick ").dropFirst().first?
|
||||
.components(separatedBy: " ").first else {
|
||||
print(" Magick: Not found")
|
||||
return false
|
||||
}
|
||||
print(" Magick: \(version)")
|
||||
} catch {
|
||||
print(" Magick: Failed to get version (\(error))")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private func checkCwebpAvailability() -> Bool {
|
||||
do {
|
||||
let output = try safeShell("cwebp -version")
|
||||
let version = output.components(separatedBy: ".").compactMap { Int($0.trimmed) }
|
||||
if version.count > 1 {
|
||||
print(" cwebp: \(version.map { "\($0)" }.joined(separator: "."))")
|
||||
} else {
|
||||
print(" cwebp: Not found")
|
||||
return false
|
||||
}
|
||||
} catch {
|
||||
print(" cwebp: Failed to get version (\(error))")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private func checkAvifAvailability() -> Bool {
|
||||
do {
|
||||
let output = try safeShell("npx avif --version")
|
||||
let version = output.components(separatedBy: ".").compactMap { Int($0.trimmed) }
|
||||
if version.count > 1 {
|
||||
print(" avif: \(version.map { "\($0)" }.joined(separator: "."))")
|
||||
} else {
|
||||
print(" avif: Not found")
|
||||
return false
|
||||
}
|
||||
} catch {
|
||||
print(" avif: Failed to get version (\(error))")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
@ -8,8 +8,6 @@ enum MinificationType {
|
||||
case css
|
||||
}
|
||||
|
||||
typealias PageMap = [(language: String, pages: [String : String])]
|
||||
|
||||
final class GenerationResultsHandler {
|
||||
|
||||
/// The content folder where the input data is stored
|
||||
@ -25,7 +23,7 @@ final class GenerationResultsHandler {
|
||||
|
||||
This relation is used to generate relative links to pages using the ``Element.id`
|
||||
*/
|
||||
private let pageMap: PageMap
|
||||
private let pagePaths: [String: String]
|
||||
|
||||
private let configuration: Configuration
|
||||
|
||||
@ -33,11 +31,11 @@ final class GenerationResultsHandler {
|
||||
|
||||
private let numberOfTotalPages: Int
|
||||
|
||||
init(in input: URL, to output: URL, configuration: Configuration, fileUpdates: FileUpdateChecker, pageMap: PageMap, pageCount: Int) {
|
||||
init(in input: URL, to output: URL, configuration: Configuration, fileUpdates: FileUpdateChecker, pagePaths: [String: String], pageCount: Int) {
|
||||
self.contentFolder = input
|
||||
self.fileUpdates = fileUpdates
|
||||
self.outputFolder = output
|
||||
self.pageMap = pageMap
|
||||
self.pagePaths = pagePaths
|
||||
self.configuration = configuration
|
||||
self.numberOfTotalPages = pageCount
|
||||
}
|
||||
@ -88,8 +86,8 @@ final class GenerationResultsHandler {
|
||||
|
||||
// MARK: Page data
|
||||
|
||||
func getPagePath(for id: String, source: String, language: String) -> String? {
|
||||
guard let pagePath = pageMap.first(where: { $0.language == language})?.pages[id] else {
|
||||
func getPagePath(for id: String, source: String) -> String? {
|
||||
guard let pagePath = pagePaths[id] else {
|
||||
missingLinkedPages[id] = source
|
||||
return nil
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ final class ImageGenerator {
|
||||
|
||||
private let numberOfTotalImages: Int
|
||||
|
||||
private lazy var numberOfImagesToCreate = jobs.reduce(0) { $0 + $1.images.count } + multiJobs.count * 2
|
||||
private lazy var numberImagesToCreate = jobs.reduce(0) { $0 + $1.images.count } + multiJobs.count * 2
|
||||
|
||||
private var numberOfImagesToOptimize = 0
|
||||
|
||||
@ -100,7 +100,7 @@ final class ImageGenerator {
|
||||
for (baseImage, source) in multiJobs {
|
||||
createMultiImages(from: baseImage, path: source)
|
||||
}
|
||||
print(" Generated images: \(numberOfGeneratedImages)/\(numberOfImagesToCreate)")
|
||||
print(" Generated images: \(numberOfGeneratedImages)/\(numberImagesToCreate)")
|
||||
optimizeImages()
|
||||
print(" Optimized images: \(numberOfOptimizedImages)/\(numberOfImagesToOptimize)")
|
||||
|
||||
@ -219,18 +219,14 @@ final class ImageGenerator {
|
||||
generatedImages.insert(webpPath)
|
||||
didGenerateImage()
|
||||
|
||||
compress(at: sourcePath)
|
||||
compress(at: source)
|
||||
}
|
||||
|
||||
private func createAVIF(at destination: String, from source: String, quality: Int = 55, effort: Int = 5) {
|
||||
let folder = destination.dropAfterLast("/")
|
||||
let command = "npx avif --input=\(source) --quality=\(quality) --effort=\(effort) --output=\(folder) --overwrite"
|
||||
do {
|
||||
let output = try safeShell(command)
|
||||
if output == "" {
|
||||
return
|
||||
}
|
||||
markImageAsFailed(destination, error: "Failed to create AVIF image: \(output)")
|
||||
_ = try safeShell(command)
|
||||
} catch {
|
||||
markImageAsFailed(destination, error: "Failed to create AVIF image")
|
||||
}
|
||||
@ -239,26 +235,18 @@ final class ImageGenerator {
|
||||
private func createWEBP(at destination: String, from source: String, quality: Int = 75) {
|
||||
let command = "cwebp \(source) -q \(quality) -o \(destination)"
|
||||
do {
|
||||
let output = try safeShell(command)
|
||||
if !output.contains("Error") {
|
||||
return
|
||||
}
|
||||
markImageAsFailed(destination, error: "Failed to create WEBP image: \(output)")
|
||||
_ = try safeShell(command)
|
||||
} catch {
|
||||
markImageAsFailed(destination, error: "Failed to create WEBP image: \(error)")
|
||||
markImageAsFailed(destination, error: "Failed to create WEBP image")
|
||||
}
|
||||
}
|
||||
|
||||
private func compress(at destination: String, quality: Int = 70) {
|
||||
let command = "magick convert \(destination) -quality \(quality)% \(destination)"
|
||||
do {
|
||||
let output = try safeShell(command)
|
||||
if output == "" {
|
||||
return
|
||||
}
|
||||
markImageAsFailed(destination, error: "Failed to compress image: \(output)")
|
||||
_ = try safeShell(command)
|
||||
} catch {
|
||||
markImageAsFailed(destination, error: "Failed to compress image: \(error)")
|
||||
markImageAsFailed(destination, error: "Failed to compress image")
|
||||
}
|
||||
}
|
||||
|
||||
@ -266,9 +254,8 @@ final class ImageGenerator {
|
||||
let all = generatedImages
|
||||
.filter { imageOptimSupportedFileExtensions.contains($0.lastComponentAfter(".")) }
|
||||
.map { output.appendingPathComponent($0).path }
|
||||
numberOfImagesToOptimize = all.count
|
||||
for i in stride(from: 0, to: numberOfImagesToOptimize, by: imageOptimizationBatchSize) {
|
||||
let endIndex = min(i+imageOptimizationBatchSize, numberOfImagesToOptimize)
|
||||
for i in stride(from: 0, to: all.count, by: imageOptimizationBatchSize) {
|
||||
let endIndex = min(i+imageOptimizationBatchSize, all.count)
|
||||
let batch = all[i..<endIndex]
|
||||
if optimizeImageBatch(batch) {
|
||||
optimizedImages.formUnion(batch)
|
||||
@ -280,18 +267,11 @@ final class ImageGenerator {
|
||||
private func optimizeImageBatch(_ batch: ArraySlice<String>) -> Bool {
|
||||
let command = "imageoptim " + batch.joined(separator: " ")
|
||||
do {
|
||||
let output = try safeShell(command)
|
||||
if output.contains("Finished") {
|
||||
return true
|
||||
}
|
||||
|
||||
for image in batch {
|
||||
markImageAsFailed(image, error: "Failed to optimize image: \(output)")
|
||||
}
|
||||
_ = try safeShell(command)
|
||||
return true
|
||||
} catch {
|
||||
for image in batch {
|
||||
markImageAsFailed(image, error: "Failed to optimize image: \(error)")
|
||||
markImageAsFailed(image, error: "Failed to optimize image")
|
||||
}
|
||||
return false
|
||||
}
|
||||
@ -301,7 +281,7 @@ final class ImageGenerator {
|
||||
|
||||
private func didGenerateImage(count: Int = 1) {
|
||||
numberOfGeneratedImages += count
|
||||
print(" Generated images: \(numberOfGeneratedImages)/\(numberOfImagesToCreate) \r", terminator: "")
|
||||
print(" Generated images: \(numberOfGeneratedImages)/\(numberImagesToCreate) \r", terminator: "")
|
||||
fflush(stdout)
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,7 @@ struct CHGenerator: ParsableCommand {
|
||||
}
|
||||
|
||||
private func loadConfiguration(at configPath: String) -> Configuration? {
|
||||
print(" ")
|
||||
print("--- CONFIGURATION ----------------------------------")
|
||||
print(" ")
|
||||
print(" Configuration file: \(configPath)")
|
||||
@ -31,7 +32,7 @@ private func loadConfiguration(at configPath: String) -> Configuration? {
|
||||
return config
|
||||
}
|
||||
|
||||
private func loadSiteData(in folder: URL, runFolder: URL) throws -> (root: Element, pageMap: PageMap)? {
|
||||
private func loadSiteData(in folder: URL, runFolder: URL) throws -> (root: Element, ids: [String : String])? {
|
||||
print("--- SOURCE FILES -----------------------------------")
|
||||
print(" ")
|
||||
|
||||
@ -48,22 +49,22 @@ private func loadSiteData(in folder: URL, runFolder: URL) throws -> (root: Eleme
|
||||
print(" Error: No site root loaded, aborting generation")
|
||||
return nil
|
||||
}
|
||||
let pageMap = root.languages.map { (language: $0.language, pages: root.getExternalPageMap(language: $0.language)) }
|
||||
let ids = root.getContainedIds(log: log)
|
||||
log.printMetadataScanOverview(languages: root.languages.count)
|
||||
return (root, pageMap)
|
||||
return (root, ids)
|
||||
}
|
||||
|
||||
private func generatePages(from root: Element, configuration: Configuration, fileUpdates: FileUpdateChecker, pageMap: PageMap, runFolder: URL) -> (ImageData, FileData)? {
|
||||
private func generatePages(from root: Element, configuration: Configuration, fileUpdates: FileUpdateChecker, ids: [String: String], runFolder: URL) -> (ImageData, FileData)? {
|
||||
print("--- GENERATION -------------------------------------")
|
||||
print(" ")
|
||||
|
||||
let pageCount = pageMap.reduce(0) { $0 + $1.pages.count }
|
||||
let pageCount = ids.count * root.languages.count
|
||||
let results = GenerationResultsHandler(
|
||||
in: configuration.contentDirectory,
|
||||
to: configuration.outputDirectory,
|
||||
configuration: configuration,
|
||||
fileUpdates: fileUpdates,
|
||||
pageMap: pageMap,
|
||||
pagePaths: ids,
|
||||
pageCount: pageCount)
|
||||
|
||||
defer { results.printOverview() }
|
||||
@ -110,8 +111,8 @@ private func copyFiles(files: FileData, configuration: Configuration, runFolder:
|
||||
generator.generate()
|
||||
}
|
||||
|
||||
private func finish(start: Date, complete: Bool) {
|
||||
print("--- SUMMARY ----------------------------------------")
|
||||
private func finish(start: Date) {
|
||||
print("----------------------------------------------------")
|
||||
print(" ")
|
||||
let duration = Int(-start.timeIntervalSinceNow.rounded())
|
||||
if duration < 60 {
|
||||
@ -121,25 +122,11 @@ private func finish(start: Date, complete: Bool) {
|
||||
} else {
|
||||
print(String(format: " Duration: %d:%02d:%02d", duration / 3600, (duration / 60) % 60, duration % 60))
|
||||
}
|
||||
print(" Complete: \(complete ? "Yes" : "No")")
|
||||
print(" ")
|
||||
print("----------------------------------------------------")
|
||||
print("")
|
||||
}
|
||||
|
||||
private func generate(configPath: String) throws {
|
||||
let start = Date()
|
||||
var complete = false
|
||||
|
||||
defer {
|
||||
// 6. Print summary
|
||||
finish(start: start, complete: complete)
|
||||
}
|
||||
|
||||
print(" ")
|
||||
|
||||
guard checkDependencies() else {
|
||||
return
|
||||
}
|
||||
|
||||
// 1. Load configuration
|
||||
guard let configuration = loadConfiguration(at: configPath) else {
|
||||
@ -149,7 +136,7 @@ private func generate(configPath: String) throws {
|
||||
let runFolder = configuration.contentDirectory.appendingPathComponent("run")
|
||||
|
||||
// 2. Scan site elements
|
||||
guard let (siteRoot, pageMap) = try loadSiteData(in: configuration.contentDirectory, runFolder: runFolder) else {
|
||||
guard let (siteRoot, ids) = try loadSiteData(in: configuration.contentDirectory, runFolder: runFolder) else {
|
||||
return
|
||||
}
|
||||
|
||||
@ -165,7 +152,7 @@ private func generate(configPath: String) throws {
|
||||
|
||||
// 3. Generate pages
|
||||
|
||||
guard let (images, files) = generatePages(from: siteRoot, configuration: configuration, fileUpdates: fileUpdates, pageMap: pageMap, runFolder: runFolder) else {
|
||||
guard let (images, files) = generatePages(from: siteRoot, configuration: configuration, fileUpdates: fileUpdates, ids: ids, runFolder: runFolder) else {
|
||||
return
|
||||
}
|
||||
|
||||
@ -175,5 +162,6 @@ private func generate(configPath: String) throws {
|
||||
// 5. Copy/minify files
|
||||
copyFiles(files: files, configuration: configuration, runFolder: runFolder)
|
||||
|
||||
complete = true
|
||||
// 6. Print summary
|
||||
finish(start: start)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user