Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Adds RSS Item Categories as ParsedArticle Tags #69

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/ObjC/RSParsedArticle.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
@property (nonatomic, nullable) NSString *permalink;
@property (nonatomic, nullable) NSSet<RSParsedAuthor *> *authors;
@property (nonatomic, nullable) NSSet<RSParsedEnclosure *> *enclosures;
@property (nonatomic, nullable) NSSet<NSString *> *categories;
@property (nonatomic, nullable) NSDate *datePublished;
@property (nonatomic, nullable) NSDate *dateModified;
@property (nonatomic, nonnull) NSDate *dateParsed;
@property (nonatomic, nullable) NSString *language;

- (void)addEnclosure:(RSParsedEnclosure *_Nonnull)enclosure;
- (void)addCategory:(NSString *_Nonnull)category;
- (void)addAuthor:(RSParsedAuthor *_Nonnull)author;

@end
Expand Down
12 changes: 12 additions & 0 deletions Sources/ObjC/RSParsedArticle.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ - (void)addAuthor:(RSParsedAuthor *)author {
}
}

#pragma mark - Categories

- (void)addCategory:(NSString *)category {

if (self.categories) {
self.categories = [self.categories setByAddingObject:category];
}
else {
self.categories = [NSSet setWithObject:category];
}
}

#pragma mark - articleID

- (NSString *)articleID {
Expand Down
14 changes: 14 additions & 0 deletions Sources/ObjC/RSRSSParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ - (RSParsedFeed *)parseFeed {
static const char *kLanguage = "language";
static const NSInteger kLanguageLength = 9;

static const char *kCategory = "category";
static const NSInteger kCategoryLength = 9;

#pragma mark - Parsing

- (void)parse {
Expand Down Expand Up @@ -240,6 +243,14 @@ - (void)addDCElement:(const xmlChar *)localName {
}
}

- (void)addCategoryWithString:(NSString *)categoryString {

if (RSParserStringIsEmpty(categoryString)) {
return;
}

[self.currentArticle addCategory:categoryString];
}

- (void)addGuid {

Expand Down Expand Up @@ -356,6 +367,9 @@ - (void)addArticleElement:(const xmlChar *)localName prefix:(const xmlChar *)pre
else if (RSSAXEqualTags(localName, kLink, kLinkLength)) {
self.currentArticle.link = [self urlString:[self currentString]];
}
else if (RSSAXEqualTags(localName, kCategory, kCategoryLength)) {
[self addCategoryWithString:[self currentString]];
}
else if (RSSAXEqualTags(localName, kDescription, kDescriptionLength)) {

if (!self.currentArticle.body) {
Expand Down
3 changes: 2 additions & 1 deletion Sources/Swift/Feeds/XML/RSParsedFeedTransformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ private extension RSParsedFeedTransformer {
let dateModified = parsedArticle.dateModified
let authors = parsedAuthors(parsedArticle.authors)
let attachments = parsedAttachments(parsedArticle.enclosures)
let tags = parsedArticle.categories

return ParsedItem(syncServiceID: nil, uniqueID: uniqueID, feedURL: parsedArticle.feedURL, url: url, externalURL: externalURL, title: title, language: language, contentHTML: contentHTML, contentText: nil, summary: nil, imageURL: nil, bannerImageURL: nil, datePublished: datePublished, dateModified: dateModified, authors: authors, tags: nil, attachments: attachments)
return ParsedItem(syncServiceID: nil, uniqueID: uniqueID, feedURL: parsedArticle.feedURL, url: url, externalURL: externalURL, title: title, language: language, contentHTML: contentHTML, contentText: nil, summary: nil, imageURL: nil, bannerImageURL: nil, datePublished: datePublished, dateModified: dateModified, authors: authors, tags: tags, attachments: attachments)
}

static func parsedAuthors(_ authors: Set<RSParsedAuthor>?) -> Set<ParsedAuthor>? {
Expand Down
8 changes: 8 additions & 0 deletions Tests/RSParserTests/RSSParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ class RSSParserTests: XCTestCase {
XCTAssertEqual(parsedFeed.language, "en-US")
}

func testFeedCategoriesAsTags() {
let d = parserData("dcrainmaker", "xml", "https://www.dcrainmaker.com/")
let parsedFeed = try! FeedParser.parse(d)!
for article in parsedFeed.items {
XCTAssertNotNil(article.tags)
}
}

// func testFeedWithGB2312Encoding() {
// // This feed has an encoding we don’t run into very often.
// // https://github.com/Ranchero-Software/NetNewsWire/issues/1477
Expand Down