Continue porting ParserObjC to Swift.

This commit is contained in:
Brent Simmons 2024-08-18 18:18:25 -07:00
parent 4b646e42c2
commit d13f0f48ed
17 changed files with 33 additions and 160 deletions

View File

@ -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")]),
]

View File

@ -7,7 +7,6 @@
//
import Foundation
import ParserObjC
// FeedParser handles RSS, Atom, JSON Feed, and RSS-in-JSON.
// You dont need to know the type of feed.

View File

@ -7,9 +7,6 @@
//
import Foundation
#if SWIFT_PACKAGE
import ParserObjC
#endif
public enum FeedType: Sendable {
case rss

View File

@ -7,9 +7,6 @@
//
import Foundation
#if SWIFT_PACKAGE
import ParserObjC
#endif
// See https://jsonfeed.org/version/1.1

View File

@ -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

View File

@ -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) {

View File

@ -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.

View File

@ -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.

View File

@ -7,7 +7,6 @@
//
import Foundation
import ParserObjC
// RSSParser wraps the Objective-C RSRSSParser.
//

View File

@ -1,11 +0,0 @@
//
// File.swift
//
//
// Created by Brent Simmons on 4/7/24.
//
import Foundation
import ParserObjC
extension ParserData: @unchecked Sendable {}

View File

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

View File

@ -6,6 +6,5 @@
//
import Foundation
import ParserObjC
extension RSHTMLMetadataParser: @unchecked Sendable {}

View File

@ -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 <NSObject>
+ (BOOL)canParseFeed:(RSXMLData * _Nonnull)xmlData;
- (nonnull instancetype)initWithXMLData:(RSXMLData * _Nonnull)xmlData;
- (nullable RSParsedFeed *)parseFeed:(NSError * _Nullable * _Nullable)error;
@end

View File

@ -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

View File

@ -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

View File

@ -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; // Dont know which property it is. Guess based on contents of the string. Common with RSS.
@end

View File

@ -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 its 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