-
Notifications
You must be signed in to change notification settings - Fork 6
/
MVMessageParser.m
112 lines (105 loc) · 4 KB
/
MVMessageParser.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#import "MVURLKit.h"
#import "MVMessageParser.h"
#import "MVMessage.h"
#import "MVService.h"
#import "MVDribbbleShotService.h"
#import "MVTwitterTweetService.h"
#import "MVYoutubeVideoService.h"
#import "MVCloudAppLinkService.h"
#import "MVVimeoVideoService.h"
#import "MVFlickrPhotoService.h"
#import "MVImageService.h"
#import "MVDroplrLinkService.h"
#import "NSMutableAttributedString+LinksDetection.h"
#import "NSMutableAttributedString+Trimming.h"
@interface MVMessageParser ()
- (NSObject<MVService>*)serviceForURL:(NSURL*)url;
@end
@implementation MVMessageParser
- (NSArray*)parseMessageForURLs:(NSString*)message
mentionRanges:(NSSet*)ranges
fetchServicesAutomatically:(BOOL)fetchServicesAutomatically
{
if(!message)
return [NSArray array];
NSMutableArray *messages = [NSMutableArray array];
__block NSMutableAttributedString *lastAttributedString =
[[NSMutableAttributedString alloc] init];
NSMutableAttributedString *attributedMessage =
[[NSMutableAttributedString alloc] initWithString:message];
NSRange stringRange = NSMakeRange(0, message.length);
NSValue *range;
for(range in ranges)
{
[attributedMessage addAttribute:kMVMentionAttributeName
value:@"YES"
range:NSIntersectionRange([range rangeValue],
stringRange)];
}
[attributedMessage mv_detectLinks];
[attributedMessage mv_detectEmails];
[attributedMessage enumerateAttribute:NSLinkAttributeName
inRange:NSMakeRange(0, attributedMessage.length)
options:0
usingBlock:^(id value, NSRange enumeratedRange, BOOL *stop)
{
BOOL appendAttributedString = YES;
if(value && [value isKindOfClass:[NSURL class]])
{
NSURL *url = (NSURL*)value;
NSObject<MVService>* service = [self serviceForURL:url];
if(service)
{
[lastAttributedString mv_trimWhiteSpaces];
if(lastAttributedString.length > 0)
{
MVMessage *message = [[MVMessage alloc] initWithAttributedString:lastAttributedString];
[messages addObject:message];
lastAttributedString = [[NSMutableAttributedString alloc] init];
}
if(fetchServicesAutomatically)
[service fetchInformation];
MVMessage *message = [[MVMessage alloc] initWithAttributedString:[attributedMessage
attributedSubstringFromRange:enumeratedRange]
service:service];
[messages addObject:message];
appendAttributedString = NO;
}
}
if(appendAttributedString)
{
[lastAttributedString appendAttributedString:
[attributedMessage attributedSubstringFromRange:enumeratedRange]];
}
}];
[lastAttributedString mv_trimWhiteSpaces];
if(lastAttributedString.length > 0)
{
MVMessage *message = [[MVMessage alloc] initWithAttributedString:lastAttributedString];
[messages addObject:message];
}
return messages;
}
- (NSObject<MVService>*)serviceForURL:(NSURL*)url
{
NSObject<MVService>* service;
NSArray *serviceClassnames = [NSArray arrayWithObjects:
@"MVDribbbleShotService",
@"MVTwitterTweetService",
@"MVYoutubeVideoService",
@"MVCloudAppLinkService",
@"MVDroplrLinkService",
@"MVVimeoVideoService",
@"MVFlickrPhotoService",
@"MVImageService",
nil];
NSString *serviceClassname;
for(serviceClassname in serviceClassnames)
{
service = [NSClassFromString(serviceClassname) serviceForURL:url];
if(service)
return service;
}
return nil;
}
@end