Compare commits
No commits in common. "3d9551117d0c332cb21b1af55c1b520d1bd9c2db" and "cfb68f5237c89dd2e047e47d7c385cbe6abf9557" have entirely different histories.
3d9551117d
...
cfb68f5237
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +0,0 @@
|
||||
|
||||
config.json
|
@ -78,13 +78,6 @@ struct Element {
|
||||
*/
|
||||
let requiredFiles: Set<String>
|
||||
|
||||
/**
|
||||
Additional images required by the element.
|
||||
|
||||
These images are specified as: `source_name destination_name width (height)`.
|
||||
*/
|
||||
let images: [ManualImage]
|
||||
|
||||
/**
|
||||
The style of thumbnail to use when generating overviews.
|
||||
|
||||
@ -148,7 +141,7 @@ struct Element {
|
||||
- Parameter folder: The root folder of the site content.
|
||||
- Note: Uses global objects.
|
||||
*/
|
||||
init?(atRoot folder: URL) {
|
||||
init?(atRoot folder: URL) throws {
|
||||
self.inputFolder = folder
|
||||
self.path = ""
|
||||
|
||||
@ -167,7 +160,6 @@ struct Element {
|
||||
self.sortIndex = log.unused(metadata.sortIndex, "sortIndex", source: source)
|
||||
self.externalFiles = metadata.externalFiles ?? []
|
||||
self.requiredFiles = metadata.requiredFiles ?? [] // Paths are already relative to root
|
||||
self.images = metadata.images?.compactMap { ManualImage(input: $0, path: "") } ?? []
|
||||
self.thumbnailStyle = log.unused(metadata.thumbnailStyle, "thumbnailStyle", source: source) ?? .large
|
||||
self.useManualSorting = log.unused(metadata.useManualSorting, "useManualSorting", source: source) ?? true
|
||||
self.overviewItemCount = metadata.overviewItemCount ?? Element.overviewItemCountDefault
|
||||
@ -183,10 +175,10 @@ struct Element {
|
||||
}
|
||||
|
||||
files.add(page: path, id: id)
|
||||
self.readElements(in: folder, source: nil)
|
||||
try self.readElements(in: folder, source: nil)
|
||||
}
|
||||
|
||||
mutating func readElements(in folder: URL, source: String?) {
|
||||
mutating func readElements(in folder: URL, source: String?) throws {
|
||||
let subFolders: [URL]
|
||||
do {
|
||||
subFolders = try FileManager.default
|
||||
@ -196,13 +188,13 @@ struct Element {
|
||||
log.add(error: "Failed to read subfolders", source: source ?? "root", error: error)
|
||||
return
|
||||
}
|
||||
self.elements = subFolders.compactMap { subFolder in
|
||||
self.elements = try subFolders.compactMap { subFolder in
|
||||
let s = (source.unwrapped { $0 + "/" } ?? "") + subFolder.lastPathComponent
|
||||
return Element(parent: self, folder: subFolder, path: s)
|
||||
return try Element(parent: self, folder: subFolder, path: s)
|
||||
}
|
||||
}
|
||||
|
||||
init?(parent: Element, folder: URL, path: String) {
|
||||
init?(parent: Element, folder: URL, path: String) throws {
|
||||
self.inputFolder = folder
|
||||
self.path = path
|
||||
|
||||
@ -236,7 +228,6 @@ struct Element {
|
||||
// TODO: Propagate external files from the parent if subpath matches?
|
||||
self.externalFiles = Element.rootPaths(for: metadata.externalFiles, path: path)
|
||||
self.requiredFiles = Element.rootPaths(for: metadata.requiredFiles, path: path)
|
||||
self.images = metadata.images?.compactMap { ManualImage(input: $0, path: path) } ?? []
|
||||
self.thumbnailStyle = log.thumbnailStyle(metadata.thumbnailStyle, source: source)
|
||||
self.useManualSorting = metadata.useManualSorting ?? false
|
||||
self.overviewItemCount = metadata.overviewItemCount ?? parent.overviewItemCount
|
||||
@ -261,7 +252,7 @@ struct Element {
|
||||
// All properties initialized
|
||||
|
||||
files.add(page: path, id: id)
|
||||
self.readElements(in: folder, source: path)
|
||||
try self.readElements(in: folder, source: path)
|
||||
}
|
||||
}
|
||||
|
||||
@ -374,20 +365,12 @@ extension Element {
|
||||
}
|
||||
|
||||
static func containedFileRelativeToRoot(filePath: String, folder path: String) -> String? {
|
||||
if path == "" {
|
||||
return filePath
|
||||
}
|
||||
if filePath.hasPrefix("/") || filePath.hasPrefix("http") || filePath.hasPrefix("mailto:") {
|
||||
return nil
|
||||
}
|
||||
return "\(path)/\(filePath)"
|
||||
}
|
||||
|
||||
/**
|
||||
Convert a set of relative paths to paths that are relative to the root element.
|
||||
- Parameter input: The set of paths to convert.
|
||||
- Parameter path: The path to the folder where the paths are currently relative to.
|
||||
*/
|
||||
static func rootPaths(for input: Set<String>?, path: String) -> Set<String> {
|
||||
guard let input = input else {
|
||||
return []
|
||||
@ -553,44 +536,3 @@ extension Element {
|
||||
elements.forEach { $0.printTree(indentation: indentation + " ") }
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Images
|
||||
|
||||
extension Element {
|
||||
|
||||
struct ManualImage {
|
||||
|
||||
let sourcePath: String
|
||||
|
||||
let destinationPath: String
|
||||
|
||||
let desiredWidth: Int
|
||||
|
||||
let desiredHeight: Int?
|
||||
|
||||
init?(input: String, path: String) {
|
||||
let parts = input.components(separatedBy: " ").filter { !$0.isEmpty }
|
||||
guard parts.count == 3 || parts.count == 4 else {
|
||||
log.add(error: "Invalid image specification, expected 'source dest width (height)", source: path)
|
||||
return nil
|
||||
}
|
||||
guard let width = Int(parts[2]) else {
|
||||
log.add(error: "Invalid width for image \(parts[0])", source: path)
|
||||
return nil
|
||||
}
|
||||
|
||||
self.sourcePath = Element.relativeToRoot(filePath: parts[0], folder: path)
|
||||
self.destinationPath = Element.relativeToRoot(filePath: parts[1], folder: path)
|
||||
self.desiredWidth = width
|
||||
guard parts.count == 4 else {
|
||||
self.desiredHeight = nil
|
||||
return
|
||||
}
|
||||
guard let height = Int(parts[3]) else {
|
||||
log.add(error: "Invalid height for image \(parts[0])", source: path)
|
||||
return nil
|
||||
}
|
||||
self.desiredHeight = height
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -77,13 +77,6 @@ struct GenericMetadata {
|
||||
*/
|
||||
let requiredFiles: Set<String>?
|
||||
|
||||
/**
|
||||
Additional images required by the element.
|
||||
|
||||
These images are specified as: `source_name destination_name width (height)`.
|
||||
*/
|
||||
let images: Set<String>?
|
||||
|
||||
/**
|
||||
The style of thumbnail to use when generating overviews.
|
||||
|
||||
@ -135,7 +128,6 @@ extension GenericMetadata: Codable {
|
||||
.sortIndex,
|
||||
.externalFiles,
|
||||
.requiredFiles,
|
||||
.images,
|
||||
.thumbnailStyle,
|
||||
.useManualSorting,
|
||||
.overviewItemCount,
|
||||
@ -206,7 +198,6 @@ extension GenericMetadata {
|
||||
sortIndex: 1,
|
||||
externalFiles: [],
|
||||
requiredFiles: [],
|
||||
images: [],
|
||||
thumbnailStyle: "",
|
||||
useManualSorting: false,
|
||||
overviewItemCount: 6,
|
||||
|
@ -1,20 +1,8 @@
|
||||
import Foundation
|
||||
|
||||
struct Configuration: Codable {
|
||||
struct Configuration {
|
||||
|
||||
let pageImageWidth: Int
|
||||
|
||||
let minifyCSSandJS: Bool
|
||||
|
||||
let contentPath: String
|
||||
|
||||
let outputPath: String
|
||||
|
||||
var contentDirectory: URL {
|
||||
.init(fileURLWithPath: contentPath)
|
||||
}
|
||||
|
||||
var outputDirectory: URL {
|
||||
.init(fileURLWithPath: outputPath)
|
||||
}
|
||||
}
|
||||
|
@ -9,15 +9,17 @@ struct SiteGenerator {
|
||||
self.templates = try TemplateFactory(templateFolder: templatesFolder)
|
||||
}
|
||||
|
||||
func generate(site: Element) {
|
||||
site.languages.forEach {
|
||||
generate(site: site, metadata: $0)
|
||||
func generate(site: Element) throws {
|
||||
site.requiredFiles.forEach(files.require)
|
||||
site.externalFiles.forEach(files.exclude)
|
||||
try site.languages.forEach {
|
||||
try generate(site: site, metadata: $0)
|
||||
}
|
||||
}
|
||||
|
||||
private func generate(site: Element, metadata: Element.LocalizedMetadata) {
|
||||
private func generate(site: Element, metadata: Element.LocalizedMetadata) throws {
|
||||
let language = metadata.language
|
||||
let template = LocalizedSiteTemplate(
|
||||
let template = try LocalizedSiteTemplate(
|
||||
factory: templates,
|
||||
language: language,
|
||||
site: site)
|
||||
@ -31,7 +33,8 @@ struct SiteGenerator {
|
||||
// Move recursively down to all pages
|
||||
elementsToProcess.append(contentsOf: element.elements)
|
||||
|
||||
processAllFiles(for: element)
|
||||
element.requiredFiles.forEach(files.require)
|
||||
element.externalFiles.forEach(files.exclude)
|
||||
|
||||
if !element.elements.isEmpty {
|
||||
overviewGenerator.generate(section: element, language: language)
|
||||
@ -45,16 +48,4 @@ struct SiteGenerator {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func processAllFiles(for element: Element) {
|
||||
element.requiredFiles.forEach(files.require)
|
||||
element.externalFiles.forEach(files.exclude)
|
||||
element.images.forEach {
|
||||
files.requireImage(
|
||||
source: $0.sourcePath,
|
||||
destination: $0.destinationPath,
|
||||
width: $0.desiredWidth,
|
||||
desiredHeight: $0.desiredHeight)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ struct LocalizedSiteTemplate {
|
||||
factory.page
|
||||
}
|
||||
|
||||
init(factory: TemplateFactory, language: String, site: Element) {
|
||||
init(factory: TemplateFactory, language: String, site: Element) throws {
|
||||
self.author = site.author
|
||||
self.factory = factory
|
||||
|
||||
@ -61,7 +61,7 @@ struct LocalizedSiteTemplate {
|
||||
url: $0.path + Element.htmlPagePathAddition(for: language))
|
||||
}
|
||||
|
||||
self.topBar = .init(
|
||||
self.topBar = try .init(
|
||||
factory: factory,
|
||||
language: language,
|
||||
sections: sections,
|
||||
|
@ -10,7 +10,7 @@ struct PrefilledTopBarTemplate {
|
||||
|
||||
private let factory: TemplateFactory
|
||||
|
||||
init(factory: TemplateFactory, language: String, sections: [SectionInfo], topBarWebsiteTitle: String) {
|
||||
init(factory: TemplateFactory, language: String, sections: [SectionInfo], topBarWebsiteTitle: String) throws {
|
||||
self.factory = factory
|
||||
self.language = language
|
||||
self.sections = sections
|
||||
|
@ -1,33 +1,28 @@
|
||||
import Foundation
|
||||
|
||||
private let contentDirectory = URL(fileURLWithPath: "/Users/ch/Projects/MakerSpace")
|
||||
private let outputDirectory = URL(fileURLWithPath: "/Users/ch/Projects/MakerSpace/Site")
|
||||
|
||||
let configUrl = URL(fileURLWithPath: "/Users/ch/Projects/MakerSpace/CHGenerator/config.json")
|
||||
|
||||
let configuration: Configuration
|
||||
do {
|
||||
let data = try Data(contentsOf: configUrl)
|
||||
configuration = try JSONDecoder().decode(from: data)
|
||||
} catch {
|
||||
print("Failed to read configuration: \(error)")
|
||||
exit(1)
|
||||
}
|
||||
let configuration = Configuration(
|
||||
pageImageWidth: 748,
|
||||
minifyCSSandJS: true)
|
||||
|
||||
let log = ValidationLog()
|
||||
let files = FileSystem(
|
||||
in: configuration.contentDirectory,
|
||||
to: configuration.outputDirectory)
|
||||
let files = FileSystem(in: contentDirectory, to: outputDirectory)
|
||||
|
||||
guard let siteData = Element(atRoot: configuration.contentDirectory) else {
|
||||
private let siteData: Element
|
||||
do {
|
||||
guard let element = try Element(atRoot: contentDirectory) else {
|
||||
exit(0)
|
||||
}
|
||||
siteData = element
|
||||
} catch {
|
||||
print(error)
|
||||
exit(0)
|
||||
}
|
||||
|
||||
do {
|
||||
let siteGenerator = try SiteGenerator()
|
||||
siteGenerator.generate(site: siteData)
|
||||
} catch {
|
||||
print("Failed to generate website: \(error)")
|
||||
exit(2)
|
||||
}
|
||||
private let siteGenerator = try SiteGenerator()
|
||||
try siteGenerator.generate(site: siteData)
|
||||
|
||||
files.printGeneratedPages()
|
||||
files.printEmptyPages()
|
||||
@ -36,5 +31,4 @@ files.printDraftPages()
|
||||
files.createImages()
|
||||
print("Images generated")
|
||||
files.copyRequiredFiles()
|
||||
files.printExternalFiles()
|
||||
files.writeHashes()
|
||||
|
@ -1,6 +0,0 @@
|
||||
{
|
||||
"pageImageWidth" : 748,
|
||||
"minifyCSSandJS" : true,
|
||||
"contentPath" : "/path/to/content/folder",
|
||||
"outputPath" : "/path/to/output/folder")
|
||||
}
|
13
install.sh
13
install.sh
@ -1,13 +0,0 @@
|
||||
#!/bin/bash
|
||||
# Install the required dependencies for CHGenerator
|
||||
|
||||
# Note: The following is only required if `minifyCSSandJS=True`
|
||||
# is set in the generator configuration
|
||||
|
||||
# Install the Javascript minifier
|
||||
# https://github.com/mishoo/UglifyJS
|
||||
npm install uglify-js -g
|
||||
|
||||
# Install the clean-css minifier
|
||||
# https://github.com/clean-css/clean-css-cli
|
||||
npm install clean-css-cli -g
|
Loading…
Reference in New Issue
Block a user