Skip to content

Commit

Permalink
fixes and new warnings (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion authored Aug 1, 2023
1 parent 7c6fa82 commit 7e8e706
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .periphery.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
retain_public: true
targets:
- ContributeWordpress
- ContributeWordPress
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public struct PostsExportSynDecoder: PostsExportDecoder {
private let decoder: WordPressDecoder = SynDecoder()

/// Returns an array of URLs for all of the files in the given directory.
// swiftlint:disable:next line_length
private let fileURLsFromDirectory: (_ atDirectory: URL) throws -> [URL] = Self.defaultFileURLs(atDirectory:)
private let fileURLsFromDirectory: (URL) throws -> [URL] =
Self.defaultFileURLs(atDirectory:)

Check warning on line 16 in Sources/ContributeWordPress/Decoder/PostsExportSynDecoder.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Decoder/PostsExportSynDecoder.swift#L16

Added line #L16 was not covered by tests

/// Returns the last path component of the given URL without its extension.
/// This will be the section id for all posts found in the export file at given URL.
Expand Down Expand Up @@ -63,14 +63,15 @@ extension PostsExportSynDecoder {
/// - Parameter directoryURL: The directory URL.
/// - Returns: An array of export file URLs that can be found in the given directory.
/// - Throws: An error if the directory could not be enumerated.
private static func defaultFileURLs(atDirectory directoryURL: URL) throws -> [URL] {
private static func defaultFileURLs(atDirectory directoryURL: URL) -> [URL] {
let enumerator = FileManager.default.enumerator(
at: directoryURL,
includingPropertiesForKeys: nil
)

guard let enumerator = enumerator else {
throw ImportError.directory(directoryURL)
assertionFailure("\(directoryURL) returned empty enumerator.")
return []
}

return enumerator.compactMap { $0 as? URL }
Expand Down
7 changes: 5 additions & 2 deletions Sources/ContributeWordPress/Images/AssetDownloader.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// swiftlint:disable function_body_length
import Contribute
import Foundation
import SyndiKit
Expand All @@ -7,6 +6,10 @@ import SyndiKit
import FoundationNetworking
#endif

enum WordPressError: Error {
case assetDownloadErrors([URL: Error])
}

/// A type that downloads assets required by WordPress posts.
public struct AssetDownloader: Downloader {
private let urlDownloader: URLDownloader
Expand Down Expand Up @@ -55,7 +58,7 @@ public struct AssetDownloader: Downloader {
group.wait()

guard errors.isEmpty else {
throw ImportError.assetDownloads(errors)
throw WordPressError.assetDownloadErrors(errors)

Check warning on line 61 in Sources/ContributeWordPress/Images/AssetDownloader.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Images/AssetDownloader.swift#L61

Added line #L61 was not covered by tests
}
}
}
22 changes: 22 additions & 0 deletions Sources/ContributeWordPress/Images/WordPressAssetImport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,25 @@ public struct WordPressAssetImport: Hashable {
)
}
}

extension WordPressAssetImport {
public static func extractAssetImports(
from posts: [WordPressPost],
using importSettings: WordPressMarkdownProcessorSettings
) -> [WordPressAssetImport] {
importSettings.assetsImagesRegex
.matchUrls(in: posts)
.compactMap { match in
WordPressAssetImport(
forPost: match.post,
sourceURL: match.sourceURL,
assetRoot: importSettings.assetDirectoryPath,
resourcePathURL: importSettings.resourcesPathURL,
importPathURL: importSettings.importAssetPathURL
)
}

Check warning on line 81 in Sources/ContributeWordPress/Images/WordPressAssetImport.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Images/WordPressAssetImport.swift#L70-L81

Added lines #L70 - L81 were not covered by tests
}
}

public typealias AssetImportFactory =
([WordPressPost], WordPressMarkdownProcessorSettings) -> [WordPressAssetImport]
63 changes: 13 additions & 50 deletions Sources/ContributeWordPress/WordPressMarkdownProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ public struct WordPressMarkdownProcessor<
> where ContentURLGeneratorType.SourceType == WordPressSource,
MarkdownContentBuilderType.SourceType == WordPressSource {
private let exportDecoder: PostsExportDecoder
private var assetDownloader: Downloader
private let assetDownloader: Downloader
private let redirectWriter: RedirectFileWriter
private let destinationURLGenerator: ContentURLGeneratorType
private let contentBuilder: MarkdownContentBuilderType
private let postFilters: [PostFilter]
private let assetImportFactory: AssetImportFactory

/// Initializes a new `WordPressMarkdownProcessor` instance.
///
Expand All @@ -33,14 +34,16 @@ public struct WordPressMarkdownProcessor<
assetDownloader: Downloader,
destinationURLGenerator: ContentURLGeneratorType,
contentBuilder: MarkdownContentBuilderType,
postFilters: [PostFilter]
postFilters: [PostFilter],
assetImportFactory: @escaping AssetImportFactory
) {
self.exportDecoder = exportDecoder
self.redirectWriter = redirectWriter
self.assetDownloader = assetDownloader

Check warning on line 42 in Sources/ContributeWordPress/WordPressMarkdownProcessor.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/WordPressMarkdownProcessor.swift#L40-L42

Added lines #L40 - L42 were not covered by tests
self.destinationURLGenerator = destinationURLGenerator
self.contentBuilder = contentBuilder
self.postFilters = postFilters
self.assetImportFactory = assetImportFactory

Check warning on line 46 in Sources/ContributeWordPress/WordPressMarkdownProcessor.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/WordPressMarkdownProcessor.swift#L46

Added line #L46 was not covered by tests
}

/// Writes all posts to the given content path URL.
Expand Down Expand Up @@ -96,23 +99,11 @@ public struct WordPressMarkdownProcessor<
inDirectory: settings.resourcesPathURL
)

// 3. Calculate assets root for assets under resources directory.
let assetRelative = settings.resourceAssetPathURL.relativePath(
from: settings.resourcesPathURL
) ?? settings.resourcesPathURL.path

let directoryPrefix = settings.assetSiteURL.host?
.components(separatedBy: ".")
.first ?? "default"
let assetRoot = ["", assetRelative, directoryPrefix].joined(separator: "/")
#warning("Should this just use `.filter(self.postFilters.postSatisfiesAll)`?")
let importPosts = allPosts.flatMap(\.value).filter { $0.type == "post" }

Check warning on line 103 in Sources/ContributeWordPress/WordPressMarkdownProcessor.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/WordPressMarkdownProcessor.swift#L102-L103

Added lines #L102 - L103 were not covered by tests

// 4. Build asset imports from all posts
let assetsImports: [WordPressAssetImport] = extractAssetImports(
from: allPosts.flatMap(\.value).filter { $0.type == "post" },
with: "\(settings.assetSiteURL)/wp-content/uploads([^\"]+)",
assetRoot: assetRoot,
settings: settings
)
let assetsImports: [WordPressAssetImport] = assetImportFactory(importPosts, settings)

Check warning on line 106 in Sources/ContributeWordPress/WordPressMarkdownProcessor.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/WordPressMarkdownProcessor.swift#L106

Added line #L106 was not covered by tests

// 5. Download all assets
try assetDownloader.download(

Check warning on line 109 in Sources/ContributeWordPress/WordPressMarkdownProcessor.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/WordPressMarkdownProcessor.swift#L108-L109

Added lines #L108 - L109 were not covered by tests
Expand All @@ -121,46 +112,15 @@ public struct WordPressMarkdownProcessor<
allowsOverwrites: settings.overwriteAssets
)

// 6.
let htmlFromPost: (WordPressPost) -> String = { post in
post.body.replacingOccurrences(
of: "\(settings.assetSiteURL)/wp-content/uploads",
with: assetRoot
)
}

// 7. Starts writing the markdown files for all WordPress post,
try writeAllPosts(
allPosts,
withAssets: assetsImports,
to: settings.contentPathURL,
using: type(of: settings).markdownFrom(html:),
htmlFromPost: htmlFromPost
htmlFromPost: settings.htmlFromPost

Check warning on line 121 in Sources/ContributeWordPress/WordPressMarkdownProcessor.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/WordPressMarkdownProcessor.swift#L121

Added line #L121 was not covered by tests
)
}

private func extractAssetImports(
from allPosts: [WordPressPost],
with pattern: String,
assetRoot: String,
settings: WordPressMarkdownProcessorSettings
) -> [WordPressAssetImport] {
guard let regex = try? NSRegularExpression(pattern: pattern) else {
return []
}

return regex
.matchUrls(in: allPosts)
.compactMap { match in
WordPressAssetImport(
forPost: match.post,
sourceURL: match.sourceURL,
assetRoot: assetRoot,
resourcePathURL: settings.resourcesPathURL,
importPathURL: settings.importAssetPathURL
)
}
}
}

