Parsing Markdown with Objective-C using the original Perl script


/ Published in: Objective C
Save to your folder(s)

Simply make sure you have `Markdown.pl` (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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.