From 6080caa391c0626664638252a0dcdf3a902c40b2 Mon Sep 17 00:00:00 2001 From: Nicolas A Perez Date: Thu, 10 Mar 2016 14:45:56 -0500 Subject: [PATCH 1/3] we can get code blocks from the text. --- macdown-gistit-tests/Info.plist | 24 ++++++++++++++++ macdown-gistit/MacDownGistItController.m | 35 ++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 macdown-gistit-tests/Info.plist diff --git a/macdown-gistit-tests/Info.plist b/macdown-gistit-tests/Info.plist new file mode 100644 index 0000000..ba72822 --- /dev/null +++ b/macdown-gistit-tests/Info.plist @@ -0,0 +1,24 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + BNDL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + + diff --git a/macdown-gistit/MacDownGistItController.m b/macdown-gistit/MacDownGistItController.m index 336cfaa..acd4f9e 100644 --- a/macdown-gistit/MacDownGistItController.m +++ b/macdown-gistit/MacDownGistItController.m @@ -33,6 +33,35 @@ - (BOOL)run:(id)sender return [self gistify:dc.currentDocument]; } + +- (NSArray *) blocksFrom: (NSString *) text { + + NSString * str = text; + + NSMutableArray * result = [NSMutableArray array]; + + while (true) { + NSRange range = [str rangeOfString: @"```"]; + + if (range.length > 0) { + str = [NSString stringWithString: [str substringFromIndex: range.location + range.length]]; + + NSRange codeRange = [str rangeOfString: @"```"]; + + NSString * code = [str substringToIndex:codeRange.location]; + + [result addObject: code]; + + str = [NSString stringWithString: [str substringFromIndex: codeRange.location + codeRange.length]]; + } + else break; + } + + return result; + +} + + - (BOOL)gistify:(NSDocument *)document { id markdownSource = (id)document; @@ -43,6 +72,12 @@ - (BOOL)gistify:(NSDocument *)document if (!fileName.length) fileName = @"Untitled"; + + + markdown = [[self blocksFrom: markdown] objectAtIndex: 0]; + + + NSURL * url = [NSURL URLWithString:MacDownGistListLink]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url From ebf050b5e1fae68ab20b1f01184da6e13845519f Mon Sep 17 00:00:00 2001 From: Nicolas A Perez Date: Thu, 10 Mar 2016 15:13:44 -0500 Subject: [PATCH 2/3] All code blocks are created as gists independently. --- macdown-gistit/MacDownGistItController.m | 128 ++++++++++------------- 1 file changed, 57 insertions(+), 71 deletions(-) diff --git a/macdown-gistit/MacDownGistItController.m b/macdown-gistit/MacDownGistItController.m index acd4f9e..f00b60b 100644 --- a/macdown-gistit/MacDownGistItController.m +++ b/macdown-gistit/MacDownGistItController.m @@ -37,7 +37,6 @@ - (BOOL)run:(id)sender - (NSArray *) blocksFrom: (NSString *) text { NSString * str = text; - NSMutableArray * result = [NSMutableArray array]; while (true) { @@ -62,7 +61,7 @@ - (NSArray *) blocksFrom: (NSString *) text { } -- (BOOL)gistify:(NSDocument *)document +- (BOOL) gistify:(NSDocument *)document { id markdownSource = (id)document; NSString *markdown = markdownSource.markdown; @@ -74,82 +73,69 @@ - (BOOL)gistify:(NSDocument *)document - markdown = [[self blocksFrom: markdown] objectAtIndex: 0]; - + NSArray * blocks = [self blocksFrom: markdown]; + __block NSString * result = @"Your Gists urls:\n"; + + __block int m = 0; - NSURL * url = [NSURL URLWithString:MacDownGistListLink]; - NSMutableURLRequest *req = + for (NSString * block in blocks) { + + NSURL * url = [NSURL URLWithString:MacDownGistListLink]; + NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:0.0]; - [req addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; - [req addValue:@"application/json" forHTTPHeaderField:@"Accept"]; - - NSDictionary *object = @{ - @"description": @"Uploaded by MacDown. http://macdown.uranusjr.com", - @"public": @YES, - @"files": @{fileName: @{@"content": markdown}}, - }; - NSData *data = [NSJSONSerialization dataWithJSONObject:object - options:0 error:NULL]; - if (!data) - return NO; - - req.HTTPMethod = @"POST"; - req.HTTPBody = data; - - NSURLSessionConfiguration *conf = + [req addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; + [req addValue:@"application/json" forHTTPHeaderField:@"Accept"]; + + NSDictionary *object = @{ + @"description": @"Uploaded by MacDown. http://macdown.uranusjr.com", + @"public": @YES, + @"files": @{fileName: @{@"content": block}}, + }; + NSData *data = [NSJSONSerialization dataWithJSONObject:object + options:0 error:NULL]; + + if (!data) + return NO; + + req.HTTPMethod = @"POST"; + req.HTTPBody = data; + + NSURLSessionConfiguration *conf = [NSURLSessionConfiguration defaultSessionConfiguration]; - NSURLSession *session = [NSURLSession sessionWithConfiguration:conf]; - NSURLSessionTask *task = [session dataTaskWithRequest:req - completionHandler:^( - NSData *data, NSURLResponse *res, NSError *error) { - - NSHTTPURLResponse *r = (id)res; - NSString *json = data ? - [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : - nil; - NSDictionary *object = data ? - [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL] : - nil; - NSString *urlstring = object[@"html_url"]; - - NSAlert *alert = [[NSAlert alloc] init]; - if (error) - { - alert = [NSAlert alertWithError:error]; - } - else if (![res respondsToSelector:@selector(statusCode)]) - { - alert.alertStyle = NSWarningAlertStyle; - alert.messageText = @"Unknown error"; - } - else if (r.statusCode != 201 || !urlstring) - { - alert.alertStyle = NSWarningAlertStyle; - NSString *f = @"Unexpection return code %ld"; - alert.messageText = [NSString stringWithFormat:f, r.statusCode]; - if (json) - alert.informativeText = json; - } - - alert.alertStyle = NSInformationalAlertStyle; - alert.messageText = @"Gist created"; - alert.informativeText = [NSString stringWithFormat: - @"You gist is at %@\nThe URL has been copied into your clipboard.", - urlstring]; - - NSPasteboard *pb = [NSPasteboard generalPasteboard]; - [pb clearContents]; - [pb writeObjects:@[urlstring]]; - - dispatch_async(dispatch_get_main_queue(), ^{ - [alert runModal]; - }); - }]; - [task resume]; + NSURLSession *session = [NSURLSession sessionWithConfiguration:conf]; + NSURLSessionTask *task = [session dataTaskWithRequest:req + completionHandler:^( + NSData *data, NSURLResponse *res, NSError *error) { + + NSHTTPURLResponse *r = (id)res; + NSString *json = data ? + [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : + nil; + NSDictionary *object = data ? + [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL] : + nil; + NSString *urlstring = object[@"html_url"]; + + + result = [result stringByAppendingString: [@"\n" stringByAppendingString: urlstring]]; + m = m + 1; + }]; + [task resume]; + } + + while (m < blocks.count) {} + + NSAlert *alert = [[NSAlert alloc] init]; + alert.messageText = result; + + dispatch_async(dispatch_get_main_queue(), ^{ + [alert runModal]; + }); + return YES; } From 6672c34485c4b7c621cf3d6e11d3f5f7585edff9 Mon Sep 17 00:00:00 2001 From: Nicolas A Perez Date: Thu, 10 Mar 2016 17:13:43 -0500 Subject: [PATCH 3/3] now gists are created with the proper extension :) --- macdown-gistit/MacDownGistItController.m | 42 +++++++++++++++++++----- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/macdown-gistit/MacDownGistItController.m b/macdown-gistit/MacDownGistItController.m index f00b60b..6e97690 100644 --- a/macdown-gistit/MacDownGistItController.m +++ b/macdown-gistit/MacDownGistItController.m @@ -9,6 +9,18 @@ #import #import "MacDownGistItController.h" +@interface Gist : NSObject { + @public NSString * text; + @public NSString * ext; + +} + +@end + +@implementation Gist + + +@end static NSString * const MacDownGistListLink = @"https://api.github.com/gists"; @@ -45,11 +57,23 @@ - (NSArray *) blocksFrom: (NSString *) text { if (range.length > 0) { str = [NSString stringWithString: [str substringFromIndex: range.location + range.length]]; + NSRange extRange = [str rangeOfString: @"\n"]; + + NSString * ext = [str substringToIndex: extRange.location]; + + str = [str substringFromIndex: extRange.location + extRange.length]; + NSRange codeRange = [str rangeOfString: @"```"]; NSString * code = [str substringToIndex:codeRange.location]; - [result addObject: code]; + + Gist * gist = [[Gist alloc] init]; + + gist->text = code; + gist->ext = ext; + + [result addObject: gist]; str = [NSString stringWithString: [str substringFromIndex: codeRange.location + codeRange.length]]; } @@ -79,7 +103,7 @@ - (BOOL) gistify:(NSDocument *)document __block int m = 0; - for (NSString * block in blocks) { + for (Gist * block in blocks) { NSURL * url = [NSURL URLWithString:MacDownGistListLink]; NSMutableURLRequest *req = @@ -89,10 +113,12 @@ - (BOOL) gistify:(NSDocument *)document [req addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [req addValue:@"application/json" forHTTPHeaderField:@"Accept"]; + NSString * fName = [fileName stringByAppendingString: [@"." stringByAppendingString: block->ext]]; + NSDictionary *object = @{ @"description": @"Uploaded by MacDown. http://macdown.uranusjr.com", @"public": @YES, - @"files": @{fileName: @{@"content": block}}, + @"files": @{fName: @{@"content": block->text}}, }; NSData *data = [NSJSONSerialization dataWithJSONObject:object options:0 error:NULL]; @@ -109,11 +135,11 @@ - (BOOL) gistify:(NSDocument *)document NSURLSessionTask *task = [session dataTaskWithRequest:req completionHandler:^( NSData *data, NSURLResponse *res, NSError *error) { - - NSHTTPURLResponse *r = (id)res; - NSString *json = data ? - [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : - nil; +// +// NSHTTPURLResponse *r = (id)res; +// NSString *json = data ? +// [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : +// nil; NSDictionary *object = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL] : nil;