Skip to content
Browse files

Implemented some basic cleaning up when converting from Markdown to t…

…ext.

At present only Markdown inline links are converted so instead of `[description](address "title")` we only write `description`.
  • Loading branch information...
1 parent ac26896 commit ae61b04d969dfcd6f18feaef378aba0196659f5f @tomaz committed Feb 22, 2011
View
27 Application/GBApplicationSettingsProvider.m
@@ -164,7 +164,32 @@ - (NSString *)stringByConvertingMarkdownToHTML:(NSString *)markdown {
}
- (NSString *)stringByConvertingMarkdownToText:(NSString *)markdown {
- return markdown;
+ NSRange searchRange = NSMakeRange(0, [markdown length]);
+ NSMutableString *result = [NSMutableString stringWithCapacity:[markdown length]];
+
+ while (YES) {
+ NSArray *linkCaptures = [markdown captureComponentsMatchedByRegex:self.commentComponents.markdownInlineLinkRegex range:searchRange];
+ if ([linkCaptures count] == 0) break;
+
+ NSString *description = [linkCaptures objectAtIndex:1];
+ NSRange range = [markdown rangeOfString:[linkCaptures objectAtIndex:0] options:0 range:searchRange];
+ if (range.location > searchRange.location) {
+ NSRange skippedRange = NSMakeRange(searchRange.location, range.location - searchRange.location);
+ NSString *skipped = [markdown substringWithRange:skippedRange];
+ [result appendString:skipped];
+ }
+
+ [result appendString:description];
+ searchRange.location = range.location + range.length;
+ searchRange.length = [markdown length] - searchRange.location;
+ if (searchRange.location >= [markdown length]) break;
+ }
+
+ if (searchRange.location < [markdown length] - 1) {
+ NSString *remaining = [markdown substringFromIndex:searchRange.location];
+ [result appendString:remaining];
+ }
+ return result;
}
- (NSString *)stringByEscapingHTML:(NSString *)string {
View
4 Application/GBCommentComponentsProvider.m
@@ -77,11 +77,11 @@ - (NSString *)relatedSymbolRegex {
#pragma mark Markdown detection
- (NSString *)markdownInlineLinkRegex {
- GBRETURN_ON_DEMAND(@"\\[(.+)\\]\\(([^\\s]+)(?:\\s*\"(.+)\")?\\)");
+ GBRETURN_ON_DEMAND(@"\\[([^]]+)\\]\\(([^\\s]+)(?:\\s*\"([^\"]+)\")?\\)");
}
- (NSString *)markdownReferenceLinkRegex {
- GBRETURN_ON_DEMAND(@"(?s:\\[(.+)\\]:\\s*([^\\s]+)(?:\\s*\"(.+)\")?\\s*$)");
+ GBRETURN_ON_DEMAND(@"(?s:\\[([^]]+)\\]:\\s*([^\\s]+)(?:\\s*\"([^\"]+)\")?\\s*$)");
}
#pragma mark Cross references detection
View
4 Model/GBCommentComponent.h
@@ -69,11 +69,11 @@
*/
@property (readonly) NSString *htmlValue;
-/** Component's text value, derived by passing assigned `stringValue` through text processor.
+/** Component's text value, derived by passing assigned `markdownValue` through text processor.
The result is suitable for using in documentation set tokens file. Using converted HTML may result in errors when indexing due to usage of escaped HTML symbols (for example any `&ndash;` would result in docsetutil error `Entity 'ndash' not defined`. This value is derived when first used, the value is cached afterwards and cached value is returned from subsequent calls. Internally [GBApplicationSettingsProvider stringByConvertingMarkdownToText:] is used for conversion.
- @warning *Important:* This value requires `settings` to be assigned! If settings are not assigned, the value of `stringValue` is returned but is not cached, so that any subsequent assigning of settings would pick up proper text.
+ @warning *Important:* This value requires `settings` to be assigned! If settings are not assigned, the value of `markdownValue` is returned but is not cached, so that any subsequent assigning of settings would pick up proper text.
@see htmlValue
*/
View
4 Model/GBCommentComponent.m
@@ -34,9 +34,9 @@ - (NSString *)htmlValue {
}
- (NSString *)textValue {
- if (!self.settings) return self.stringValue;
+ if (!self.settings) return self.markdownValue;
if (_textValue) return _textValue;
- _textValue = [self.settings stringByConvertingMarkdownToText:self.stringValue];
+ _textValue = [self.settings stringByConvertingMarkdownToText:self.markdownValue];
return _textValue;
}
View
19 Testing/GBApplicationSettingsProviderTesting.m
@@ -410,6 +410,25 @@ - (void)testTemplateFilenameForOutputPath_shuoldReturnCorrectResults {
assertThat([settings templateFilenameForOutputPath:@"path/file-template"], is(@"path/file-template"));
assertThat([settings templateFilenameForOutputPath:@"path/file-template.html"], is(@"path/file-template.html"));
}
+
+#pragma mark Text conversion methods
+
+- (void)testStringByConvertingToText_shouldConvertMarkdownReferences {
+ // setup
+ GBApplicationSettingsProvider *settings = [GBApplicationSettingsProvider provider];
+ // execute
+ NSString *result1 = [settings stringByConvertingMarkdownToText:@"simple text"];
+ NSString *result2 = [settings stringByConvertingMarkdownToText:@"[description](address)"];
+ NSString *result3 = [settings stringByConvertingMarkdownToText:@"[description](address \"title\")"];
+ NSString *result4 = [settings stringByConvertingMarkdownToText:@"prefix [description](address) suffix"];
+ NSString *result5 = [settings stringByConvertingMarkdownToText:@"[description1](address) [description2](address) [description3](address)"];
+ // verify
+ assertThat(result1, is(@"simple text"));
+ assertThat(result2, is(@"description"));
+ assertThat(result3, is(@"description"));
+ assertThat(result4, is(@"prefix description suffix"));
+ assertThat(result5, is(@"description1 description2 description3"));
+}
#pragma mark Private accessor helpers
View
16 Testing/GBCommentTesting.m
@@ -35,12 +35,26 @@ - (void)testHtmlString_shouldUseAssignedSettings {
component.markdownValue = @"markdown";
OCMockObject *settings = [GBTestObjectsRegistry mockSettingsProvider];
component.settings = settings;
- [[settings expect] stringByConvertingMarkdown:component.markdownValue];
+ [[settings expect] stringByConvertingMarkdownToHTML:component.markdownValue];
// execute
NSString *html = component.htmlValue;
// verify
[settings verify];
html = nil; // just to suppress compiler warning of unused var...
}
+- (void)testTextString_shouldUseAssignedSettings {
+ // setup
+ GBCommentComponent *component = [GBCommentComponent componentWithStringValue:@"source"];
+ component.markdownValue = @"markdown";
+ OCMockObject *settings = [GBTestObjectsRegistry mockSettingsProvider];
+ component.settings = settings;
+ [[settings expect] stringByConvertingMarkdownToText:component.markdownValue];
+ // execute
+ NSString *html = component.textValue;
+ // verify
+ [settings verify];
+ html = nil; // just to suppress compiler warning of unused var...
+}
+
@end

0 comments on commit ae61b04

Please sign in to comment.
Something went wrong with that request. Please try again.