Parse Atom authors. Fix #260.

This commit is contained in:
Brent Simmons 2017-12-19 13:24:19 -08:00
parent 0317196cf6
commit 6c5566e184
2 changed files with 39 additions and 2 deletions

View File

@ -15,6 +15,7 @@
#import <RSParser/RSDateParser.h> #import <RSParser/RSDateParser.h>
#import <RSParser/ParserData.h> #import <RSParser/ParserData.h>
#import <RSParser/RSParsedEnclosure.h> #import <RSParser/RSParsedEnclosure.h>
#import <RSParser/RSParsedAuthor.h>
@interface RSAtomParser () <RSSAXParserDelegate> @interface RSAtomParser () <RSSAXParserDelegate>
@ -34,6 +35,7 @@
@property (nonatomic) NSDate *dateParsed; @property (nonatomic) NSDate *dateParsed;
@property (nonatomic) RSSAXParser *parser; @property (nonatomic) RSSAXParser *parser;
@property (nonatomic, readonly) RSParsedArticle *currentArticle; @property (nonatomic, readonly) RSParsedArticle *currentArticle;
@property (nonatomic) RSParsedAuthor *currentAuthor;
@property (nonatomic, readonly) NSDate *currentDate; @property (nonatomic, readonly) NSDate *currentDate;
@end @end
@ -128,6 +130,15 @@ static const NSInteger kUpdatedLength = 8;
static const char *kAuthor = "author"; static const char *kAuthor = "author";
static const NSInteger kAuthorLength = 7; static const NSInteger kAuthorLength = 7;
static const char *kName = "name";
static const NSInteger kNameLength = 5;
static const char *kEmail = "email";
static const NSInteger kEmailLength = 6;
static const char *kURI = "uri";
static const NSInteger kURILength = 4;
static const char *kEntry = "entry"; static const char *kEntry = "entry";
static const NSInteger kEntryLength = 6; static const NSInteger kEntryLength = 6;
@ -404,6 +415,7 @@ static const NSInteger kLengthLength = 7;
if (RSSAXEqualTags(localName, kAuthor, kAuthorLength)) { if (RSSAXEqualTags(localName, kAuthor, kAuthorLength)) {
self.parsingAuthor = YES; self.parsingAuthor = YES;
self.currentAuthor = [[RSParsedAuthor alloc] init];
return; return;
} }
@ -471,8 +483,25 @@ static const NSInteger kLengthLength = 7;
[self.xhtmlString appendString:@">"]; [self.xhtmlString appendString:@">"];
} }
else if (RSSAXEqualTags(localName, kAuthor, kAuthorLength)) { else if (self.parsingAuthor) {
if (RSSAXEqualTags(localName, kAuthor, kAuthorLength)) {
self.parsingAuthor = NO; self.parsingAuthor = NO;
RSParsedAuthor *author = self.currentAuthor;
if (author.name || author.emailAddress || author.url) {
[self.currentArticle addAuthor:author];
}
self.currentAuthor = nil;
}
else if (RSSAXEqualTags(localName, kName, kNameLength)) {
self.currentAuthor.name = self.parser.currentStringWithTrimmedWhitespace;
}
else if (RSSAXEqualTags(localName, kEmail, kEmailLength)) {
self.currentAuthor.emailAddress = self.parser.currentStringWithTrimmedWhitespace;
}
else if (RSSAXEqualTags(localName, kURI, kURILength)) {
self.currentAuthor.url = self.parser.currentStringWithTrimmedWhitespace;
}
} }
else if (RSSAXEqualTags(localName, kEntry, kEntryLength)) { else if (RSSAXEqualTags(localName, kEntry, kEntryLength)) {

View File

@ -54,6 +54,14 @@ class AtomParserTests: XCTestCase {
XCTAssertTrue(article.uniqueID.hasPrefix("tag:daringfireball.net,2017:/")) XCTAssertTrue(article.uniqueID.hasPrefix("tag:daringfireball.net,2017:/"))
XCTAssertEqual(article.authors!.count, 1) // TODO: parse Atom authors XCTAssertEqual(article.authors!.count, 1) // TODO: parse Atom authors
let author = article.authors!.first!
if author.name == "Daring Fireball Department of Commerce" {
XCTAssertNil(author.url)
}
else {
XCTAssertEqual(author.name, "John Gruber")
XCTAssertEqual(author.url, "http://daringfireball.net/")
}
XCTAssertNotNil(article.datePublished) XCTAssertNotNil(article.datePublished)
XCTAssert(article.attachments == nil) XCTAssert(article.attachments == nil)