Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest implementation of Markdown for a Cocoa application?

Tags:

I'm writing a Cocoa application in Objective-C, and I would like to be able to incorporate Markdown. The user will enter text in Markdown syntax, click an "export" button, and the program will output a file of XHTML.

It seems like there are a lot of options, though. I could use one of the C/C++ implementations, I could run the Perl script as a resource to my Cocoa app, I assume could use the Python implementation and the PyObjC bridge or the Perl implementation and the CamelBones or PerlObjC bridges. What would be the simplest and easiest solution? I'm not doing anything complicated like a real-time rendered preview that would require threading.

like image 697
Ellie P. Avatar asked Sep 16 '09 20:09

Ellie P.


2 Answers

I had a look at the various options, and in the end found libsoldout, a very small C implementation that's quite easy to integrate. You just need to include array.[ch], buffer.[ch], markdown.[ch], and renderers.[ch] in your Xcode project, then you can convert an NSString from markdown to HTML like so:

NSString *rawMarkdown; const char * prose = [rawMarkdown UTF8String];   struct buf *ib, *ob;         int length = rawMarkdown.length + 1;  ib = bufnew(length); bufgrow(ib, length); memcpy(ib->data, prose, length); ib->size = length;  ob = bufnew(64); markdown(ob, ib, &mkd_xhtml);  NSString *shinyNewHTML = [NSString stringWithUTF8String: ob->data]; NSLog(@"%@", shinyNewHTML);  bufrelease(ib); bufrelease(ob); 
like image 103
Michael Avatar answered Sep 18 '22 15:09

Michael


I just used the Sundown implementation which includes SmartyPants support, in an iPad app with great success. Took about 15 minutes to build a test app.

Assume you have a UITextView *textView (which you setDelegate:self) and also a UIWebView *webView in which to display the results:

- (void) textViewDidEndEditing:(UITextView *)textView {     NSString *rawMarkdown = [textView text];     const char * prose = [rawMarkdown UTF8String];       struct buf *ib, *ob;             int length = [rawMarkdown lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;      ib = bufnew(length);     bufgrow(ib, length);     memcpy(ib->data, prose, length);     ib->size = length;      ob = bufnew(64);      struct sd_callbacks callbacks;     struct html_renderopt options;     struct sd_markdown *markdown;       sdhtml_renderer(&callbacks, &options, 0);     markdown = sd_markdown_new(0, 16, &callbacks, &options);      sd_markdown_render(ob, ib->data, ib->size, markdown);     sd_markdown_free(markdown);       NSString *shinyNewHTML = [NSString stringWithUTF8String: ob->data];     [webView loadHTMLString:shinyNewHTML baseURL:[[NSURL alloc] initWithString:@""]];      bufrelease(ib);     bufrelease(ob); } 
like image 24
DreamTimeStudioZ Avatar answered Sep 21 '22 15:09

DreamTimeStudioZ