Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore cross references inside markdown fenced code blocks #535

Merged
merged 2 commits into from
Sep 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Application/GBApplicationStringsProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ - (NSDictionary *)appledocData {
result = [[NSMutableDictionary alloc] init];
[result setObject:@"appledoc" forKey:@"tool"];
[result setObject:@"2.2.1" forKey:@"version"];
[result setObject:@"1333" forKey:@"build"];
[result setObject:@"1334" forKey:@"build"];
[result setObject:@"http://appledoc.gentlebytes.com" forKey:@"homepage"];
}
return result;
Expand Down
19 changes: 16 additions & 3 deletions Processing/GBCommentsProcessor.m
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ - (NSString *)stringByPreprocessingNonLinkString:(NSString *)string withFlags:(G
if ([string length] == 0) return string;

// Formatting markers are fine, except *, which should be converted to **. To simplify cross refs detection, we handle all possible formatting markers though so we can search for cross refs within "clean" formatted text, without worrying about markers interfering with search. Note that we also handle "standard" Markdown nested formats and bold markers here, so that we properly handle cross references within.
NSString *pattern = @"(?s:(\\*__|__\\*|\\*\\*_|_\\*\\*|\\*\\*\\*|___|\\*_|_\\*|\\*\\*|__|\\*|_|==!!==|`)(.+?)\\1)";
NSString *pattern = @"(?s:(\\*__|__\\*|\\*\\*_|_\\*\\*|\\*\\*\\*|___|\\*_|_\\*|\\*\\*|__|\\*|_|==!!==|```|`)(.+?)\\1)";
NSArray *components = [string arrayOfDictionariesByMatchingRegex:pattern withKeysAndCaptures:@"marker", 1, @"value", 2, nil];
NSRange searchRange = NSMakeRange(0, [string length]);
NSMutableString *result = [NSMutableString stringWithCapacity:[string length]];
Expand All @@ -669,6 +669,7 @@ - (NSString *)stringByPreprocessingNonLinkString:(NSString *)string withFlags:(G
GBProcessingFlag linkFlags = flags;
NSString *markdownStartMarker = @"";
NSString *markdownEndMarker = nil;
BOOL isFencedCodeBlock = NO;
if ([componentMarker isEqualToString:@"*"]) {
if (self.settings.useSingleStarForBold) {
GBLogDebug(@" - Found '%@' formatted as bold at %@...", [componentText normalizedDescription], self.currentSourceInfo);
Expand All @@ -682,6 +683,12 @@ - (NSString *)stringByPreprocessingNonLinkString:(NSString *)string withFlags:(G
GBLogDebug(@" - Found '%@' formatted as italics at %@...", [componentText normalizedDescription], self.currentSourceInfo);
markdownStartMarker = @"_";
}
else if ([componentMarker isEqualToString:@"```"]) {
GBLogDebug(@" - Found '%@' formatted as fenced code block at %@...", [componentText normalizedDescription], self.currentSourceInfo);
markdownStartMarker = @"```";
isFencedCodeBlock = YES;
linkFlags |= GBProcessingFlagEmbedMarkdownLink;
}
else if ([componentMarker isEqualToString:@"`"]) {
GBLogDebug(@" - Found '%@' formatted as code at %@...", [componentText normalizedDescription], self.currentSourceInfo);
markdownStartMarker = @"`";
Expand All @@ -700,8 +707,14 @@ - (NSString *)stringByPreprocessingNonLinkString:(NSString *)string withFlags:(G
}
if (!markdownEndMarker) markdownEndMarker = markdownStartMarker;

// Get formatted text, convert it's cross references and append proper format markers and string to result.
NSString *convertedText = [self stringByConvertingCrossReferencesInString:componentText withFlags:linkFlags];
NSString *convertedText = nil;
if (!isFencedCodeBlock) {
// Get formatted text, convert its cross references and append proper format markers and string to result.
convertedText = [self stringByConvertingCrossReferencesInString:componentText withFlags:linkFlags];
} else {
// Don't process cross references inside fenced code blocks. Code samples frequently use external framework (UIKit, etc.) methods and objects whose references won't be found.
convertedText = componentText;
}
[result appendString:markdownStartMarker];
[result appendString:convertedText];
[result appendString:markdownEndMarker];
Expand Down