forked from ttscoff/nv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSString_MultiMarkdown.m
executable file
·150 lines (114 loc) · 5.49 KB
/
NSString_MultiMarkdown.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// NSString_MultiMarkdown.m
// Notation
//
// Created by Christian Tietze on 2010-10-10.
//
#import "NSString_MultiMarkdown.h"
#import "PreviewController.h"
#import "AppController.h"
#import "NoteObject.h"
@implementation NSString (MultiMarkdown)
/**
* Locating a MultiMarkdown parsing script. The options are as follows:
* 1. ~/Library/Application Support/MultiMarkdown/bin/mmd2ZettelXHTML.pl
* 2. ~/Library/Application Support/MultiMarkdown/bin/mmd2XHTML.pl
* 3. <Application>/MultiMarkdown/bin/mmd2ZettelXHTML.pl
*
* The third option should be a safe fallback since the appropriate MMD bundle
* is included and shipped with this application.
*/
+(NSString*)mmdDirectory {
// fallback path in this program's directiory
NSString *bundlePath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"multimarkdown"];
return bundlePath;
} // mmdDirectory
+(NSString*)tp2mdDirectory {
NSString *bundlePath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"tp2md.rb"];
return bundlePath;
}
+(NSString*)processTaskPaper:(NSString*)inputString
{
if (inputString)
{
NSString *taskPaperScriptPath = @"/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby";
NSString *argPath = [[NSBundle mainBundle] pathForResource:@"tp2md"
ofType:@"rb"
inDirectory:NULL];
NSTask *taskPaperTask = [[[NSTask alloc] init] autorelease];
NSPipe *stdinPipe = [NSPipe pipe];
NSPipe *stdoutPipe = [NSPipe pipe];
NSFileHandle *stdinFileHandle = [stdinPipe fileHandleForWriting];
NSFileHandle *stdoutFileHandle = [stdoutPipe fileHandleForReading];
[taskPaperTask setStandardInput:stdinPipe];
[taskPaperTask setStandardOutput:stdoutPipe];
[taskPaperTask setLaunchPath:taskPaperScriptPath];
NSArray *argArray = [NSArray arrayWithObject:argPath];
[taskPaperTask setArguments:argArray];
[taskPaperTask launch];
NSString *blech = [NSString stringWithUTF8String:[inputString UTF8String]];
[stdinFileHandle writeData:[blech dataUsingEncoding:NSUTF8StringEncoding]];
[stdinFileHandle closeFile];
NSData *outputData = [stdoutFileHandle readDataToEndOfFile];
inputString = [[[NSString alloc] initWithData:outputData
encoding:NSUTF8StringEncoding] autorelease];
[stdoutFileHandle closeFile];
[taskPaperTask waitUntilExit];
[taskPaperTask terminate];
}
return inputString;
}
+(NSString*)processMultiMarkdown:(NSString*)inputString
{
NSRange archiveFoundRange = [inputString rangeOfString:@"Archive:"];
NSRange tagFoundRange = [inputString rangeOfString:@"@taskpaper"];
if (archiveFoundRange.location != NSNotFound || tagFoundRange.location != NSNotFound) {
inputString = [self processTaskPaper:inputString];
}
NSString* mdScriptPath = [[self class] mmdDirectory];
// NSString* tpScriptPath = [[self class] tp2mdDirectory];
NSTask* task = [[[NSTask alloc] init] autorelease];
NSMutableArray* args = [NSMutableArray array];
[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: [mdScriptPath stringByExpandingTildeInPath]];
[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;
}
+(NSString*)documentWithProcessedMultiMarkdown:(NSString*)inputString
{
AppController *app = [[NSApplication sharedApplication] delegate];
NSString *processedString = [self processMultiMarkdown:inputString];
NSString *htmlString = [[PreviewController class] html];
NSString *cssString = [[PreviewController class] css];
NSMutableString *outputString = [NSMutableString stringWithString:(NSString *)htmlString];
NSString *noteTitle = ([app selectedNoteObject]) ? [NSString stringWithFormat:@"%@",titleOfNote([app selectedNoteObject])] : @"";
NSString *nvSupportPath = [[NSFileManager defaultManager] applicationSupportDirectory];
[outputString replaceOccurrencesOfString:@"{%support%}" withString:nvSupportPath options:0 range:NSMakeRange(0, [outputString length])];
[outputString replaceOccurrencesOfString:@"{%title%}" withString:noteTitle options:0 range:NSMakeRange(0, [outputString length])];
[outputString replaceOccurrencesOfString:@"{%content%}" withString:processedString options:0 range:NSMakeRange(0, [outputString length])];
[outputString replaceOccurrencesOfString:@"{%style%}" withString:cssString options:0 range:NSMakeRange(0, [outputString length])];
return outputString;
}
+(NSString*)xhtmlWithProcessedMultiMarkdown:(NSString*)inputString
{
return [self processMultiMarkdown:inputString];
}
+(NSString*)stringWithProcessedMultiMarkdown:(NSString*)inputString
{
return [self processMultiMarkdown:inputString];
} // stringWithProcessedMultiMarkdown:
@end // NSString (MultiMarkdown)