extension WordPressMarkdownProcessor {
Expand All @@ -181,6 +141,8 @@ extension WordPressMarkdownProcessor {
redirectFromatter: RedirectFormatter,
postFilters: [PostFilter],
exportDecoder: PostsExportDecoder = PostsExportSynDecoder(),
assetImportFactory: @escaping AssetImportFactory =
WordPressAssetImport.extractAssetImports(from:using:),

Check warning on line 145 in Sources/ContributeWordPress/WordPressMarkdownProcessor.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/WordPressMarkdownProcessor.swift#L145

Added line #L145 was not covered by tests
assetDownloader: Downloader = AssetDownloader(),
destinationURLGenerator: ContentURLGeneratorType = .init(),
contentBuilder: MarkdownContentBuilderType = .init(
Expand All @@ -199,7 +161,8 @@ extension WordPressMarkdownProcessor {
assetDownloader: assetDownloader,
destinationURLGenerator: destinationURLGenerator,
contentBuilder: contentBuilder,
postFilters: postFilters
postFilters: postFilters,
assetImportFactory: assetImportFactory

Check warning on line 165 in Sources/ContributeWordPress/WordPressMarkdownProcessor.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/WordPressMarkdownProcessor.swift#L159-L165

Added lines #L159 - L165 were not covered by tests
)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import SyndiKit

/// A protocol that defines the settings for the `WordPressMarkdownProcessor`.
public protocol WordPressMarkdownProcessorSettings {
Expand Down Expand Up @@ -38,9 +39,54 @@ public protocol WordPressMarkdownProcessorSettings {
/// Whether to skip downloading assets.
var skipDownload: Bool { get }

/// Name of directory to store assets relative to `ResourceAssetPathURL`
var assetDirectoryName: String { get }

@available(*, deprecated, message: "Use imports rather then prefix.")
var assetPostURLSearchPrefix: String { get }

var assetsImagesRegex: NSRegularExpression { get }

/// Converts the given HTML to Markdown.
///
/// - Parameter html: The HTML string to convert.
/// - Returns: The Markdown representation of the HTML.
static func markdownFrom(html: String) throws -> String

/// Converts the given HTML to Markdown.
///
/// - Parameter html: The HTML string to convert.
/// - Returns: The Markdown representation of the HTML.
func htmlFromPost(_ post: WordPressPost) -> String
}

extension WordPressMarkdownProcessorSettings {
public var assetDirectoryName: String {
assetSiteURL.host?
.components(separatedBy: ".")
.first ?? "default"
}

Check warning on line 68 in Sources/ContributeWordPress/WordPressMarkdownProcessorSettings.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/WordPressMarkdownProcessorSettings.swift#L64-L68

Added lines #L64 - L68 were not covered by tests

public var assetDirectoryPath: String {
let assetRelative = resourceAssetPathURL.relativePath(
from: resourcesPathURL
) ?? resourcesPathURL.path

return ["", assetRelative, assetDirectoryName].joined(separator: "/")
}

Check warning on line 76 in Sources/ContributeWordPress/WordPressMarkdownProcessorSettings.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/WordPressMarkdownProcessorSettings.swift#L70-L76

Added lines #L70 - L76 were not covered by tests

public func defaultAssetsImagesRegex() throws -> NSRegularExpression {
try NSRegularExpression(pattern: "\(assetSiteURL)/wp-content/uploads([^\"]+)")
}

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

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/WordPressMarkdownProcessorSettings.swift#L78-L80

Added lines #L78 - L80 were not covered by tests

public var assetPostURLSearchPrefix: String {
"\(assetSiteURL)/wp-content/uploads"
}

Check warning on line 84 in Sources/ContributeWordPress/WordPressMarkdownProcessorSettings.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/WordPressMarkdownProcessorSettings.swift#L82-L84

Added lines #L82 - L84 were not covered by tests

public func htmlFromPost(_ post: WordPressPost) -> String {
post.body.replacingOccurrences(
of: assetPostURLSearchPrefix,
with: assetDirectoryPath
)
}

Check warning on line 91 in Sources/ContributeWordPress/WordPressMarkdownProcessorSettings.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/WordPressMarkdownProcessorSettings.swift#L86-L91

Added lines #L86 - L91 were not covered by tests
}

0 comments on commit 7e8e706

Please sign in to comment.