Skip to content

Commit

Permalink
adding struct Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Aug 4, 2023
1 parent 36ffc69 commit 6e646dc
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 7 deletions.
44 changes: 37 additions & 7 deletions Sources/ContributeWordPress/ProcessorSettings.swift
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 }
Expand All @@ -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
}

Check warning on line 65 in Sources/ContributeWordPress/ProcessorSettings.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/ProcessorSettings.swift#L59-L65

Added lines #L59 - L65 were not covered by tests

/// 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
}
}

Check warning on line 80 in Sources/ContributeWordPress/ProcessorSettings.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/ProcessorSettings.swift#L72-L80

Added lines #L72 - L80 were not covered by tests

/// The URL for the asset path located under `resourcesPathURL`.
public var resourceAssetPathURL: URL {
resourcesPathURL.appendingPathComponent(assetRelativePath)
Expand Down
6 changes: 6 additions & 0 deletions Sources/ContributeWordPress/PublishDefaults.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation

public enum PublishDefaults {
public static let contentDirectoryName = "Content"
public static let resourcesDirectoryName = "Resources"
}
113 changes: 113 additions & 0 deletions Sources/ContributeWordPress/Settings+Init.swift
Original file line number Diff line number Diff line change
@@ -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
)
}

Check warning on line 24 in Sources/ContributeWordPress/Settings+Init.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Settings+Init.swift#L11-L24

Added lines #L11 - L24 were not covered by tests

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:)
)
}

Check warning on line 44 in Sources/ContributeWordPress/Settings+Init.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Settings+Init.swift#L34-L44

Added lines #L34 - L44 were not covered by tests

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
)
}

Check warning on line 65 in Sources/ContributeWordPress/Settings+Init.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Settings+Init.swift#L53-L65

Added lines #L53 - L65 were not covered by tests

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:),

Check warning on line 75 in Sources/ContributeWordPress/Settings+Init.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Settings+Init.swift#L75

Added line #L75 was not covered by tests
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
)
}

Check warning on line 88 in Sources/ContributeWordPress/Settings+Init.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Settings+Init.swift#L77-L88

Added lines #L77 - L88 were not covered by tests

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:),

Check warning on line 97 in Sources/ContributeWordPress/Settings+Init.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Settings+Init.swift#L97

Added line #L97 was not covered by tests
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
)
}

Check warning on line 112 in Sources/ContributeWordPress/Settings+Init.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Settings+Init.swift#L99-L112

Added lines #L99 - L112 were not covered by tests
}
40 changes: 40 additions & 0 deletions Sources/ContributeWordPress/Settings.swift
Original file line number Diff line number Diff line change
@@ -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
}

Check warning on line 35 in Sources/ContributeWordPress/Settings.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Settings.swift#L26-L35

Added lines #L26 - L35 were not covered by tests

public func markdownFrom(html: String) throws -> String {
try markdownFromHTML(html)
}

Check warning on line 39 in Sources/ContributeWordPress/Settings.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Settings.swift#L37-L39

Added lines #L37 - L39 were not covered by tests
}

0 comments on commit 6e646dc

Please sign in to comment.