Skip to content

Commit

Permalink
implemented angle bracket links
Browse files Browse the repository at this point in the history
  • Loading branch information
odrobnik committed Oct 23, 2013
1 parent deb913d commit 14746da
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 54 deletions.
132 changes: 78 additions & 54 deletions Core/Source/DTMarkdownParser.m
Expand Up @@ -142,6 +142,10 @@ - (NSString *)_effectiveMarkerPrefixOfString:(NSString *)string
{
return @"`";
}
else if ([string hasPrefix:@"<"])
{
return @"<";
}

return nil;
}
Expand Down Expand Up @@ -205,7 +209,7 @@ - (void)_processLine:(NSString *)line
NSScanner *scanner = [NSScanner scannerWithString:line];
scanner.charactersToBeSkipped = nil;

NSCharacterSet *markerChars = [NSCharacterSet characterSetWithCharactersInString:@"*_~[!`"];
NSCharacterSet *markerChars = [NSCharacterSet characterSetWithCharactersInString:@"*_~[!`<"];

while (![scanner isAtEnd])
{
Expand All @@ -230,80 +234,100 @@ - (void)_processLine:(NSString *)line

NSAssert(effectiveOpeningMarker, @"There should be a closing marker to look for because we only get here from having scanned for marker characters");


if ([effectiveOpeningMarker isEqualToString:@"!["] || [effectiveOpeningMarker isEqualToString:@"["])
if ([effectiveOpeningMarker isEqualToString:@"!["] || [effectiveOpeningMarker isEqualToString:@"["] || [effectiveOpeningMarker isEqualToString:@"<"])
{
NSDictionary *attributes = nil;

if ([scanner scanUpToString:@"]" intoString:&enclosedPart])
NSString *closingMarker;
BOOL isSimpleHREF;

if ([effectiveOpeningMarker isEqualToString:@"<"])
{
closingMarker = @">";
isSimpleHREF = YES;
}
else
{
closingMarker = @"]";
isSimpleHREF = NO;
}

if ([scanner scanUpToString:closingMarker intoString:&enclosedPart])
{
// scan closing part of link
if ([scanner scanString:@"]" intoString:NULL])
if ([scanner scanString:closingMarker intoString:NULL])
{
// skip whitespace
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL];

if ([scanner scanString:@"(" intoString:NULL])
if (isSimpleHREF)
{
// has potentially inline address

NSString *hyperlink;
attributes = [NSDictionary dictionaryWithObject:enclosedPart forKey:@"href"];
}
else
{
// skip whitespace
[scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL];

if ([scanner scanUpToString:@")" intoString:&hyperlink])
if ([scanner scanString:@"(" intoString:NULL])
{
// see if it is closed too
if ([scanner scanString:@")" intoString:NULL])
// has potentially inline address

NSString *hyperlink;

if ([scanner scanUpToString:@")" intoString:&hyperlink])
{
NSString *URLString;
NSString *title;

NSScanner *urlScanner = [NSScanner scannerWithString:hyperlink];
urlScanner.charactersToBeSkipped = nil;

if ([urlScanner scanMarkdownHyperlink:&URLString title:&title])
// see if it is closed too
if ([scanner scanString:@")" intoString:NULL])
{
NSMutableDictionary *tmpDict = [NSMutableDictionary dictionary];
NSString *URLString;
NSString *title;

if ([URLString length])
{
tmpDict[@"href"] = URLString;
}
NSScanner *urlScanner = [NSScanner scannerWithString:hyperlink];
urlScanner.charactersToBeSkipped = nil;

if ([title length])
if ([urlScanner scanMarkdownHyperlink:&URLString title:&title])
{
tmpDict[@"title"] = title;
}

if ([tmpDict count])
{
attributes = [tmpDict copy];
NSMutableDictionary *tmpDict = [NSMutableDictionary dictionary];

if ([URLString length])
{
tmpDict[@"href"] = URLString;
}

if ([title length])
{
tmpDict[@"title"] = title;
}

if ([tmpDict count])
{
attributes = [tmpDict copy];
}
}
}
}
}
}
else if ([scanner scanString:@"[" intoString:NULL])
{
// has potentially address via ref

NSString *reference;

if ([scanner scanUpToString:@"]" intoString:&reference])
else if ([scanner scanString:@"[" intoString:NULL])
{
// see if it is closed too
if ([scanner scanString:@"]" intoString:NULL])
// has potentially address via ref

NSString *reference;

if ([scanner scanUpToString:@"]" intoString:&reference])
{
attributes = _references[[reference lowercaseString]];
// see if it is closed too
if ([scanner scanString:@"]" intoString:NULL])
{
attributes = _references[[reference lowercaseString]];
}
}
}
else
{
// could be []

if ([scanner scanString:@"]" intoString:NULL])
else
{
reference = [enclosedPart lowercaseString];
attributes = _references[reference];
// could be []

if ([scanner scanString:@"]" intoString:NULL])
{
reference = [enclosedPart lowercaseString];
attributes = _references[reference];
}
}
}
}
Expand All @@ -313,7 +337,7 @@ - (void)_processLine:(NSString *)line
// only output hyperlink if all is ok
if (attributes)
{
if ([effectiveOpeningMarker isEqualToString:@"["])
if ([effectiveOpeningMarker isEqualToString:@"["] || isSimpleHREF)
{
[self _pushTag:@"a" attributes:attributes];
[self _reportCharacters:enclosedPart];
Expand Down
15 changes: 15 additions & 0 deletions Test/Source/DTMarkdownParserTest.m
Expand Up @@ -818,6 +818,21 @@ - (void)testDoubleSquareLinkMissingClose
STAssertEqualObjects(actual, expected, @"Expected result did not match");
}

- (void)testLinkWithAngleBrackets
{
NSString *string = @"This is a link to <http://foo.com>\n";

DTMarkdownParser *parser = [self _parserForString:string options:0];

BOOL result = [parser parse];
STAssertTrue(result, @"Parser should return YES");

NSString *expected = @"<p>This is a link to <a href=\"http://foo.com\">http://foo.com</a></p>\n";
NSString *actual = [self _HTMLFromInvocations];

STAssertEqualObjects(actual, expected, @"Expected result did not match");
}

#pragma mark - Images

- (void)testInlineImage
Expand Down

0 comments on commit 14746da

Please sign in to comment.