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..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";
@@ -33,7 +45,47 @@ - (BOOL)run:(id)sender
return [self gistify:dc.currentDocument];
}
-- (BOOL)gistify:(NSDocument *)document
+
+- (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 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];
+
+
+ Gist * gist = [[Gist alloc] init];
+
+ gist->text = code;
+ gist->ext = ext;
+
+ [result addObject: gist];
+
+ str = [NSString stringWithString: [str substringFromIndex: codeRange.location + codeRange.length]];
+ }
+ else break;
+ }
+
+ return result;
+
+}
+
+
+- (BOOL) gistify:(NSDocument *)document
{
id markdownSource = (id)document;
NSString *markdown = markdownSource.markdown;
@@ -43,78 +95,73 @@ - (BOOL)gistify:(NSDocument *)document
if (!fileName.length)
fileName = @"Untitled";
- NSURL * url = [NSURL URLWithString:MacDownGistListLink];
- NSMutableURLRequest *req =
+
+
+ NSArray * blocks = [self blocksFrom: markdown];
+
+ __block NSString * result = @"Your Gists urls:\n";
+
+ __block int m = 0;
+
+ for (Gist * 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"];
+
+ NSString * fName = [fileName stringByAppendingString: [@"." stringByAppendingString: block->ext]];
+
+ NSDictionary *object = @{
+ @"description": @"Uploaded by MacDown. http://macdown.uranusjr.com",
+ @"public": @YES,
+ @"files": @{fName: @{@"content": block->text}},
+ };
+ 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;
}