Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Commit

Permalink
hyperlink mxids and room aliases: MXKEventFormatter: Add treatMatrixU…
Browse files Browse the repository at this point in the history
…serIdAsLink configuration property

element-hq/element-ios#442
  • Loading branch information
manuroe committed Aug 26, 2016
1 parent a63973b commit d22558f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions MatrixKit/Utils/MXKEventFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ typedef enum : NSUInteger {
*/
@property (nonatomic) BOOL isForSubtitle;

/**
Flag indicating if the formatter must create clickable links for Matrix user ids.
Default is NO.
*/
@property (nonatomic) BOOL treatMatrixUserIdAsLink;

/**
Initialise the event formatter.
Expand Down
34 changes: 33 additions & 1 deletion MatrixKit/Utils/MXKEventFormatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ - (NSAttributedString*)renderString:(NSString*)string forEvent:(MXEvent*)event
[str addAttribute:NSForegroundColorAttributeName value:[self textColorForEvent:event] range:wholeString];
[str addAttribute:NSFontAttributeName value:[self fontForEvent:event] range:wholeString];

// If enabled, make links clicable
// If enabled, make links clickable
if (!([[_settings httpLinkScheme] isEqualToString: @"http"] &&
[[_settings httpsLinkScheme] isEqualToString: @"https"]))
{
Expand Down Expand Up @@ -947,6 +947,9 @@ - (NSAttributedString*)renderString:(NSString*)string forEvent:(MXEvent*)event
}
}

// Apply additional treatments
str = [self postRenderAttributedString:str];

return str;
}

Expand Down Expand Up @@ -976,12 +979,41 @@ - (NSAttributedString*)renderHTMLString:(NSString*)htmlString forEvent:(MXEvent*
// webview.
NSAttributedString *str = [[NSAttributedString alloc] initWithHTMLData:[html dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:NULL];

// Apply additional treatments
str = [self postRenderAttributedString:str];

// DTCoreText adds a newline at the end of plain text ( https://github.com/Cocoanetics/DTCoreText/issues/779 )
// or after a blockquote section.
// Trim trailing newlines
return [self removeTrailingNewlines:str];
}

- (NSAttributedString*)postRenderAttributedString:(NSAttributedString*)attributedString
{
__block NSMutableAttributedString *postRenderAttributedString;

// If enabled, make user id clickable
if (_treatMatrixUserIdAsLink)
{
// Look for each user id present in the string
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"@[A-Z0-9]+:[A-Z0-9.-]+\\.[A-Z]{2,}" options:NSRegularExpressionCaseInsensitive error:nil];

[regex enumerateMatchesInString:attributedString.string options:0 range:NSMakeRange(0, attributedString.length) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {

if (!postRenderAttributedString)
{
postRenderAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString];
}

// And make it clickable
NSString *userId = [attributedString.string substringWithRange:match.range];
[postRenderAttributedString addAttribute:NSLinkAttributeName value:userId range:match.range];
}];
}

return postRenderAttributedString ? postRenderAttributedString : attributedString;
}

- (NSAttributedString*)removeTrailingNewlines:(NSAttributedString*)attributedString
{
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString];
Expand Down

0 comments on commit d22558f

Please # to comment.