From 5ec0964594515201bd531291c90edd3a1f30cbe6 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 18 Aug 2024 18:58:53 -0700 Subject: [PATCH] Port RSHTMLTag and RSOMLFeedSpecifier to Swift. --- .../Parser/Sources/Parser/HTML/HTMLTag.swift | 24 +++++++++ .../Parser/OPML/OPMLFeedSpecifier.swift | 40 +++++++++++++++ .../Parser/Utilities/String+RSParser.swift | 6 +++ .../ParserObjC/Sources/ParserObjC/RSHTMLTag.h | 33 ------------ .../ParserObjC/Sources/ParserObjC/RSHTMLTag.m | 43 ---------------- .../Sources/ParserObjC/RSOPMLFeedSpecifier.h | 24 --------- .../Sources/ParserObjC/RSOPMLFeedSpecifier.m | 51 ------------------- 7 files changed, 70 insertions(+), 151 deletions(-) create mode 100644 Modules/Parser/Sources/Parser/HTML/HTMLTag.swift create mode 100644 Modules/Parser/Sources/Parser/OPML/OPMLFeedSpecifier.swift delete mode 100644 Modules/ParserObjC/Sources/ParserObjC/RSHTMLTag.h delete mode 100644 Modules/ParserObjC/Sources/ParserObjC/RSHTMLTag.m delete mode 100755 Modules/ParserObjC/Sources/ParserObjC/RSOPMLFeedSpecifier.h delete mode 100755 Modules/ParserObjC/Sources/ParserObjC/RSOPMLFeedSpecifier.m diff --git a/Modules/Parser/Sources/Parser/HTML/HTMLTag.swift b/Modules/Parser/Sources/Parser/HTML/HTMLTag.swift new file mode 100644 index 000000000..27acc83aa --- /dev/null +++ b/Modules/Parser/Sources/Parser/HTML/HTMLTag.swift @@ -0,0 +1,24 @@ +// +// HTMLTag.swift +// +// +// Created by Brent Simmons on 8/18/24. +// + +import Foundation + +public struct HTMLTag: Sendable { + + public enum HTMLTagType { + case link + case meta + } + + public let tagType: HTMLTagType + public let attributes: [String: String]? + + public init(tagType: TagType, attributes: [String : String]?) { + self.tagType = tagType + self.attributes = attributes + } +} diff --git a/Modules/Parser/Sources/Parser/OPML/OPMLFeedSpecifier.swift b/Modules/Parser/Sources/Parser/OPML/OPMLFeedSpecifier.swift new file mode 100644 index 000000000..ee6b1b456 --- /dev/null +++ b/Modules/Parser/Sources/Parser/OPML/OPMLFeedSpecifier.swift @@ -0,0 +1,40 @@ +// +// File.swift +// +// +// Created by Brent Simmons on 8/18/24. +// + +import Foundation + +public struct OPMLFeedSpecifier: Sendable { + + let title: String? + let feedDescription: String? + let homePageURL: String? + let feedURL: String + + init(title: String?, feedDescription: String?, homePageURL: String?, feedURL: String) { + + if String.isEmptyOrNil(title) { + self.title = nil + } else { + self.title = title + } + + if String.isEmptyOrNil(feedDescription) { + self.feedDescription = nil + } else { + self.feedDescription = feedDescription + } + + if String.isEmptyOrNil(homePageURL) { + self.homePageURL = nil + } else { + self.homePageURL = homePageURL + } + + self.feedURL = feedURL + } +} + diff --git a/Modules/Parser/Sources/Parser/Utilities/String+RSParser.swift b/Modules/Parser/Sources/Parser/Utilities/String+RSParser.swift index 9922be93a..9ed9c0b51 100644 --- a/Modules/Parser/Sources/Parser/Utilities/String+RSParser.swift +++ b/Modules/Parser/Sources/Parser/Utilities/String+RSParser.swift @@ -14,4 +14,10 @@ extension String { return self.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ? nil : self } + static func isEmptyOrNil(_ s: String?) { + if let s { + return s.isEmpty + } + return true + } } diff --git a/Modules/ParserObjC/Sources/ParserObjC/RSHTMLTag.h b/Modules/ParserObjC/Sources/ParserObjC/RSHTMLTag.h deleted file mode 100644 index e8e9cb426..000000000 --- a/Modules/ParserObjC/Sources/ParserObjC/RSHTMLTag.h +++ /dev/null @@ -1,33 +0,0 @@ -// -// RSHTMLTag.h -// RSParser -// -// Created by Brent Simmons on 11/26/17. -// Copyright © 2017 Ranchero Software, LLC. All rights reserved. -// - -@import Foundation; - -NS_ASSUME_NONNULL_BEGIN - -extern NSString *RSHTMLTagNameLink; // @"link" -extern NSString *RSHTMLTagNameMeta; // @"meta" - -typedef NS_ENUM(NSInteger, RSHTMLTagType) { - RSHTMLTagTypeLink, - RSHTMLTagTypeMeta -}; - -@interface RSHTMLTag : NSObject - -- (instancetype)initWithType:(RSHTMLTagType)type attributes:(NSDictionary *)attributes; - -+ (RSHTMLTag *)linkTagWithAttributes:(NSDictionary *)attributes; -+ (RSHTMLTag *)metaTagWithAttributes:(NSDictionary *)attributes; - -@property (nonatomic, readonly) RSHTMLTagType type; -@property (nonatomic, readonly) NSDictionary *attributes; - -@end - -NS_ASSUME_NONNULL_END diff --git a/Modules/ParserObjC/Sources/ParserObjC/RSHTMLTag.m b/Modules/ParserObjC/Sources/ParserObjC/RSHTMLTag.m deleted file mode 100644 index 5b0262ffb..000000000 --- a/Modules/ParserObjC/Sources/ParserObjC/RSHTMLTag.m +++ /dev/null @@ -1,43 +0,0 @@ -// -// RSHTMLTag.m -// RSParser -// -// Created by Brent Simmons on 11/26/17. -// Copyright © 2017 Ranchero Software, LLC. All rights reserved. -// - -#import "RSHTMLTag.h" - -NSString *RSHTMLTagNameLink = @"link"; -NSString *RSHTMLTagNameMeta = @"meta"; - -@implementation RSHTMLTag - -- (instancetype)initWithType:(RSHTMLTagType)type attributes:(NSDictionary *)attributes { - - self = [super init]; - if (!self) { - return nil; - } - - _type = type; - _attributes = attributes; - - return self; -} - -+ (RSHTMLTag *)linkTagWithAttributes:(NSDictionary *)attributes { - - return [[self alloc] initWithType:RSHTMLTagTypeLink attributes:attributes]; -} - -+ (RSHTMLTag *)metaTagWithAttributes:(NSDictionary *)attributes { - - return [[self alloc] initWithType:RSHTMLTagTypeMeta attributes:attributes]; -} - -- (NSString *)description { - return [NSString stringWithFormat:@"<%@: %p> type: %ld attributes: %@", NSStringFromClass([self class]), self, (long)self.type, self.attributes]; -} - -@end diff --git a/Modules/ParserObjC/Sources/ParserObjC/RSOPMLFeedSpecifier.h b/Modules/ParserObjC/Sources/ParserObjC/RSOPMLFeedSpecifier.h deleted file mode 100755 index 8c4aea6b0..000000000 --- a/Modules/ParserObjC/Sources/ParserObjC/RSOPMLFeedSpecifier.h +++ /dev/null @@ -1,24 +0,0 @@ -// -// RSOPMLFeedSpecifier.h -// RSParser -// -// Created by Brent Simmons on 2/28/16. -// Copyright © 2016 Ranchero Software, LLC. All rights reserved. -// - -@import Foundation; - -NS_ASSUME_NONNULL_BEGIN - -@interface RSOPMLFeedSpecifier : NSObject - -- (instancetype)initWithTitle:(NSString * _Nullable)title feedDescription:(NSString * _Nullable)feedDescription homePageURL:(NSString * _Nullable)homePageURL feedURL:(NSString *)feedURL; - -@property (nonatomic, nullable, readonly) NSString *title; -@property (nonatomic, nullable, readonly) NSString *feedDescription; -@property (nonatomic, nullable, readonly) NSString *homePageURL; -@property (nonatomic, readonly) NSString *feedURL; - -@end - -NS_ASSUME_NONNULL_END diff --git a/Modules/ParserObjC/Sources/ParserObjC/RSOPMLFeedSpecifier.m b/Modules/ParserObjC/Sources/ParserObjC/RSOPMLFeedSpecifier.m deleted file mode 100755 index bb32ccf54..000000000 --- a/Modules/ParserObjC/Sources/ParserObjC/RSOPMLFeedSpecifier.m +++ /dev/null @@ -1,51 +0,0 @@ -// -// RSOPMLFeedSpecifier.m -// RSParser -// -// Created by Brent Simmons on 2/28/16. -// Copyright © 2016 Ranchero Software, LLC. All rights reserved. -// - -#import "RSOPMLFeedSpecifier.h" -#import "RSParserInternal.h" - - - -@implementation RSOPMLFeedSpecifier - -- (instancetype)initWithTitle:(NSString *)title feedDescription:(NSString *)feedDescription homePageURL:(NSString *)homePageURL feedURL:(NSString *)feedURL { - - NSParameterAssert(!RSParserStringIsEmpty(feedURL)); - - self = [super init]; - if (!self) { - return nil; - } - - if (RSParserStringIsEmpty(title)) { - _title = nil; - } - else { - _title = title; - } - - if (RSParserStringIsEmpty(feedDescription)) { - _feedDescription = nil; - } - else { - _feedDescription = feedDescription; - } - - if (RSParserStringIsEmpty(homePageURL)) { - _homePageURL = nil; - } - else { - _homePageURL = homePageURL; - } - - _feedURL = feedURL; - - return self; -} - -@end