forked from wikimedia/wikipedia-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add syntax highlight logic in formatter
- Loading branch information
1 parent
6d07478
commit f3c4bcd
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
Components/Sources/ComponentsObjC/WKSourceEditorFormatterStrikethrough.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,70 @@ | ||
#import "WKSourceEditorFormatterStrikethrough.h" | ||
#import "WKSourceEditorColors.h" | ||
|
||
@interface WKSourceEditorFormatterStrikethrough () | ||
|
||
@property (nonatomic, strong) NSDictionary *strikethroughAttributes; | ||
@property (nonatomic, strong) NSRegularExpression *strikethroughRegex; | ||
|
||
@end | ||
|
||
@implementation WKSourceEditorFormatterStrikethrough | ||
|
||
- (instancetype)initWithColors:(WKSourceEditorColors *)colors fonts:(WKSourceEditorFonts *)fonts { | ||
self = [super initWithColors:colors fonts:fonts]; | ||
if (self) { | ||
_strikethroughAttributes = @{ | ||
NSForegroundColorAttributeName: colors.greenForegroundColor, | ||
WKSourceEditorCustomKeyColorGreen: [NSNumber numberWithBool:YES] | ||
}; | ||
|
||
_strikethroughRegex = [[NSRegularExpression alloc] initWithPattern:@"(<s>)(\\s*.*?)(<\\/s>)" options:0 error:nil]; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- (void)addSyntaxHighlightingToAttributedString:(nonnull NSMutableAttributedString *)attributedString inRange:(NSRange)range { | ||
[self.strikethroughRegex enumerateMatchesInString:attributedString.string | ||
options:0 | ||
range:range | ||
usingBlock:^(NSTextCheckingResult *_Nullable result, NSMatchingFlags flags, BOOL *_Nonnull stop) { | ||
NSRange fullMatch = [result rangeAtIndex:0]; | ||
NSRange openingRange = [result rangeAtIndex:1]; | ||
NSRange contentRange = [result rangeAtIndex:2]; | ||
NSRange closingRange = [result rangeAtIndex:3]; | ||
|
||
if (openingRange.location != NSNotFound) { | ||
[attributedString addAttributes:self.strikethroughAttributes range:openingRange]; | ||
} | ||
|
||
if (closingRange.location != NSNotFound) { | ||
[attributedString addAttributes:self.strikethroughAttributes range:closingRange]; | ||
} | ||
}]; | ||
} | ||
|
||
- (void)updateColors:(WKSourceEditorColors *)colors inAttributedString:(NSMutableAttributedString *)attributedString inRange:(NSRange)range { | ||
|
||
NSMutableDictionary *mutAttributes = [[NSMutableDictionary alloc] initWithDictionary:self.strikethroughAttributes]; | ||
[mutAttributes setObject:colors.greenForegroundColor forKey:NSForegroundColorAttributeName]; | ||
self.strikethroughAttributes = [[NSDictionary alloc] initWithDictionary:mutAttributes]; | ||
|
||
[attributedString enumerateAttribute:WKSourceEditorCustomKeyColorGreen | ||
inRange:range | ||
options:nil | ||
usingBlock:^(id value, NSRange localRange, BOOL *stop) { | ||
if ([value isKindOfClass: [NSNumber class]]) { | ||
NSNumber *numValue = (NSNumber *)value; | ||
if ([numValue boolValue] == YES) { | ||
[attributedString addAttributes:self.strikethroughAttributes range:localRange]; | ||
} | ||
} | ||
}]; | ||
} | ||
|
||
- (void)updateFonts:(WKSourceEditorFonts *)fonts inAttributedString:(NSMutableAttributedString *)attributedString inRange:(NSRange)range { | ||
// No special font handling needed for references | ||
} | ||
|
||
@end |