Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/dbloete/ioctocat
Browse files Browse the repository at this point in the history
  • Loading branch information
aharren committed May 28, 2011
2 parents 8af8c78 + 980dc49 commit cc3e81b
Show file tree
Hide file tree
Showing 69 changed files with 735 additions and 860 deletions.
4 changes: 3 additions & 1 deletion Classes/DiffController.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "DiffController.h"
#import "NSString+Extensions.h"


@interface DiffController ()
Expand Down Expand Up @@ -49,7 +50,8 @@ - (void)viewDidUnload {
}

- (NSString *)htmlFormatDiff:(NSString *)theDiff {
NSArray *lines = [theDiff componentsSeparatedByString:@"\n"];
NSString *escaped = [theDiff escapeHTML];
NSArray *lines = [escaped componentsSeparatedByString:@"\n"];
NSMutableString *diff = [NSMutableString string];
for (NSString *line in lines) {
if ([line hasPrefix:@"@@"]) {
Expand Down
2 changes: 2 additions & 0 deletions Classes/Extensions/NSString+Extensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
@interface NSString (Extensions)
- (NSString *)lowercaseFirstCharacter;
- (BOOL)isEmpty;
- (NSString *)escapeHTML;
- (NSString *)stringByDecodingXMLEntities;
@end
85 changes: 85 additions & 0 deletions Classes/Extensions/NSString+Extensions.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,89 @@ - (BOOL)isEmpty {
return [trimmed isEqualToString:@""];
}

- (NSString *)escapeHTML {
NSMutableString *result = [[NSMutableString alloc] initWithString:self];
[result replaceOccurrencesOfString:@"&" withString:@"&" options:NSLiteralSearch range:NSMakeRange(0, [result length])];
[result replaceOccurrencesOfString:@"<" withString:@"&lt;" options:NSLiteralSearch range:NSMakeRange(0, [result length])];
[result replaceOccurrencesOfString:@">" withString:@"&gt;" options:NSLiteralSearch range:NSMakeRange(0, [result length])];
[result replaceOccurrencesOfString:@"\"" withString:@"&quot;" options:NSLiteralSearch range:NSMakeRange(0, [result length])];
[result replaceOccurrencesOfString:@"'" withString:@"&#39;" options:NSLiteralSearch range:NSMakeRange(0, [result length])];
return [result autorelease];
}

// implementation by Daniel Dickison and Walty
// http://stackoverflow.com/questions/1105169/html-character-decoding-in-objective-c-cocoa-touch
- (NSString *)stringByDecodingXMLEntities {
NSUInteger myLength = [self length];
NSUInteger ampIndex = [self rangeOfString:@"&" options:NSLiteralSearch].location;

// Short-circuit if there are no ampersands.
if (ampIndex == NSNotFound) {
return self;
}
// Make result string with some extra capacity.
NSMutableString *result = [NSMutableString stringWithCapacity:(myLength * 1.25)];

// First iteration doesn't need to scan to & since we did that already, but for code simplicity's sake we'll do it again with the scanner.
NSScanner *scanner = [NSScanner scannerWithString:self];
[scanner setCharactersToBeSkipped:nil];

NSCharacterSet *boundaryCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@" \t\n\r;"];

do {
// Scan up to the next entity or the end of the string.
NSString *nonEntityString;
if ([scanner scanUpToString:@"&" intoString:&nonEntityString]) {
[result appendString:nonEntityString];
}
if ([scanner isAtEnd]) {
goto finish;
}
// Scan either a HTML or numeric character entity reference.
if ([scanner scanString:@"&amp;" intoString:NULL])
[result appendString:@"&"];
else if ([scanner scanString:@"&apos;" intoString:NULL])
[result appendString:@"'"];
else if ([scanner scanString:@"&quot;" intoString:NULL])
[result appendString:@"\""];
else if ([scanner scanString:@"&lt;" intoString:NULL])
[result appendString:@"<"];
else if ([scanner scanString:@"&gt;" intoString:NULL])
[result appendString:@">"];
else if ([scanner scanString:@"&#" intoString:NULL]) {
BOOL gotNumber;
unsigned charCode;
NSString *xForHex = @"";

// Is it hex or decimal?
if ([scanner scanString:@"x" intoString:&xForHex]) {
gotNumber = [scanner scanHexInt:&charCode];
}
else {
gotNumber = [scanner scanInt:(int*)&charCode];
}

if (gotNumber) {
[result appendFormat:@"%C", charCode];
[scanner scanString:@";" intoString:NULL];
}
else {
NSString *unknownEntity = @"";
[scanner scanUpToCharactersFromSet:boundaryCharacterSet intoString:&unknownEntity];
[result appendFormat:@"&#%@%@", xForHex, unknownEntity];
NSLog(@"Expected numeric character entity but got &#%@%@;", xForHex, unknownEntity);
}
}
else {
NSString *amp;
[scanner scanString:@"&" intoString:&amp]; //an isolated & symbol
[result appendString:amp];
}
}
while (![scanner isAtEnd]);

finish:
return result;
}

@end
2 changes: 1 addition & 1 deletion Classes/FeedEntryController.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ - (IBAction)showInWebView:(id)sender {

- (IBAction)showRepository:(id)sender {
id item = entry.eventItem;
GHRepository *repository;
GHRepository *repository = nil;
if ([item isKindOfClass:[GHRepository class]]) {
repository = item;
} else if ([item isKindOfClass:[GHIssue class]]) {
Expand Down
18 changes: 13 additions & 5 deletions Classes/GHFeedEntry.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,32 @@ - (id)eventItem {
NSString *owner = [repoComps objectAtIndex:0];
NSString *name = [repoComps objectAtIndex:1];
GHRepository *repository = [GHRepository repositoryWithOwner:owner andName:name];
GHIssue *issue = [[GHIssue alloc] initWithRepository:repository];
GHIssue *issue = [GHIssue issueWithRepository:repository];
issue.num = num;
self.eventItem = issue;
[issue release];
} else if ([eventType isEqualToString:@"pull_request"]) {
NSArray *comps = [title componentsSeparatedByString:@" on "];
// NSArray *reqComps = [[comps objectAtIndex:0] componentsSeparatedByString:@" "];
// NSInteger num = [[issueComps lastObject] intValue];
NSArray *repoComps = [[comps objectAtIndex:1] componentsSeparatedByString:@"/"];
NSString *owner = [repoComps objectAtIndex:0];
NSString *name = [repoComps objectAtIndex:1];
self.eventItem = [GHRepository repositoryWithOwner:owner andName:name];
} else if ([eventType isEqualToString:@"comment"]) {
} else if ([eventType isEqualToString:@"commit_comment"]) {
NSArray *comps1 = [title componentsSeparatedByString:@" on "];
NSArray *comps2 = [[comps1 objectAtIndex:1] componentsSeparatedByString:@"/"];
NSString *owner = [comps2 objectAtIndex:0];
NSString *name = [comps2 objectAtIndex:1];
self.eventItem = [GHRepository repositoryWithOwner:owner andName:name];
} else if ([eventType isEqualToString:@"issue_comment"]) {
NSArray *comps = [title componentsSeparatedByString:@" on "];
NSArray *issueComps = [[comps objectAtIndex:1] componentsSeparatedByString:@" "];
NSInteger num = [[issueComps lastObject] intValue];
NSArray *repoComps = [[comps objectAtIndex:2] componentsSeparatedByString:@"/"];
NSString *owner = [repoComps objectAtIndex:0];
NSString *name = [repoComps objectAtIndex:1];
GHRepository *repository = [GHRepository repositoryWithOwner:owner andName:name];
GHIssue *issue = [GHIssue issueWithRepository:repository];
issue.num = num;
self.eventItem = issue;
} else if ([eventType isEqualToString:@"follow"]) {
NSArray *comps1 = [title componentsSeparatedByString:@" following "];
NSString *username = [comps1 objectAtIndex:1];
Expand Down
9 changes: 7 additions & 2 deletions Classes/GHFeedParserDelegate.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "GHFeedParserDelegate.h"
#import "GHFeedEntry.h"
#import "iOctocat.h"
#import "NSString+Extensions.h"


@implementation GHFeedParserDelegate
Expand Down Expand Up @@ -62,7 +63,9 @@ - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName names
} else if ([event hasPrefix:@"Follow"]) {
currentEntry.eventType = @"follow";
} else if ([event hasPrefix:@"CommitComment"]) {
currentEntry.eventType = @"comment";
currentEntry.eventType = @"commit_comment";
} else if ([event hasPrefix:@"IssueComment"]) {
currentEntry.eventType = @"issue_comment";
} else if ([event hasPrefix:@"Push"]) {
currentEntry.eventType = @"push";
} else if ([event hasPrefix:@"Commit"] || [event hasPrefix:@"Grit::Commit"]) {
Expand All @@ -88,7 +91,9 @@ - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName names
}
} else if ([elementName isEqualToString:@"updated"]) {
currentEntry.date = [iOctocat parseDate:currentElementValue withFormat:kISO8601TimeFormat];
} else if ([elementName isEqualToString:@"title"] || [elementName isEqualToString:@"content"]) {
} else if ([elementName isEqualToString:@"title"]) {
[currentEntry setValue:[currentElementValue stringByDecodingXMLEntities] forKey:elementName];
} else if ([elementName isEqualToString:@"content"]) {
[currentEntry setValue:currentElementValue forKey:elementName];
} else if ([elementName isEqualToString:@"name"]) {
currentEntry.authorName = currentElementValue;
Expand Down
5 changes: 3 additions & 2 deletions Classes/GHRepository.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "GHRepository.h"
#import "GHResource.h"
#import "iOctocat.h"
#import "GHIssues.h"
#import "GHNetworks.h"
Expand Down Expand Up @@ -105,8 +106,8 @@ - (int)compareByName:(GHRepository *)theOtherRepository {
- (void)setValuesFromDict:(NSDictionary *)theDict {
NSDictionary *resource = [theDict objectForKey:@"repository"] ? [theDict objectForKey:@"repository"] : theDict;

self.githubURL = [[resource objectForKey:@"blog"] isKindOfClass:[NSNull class]] ? nil : [NSURL URLWithString:[resource objectForKey:@"url"]];
self.homepageURL = [[resource objectForKey:@"blog"] isKindOfClass:[NSNull class]] ? nil : [NSURL URLWithString:[resource objectForKey:@"homepage"]];
self.githubURL = [NSURL URLWithString:[resource objectForKey:@"url"]];
self.homepageURL = [GHResource smartURLFromString:[resource objectForKey:@"homepage"]];
self.descriptionText = [resource objectForKey:@"description"];
self.isFork = [[resource objectForKey:@"fork"] boolValue];
self.isPrivate = [[resource objectForKey:@"private"] boolValue];
Expand Down
1 change: 1 addition & 0 deletions Classes/GHResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ typedef enum {
+ (ASIFormDataRequest *)authenticatedRequestForURL:(NSURL *)theURL;
+ (GHResource *)at:(NSString *)formatString, ...;
+ (id)resourceWithURL:(NSURL *)theURL;
+ (NSURL *)smartURLFromString:(NSString *)theString;
- (id)initWithURL:(NSURL *)theURL;
- (void)addDelegate:(id)delegate;
- (void)removeDelegate:(id)delegate;
Expand Down
15 changes: 15 additions & 0 deletions Classes/GHResource.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#import "iOctocat.h"
#import "CJSONDeserializer.h"
#import "NSURL+Extensions.h"
#import "NSString+Extensions.h"


@interface GHResource ()
Expand Down Expand Up @@ -35,6 +36,20 @@ + (id)resourceWithURL:(NSURL *)theURL {
return [[[[self class] alloc] initWithURL:theURL] autorelease];
}

+ (NSURL *)smartURLFromString:(NSString *)theString {
if (!theString || [theString isKindOfClass:[NSNull class]] || [theString isEmpty]) {
return nil;
} else {
NSURL *url = [NSURL URLWithString:theString];
if ([url scheme]) {
return url;
} else {
theString = [@"http://" stringByAppendingString:theString];
return [NSURL URLWithString:theString];
}
}
}

- (id)initWithURL:(NSURL *)theURL {
[super init];
self.resourceURL = theURL;
Expand Down
8 changes: 6 additions & 2 deletions Classes/GHUser.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#import "GHFeed.h"
#import "GHRepository.h"
#import "GHRepositories.h"
#import "GHResource.h"
#import "GravatarLoader.h"
#import "ASIFormDataRequest.h"
#import "CJSONDeserializer.h"
Expand Down Expand Up @@ -129,11 +130,14 @@ - (void)setValuesFromDict:(NSDictionary *)theDict {

if (![login isEqualToString:[resource objectForKey:@"login"]]) self.login = [resource objectForKey:@"login"];
self.name = [[resource objectForKey:@"name"] isKindOfClass:[NSNull class]] ? nil : [resource objectForKey:@"name"];
self.email = [[resource objectForKey:@"email"] isKindOfClass:[NSNull class]] ? nil : [resource objectForKey:@"email"];
NSString *mail = [resource objectForKey:@"email"];
if (![mail isKindOfClass:[NSNull class]] && ![mail isEmpty]) {
self.email = [resource objectForKey:@"email"];
}
self.company = [[resource objectForKey:@"company"] isKindOfClass:[NSNull class]] ? nil : [resource objectForKey:@"company"];
self.location = [[resource objectForKey:@"location"] isKindOfClass:[NSNull class]] ? nil : [resource objectForKey:@"location"];
self.gravatarHash = [[resource objectForKey:@"gravatar_id"] isKindOfClass:[NSNull class]] ? nil : [resource objectForKey:@"gravatar_id"];
self.blogURL = [[resource objectForKey:@"blog"] isKindOfClass:[NSNull class]] ? nil : [NSURL URLWithString:[resource objectForKey:@"blog"]];
self.blogURL = [GHResource smartURLFromString:[resource objectForKey:@"blog"]];
self.publicGistCount = [[resource objectForKey:@"public_gist_count"] integerValue];
self.privateGistCount = [[resource objectForKey:@"private_gist_count"] integerValue];
self.publicRepoCount = [[resource objectForKey:@"public_repo_count"] integerValue];
Expand Down
1 change: 1 addition & 0 deletions Classes/NetworksController.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ - (void)viewDidLoad {
}

- (void)dealloc {
[repository.networks removeObserver:self forKeyPath:kResourceLoadingStatusKeyPath];
[loadingNetworksCell release];
[noNetworksCell release];
[networkCell release];
Expand Down
10 changes: 6 additions & 4 deletions Classes/TextCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ - (void)dealloc {

- (void)setContentText:(NSString *)theText {
contentTextLabel.text = theText;
[contentTextLabel sizeToFit];
if (contentTextLabel.frame.size.width > maxWidth) {
contentTextLabel.frame = CGRectMake(contentTextLabel.frame.origin.x, contentTextLabel.frame.origin.y, maxWidth, self.height);
}
CGSize constraint = CGSizeMake(maxWidth, 20000);
CGSize size = [theText sizeWithFont:contentTextLabel.font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
if (size.height < 24) {
size.height = 24;
}
contentTextLabel.frame = CGRectMake(contentTextLabel.frame.origin.x, contentTextLabel.frame.origin.y, maxWidth, size.height);
}

- (CGFloat)height {
Expand Down
10 changes: 8 additions & 2 deletions Classes/UserCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ @implementation UserCell
@synthesize user;

- (void)dealloc {
[user removeObserver:self forKeyPath:kUserGravatarKeyPath];
[user release];
[userLabel release];
[gravatarView release];
[super dealloc];
}

- (void)viewWillAppear: (BOOL)animated {
[user addObserver:self forKeyPath:kUserGravatarKeyPath options:NSKeyValueObservingOptionNew context:nil];
}

- (void)viewWillDisappear: (BOOL)animated {
[user removeObserver:self forKeyPath:kUserGravatarKeyPath];
}

- (void)setUser:(GHUser *)aUser {
[aUser retain];
[user release];
user = aUser;
userLabel.text = user.login;
[user addObserver:self forKeyPath:kUserGravatarKeyPath options:NSKeyValueObservingOptionNew context:nil];
gravatarView.image = user.gravatar;
}

Expand Down
Loading

0 comments on commit cc3e81b

Please sign in to comment.