2017-06-21 06:18:46 +02:00
|
|
|
//
|
|
|
|
// RSHTMLMetadataParser.m
|
2017-06-26 01:32:07 +02:00
|
|
|
// RSParser
|
2017-06-21 06:18:46 +02:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 3/6/16.
|
|
|
|
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import <libxml/xmlstring.h>
|
|
|
|
#import "RSHTMLMetadataParser.h"
|
|
|
|
#import "RSHTMLMetadata.h"
|
|
|
|
#import "RSSAXHTMLParser.h"
|
|
|
|
#import "RSSAXParser.h"
|
2017-06-26 01:32:07 +02:00
|
|
|
#import "RSParserInternal.h"
|
|
|
|
#import <RSParser/RSParser-Swift.h>
|
2017-06-21 06:18:46 +02:00
|
|
|
|
|
|
|
@interface RSHTMLMetadataParser () <RSSAXHTMLParserDelegate>
|
|
|
|
|
2017-06-26 01:32:07 +02:00
|
|
|
@property (nonatomic, readonly) ParserData *parserData;
|
2017-06-21 06:18:46 +02:00
|
|
|
@property (nonatomic, readwrite) RSHTMLMetadata *metadata;
|
|
|
|
@property (nonatomic) NSMutableArray *dictionaries;
|
|
|
|
@property (nonatomic) BOOL didFinishParsing;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation RSHTMLMetadataParser
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - Class Methods
|
|
|
|
|
2017-06-26 01:32:07 +02:00
|
|
|
+ (RSHTMLMetadata *)HTMLMetadataWithParserData:(ParserData *)parserData {
|
2017-06-21 06:18:46 +02:00
|
|
|
|
2017-06-26 01:32:07 +02:00
|
|
|
RSHTMLMetadataParser *parser = [[self alloc] initWithParserData:parserData];
|
2017-06-21 06:18:46 +02:00
|
|
|
return parser.metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - Init
|
|
|
|
|
2017-06-26 01:32:07 +02:00
|
|
|
- (instancetype)initWithParserData:(ParserData *)parserData {
|
2017-06-21 06:18:46 +02:00
|
|
|
|
2017-06-26 01:32:07 +02:00
|
|
|
NSParameterAssert(parserData.data);
|
|
|
|
NSParameterAssert(parserData.url);
|
2017-06-21 06:18:46 +02:00
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2017-06-26 01:32:07 +02:00
|
|
|
_parserData = parserData;
|
2017-06-21 06:18:46 +02:00
|
|
|
_dictionaries = [NSMutableArray new];
|
|
|
|
|
|
|
|
[self parse];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - Parse
|
|
|
|
|
|
|
|
- (void)parse {
|
|
|
|
|
|
|
|
RSSAXHTMLParser *parser = [[RSSAXHTMLParser alloc] initWithDelegate:self];
|
2017-06-26 01:32:07 +02:00
|
|
|
[parser parseData:self.parserData.data];
|
2017-06-21 06:18:46 +02:00
|
|
|
[parser finishParsing];
|
|
|
|
|
2017-06-26 01:32:07 +02:00
|
|
|
self.metadata = [[RSHTMLMetadata alloc] initWithURLString:self.parserData.url dictionaries:[self.dictionaries copy]];
|
2017-06-21 06:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static NSString *kHrefKey = @"href";
|
|
|
|
static NSString *kSrcKey = @"src";
|
|
|
|
static NSString *kRelKey = @"rel";
|
|
|
|
|
|
|
|
- (NSString *)linkForDictionary:(NSDictionary *)d {
|
|
|
|
|
2017-06-25 23:06:01 +02:00
|
|
|
NSString *link = [d rsparser_objectForCaseInsensitiveKey:kHrefKey];
|
2017-06-21 06:18:46 +02:00
|
|
|
if (link) {
|
|
|
|
return link;
|
|
|
|
}
|
|
|
|
|
2017-06-25 23:06:01 +02:00
|
|
|
return [d rsparser_objectForCaseInsensitiveKey:kSrcKey];
|
2017-06-21 06:18:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)handleLinkAttributes:(NSDictionary *)d {
|
|
|
|
|
2017-06-25 23:06:01 +02:00
|
|
|
if (RSParserStringIsEmpty([d rsparser_objectForCaseInsensitiveKey:kRelKey])) {
|
2017-06-21 06:18:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-06-25 23:06:01 +02:00
|
|
|
if (RSParserStringIsEmpty([self linkForDictionary:d])) {
|
2017-06-21 06:18:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self.dictionaries addObject:d];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - RSSAXHTMLParserDelegate
|
|
|
|
|
|
|
|
static const char *kBody = "body";
|
|
|
|
static const NSInteger kBodyLength = 5;
|
|
|
|
static const char *kLink = "link";
|
|
|
|
static const NSInteger kLinkLength = 5;
|
|
|
|
|
|
|
|
- (void)saxParser:(RSSAXHTMLParser *)SAXParser XMLStartElement:(const xmlChar *)localName attributes:(const xmlChar **)attributes {
|
|
|
|
|
|
|
|
if (self.didFinishParsing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RSSAXEqualTags(localName, kBody, kBodyLength)) {
|
|
|
|
self.didFinishParsing = YES;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!RSSAXEqualTags(localName, kLink, kLinkLength)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSDictionary *d = [SAXParser attributesDictionary:attributes];
|
2017-06-26 01:32:07 +02:00
|
|
|
if (!RSParserObjectIsEmpty(d)) {
|
2017-06-21 06:18:46 +02:00
|
|
|
[self handleLinkAttributes:d];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|