Skip to content

Instantly share code, notes, and snippets.

@ChrisBuchholz
Last active August 29, 2015 14:10
Show Gist options
  • Save ChrisBuchholz/de47dea8d7d3a11ceae7 to your computer and use it in GitHub Desktop.
Save ChrisBuchholz/de47dea8d7d3a11ceae7 to your computer and use it in GitHub Desktop.
NSMutableArray *banBookey = [NSMutableArray array];
[string enumerateSubstringsInRange:NSMakeRange(0, string.length) options:NSStringEnumerationByLines usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
for (id key in patternsByLine) {
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:patternsByLine[key] options:NSRegularExpressionCaseInsensitive error:&error];
[regex enumerateMatchesInString:substring options:0 range:NSMakeRange(0, substring.length) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
unsigned long startBan = substringRange.location + match.range.location;
unsigned long endBan = startBan + match.range.length;
BOOL paint = YES;
for (unsigned long i = startBan; i < endBan; i++) {
NSNumber *num = [NSNumber numberWithUnsignedLong:i];
if ([banBookey containsObject:num]) {
paint = NO;
} else {
[banBookey addObject:num];
}
}
if (paint) {
[attrString addAttributes:attributes[key] range:substringRange];
NSRange colorRange = match.range;
colorRange.location = colorRange.location + substringRange.location;
[attrString addAttributes:attributes[SHTagAttributes] range:colorRange];
}
}];
}
}];
for (id key in patterns) {
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:patterns[key] options:NSRegularExpressionCaseInsensitive error:&error];
[regex enumerateMatchesInString:string options:0 range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
unsigned long startBan = match.range.location;
unsigned long endBan = startBan + match.range.length;
BOOL paint = YES;
for (unsigned long i = startBan; i < endBan; i++) {
NSNumber *num = [NSNumber numberWithUnsignedLong:i];
if ([banBookey containsObject:num]) {
paint = NO;
} else {
[banBookey addObject:num];
}
}
if (paint) {
[attrString addAttributes:attributes[key] range:match.range];
[attrString addAttributes:attributes[SHTagAttributes] range:[match rangeAtIndex:1]];
[attrString addAttributes:attributes[SHTagAttributes] range:[match rangeAtIndex:3]];
}
}];
}
// match headings and lists
NSRegularExpression *heading1Regex = [NSRegularExpression regularExpressionWithPattern:@"(\\A|\\n)\\s?#" options:NSRegularExpressionCaseInsensitive error:&error];
NSRegularExpression *heading2Regex = [NSRegularExpression regularExpressionWithPattern:@"(\\A|\\n)\\s?##" options:NSRegularExpressionCaseInsensitive error:&error];
NSRegularExpression *heading3Regex = [NSRegularExpression regularExpressionWithPattern:@"(\\A|\\n)\\s?###" options:NSRegularExpressionCaseInsensitive error:&error];
NSRegularExpression *listRegex = [NSRegularExpression regularExpressionWithPattern:@"(\\A|\\n)\\s?[*-]\\s?" options:NSRegularExpressionCaseInsensitive error:&error];
[string enumerateSubstringsInRange:NSMakeRange(0, string.length) options:NSStringEnumerationByLines usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
// lists
[listRegex enumerateMatchesInString:substring options:0 range:NSMakeRange(0, substring.length) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
[attrString addAttributes:attributes[SHListParagraphAttributes] range:substringRange];
NSRange colorRange = match.range;
colorRange.location = colorRange.location + substringRange.location;
[attrString addAttributes:attributes[SHTagAttributes] range:colorRange];
}];
// heading 1
[heading1Regex enumerateMatchesInString:substring options:0 range:NSMakeRange(0, substring.length) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
[attrString addAttributes:attributes[SHHeading1Attributes] range:substringRange];
NSRange colorStartRange = NSMakeRange(substringRange.location, 1);
[attrString addAttributes:attributes[SHTagAttributes] range:colorStartRange];
}];
// heading 2
[heading2Regex enumerateMatchesInString:substring options:0 range:NSMakeRange(0, substring.length) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
[attrString addAttributes:attributes[SHHeading2Attributes] range:substringRange];
NSRange colorStartRange = NSMakeRange(substringRange.location, 2);
[attrString addAttributes:attributes[SHTagAttributes] range:colorStartRange];
}];
// heading 3
[heading3Regex enumerateMatchesInString:substring options:0 range:NSMakeRange(0, substring.length) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
[attrString addAttributes:attributes[SHHeading3Attributes] range:substringRange];
NSRange colorStartRange = NSMakeRange(substringRange.location, 3);
[attrString addAttributes:attributes[SHTagAttributes] range:colorStartRange];
}];
}];
// highlight urls
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];
[detector enumerateMatchesInString:string
options:kNilOptions
range:NSMakeRange(0, string.length)
usingBlock:
^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
[attrString addAttributes:attributes[SHURLAttributes] range:result.range];
}];
// italic
NSRegularExpression *italicRegex = [NSRegularExpression regularExpressionWithPattern:@"(\\*|\\_)(.+?)(\\*|\\_)" options:NSRegularExpressionCaseInsensitive error:&error];
[italicRegex enumerateMatchesInString:string options:0 range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
[attrString addAttributes:attributes[SHItalicAttributes] range:match.range];
NSRange colorStartRange = NSMakeRange(match.range.location, 1);
NSRange colorEndRange = NSMakeRange(match.range.location + match.range.length - 1, 1);
[attrString addAttributes:attributes[SHTagAttributes] range:colorStartRange];
[attrString addAttributes:attributes[SHTagAttributes] range:colorEndRange];
}];
// bold
NSRegularExpression *boldRegex = [NSRegularExpression regularExpressionWithPattern:@"(\\*\\*|\\_\\_)(.+?)(\\*\\*|\\_\\_)" options:NSRegularExpressionCaseInsensitive error:&error];
[boldRegex enumerateMatchesInString:string options:0 range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
[attrString addAttributes:attributes[SHBoldAttributes] range:match.range];
NSRange colorStartRange = NSMakeRange(match.range.location, 2);
NSRange colorEndRange = NSMakeRange(match.range.location + match.range.length - 2, 2);
[attrString addAttributes:attributes[SHTagAttributes] range:colorStartRange];
[attrString addAttributes:attributes[SHTagAttributes] range:colorEndRange];
}];
NSDictionary *attributes = [TextStyle getAttributes];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
[attrString addAttributes:attributes[SHDefaultAttributes] range:NSMakeRange(0, attrString.length)];
__block NSError *error = nil;
NSMutableDictionary *patternsByLine = [@{} mutableCopy];
patternsByLine[SHHeading1Attributes] = @"(\\A|\\n)\\s?#[^\\#]";
patternsByLine[SHHeading2Attributes] = @"(\\A|\\n)\\s?##[^\\#]";
patternsByLine[SHHeading3Attributes] = @"(\\A|\\n)\\s?###[^\\#]";
patternsByLine[SHListParagraphAttributes] = @"(\\A|\\n)\\s?[*-]\\s";
NSMutableDictionary *patterns = [@{} mutableCopy];
patterns[SHItalicAttributes] = @"(\\*|\\_)(.+?)(\\*|\\_)";
patterns[SHBoldAttributes] = @"(\\*\\*|\\_\\_)(.+?)(\\*\\*|\\_\\_)";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment