diff --git a/Frameworks/RSCore/RSCore/NSString+RSCore.h b/Frameworks/RSCore/RSCore/NSString+RSCore.h index 1f8e9354c..1a454fa89 100755 --- a/Frameworks/RSCore/RSCore/NSString+RSCore.h +++ b/Frameworks/RSCore/RSCore/NSString+RSCore.h @@ -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.*/ diff --git a/Frameworks/RSCore/RSCore/NSString+RSCore.m b/Frameworks/RSCore/RSCore/NSString+RSCore.m index c28a7e9c0..9e2ed073d 100755 --- a/Frameworks/RSCore/RSCore/NSString+RSCore.m +++ b/Frameworks/RSCore/RSCore/NSString+RSCore.m @@ -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:@"
" withString:@"\n\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)]; + [preflightedCopy replaceOccurrencesOfString:@"
" withString:@"\n\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)]; + [preflightedCopy replaceOccurrencesOfString:@"

" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)]; + [preflightedCopy replaceOccurrencesOfString:@"

" withString:@"\n\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)]; + [preflightedCopy replaceOccurrencesOfString:@"
" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)]; + [preflightedCopy replaceOccurrencesOfString:@"
" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)]; + [preflightedCopy replaceOccurrencesOfString:@"
" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)]; + [preflightedCopy replaceOccurrencesOfString:@"
" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)]; + [preflightedCopy replaceOccurrencesOfString:@"
" withString:@"\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, preflightedCopy.length)]; + [preflightedCopy replaceOccurrencesOfString:@"" 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 {