Add method to convert HTML to plain text suitable for copying to text-based apps. It could be much improved, but it’s better than nothing for right now.
This commit is contained in:
parent
9ec6188395
commit
a8bd15e9b4
|
@ -59,6 +59,7 @@ typedef struct {
|
|||
- (NSString *)rs_stringByStrippingSuffix:(NSString *)suffix caseSensitive:(BOOL)caseSensitive;
|
||||
|
||||
- (NSString *)rs_stringByStrippingHTML:(NSUInteger)maxCharacters;
|
||||
- (NSString *)rs_stringByConvertingToPlainText;
|
||||
|
||||
/*Filename from path, file URL string, or external URL string.*/
|
||||
|
||||
|
|
|
@ -301,6 +301,49 @@ NSString *RSStringReplaceAll(NSString *stringToSearch, NSString *searchFor, NSSt
|
|||
return (__bridge_transfer NSString *)s;
|
||||
}
|
||||
|
||||
- (NSString *)rs_stringByConvertingToPlainText {
|
||||
|
||||
if (![self containsString:@"<"]) {
|
||||
return self;
|
||||
}
|
||||
|
||||
NSMutableString *preflightedCopy = [self mutableCopy];
|
||||
[preflightedCopy replaceOccurrencesOfString:@"<blockquote>" withString:@"\n\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)];
|
||||
[preflightedCopy replaceOccurrencesOfString:@"</blockquote>" withString:@"\n\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)];
|
||||
[preflightedCopy replaceOccurrencesOfString:@"<p>" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)];
|
||||
[preflightedCopy replaceOccurrencesOfString:@"</p>" withString:@"\n\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)];
|
||||
[preflightedCopy replaceOccurrencesOfString:@"<div>" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)];
|
||||
[preflightedCopy replaceOccurrencesOfString:@"</div>" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)];
|
||||
[preflightedCopy replaceOccurrencesOfString:@"<br>" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)];
|
||||
[preflightedCopy replaceOccurrencesOfString:@"<br />" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)];
|
||||
[preflightedCopy replaceOccurrencesOfString:@"<br/>" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)];
|
||||
[preflightedCopy replaceOccurrencesOfString:@"</li>" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)];
|
||||
|
||||
CFMutableStringRef s = CFStringCreateMutable(kCFAllocatorDefault, (CFIndex)preflightedCopy.length);
|
||||
NSUInteger level = 0;
|
||||
|
||||
for (NSUInteger i = 0; i < preflightedCopy.length; i++) {
|
||||
|
||||
unichar ch = [preflightedCopy characterAtIndex:i];
|
||||
|
||||
if (ch == '<') {
|
||||
level++;
|
||||
}
|
||||
else if (ch == '>') {
|
||||
level--;
|
||||
}
|
||||
else if (level == 0) {
|
||||
CFStringAppendCharacters(s, &ch, 1);
|
||||
}
|
||||
}
|
||||
|
||||
NSMutableString *plainTextString = [(__bridge_transfer NSString *)s mutableCopy];
|
||||
while ([plainTextString rangeOfString:@"\n\n\n"].location != NSNotFound) {
|
||||
[plainTextString replaceOccurrencesOfString:@"\n\n\n" withString:@"\n\n" options:NSLiteralSearch range:NSMakeRange(0, plainTextString.length)];
|
||||
}
|
||||
|
||||
return plainTextString;
|
||||
}
|
||||
|
||||
- (NSString *)rs_filename {
|
||||
|
||||
|
|
Loading…
Reference in New Issue