mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-03-25 09:05:47 +00:00
Expose adding plugins as a public API on SiteGenerator
This commit is contained in:
parent
1f3be38c5c
commit
487875098a
4 changed files with 55 additions and 16 deletions
|
|
@ -0,0 +1,30 @@
|
|||
//
|
||||
// AnyPlugin.swift
|
||||
// SiteGenerator
|
||||
//
|
||||
// Created by Sami Samhuri on 2019-12-14.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct AnyPlugin: Plugin {
|
||||
private let _setUp: (Site, URL) throws -> Void
|
||||
private let _render: (Site, URL, TemplateRenderer) throws -> Void
|
||||
|
||||
init<PluginType: Plugin>(_ plugin: PluginType) {
|
||||
self._setUp = { site, sourceURL in
|
||||
try plugin.setUp(site: site, sourceURL: sourceURL)
|
||||
}
|
||||
self._render = { site, targetURL, templateRenderer in
|
||||
try plugin.render(site: site, targetURL: targetURL, templateRenderer: templateRenderer)
|
||||
}
|
||||
}
|
||||
|
||||
func setUp(site: Site, sourceURL: URL) throws {
|
||||
try _setUp(site, sourceURL)
|
||||
}
|
||||
|
||||
func render(site: Site, targetURL: URL, templateRenderer: TemplateRenderer) throws {
|
||||
try _render(site, targetURL, templateRenderer)
|
||||
}
|
||||
}
|
||||
|
|
@ -8,8 +8,6 @@
|
|||
import Foundation
|
||||
|
||||
public protocol Plugin {
|
||||
init(options: [String: Any])
|
||||
|
||||
func setUp(site: Site, sourceURL: URL) throws
|
||||
|
||||
func render(site: Site, targetURL: URL, templateRenderer: TemplateRenderer) throws
|
||||
|
|
|
|||
|
|
@ -10,4 +10,16 @@ import Foundation
|
|||
public enum SitePlugin: String, Codable {
|
||||
case posts
|
||||
case projects
|
||||
|
||||
func construct(options: [String: Any]) -> AnyPlugin {
|
||||
switch self {
|
||||
case .posts:
|
||||
let plugin = PostsPlugin(options: options)
|
||||
return AnyPlugin(plugin)
|
||||
|
||||
case .projects:
|
||||
let plugin = ProjectsPlugin(options: options)
|
||||
return AnyPlugin(plugin)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,10 +13,10 @@ public final class SiteGenerator {
|
|||
let templateRenderer: TemplateRenderer
|
||||
|
||||
// Site properties
|
||||
let site: Site
|
||||
let sourceURL: URL
|
||||
var plugins: [Plugin] = []
|
||||
let renderers: [Renderer]
|
||||
public let site: Site
|
||||
public let sourceURL: URL
|
||||
public private(set) var plugins: [Plugin] = []
|
||||
public let renderers: [Renderer]
|
||||
|
||||
let ignoredFilenames = [".DS_Store", ".gitkeep"]
|
||||
|
||||
|
|
@ -39,17 +39,16 @@ public final class SiteGenerator {
|
|||
}
|
||||
|
||||
private func initializePlugins() throws {
|
||||
plugins = site.plugins.map { (sitePlugin, options) in
|
||||
switch sitePlugin {
|
||||
case .projects:
|
||||
return ProjectsPlugin(options: options)
|
||||
case .posts:
|
||||
return PostsPlugin(options: options)
|
||||
}
|
||||
}
|
||||
for plugin in plugins {
|
||||
try plugin.setUp(site: site, sourceURL: sourceURL)
|
||||
plugins = site.plugins.map { pair in
|
||||
let (sitePlugin, options) = pair
|
||||
return sitePlugin.construct(options: options)
|
||||
}
|
||||
try plugins.forEach(addPlugin)
|
||||
}
|
||||
|
||||
public func addPlugin(_ plugin: Plugin) throws {
|
||||
try plugin.setUp(site: site, sourceURL: sourceURL)
|
||||
plugins.append(plugin)
|
||||
}
|
||||
|
||||
public func generate(targetURL: URL) throws {
|
||||
|
|
|
|||
Loading…
Reference in a new issue