Posted By

daknok on 06/12/11


markdown Objective-c

Versions (?)

Parsing Markdown with Objective-C using the original Perl script

 / Published in: Objective C

Simply make sure you have (from Daring Fireball) inside of your application bundle's resources folder. Perl has to be installed on the target machine. If you are developing for iOS, you should build Perl for iOS, pack it with the application and change the markdownTask's launch path.

  1. + (NSString *)htmlFromMarkdown:(NSString *)markdown {
  2. NSTask *markdownTask = [[NSTask alloc] init];
  3. [markdownTask setLaunchPath:@"/usr/bin/perl"];
  4. [markdownTask setArguments:[NSArray arrayWithObjects:[[NSBundle mainBundle] pathForResource:@"Markdown" ofType:@"pl"], nil]];
  5. NSPipe *markdownPipe = [NSPipe pipe];
  6. NSPipe *htmlPipe = [NSPipe pipe];
  7. [markdownTask setStandardInput:markdownPipe];
  8. [markdownTask setStandardOutput:htmlPipe];
  9. [markdownTask launch];
  10. NSData *markdownData = [markdown dataUsingEncoding:NSUTF8StringEncoding];
  11. [[markdownPipe fileHandleForWriting] writeData:markdownData];
  12. [[markdownPipe fileHandleForWriting] closeFile];
  13. NSString *output = [[[NSString alloc] initWithData:[[htmlPipe fileHandleForReading] readDataToEndOfFile] encoding:NSUTF8StringEncoding] autorelease];
  14. [markdownTask waitUntilExit];
  15. [markdownTask release];
  16. return output;
  17. }

Report this snippet  

You need to login to post a comment.