Skip to content

Commit

Permalink
shrinking files
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Aug 4, 2023
1 parent 8cc80e0 commit be5d44e
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 275 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/brightdigit/Contribute.git",
"state" : {
"branch" : "v1.0.0",
"revision" : "72290ab018d72870cf243fe99366fab63a7ef979"
"revision" : "136c566f0f5c893547a8158878d15634d3652553",
"version" : "1.0.0-alpha.3"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
dependencies: [
.package(
url: "https://github.com/brightdigit/Contribute.git",
branch: "v1.0.0"
from: "1.0.0-alpha.3"
),
.package(
url: "https://github.com/brightdigit/SyndiKit.git",
Expand Down
81 changes: 81 additions & 0 deletions Sources/ContributeWordPress/Decoder/WordPressSite+RSSChannel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import Foundation
import SyndiKit

extension WordPressSite {
/// Returns the import directory name.
public var importDirectoryName: String {
baseURL.firstHostComponent ??
baseSiteURL?.firstHostComponent ??
"default"
}

Check warning on line 10 in Sources/ContributeWordPress/Decoder/WordPressSite+RSSChannel.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Decoder/WordPressSite+RSSChannel.swift#L6-L10

Added lines #L6 - L10 were not covered by tests

/// Initializes a `WordPressSite` instance from an ``RSSChannel``.
///
/// - Parameters:
/// - channel: The `RSSChannel` instance.
/// - Throws: An error if initialization fails.
public init(channel: RSSChannel) throws {
try self.init(
channel: channel,
relativeResourcePath: WordPressSite.wpContentUploadsRelativePath
)
}

Check warning on line 22 in Sources/ContributeWordPress/Decoder/WordPressSite+RSSChannel.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Decoder/WordPressSite+RSSChannel.swift#L17-L22

Added lines #L17 - L22 were not covered by tests

/// Initializes a `WordPressSite` instance from an ``RSSChannel`` with
/// a relative resource path.
///
/// - Parameters:
/// - channel: The RSSChannel instance.
/// - relativeResourcePath: The relative resource path.
/// - Throws: An error if initialization fails.
public init(
channel: RSSChannel,
relativeResourcePath: String
) throws {
let assetURLRegex = try Self.defaultAssetURLRegex(
forSite: channel,
relativeResourcePath: relativeResourcePath
)
self.init(
title: channel.title,
link: channel.link,
categories: channel.wpCategories,
tags: channel.wpTags,
baseSiteURL: channel.wpBaseSiteURL,
baseBlogURL: channel.wpBaseBlogURL,
posts: channel.items.compactMap(\.wpPost),
assetURLRegex: assetURLRegex
)
}

Check warning on line 49 in Sources/ContributeWordPress/Decoder/WordPressSite+RSSChannel.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Decoder/WordPressSite+RSSChannel.swift#L34-L49

Added lines #L34 - L49 were not covered by tests

/// Returns the default regular expression for matching asset urls.
///
/// - Parameters:
/// - site: The `BaseURLSite` instance.
/// - relativeResourcePath: The relative resource path where assets would be located.
/// - Returns: The default regular expression for matching asset urls.
/// - Throws: An error if the regular expression cannot be created.
public static func defaultAssetURLRegex(
forSite site: BaseURLSite,
relativeResourcePath: String = WordPressSite.wpContentUploadsRelativePath
) throws -> NSRegularExpression {
try Self.defaultAssetURLRegex(
forAssetSiteURL: site.baseURL,
relativeResourcePath: relativeResourcePath
)
}

Check warning on line 66 in Sources/ContributeWordPress/Decoder/WordPressSite+RSSChannel.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Decoder/WordPressSite+RSSChannel.swift#L61-L66

Added lines #L61 - L66 were not covered by tests

/// Returns the default regular expression for matching asset url.
///
/// - Parameters:
/// - assetSiteURL: The asset site URL.
/// - relativeResourcePath: The relative resource path.
/// - Returns: The default regular expression for matching asset url.
/// - Throws: An error if the regular expression cannot be created.
public static func defaultAssetURLRegex(
forAssetSiteURL assetSiteURL: URL,
relativeResourcePath: String = WordPressSite.wpContentUploadsRelativePath
) throws -> NSRegularExpression {
try NSRegularExpression(pattern: "\(assetSiteURL)/\(relativeResourcePath)([^\"]+)")
}

Check warning on line 80 in Sources/ContributeWordPress/Decoder/WordPressSite+RSSChannel.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Decoder/WordPressSite+RSSChannel.swift#L78-L80

Added lines #L78 - L80 were not covered by tests
}
79 changes: 0 additions & 79 deletions Sources/ContributeWordPress/Decoder/WordPressSite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,82 +72,3 @@ public struct WordPressSite: BaseURLSite {
self.assetURLRegex = assetURLRegex
}

Check warning on line 73 in Sources/ContributeWordPress/Decoder/WordPressSite.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Decoder/WordPressSite.swift#L62-L73

Added lines #L62 - L73 were not covered by tests
}

extension WordPressSite {
/// Returns the import directory name.
public var importDirectoryName: String {
baseURL.firstHostComponent ??
baseSiteURL?.firstHostComponent ??
"default"
}

/// Initializes a `WordPressSite` instance from an ``RSSChannel``.
///
/// - Parameters:
/// - channel: The `RSSChannel` instance.
/// - Throws: An error if initialization fails.
public init(channel: RSSChannel) throws {
try self.init(
channel: channel,
relativeResourcePath: WordPressSite.wpContentUploadsRelativePath
)
}

/// Initializes a `WordPressSite` instance from an ``RSSChannel`` with
/// a relative resource path.
///
/// - Parameters:
/// - channel: The RSSChannel instance.
/// - relativeResourcePath: The relative resource path.
/// - Throws: An error if initialization fails.
public init(
channel: RSSChannel,
relativeResourcePath: String
) throws {
let assetURLRegex = try Self.defaultAssetURLRegex(
forSite: channel,
relativeResourcePath: relativeResourcePath
)
self.init(
title: channel.title,
link: channel.link,
categories: channel.wpCategories,
tags: channel.wpTags,
baseSiteURL: channel.wpBaseSiteURL,
baseBlogURL: channel.wpBaseBlogURL,
posts: channel.items.compactMap(\.wpPost),
assetURLRegex: assetURLRegex
)
}

/// Returns the default regular expression for matching asset urls.
///
/// - Parameters:
/// - site: The `BaseURLSite` instance.
/// - relativeResourcePath: The relative resource path where assets would be located.
/// - Returns: The default regular expression for matching asset urls.
/// - Throws: An error if the regular expression cannot be created.
public static func defaultAssetURLRegex(
forSite site: BaseURLSite,
relativeResourcePath: String = WordPressSite.wpContentUploadsRelativePath
) throws -> NSRegularExpression {
try Self.defaultAssetURLRegex(
forAssetSiteURL: site.baseURL,
relativeResourcePath: relativeResourcePath
)
}

/// Returns the default regular expression for matching asset url.
///
/// - Parameters:
/// - assetSiteURL: The asset site URL.
/// - relativeResourcePath: The relative resource path.
/// - Returns: The default regular expression for matching asset url.
/// - Throws: An error if the regular expression cannot be created.
public static func defaultAssetURLRegex(
forAssetSiteURL assetSiteURL: URL,
relativeResourcePath: String = WordPressSite.wpContentUploadsRelativePath
) throws -> NSRegularExpression {
try NSRegularExpression(pattern: "\(assetSiteURL)/\(relativeResourcePath)([^\"]+)")
}
}
15 changes: 0 additions & 15 deletions Sources/ContributeWordPress/HTMLtoMarkdown.swift

This file was deleted.

59 changes: 59 additions & 0 deletions Sources/ContributeWordPress/Images/AssetImport+WordPress.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import Foundation
import SyndiKit

extension AssetImport {
/// Extracts asset imports from a ``WordPressSite`` using the specified import settings.
///
/// - Parameters:
/// - site: The WordPressSite instance.
/// - importSettings: The ProcessorSettings instance.
/// - Returns: An array of AssetImport instances.
public static func extractAssetImports(
from site: WordPressSite,
using importSettings: ProcessorSettings
) -> [AssetImport] {
let assetRoot = [
"",
importSettings.assetRelativePath,
site.importDirectoryName
].joined(separator: "/")
return matchUrls(
in: site.posts,
using: site.assetURLRegex
)
.compactMap { match in
AssetImport(
forPost: match.post,
sourceURL: match.sourceURL,
assetRoot: assetRoot,
resourcesPathURL: importSettings.resourcesPathURL,
importPathURL: importSettings.importAssetPathURL
)
}
}

Check warning on line 33 in Sources/ContributeWordPress/Images/AssetImport+WordPress.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Images/AssetImport+WordPress.swift#L14-L33

Added lines #L14 - L33 were not covered by tests

private static func matchUrls(
in posts: [WordPressPost],
using regex: NSRegularExpression
) -> [(sourceURL: URL, post: WordPressPost)] {
posts
.flatMap { post in
regex
.matches(
in: post.body,
range: NSRange(post.body.startIndex..., in: post.body)
)
.compactMap { match in
guard let range = Range(match.range, in: post.body) else {
return nil
}

guard let url = URL(string: String(post.body[range])) else {
return nil
}

return (sourceURL: url, post: post)
}
}
}

Check warning on line 58 in Sources/ContributeWordPress/Images/AssetImport+WordPress.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Images/AssetImport+WordPress.swift#L38-L58

Added lines #L38 - L58 were not covered by tests
}
61 changes: 0 additions & 61 deletions Sources/ContributeWordPress/Images/AssetImport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import SyndiKit
import FoundationNetworking
#endif

/// A typealias that represents a factory function for creating asset imports.
public typealias AssetImportFactory =
(_ site: WordPressSite, _ importSettings: ProcessorSettings) -> [AssetImport]

/// A type that holds information about an asset imported from a `WordPressPost`.
public struct AssetImport: Hashable {
/// The source `URL` from where asset will be imported.
Expand Down Expand Up @@ -74,60 +70,3 @@ public struct AssetImport: Hashable {
)
}

Check warning on line 71 in Sources/ContributeWordPress/Images/AssetImport.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Images/AssetImport.swift#L55-L71

Added lines #L55 - L71 were not covered by tests
}

extension AssetImport {
/// Extracts asset imports from a ``WordPressSite`` using the specified import settings.
///
/// - Parameters:
/// - site: The WordPressSite instance.
/// - importSettings: The ProcessorSettings instance.
/// - Returns: An array of AssetImport instances.
public static func extractAssetImports(
from site: WordPressSite,
using importSettings: ProcessorSettings
) -> [AssetImport] {
let assetRoot = [
"",
importSettings.assetRelativePath,
site.importDirectoryName
].joined(separator: "/")
return matchUrls(
in: site.posts,
using: site.assetURLRegex
)
.compactMap { match in
AssetImport(
forPost: match.post,
sourceURL: match.sourceURL,
assetRoot: assetRoot,
resourcesPathURL: importSettings.resourcesPathURL,
importPathURL: importSettings.importAssetPathURL
)
}
}

private static func matchUrls(
in posts: [WordPressPost],
using regex: NSRegularExpression
) -> [(sourceURL: URL, post: WordPressPost)] {
posts
.flatMap { post in
regex
.matches(
in: post.body,
range: NSRange(post.body.startIndex..., in: post.body)
)
.compactMap { match in
guard let range = Range(match.range, in: post.body) else {
return nil
}

guard let url = URL(string: String(post.body[range])) else {
return nil
}

return (sourceURL: url, post: post)
}
}
}
}
5 changes: 5 additions & 0 deletions Sources/ContributeWordPress/Images/AssetImportFactory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

/// A typealias that represents a factory function for creating asset imports.
public typealias AssetImportFactory =
(_ site: WordPressSite, _ importSettings: ProcessorSettings) -> [AssetImport]
46 changes: 46 additions & 0 deletions Sources/ContributeWordPress/MarkdownProcessor+Begin.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Contribute
import Foundation
import SyndiKit
import Yams

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

/// A type that processes WordPress sites and generates Markdowns for their posts.
extension MarkdownProcessor {
/// Begins the processing of the WordPress posts.
///
/// - Parameter settings: The required settings for processing WordPress exports.
/// - Throws: An error if the processing failed at any step.
public func begin(
withSettings settings: ProcessorSettings
) throws {
// 1. Decodes WordPress site from exports directory.
let allSites = try exportDecoder.sites(fromExportsAt: settings.exportsDirectoryURL)

// 2. Writes redirects for all decoded WordPress posts.
try redirectWriter.writeRedirects(
fromSites: allSites,
inDirectory: settings.resourcesPathURL
)

// 3. Extract and download asset imports for all WordPress sites.
let assetImports: [AssetImport] = allSites.values.flatMap {
assetImportFactory($0, settings)
}

try assetDownloader.download(
assets: assetImports,
dryRun: settings.skipDownload,
allowsOverwrites: settings.overwriteAssets
)

// 4. Starts writing the markdown files for all WordPress posts for each site.
try writeAllPosts(
fromSites: allSites,
withAssets: assetImports,
withSettings: settings
)
}

Check warning on line 45 in Sources/ContributeWordPress/MarkdownProcessor+Begin.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/MarkdownProcessor+Begin.swift#L18-L45

Added lines #L18 - L45 were not covered by tests
}
Loading

0 comments on commit be5d44e

Please sign in to comment.