forked from suminb/readown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Markdown.m
85 lines (66 loc) · 1.53 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// Markdown.m
// Readown
//
// Created by Hong, MinHee on 08. 04. 12.
//
#import "Markdown.h"
#import <Cocoa/Cocoa.h>
NSString *__markdown_run_php_script(NSString *text, NSString* bundleName)
{
NSTask *task = [[[NSTask alloc] init] autorelease];
NSPipe *inPipe = [NSPipe pipe], *outPipe = [NSPipe pipe];
NSFileHandle *inHandle = [inPipe fileHandleForWriting],
*outHandle = [outPipe fileHandleForReading];
NSData *outData = nil;
[task setLaunchPath:@"/usr/bin/env"];
[task setArguments:
[NSArray arrayWithObjects:
@"php",
[[NSBundle mainBundle] pathForResource:bundleName ofType:@"php"],
nil
]
];
[task setStandardInput:inPipe];
[task setStandardOutput:outPipe];
[task setStandardError:outPipe];
[task launch];
[inHandle writeData:[text dataUsingEncoding:NSUTF8StringEncoding]];
[inHandle closeFile];
outData = [outHandle readDataToEndOfFile];
[outHandle closeFile];
[task waitUntilExit];
if(outData) {
text = [
[[NSString alloc]
initWithData:outData
encoding:NSUTF8StringEncoding
] autorelease
];
if(text)
return text;
}
return @"";
}
NSString *Markdown(NSString *text)
{
return __markdown_run_php_script(text, @"Markdown");
}
NSString *SmartyPants(NSString *text)
{
return __markdown_run_php_script(text, @"SmartyPants");
}
@implementation NSString (Markdown)
- (NSString*)stringWithMarkdown
{
return Markdown(self);
}
- (NSString*)stringWithSmartyPants
{
return SmartyPants(self);
}
- (NSString*)stringWithMarkdownAndSmartyPants
{
return SmartyPants(Markdown(self));
}
@end