diff --git a/Sources/ContributeWordPress/Decoder/BaseURLSite.swift b/Sources/ContributeWordPress/Decoder/BaseURLSite.swift new file mode 100644 index 00000000..bb8a637f --- /dev/null +++ b/Sources/ContributeWordPress/Decoder/BaseURLSite.swift @@ -0,0 +1,19 @@ +import Foundation +import SyndiKit + +protocol BaseURLSite { + var link: URL { get } + var baseBlogURL: URL? { get } +} + +extension BaseURLSite { + public var baseURL: URL { + baseBlogURL ?? link + } +} + +extension RSSChannel: BaseURLSite { + var baseBlogURL: URL? { + wpBaseBlogURL + } +} diff --git a/Sources/ContributeWordPress/Decoder/WordPressSite.swift b/Sources/ContributeWordPress/Decoder/WordPressSite.swift index 898377e9..e5f2d8c7 100644 --- a/Sources/ContributeWordPress/Decoder/WordPressSite.swift +++ b/Sources/ContributeWordPress/Decoder/WordPressSite.swift @@ -1,7 +1,7 @@ import Foundation import SyndiKit -public struct WordPressSite { +public struct WordPressSite: BaseURLSite { public static let wpContentUploadsRelativePath = "wp-content/uploads" public init( title: String, @@ -61,9 +61,8 @@ extension WordPressSite { channel: RSSChannel, relativeResourcePath: String ) throws { - let baseURL = channel.wpBaseBlogURL ?? channel.link let assetsImagesRegex = try Self.defaultAssetsImagesRegex( - forAssetSiteURL: baseURL, + forSite: channel, relativeResourcePath: relativeResourcePath ) self.init( @@ -78,21 +77,26 @@ extension WordPressSite { ) } - public static func defaultAssetsImagesRegex( + static func defaultAssetsImagesRegex( + forSite site: BaseURLSite, + relativeResourcePath: String = WordPressSite.wpContentUploadsRelativePath + ) throws -> NSRegularExpression { + try Self.defaultAssetsImagesRegex( + forAssetSiteURL: site.baseURL, + relativeResourcePath: relativeResourcePath + ) + } + + static func defaultAssetsImagesRegex( forAssetSiteURL assetSiteURL: URL, relativeResourcePath: String = WordPressSite.wpContentUploadsRelativePath ) throws -> NSRegularExpression { try NSRegularExpression(pattern: "\(assetSiteURL)/\(relativeResourcePath)([^\"]+)") } - public var importDirectoryName: String { - baseBlogURL?.firstHostComponent ?? - link.firstHostComponent ?? + var importDirectoryName: String { + baseURL.firstHostComponent ?? baseSiteURL?.firstHostComponent ?? "default" } - - public var baseURL: URL { - baseBlogURL ?? link - } }