-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSString-Markdown.m
38 lines (26 loc) · 1.16 KB
/
NSString-Markdown.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#import "NSString-Markdown.h"
@implementation NSString (Markdown)
+ (NSString*)stringWithProcessedMarkdown:(NSString*)inputString
{
NSString* mdScriptPath = [[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Markdown_1.0.1"] stringByAppendingPathComponent:@"Markdown.pl"];
NSTask* task = [[NSTask alloc] init];
NSMutableArray* args = [NSMutableArray array];
[args addObject:mdScriptPath];
[task setArguments:args];
NSPipe* stdinPipe = [NSPipe pipe];
NSPipe* stdoutPipe = [NSPipe pipe];
NSFileHandle* stdinFileHandle = [stdinPipe fileHandleForWriting];
NSFileHandle* stdoutFileHandle = [stdoutPipe fileHandleForReading];
[task setStandardInput:stdinPipe];
[task setStandardOutput:stdoutPipe];
[task setLaunchPath:@"/usr/bin/perl"];
[task launch];
[stdinFileHandle writeData:[inputString dataUsingEncoding:NSUTF8StringEncoding]];
[stdinFileHandle closeFile];
NSData* outputData = [stdoutFileHandle readDataToEndOfFile];
NSString* outputString = [[[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding] autorelease];
[stdoutFileHandle closeFile];
[task waitUntilExit];
return outputString;
}
@end