diff --git a/Sources/ContributeWordPress/ProcessorSettings.swift b/Sources/ContributeWordPress/ProcessorSettings.swift index f077cd34..8d53d371 100644 --- a/Sources/ContributeWordPress/ProcessorSettings.swift +++ b/Sources/ContributeWordPress/ProcessorSettings.swift @@ -1,6 +1,12 @@ import Foundation import SyndiKit +public enum AssetImportSetting { + case none + case download + case copyFilesFrom(URL) +} + /// A protocol that defines the settings for the `WordPressMarkdownProcessor`. public protocol ProcessorSettings { /// The URL for the content path @@ -18,16 +24,10 @@ public protocol ProcessorSettings { /// Example: /..../WordPress/exports/ var exportsDirectoryURL: URL { get } - /// The URL of the directory that the assets should be imported. - /// - /// Example: /..../WordPress/html/ - var importAssetPathURL: URL? { get } - /// Whether to overwrite existing assets. var overwriteAssets: Bool { get } - /// Whether to skip downloading assets. - var skipDownload: Bool { get } + var assetImportSetting: AssetImportSetting { get } /// Name of directory to store assets relative to ``resourcesPathURL`` var assetRelativePath: String { get } @@ -49,6 +49,36 @@ public protocol ProcessorSettings { } extension ProcessorSettings { + /// The URL of the directory that the assets should be imported. + /// + /// Example: /..../WordPress/html/ + @available( + *, deprecated, renamed: "assetImportSetting", + message: "Use `assetImportSetting` for whether to copy from location." + ) + public var importAssetPathURL: URL? { + if case let .copyFilesFrom(url) = assetImportSetting { + return url + } + + return nil + } + + /// Whether to skip downloading assets. + @available( + *, deprecated, renamed: "assetImportSetting", + message: "Use `assetImportSetting` for whether to skip download." + ) + public var skipDownload: Bool { + switch assetImportSetting { + case .none: + return true + + default: + return false + } + } + /// The URL for the asset path located under `resourcesPathURL`. public var resourceAssetPathURL: URL { resourcesPathURL.appendingPathComponent(assetRelativePath) diff --git a/Sources/ContributeWordPress/PublishDefaults.swift b/Sources/ContributeWordPress/PublishDefaults.swift new file mode 100644 index 00000000..33cf6401 --- /dev/null +++ b/Sources/ContributeWordPress/PublishDefaults.swift @@ -0,0 +1,6 @@ +import Foundation + +public enum PublishDefaults { + public static let contentDirectoryName = "Content" + public static let resourcesDirectoryName = "Resources" +} diff --git a/Sources/ContributeWordPress/Settings+Init.swift b/Sources/ContributeWordPress/Settings+Init.swift new file mode 100644 index 00000000..ff908393 --- /dev/null +++ b/Sources/ContributeWordPress/Settings+Init.swift @@ -0,0 +1,113 @@ +import Contribute +import Foundation + +extension Settings { + public init( + rootPublishSiteURL: URL, + exportsDirectoryURL: URL, + assetImportSetting: AssetImportSetting = .download, + overwriteAssets: Bool = false, + assetRelativePath: String = WordPressSite.wpContentUploadsRelativePath, + markdownFromHTML: @escaping (String) throws -> String = { $0 } + ) { + self.init( + contentPathURL: + rootPublishSiteURL.appendingPathComponent(PublishDefaults.contentDirectoryName), + resourcesPathURL: + rootPublishSiteURL.appendingPathComponent(PublishDefaults.resourcesDirectoryName), + exportsDirectoryURL: exportsDirectoryURL, + assetImportSetting: assetImportSetting, + overwriteAssets: overwriteAssets, + assetRelativePath: assetRelativePath, + markdownFromHTML: markdownFromHTML + ) + } + + public init( + contentPathURL: URL, + resourcesPathURL: URL, + exportsDirectoryURL: URL, + markdownGenerator: MarkdownGenerator, + assetImportSetting: AssetImportSetting = .download, + overwriteAssets: Bool = false, + assetRelativePath: String = WordPressSite.wpContentUploadsRelativePath + ) { + self.init( + contentPathURL: contentPathURL, + resourcesPathURL: resourcesPathURL, + exportsDirectoryURL: exportsDirectoryURL, + assetImportSetting: assetImportSetting, + overwriteAssets: overwriteAssets, + assetRelativePath: assetRelativePath, + markdownFromHTML: markdownGenerator.markdown(fromHTML:) + ) + } + + public init( + rootPublishSiteURL: URL, + exportsDirectoryURL: URL, + markdownGenerator: MarkdownGenerator, + assetImportSetting: AssetImportSetting = .download, + overwriteAssets: Bool = false, + assetRelativePath: String = WordPressSite.wpContentUploadsRelativePath + ) { + self.init( + contentPathURL: + rootPublishSiteURL.appendingPathComponent(PublishDefaults.contentDirectoryName), + resourcesPathURL: + rootPublishSiteURL.appendingPathComponent(PublishDefaults.resourcesDirectoryName), + exportsDirectoryURL: exportsDirectoryURL, + markdownGenerator: markdownGenerator, + assetImportSetting: assetImportSetting, + overwriteAssets: overwriteAssets, + assetRelativePath: assetRelativePath + ) + } + + public init( + contentPathURL: URL, + resourcesPathURL: URL, + exportsDirectoryURL: URL, + assetImportSetting: AssetImportSetting = .download, + overwriteAssets: Bool = false, + assetRelativePath: String = WordPressSite.wpContentUploadsRelativePath, + temporaryFile: @escaping (String) throws -> URL = + PandocMarkdownGenerator.Temporary.file(fromContent:), + shellOut: @escaping (String, [String]) throws -> String + ) { + self.init( + contentPathURL: contentPathURL, + resourcesPathURL: resourcesPathURL, + exportsDirectoryURL: exportsDirectoryURL, + markdownGenerator: + PandocMarkdownGenerator(shellOut: shellOut, temporaryFile: temporaryFile), + assetImportSetting: assetImportSetting, + overwriteAssets: overwriteAssets, + assetRelativePath: assetRelativePath + ) + } + + public init( + rootPublishSiteURL: URL, + exportsDirectoryURL: URL, + assetImportSetting: AssetImportSetting = .download, + overwriteAssets: Bool = false, + assetRelativePath: String = WordPressSite.wpContentUploadsRelativePath, + temporaryFile: @escaping (String) throws -> URL = + PandocMarkdownGenerator.Temporary.file(fromContent:), + shellOut: @escaping (String, [String]) throws -> String + ) { + self.init( + contentPathURL: + rootPublishSiteURL.appendingPathComponent(PublishDefaults.contentDirectoryName), + resourcesPathURL: + rootPublishSiteURL.appendingPathComponent(PublishDefaults.resourcesDirectoryName), + exportsDirectoryURL: exportsDirectoryURL, + assetImportSetting: assetImportSetting, + overwriteAssets: overwriteAssets, + assetRelativePath: assetRelativePath, + temporaryFile: temporaryFile, + shellOut: shellOut + ) + } +} diff --git a/Sources/ContributeWordPress/Settings.swift b/Sources/ContributeWordPress/Settings.swift new file mode 100644 index 00000000..be74d901 --- /dev/null +++ b/Sources/ContributeWordPress/Settings.swift @@ -0,0 +1,40 @@ +import Contribute +import Foundation + +public struct Settings: ProcessorSettings { + public let contentPathURL: URL + + public let resourcesPathURL: URL + + public let exportsDirectoryURL: URL + + public let assetImportSetting: AssetImportSetting + + public let overwriteAssets: Bool + + public let assetRelativePath: String + + public let markdownFromHTML: (String) throws -> String + + public init( + contentPathURL: URL, + resourcesPathURL: URL, + exportsDirectoryURL: URL, + assetImportSetting: AssetImportSetting = .download, + overwriteAssets: Bool = false, + assetRelativePath: String = WordPressSite.wpContentUploadsRelativePath, + markdownFromHTML: @escaping (String) throws -> String = { $0 } + ) { + self.contentPathURL = contentPathURL + self.resourcesPathURL = resourcesPathURL + self.exportsDirectoryURL = exportsDirectoryURL + self.assetImportSetting = assetImportSetting + self.overwriteAssets = overwriteAssets + self.assetRelativePath = assetRelativePath + self.markdownFromHTML = markdownFromHTML + } + + public func markdownFrom(html: String) throws -> String { + try markdownFromHTML(html) + } +}