From d13f0f48edf54d16fe878653ab8e2d56e6a26c47 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 18 Aug 2024 18:18:25 -0700 Subject: [PATCH] Continue porting ParserObjC to Swift. --- Modules/Parser/Package.swift | 5 ++- .../Sources/Parser/Feeds/FeedParser.swift | 1 - .../Sources/Parser/Feeds/FeedType.swift | 3 -- .../Parser/Feeds/JSON/JSONFeedParser.swift | 3 -- .../Parser/Feeds/JSON/RSSInJSONParser.swift | 3 -- .../Sources/Parser/Feeds/ParsedAuthor.swift | 12 +++++++ .../Sources/Parser/Feeds/XML/AtomParser.swift | 4 --- .../Feeds/XML/RSParsedFeedTransformer.swift | 3 -- .../Sources/Parser/Feeds/XML/RSSParser.swift | 1 - .../Sources/Parser/ParserData+Parser.swift | 11 ------ .../Parser/Sources/Parser/ParserData.swift | 19 +++++++++++ .../Parser/RSHTMLMetadata+Parser.swift | 1 - .../Sources/ParserObjC/FeedParser.h | 24 ------------- .../Sources/ParserObjC/ParserData.h | 24 ------------- .../Sources/ParserObjC/ParserData.m | 26 -------------- .../Sources/ParserObjC/RSParsedAuthor.h | 19 ----------- .../Sources/ParserObjC/RSParsedAuthor.m | 34 ------------------- 17 files changed, 33 insertions(+), 160 deletions(-) delete mode 100644 Modules/Parser/Sources/Parser/ParserData+Parser.swift create mode 100644 Modules/Parser/Sources/Parser/ParserData.swift delete mode 100755 Modules/ParserObjC/Sources/ParserObjC/FeedParser.h delete mode 100644 Modules/ParserObjC/Sources/ParserObjC/ParserData.h delete mode 100644 Modules/ParserObjC/Sources/ParserObjC/ParserData.m delete mode 100644 Modules/ParserObjC/Sources/ParserObjC/RSParsedAuthor.h delete mode 100644 Modules/ParserObjC/Sources/ParserObjC/RSParsedAuthor.m diff --git a/Modules/Parser/Package.swift b/Modules/Parser/Package.swift index 3d831e4d8..6d7320717 100644 --- a/Modules/Parser/Package.swift +++ b/Modules/Parser/Package.swift @@ -14,20 +14,19 @@ let package = Package( targets: ["Parser"]), ], dependencies: [ - .package(path: "../ParserObjC"), ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. // Targets can depend on other targets in this package, and on products in packages this package depends on. .target( name: "Parser", - dependencies: ["ParserObjC"], + dependencies: [], swiftSettings: [ .enableExperimentalFeature("StrictConcurrency") ]), .testTarget( name: "ParserTests", - dependencies: ["Parser", "ParserObjC"], + dependencies: ["Parser"], exclude: ["Info.plist"], resources: [.copy("Resources")]), ] diff --git a/Modules/Parser/Sources/Parser/Feeds/FeedParser.swift b/Modules/Parser/Sources/Parser/Feeds/FeedParser.swift index 4c5eb3338..a9417e31f 100644 --- a/Modules/Parser/Sources/Parser/Feeds/FeedParser.swift +++ b/Modules/Parser/Sources/Parser/Feeds/FeedParser.swift @@ -7,7 +7,6 @@ // import Foundation -import ParserObjC // FeedParser handles RSS, Atom, JSON Feed, and RSS-in-JSON. // You don’t need to know the type of feed. diff --git a/Modules/Parser/Sources/Parser/Feeds/FeedType.swift b/Modules/Parser/Sources/Parser/Feeds/FeedType.swift index 6638b6543..4dcaaa02c 100644 --- a/Modules/Parser/Sources/Parser/Feeds/FeedType.swift +++ b/Modules/Parser/Sources/Parser/Feeds/FeedType.swift @@ -7,9 +7,6 @@ // import Foundation -#if SWIFT_PACKAGE -import ParserObjC -#endif public enum FeedType: Sendable { case rss diff --git a/Modules/Parser/Sources/Parser/Feeds/JSON/JSONFeedParser.swift b/Modules/Parser/Sources/Parser/Feeds/JSON/JSONFeedParser.swift index 733f0f92e..0e765961d 100644 --- a/Modules/Parser/Sources/Parser/Feeds/JSON/JSONFeedParser.swift +++ b/Modules/Parser/Sources/Parser/Feeds/JSON/JSONFeedParser.swift @@ -7,9 +7,6 @@ // import Foundation -#if SWIFT_PACKAGE -import ParserObjC -#endif // See https://jsonfeed.org/version/1.1 diff --git a/Modules/Parser/Sources/Parser/Feeds/JSON/RSSInJSONParser.swift b/Modules/Parser/Sources/Parser/Feeds/JSON/RSSInJSONParser.swift index ad484f6b9..74e6b0658 100644 --- a/Modules/Parser/Sources/Parser/Feeds/JSON/RSSInJSONParser.swift +++ b/Modules/Parser/Sources/Parser/Feeds/JSON/RSSInJSONParser.swift @@ -7,9 +7,6 @@ // import Foundation -#if SWIFT_PACKAGE -import ParserObjC -#endif // See https://github.com/scripting/Scripting-News/blob/master/rss-in-json/README.md // Also: http://cyber.harvard.edu/rss/rss.html diff --git a/Modules/Parser/Sources/Parser/Feeds/ParsedAuthor.swift b/Modules/Parser/Sources/Parser/Feeds/ParsedAuthor.swift index 7b7d5165e..3b97cba59 100644 --- a/Modules/Parser/Sources/Parser/Feeds/ParsedAuthor.swift +++ b/Modules/Parser/Sources/Parser/Feeds/ParsedAuthor.swift @@ -22,6 +22,18 @@ public struct ParsedAuthor: Hashable, Codable, Sendable { self.emailAddress = emailAddress } + /// Use when the actual property is unknown. Guess based on contents of the string. (This is common with RSS.) + convenience init(singleString: String) { + + if singleString.contains("@") { + init(name: nil, url: nil, avatarURL: nil, emailAddress: singleString) + } else if singleString.lowercased.hasPrefix("http") { + init(name: nil, url: singleString, avatarURL: nil, emailAddress: nil) + } else { + init(name: singleString, url: nil, avatarURL: nil, emailAddress: nil) + } + } + // MARK: - Hashable public func hash(into hasher: inout Hasher) { diff --git a/Modules/Parser/Sources/Parser/Feeds/XML/AtomParser.swift b/Modules/Parser/Sources/Parser/Feeds/XML/AtomParser.swift index 93e01dcd3..151349af7 100644 --- a/Modules/Parser/Sources/Parser/Feeds/XML/AtomParser.swift +++ b/Modules/Parser/Sources/Parser/Feeds/XML/AtomParser.swift @@ -8,10 +8,6 @@ import Foundation -#if SWIFT_PACKAGE -import ParserObjC -#endif - // RSSParser wraps the Objective-C RSAtomParser. // // The Objective-C parser creates RSParsedFeed, RSParsedArticle, etc. diff --git a/Modules/Parser/Sources/Parser/Feeds/XML/RSParsedFeedTransformer.swift b/Modules/Parser/Sources/Parser/Feeds/XML/RSParsedFeedTransformer.swift index 27a5772c3..c6d0b2ba6 100644 --- a/Modules/Parser/Sources/Parser/Feeds/XML/RSParsedFeedTransformer.swift +++ b/Modules/Parser/Sources/Parser/Feeds/XML/RSParsedFeedTransformer.swift @@ -7,9 +7,6 @@ // import Foundation -#if SWIFT_PACKAGE -import ParserObjC -#endif // RSRSSParser and RSAtomParser were written in Objective-C quite a while ago. // They create an RSParsedFeed object and related Objective-C objects. diff --git a/Modules/Parser/Sources/Parser/Feeds/XML/RSSParser.swift b/Modules/Parser/Sources/Parser/Feeds/XML/RSSParser.swift index 85b88d83f..885790e16 100644 --- a/Modules/Parser/Sources/Parser/Feeds/XML/RSSParser.swift +++ b/Modules/Parser/Sources/Parser/Feeds/XML/RSSParser.swift @@ -7,7 +7,6 @@ // import Foundation -import ParserObjC // RSSParser wraps the Objective-C RSRSSParser. // diff --git a/Modules/Parser/Sources/Parser/ParserData+Parser.swift b/Modules/Parser/Sources/Parser/ParserData+Parser.swift deleted file mode 100644 index 1563bafd9..000000000 --- a/Modules/Parser/Sources/Parser/ParserData+Parser.swift +++ /dev/null @@ -1,11 +0,0 @@ -// -// File.swift -// -// -// Created by Brent Simmons on 4/7/24. -// - -import Foundation -import ParserObjC - -extension ParserData: @unchecked Sendable {} diff --git a/Modules/Parser/Sources/Parser/ParserData.swift b/Modules/Parser/Sources/Parser/ParserData.swift new file mode 100644 index 000000000..1ef7e822a --- /dev/null +++ b/Modules/Parser/Sources/Parser/ParserData.swift @@ -0,0 +1,19 @@ +// +// ParserData.swift +// +// +// Created by Brent Simmons on 8/18/24. +// + +import Foundation + +public struct ParserData: Sendable { + + let url: String + let data: Data + + public init(url: String, data: Data) { + self.url = url + self.data = data + } +} diff --git a/Modules/Parser/Sources/Parser/RSHTMLMetadata+Parser.swift b/Modules/Parser/Sources/Parser/RSHTMLMetadata+Parser.swift index de80eb2ce..391380b22 100644 --- a/Modules/Parser/Sources/Parser/RSHTMLMetadata+Parser.swift +++ b/Modules/Parser/Sources/Parser/RSHTMLMetadata+Parser.swift @@ -6,6 +6,5 @@ // import Foundation -import ParserObjC extension RSHTMLMetadataParser: @unchecked Sendable {} diff --git a/Modules/ParserObjC/Sources/ParserObjC/FeedParser.h b/Modules/ParserObjC/Sources/ParserObjC/FeedParser.h deleted file mode 100755 index 0f8df6b07..000000000 --- a/Modules/ParserObjC/Sources/ParserObjC/FeedParser.h +++ /dev/null @@ -1,24 +0,0 @@ -// -// FeedParser.h -// RSXML -// -// Created by Brent Simmons on 7/12/15. -// Copyright © 2015 Ranchero Software, LLC. All rights reserved. -// - -@import Foundation; - -@class RSParsedFeed; -@class RSXMLData; - - -@protocol FeedParser - -+ (BOOL)canParseFeed:(RSXMLData * _Nonnull)xmlData; - -- (nonnull instancetype)initWithXMLData:(RSXMLData * _Nonnull)xmlData; - -- (nullable RSParsedFeed *)parseFeed:(NSError * _Nullable * _Nullable)error; - - -@end diff --git a/Modules/ParserObjC/Sources/ParserObjC/ParserData.h b/Modules/ParserObjC/Sources/ParserObjC/ParserData.h deleted file mode 100644 index fe4885144..000000000 --- a/Modules/ParserObjC/Sources/ParserObjC/ParserData.h +++ /dev/null @@ -1,24 +0,0 @@ -// -// ParserData.h -// RSParser -// -// Created by Brent Simmons on 10/4/17. -// Copyright © 2017 Ranchero Software, LLC. All rights reserved. -// - -@import Foundation; - -NS_ASSUME_NONNULL_BEGIN - -__attribute__((swift_attr("@Sendable"))) -@interface ParserData : NSObject - -@property (nonatomic, readonly) NSString *url; -@property (nonatomic, readonly) NSData *data; - -- (instancetype)initWithURL:(NSString *)url data:(NSData *)data; - -@end - -NS_ASSUME_NONNULL_END - diff --git a/Modules/ParserObjC/Sources/ParserObjC/ParserData.m b/Modules/ParserObjC/Sources/ParserObjC/ParserData.m deleted file mode 100644 index 68c5f0356..000000000 --- a/Modules/ParserObjC/Sources/ParserObjC/ParserData.m +++ /dev/null @@ -1,26 +0,0 @@ -// -// ParserData.m -// RSParser -// -// Created by Brent Simmons on 10/4/17. -// Copyright © 2017 Ranchero Software, LLC. All rights reserved. -// - -#import "ParserData.h" - -@implementation ParserData - -- (instancetype)initWithURL:(NSString *)url data:(NSData *)data { - - self = [super init]; - if (!self) { - return nil; - } - - _url = url; - _data = data; - - return self; -} - -@end diff --git a/Modules/ParserObjC/Sources/ParserObjC/RSParsedAuthor.h b/Modules/ParserObjC/Sources/ParserObjC/RSParsedAuthor.h deleted file mode 100644 index 2c28236a2..000000000 --- a/Modules/ParserObjC/Sources/ParserObjC/RSParsedAuthor.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// RSParsedAuthor.h -// RSParserTests -// -// Created by Brent Simmons on 12/19/17. -// Copyright © 2017 Ranchero Software, LLC. All rights reserved. -// - -@import Foundation; - -@interface RSParsedAuthor : NSObject - -@property (nonatomic, nullable) NSString *name; -@property (nonatomic, nullable) NSString *emailAddress; -@property (nonatomic, nullable) NSString *url; - -+ (instancetype _Nonnull )authorWithSingleString:(NSString *_Nonnull)s; // Don’t know which property it is. Guess based on contents of the string. Common with RSS. - -@end diff --git a/Modules/ParserObjC/Sources/ParserObjC/RSParsedAuthor.m b/Modules/ParserObjC/Sources/ParserObjC/RSParsedAuthor.m deleted file mode 100644 index 154b546c8..000000000 --- a/Modules/ParserObjC/Sources/ParserObjC/RSParsedAuthor.m +++ /dev/null @@ -1,34 +0,0 @@ -// -// RSParsedAuthor.m -// RSParserTests -// -// Created by Brent Simmons on 12/19/17. -// Copyright © 2017 Ranchero Software, LLC. All rights reserved. -// - -#import "NSString+RSParser.h" - -#import "RSParsedAuthor.h" - -@implementation RSParsedAuthor - -+ (instancetype)authorWithSingleString:(NSString *)s { - - // The author element in RSS is supposed to be email address — but often it’s a name, and sometimes a URL. - - RSParsedAuthor *author = [[self alloc] init]; - - if ([s rsparser_contains:@"@"]) { - author.emailAddress = s; - } - else if ([s.lowercaseString hasPrefix:@"http"]) { - author.url = s; - } - else { - author.name = s; - } - - return author; -} - -@end