Compare commits
No commits in common. "31edd35463b0575f35d047c69473e701abf75931" and "756629d2dc98046ebf09e6d35796898044a20811" have entirely different histories.
31edd35463
...
756629d2dc
@ -126,20 +126,6 @@ extension Element {
|
||||
This property is mandatory at root level, and is propagated to child elements.
|
||||
*/
|
||||
let relatedContentText: String
|
||||
|
||||
/**
|
||||
The text to display on the navigation element pointing to this element as the next page.
|
||||
|
||||
This property is mandatory at root level, and is propagated to child elements.
|
||||
*/
|
||||
let navigationTextAsNextPage: String
|
||||
|
||||
/**
|
||||
The text to display on a navigation element pointing to this element as the previous page.
|
||||
|
||||
This property is mandatory at root level, and is propagated to child elements.
|
||||
*/
|
||||
let navigationTextAsPreviousPage: String
|
||||
}
|
||||
}
|
||||
|
||||
@ -185,10 +171,6 @@ extension Element.LocalizedMetadata {
|
||||
self.externalUrl = log.unexpected(data.externalUrl, name: "externalUrl", source: source)
|
||||
self.relatedContentText = log
|
||||
.required(data.relatedContentText, name: "relatedContentText", source: source) ?? ""
|
||||
self.navigationTextAsNextPage = log
|
||||
.required(data.navigationTextAsNextPage, name: "navigationTextAsNextPage", source: source) ?? ""
|
||||
self.navigationTextAsPreviousPage = log
|
||||
.required(data.navigationTextAsPreviousPage, name: "navigationTextAsPreviousPage", source: source) ?? ""
|
||||
|
||||
guard isComplete else {
|
||||
return nil
|
||||
@ -225,8 +207,6 @@ extension Element.LocalizedMetadata {
|
||||
self.cornerText = data.cornerText
|
||||
self.externalUrl = data.externalUrl
|
||||
self.relatedContentText = data.relatedContentText ?? parent.relatedContentText
|
||||
self.navigationTextAsPreviousPage = data.navigationTextAsPreviousPage ?? parent.navigationTextAsPreviousPage
|
||||
self.navigationTextAsNextPage = data.navigationTextAsNextPage ?? parent.navigationTextAsNextPage
|
||||
|
||||
guard isComplete else {
|
||||
return nil
|
||||
|
@ -310,16 +310,6 @@ extension Element {
|
||||
elements.filter { $0.state.isShownInOverview }
|
||||
}
|
||||
|
||||
var linkedElements: [LinkedElement] {
|
||||
let items = sortedItems
|
||||
let connected = items.enumerated().map { i, element in
|
||||
let previous = i+1 < items.count ? items[i+1] : nil
|
||||
let next = i > 0 ? items[i-1] : nil
|
||||
return (previous, element, next)
|
||||
}
|
||||
return connected + elements.filter { !$0.state.isShownInOverview }.map { (nil, $0, nil )}
|
||||
}
|
||||
|
||||
/**
|
||||
The url of the top-level section of the element.
|
||||
*/
|
||||
|
@ -119,20 +119,6 @@ extension GenericMetadata {
|
||||
This property is mandatory at root level, and is propagated to child elements.
|
||||
*/
|
||||
let relatedContentText: String?
|
||||
|
||||
/**
|
||||
The text to display on a navigation element pointing to this element as the previous page.
|
||||
|
||||
This property is mandatory at root level, and is propagated to child elements.
|
||||
*/
|
||||
let navigationTextAsPreviousPage: String?
|
||||
|
||||
/**
|
||||
The text to display on the navigation element pointing to this element as the next page.
|
||||
|
||||
This property is mandatory at root level, and is propagated to child elements.
|
||||
*/
|
||||
let navigationTextAsNextPage: String?
|
||||
}
|
||||
}
|
||||
|
||||
@ -156,8 +142,6 @@ extension GenericMetadata.LocalizedMetadata: Codable {
|
||||
.cornerText,
|
||||
.externalUrl,
|
||||
.relatedContentText,
|
||||
.navigationTextAsPreviousPage,
|
||||
.navigationTextAsNextPage,
|
||||
]
|
||||
}
|
||||
|
||||
@ -188,9 +172,7 @@ extension GenericMetadata.LocalizedMetadata {
|
||||
thumbnailSuffix: nil,
|
||||
cornerText: nil,
|
||||
externalUrl: nil,
|
||||
relatedContentText: nil,
|
||||
navigationTextAsPreviousPage: nil,
|
||||
navigationTextAsNextPage: nil)
|
||||
relatedContentText: nil)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -212,9 +194,7 @@ extension GenericMetadata.LocalizedMetadata {
|
||||
thumbnailSuffix: nil,
|
||||
cornerText: nil,
|
||||
externalUrl: nil,
|
||||
relatedContentText: "",
|
||||
navigationTextAsPreviousPage: "",
|
||||
navigationTextAsNextPage: "")
|
||||
relatedContentText: "")
|
||||
}
|
||||
|
||||
static var full: GenericMetadata.LocalizedMetadata {
|
||||
@ -233,8 +213,6 @@ extension GenericMetadata.LocalizedMetadata {
|
||||
thumbnailSuffix: "",
|
||||
cornerText: "",
|
||||
externalUrl: "",
|
||||
relatedContentText: "",
|
||||
navigationTextAsPreviousPage: "",
|
||||
navigationTextAsNextPage: "")
|
||||
relatedContentText: "")
|
||||
}
|
||||
}
|
||||
|
@ -23,9 +23,9 @@ enum ThumbnailStyle: String, CaseIterable {
|
||||
case .large:
|
||||
return 374
|
||||
case .square:
|
||||
return 178
|
||||
return height
|
||||
case .small:
|
||||
return 78
|
||||
return height
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,8 +7,8 @@ extension NSImage {
|
||||
guard self.size.width > size.width else {
|
||||
return self
|
||||
}
|
||||
return NSImage(size: size, flipped: false) { [weak self] (resizedRect) -> Bool in
|
||||
self?.draw(in: resizedRect)
|
||||
return NSImage(size: size, flipped: false) { (resizedRect) -> Bool in
|
||||
self.draw(in: resizedRect)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -113,7 +113,6 @@ final class ImageGenerator {
|
||||
}
|
||||
|
||||
func requireImage(at destination: String, generatedFrom source: String, requiredBy path: String, width: Int, height: Int?) -> NSSize {
|
||||
requiredImages.insert(destination)
|
||||
let height = height.unwrapped(CGFloat.init)
|
||||
let sourceUrl = input.appendingPathComponent(source)
|
||||
guard sourceUrl.exists else {
|
||||
@ -159,8 +158,6 @@ final class ImageGenerator {
|
||||
}
|
||||
printMissingImages()
|
||||
printImageWarnings()
|
||||
printGeneratedImages()
|
||||
printTotalImageCount()
|
||||
}
|
||||
|
||||
private func printMissingImages() {
|
||||
@ -183,20 +180,6 @@ final class ImageGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
private func printGeneratedImages() {
|
||||
guard !generatedImages.isEmpty else {
|
||||
return
|
||||
}
|
||||
print("\(generatedImages.count) images generated:")
|
||||
for image in generatedImages {
|
||||
print(" " + image)
|
||||
}
|
||||
}
|
||||
|
||||
private func printTotalImageCount() {
|
||||
print("\(requiredImages.count) images")
|
||||
}
|
||||
|
||||
private func addWarning(_ message: String, destination: String, path: String) {
|
||||
let warning = " \(destination): \(message) required by \(path)"
|
||||
imageWarnings.insert(warning)
|
||||
@ -221,13 +204,14 @@ final class ImageGenerator {
|
||||
missingImages[source] = images.first?.path
|
||||
return
|
||||
}
|
||||
let jobs = imageHasChanged ? images : images.filter(isMissing)
|
||||
// Update all images
|
||||
jobs.forEach { job in
|
||||
// Prevent memory overflow due to repeated NSImage operations
|
||||
autoreleasepool {
|
||||
create(job: job, from: image, source: source)
|
||||
}
|
||||
if imageHasChanged {
|
||||
// Update all images
|
||||
images.forEach { create(job: $0, from: image, source: source) }
|
||||
} else {
|
||||
// Update only missing images
|
||||
images
|
||||
.filter(isMissing)
|
||||
.forEach { create(job: $0, from: image, source: source) }
|
||||
}
|
||||
}
|
||||
|
||||
@ -240,43 +224,31 @@ final class ImageGenerator {
|
||||
fatalError()
|
||||
}
|
||||
|
||||
let desiredWidth = CGFloat(image.size.width)
|
||||
|
||||
let destinationSize = image.size.scaledDown(to: desiredWidth)
|
||||
let scaledImage = image.scaledDown(to: destinationSize)
|
||||
let scaledSize = scaledImage.size
|
||||
|
||||
if abs(scaledSize.width - desiredWidth) > 2 {
|
||||
addWarning("Invalid width (\(scaledSize.width) instead of \(desiredWidth))", job: job)
|
||||
}
|
||||
|
||||
if scaledSize.width > desiredWidth {
|
||||
addWarning("Invalid width (\(scaledSize.width) instead of \(desiredWidth))", job: job)
|
||||
}
|
||||
|
||||
let destinationExtension = destinationUrl.pathExtension.lowercased()
|
||||
guard let type = ImageType(fileExtension: destinationExtension)?.fileType else {
|
||||
addWarning("Invalid image extension \(destinationExtension)", job: job)
|
||||
return
|
||||
}
|
||||
guard let tiff = scaledImage.tiffRepresentation, let tiffData = NSBitmapImageRep(data: tiff) else {
|
||||
addWarning("Failed to get data", job: job)
|
||||
return
|
||||
}
|
||||
|
||||
let desiredWidth = CGFloat(job.width)
|
||||
|
||||
let sourceRep = image.representations[0]
|
||||
let destinationSize = NSSize(width: sourceRep.pixelsWide, height: sourceRep.pixelsHigh)
|
||||
.scaledDown(to: desiredWidth)
|
||||
//image.size.scaledDown(to: desiredWidth)
|
||||
|
||||
print("\(job.destination):")
|
||||
print(" Source: \(image.size.width) x \(image.size.height)")
|
||||
print(" Wanted: \(destinationSize.width) x \(destinationSize.height) (\(job.width))")
|
||||
// create NSBitmapRep manually, if using cgImage, the resulting size is wrong
|
||||
let rep = NSBitmapImageRep(bitmapDataPlanes: nil,
|
||||
pixelsWide: Int(destinationSize.width),
|
||||
pixelsHigh: Int(destinationSize.height),
|
||||
bitsPerSample: 8,
|
||||
samplesPerPixel: 4,
|
||||
hasAlpha: true,
|
||||
isPlanar: false,
|
||||
colorSpaceName: NSColorSpaceName.deviceRGB,
|
||||
bytesPerRow: Int(destinationSize.width) * 4,
|
||||
bitsPerPixel: 32)!
|
||||
|
||||
let ctx = NSGraphicsContext(bitmapImageRep: rep)
|
||||
NSGraphicsContext.saveGraphicsState()
|
||||
NSGraphicsContext.current = ctx
|
||||
image.draw(in: NSMakeRect(0, 0, destinationSize.width, destinationSize.height))
|
||||
ctx?.flushGraphics()
|
||||
NSGraphicsContext.restoreGraphicsState()
|
||||
|
||||
// Get NSData, and save it
|
||||
guard let data = rep.representation(using: type, properties: [.compressionFactor: NSNumber(0.7)]) else {
|
||||
guard let data = tiffData.representation(using: type, properties: [.compressionFactor: NSNumber(0.7)]) else {
|
||||
addWarning("Failed to get data", job: job)
|
||||
return
|
||||
}
|
||||
@ -286,6 +258,5 @@ final class ImageGenerator {
|
||||
addWarning("Failed to write image (\(error))", job: job)
|
||||
return
|
||||
}
|
||||
generatedImages.insert(job.destination)
|
||||
}
|
||||
}
|
||||
|
@ -3,13 +3,20 @@ import Ink
|
||||
|
||||
struct PageGenerator {
|
||||
|
||||
struct NavigationLink {
|
||||
|
||||
let link: String
|
||||
|
||||
let text: String
|
||||
}
|
||||
|
||||
private let factory: LocalizedSiteTemplate
|
||||
|
||||
init(factory: LocalizedSiteTemplate) {
|
||||
self.factory = factory
|
||||
}
|
||||
|
||||
func generate(page: Element, language: String, previousPage: Element?, nextPage: Element?) {
|
||||
func generate(page: Element, language: String, nextPage: NavigationLink?, previousPage: NavigationLink?) {
|
||||
guard !page.isExternalPage else {
|
||||
return
|
||||
}
|
||||
@ -28,10 +35,10 @@ struct PageGenerator {
|
||||
|
||||
content[.header] = makeHeader(page: page, metadata: metadata, language: language)
|
||||
content[.content] = pageContent
|
||||
content[.previousPageLinkText] = previousText(for: previousPage, language: language)
|
||||
content[.previousPageUrl] = navLink(from: page, to: previousPage, language: language)
|
||||
content[.nextPageLinkText] = nextText(for: nextPage, language: language)
|
||||
content[.nextPageUrl] = navLink(from: page, to: nextPage, language: language)
|
||||
content[.previousPageLinkText] = previousPage.unwrapped { factory.factory.html.makePrevText($0.text) }
|
||||
content[.previousPageUrl] = previousPage?.link
|
||||
content[.nextPageLinkText] = nextPage.unwrapped { factory.factory.html.makeNextText($0.text) }
|
||||
content[.nextPageUrl] = nextPage?.link
|
||||
content[.footer] = page.customFooterContent()
|
||||
|
||||
if pageIncludesCode {
|
||||
@ -51,27 +58,6 @@ struct PageGenerator {
|
||||
files.generated(page: path)
|
||||
}
|
||||
|
||||
private func navLink(from element: Element, to destination: Element?, language: String) -> String? {
|
||||
guard let fullPath = destination?.fullPageUrl(for: language) else {
|
||||
return nil
|
||||
}
|
||||
return element.relativePathToOtherSiteElement(file: fullPath)
|
||||
}
|
||||
|
||||
private func previousText(for element: Element?, language: String) -> String? {
|
||||
guard let text = element?.localized(for: language).navigationTextAsPreviousPage else {
|
||||
return nil
|
||||
}
|
||||
return factory.factory.html.makePrevText(text)
|
||||
}
|
||||
|
||||
private func nextText(for element: Element?, language: String) -> String? {
|
||||
guard let text = element?.localized(for: language).navigationTextAsNextPage else {
|
||||
return nil
|
||||
}
|
||||
return factory.factory.html.makeNextText(text)
|
||||
}
|
||||
|
||||
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)?
|
||||
|
@ -1,7 +1,5 @@
|
||||
import Foundation
|
||||
|
||||
typealias LinkedElement = (previous: Element?, element: Element, next: Element?)
|
||||
|
||||
struct SiteGenerator {
|
||||
|
||||
let templates: TemplateFactory
|
||||
@ -28,21 +26,22 @@ struct SiteGenerator {
|
||||
let overviewGenerator = OverviewPageGenerator(factory: template)
|
||||
let pageGenerator = PageGenerator(factory: template)
|
||||
|
||||
var elementsToProcess: [LinkedElement] = [(nil, site, nil)]
|
||||
while let (previous, element, next) = elementsToProcess.popLast() {
|
||||
var elementsToProcess: [Element] = [site]
|
||||
while let element = elementsToProcess.popLast() {
|
||||
// Move recursively down to all pages
|
||||
elementsToProcess.append(contentsOf: element.linkedElements)
|
||||
elementsToProcess.append(contentsOf: element.elements)
|
||||
|
||||
processAllFiles(for: element)
|
||||
|
||||
if !element.elements.isEmpty {
|
||||
overviewGenerator.generate(section: element, language: language)
|
||||
} else {
|
||||
#warning("Determine previous and next pages (with relative links)")
|
||||
pageGenerator.generate(
|
||||
page: element,
|
||||
language: language,
|
||||
previousPage: previous,
|
||||
nextPage: next)
|
||||
nextPage: nil,
|
||||
previousPage: nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user