@@ -42,7 +42,7 @@ - (void)windowControllerDidLoadNib:(NSWindowController *)aController
4242 return ;
4343 }
4444
45- [self .textView setString: markdown];
45+ [self .textView setString: [ self imagesToDiagramBlocks: markdown] ];
4646 [self textViewContentToWebView ];
4747}
4848
@@ -51,9 +51,60 @@ + (BOOL)autosavesInPlace
5151 return YES ;
5252}
5353
54+ - (NSString *)imagesToDiagramBlocks : (NSString *)text
55+ {
56+ NSMutableString *markdown = [NSMutableString stringWithString: text];
57+
58+ NSRegularExpression *re;
59+ NSError *error;
60+ NSString *template;
61+
62+ // comment in diagram block
63+ re = [NSRegularExpression regularExpressionWithPattern: @" ^<!--\n ((blockdiag|seqdiag|actdiag|nwdiag|rackdiag|)(\\ s|)\\ {.*?^\\ })\n -->" options: NSRegularExpressionDotMatchesLineSeparators|NSRegularExpressionAnchorsMatchLines error: &error];
64+ template = @" $1" ;
65+ [re replaceMatchesInString: markdown options: 0 range: NSMakeRange (0 , markdown.length) withTemplate: template];
66+
67+ re = [NSRegularExpression regularExpressionWithPattern: @" \n ^!\\ [image]\\ (diagrams.*?$" options: NSRegularExpressionDotMatchesLineSeparators|NSRegularExpressionAnchorsMatchLines error: &error];
68+ template = @" " ;
69+ [re replaceMatchesInString: markdown options: 0 range: NSMakeRange (0 , markdown.length) withTemplate: template];
70+
71+ return (NSString *)markdown;
72+ }
73+
74+ - (NSString *)diagramBlocksToImages : (NSString *)text
75+ {
76+ NSMutableString *markdown = [NSMutableString stringWithString: text];
77+
78+ // comment out diagram block and add image syntax
79+ NSError *error;
80+ NSRegularExpression *re;
81+ NSString *template;
82+ re = [NSRegularExpression regularExpressionWithPattern: @" ^(blockdiag|seqdiag|actdiag|nwdiag|rackdiag|)(\\ s|)\\ {.*?^\\ }" options: NSRegularExpressionDotMatchesLineSeparators|NSRegularExpressionAnchorsMatchLines error: &error];
83+ template = @" <!--\n $0\n -->\n " ;
84+ [re replaceMatchesInString: markdown options: 0 range: NSMakeRange (0 , markdown.length) withTemplate: template];
85+
86+
87+ // replace diagram filename number
88+ re = [NSRegularExpression regularExpressionWithPattern: @" diagrams\\ /(0000).svg" options: NSRegularExpressionDotMatchesLineSeparators|NSRegularExpressionAnchorsMatchLines error: &error];
89+ NSArray *matches;
90+ int count = 0 ;
91+ while ([matches = [re matchesInString: markdown options: 0 range: NSMakeRange (0 , markdown.length)] count ] > 0 ) {
92+ NSTextCheckingResult *match = matches[0 ];
93+
94+ count = count + 1 ;
95+ template = [NSString stringWithFormat: @" %04d " , count];
96+
97+ [markdown replaceCharactersInRange: [match rangeAtIndex: 1 ] withString: template];
98+ NSLog (@" %@ " , markdown);
99+ }
100+
101+ return (NSString *)markdown;
102+ }
103+
54104- (NSData *)dataOfType : (NSString *)typeName error : (NSError **)outError
55105{
56- NSString *markdown = [self .textView string ];
106+ NSString *markdown = [self diagramBlocksToImages: [self .textView string ]];
107+
57108 NSData *data = [markdown dataUsingEncoding: NSUTF8StringEncoding];
58109 return data;
59110}
@@ -81,13 +132,22 @@ -(void)textDidChange:(NSNotification *)notification
81132-(void )textViewContentToWebView
82133{
83134 NSString *markDown = [self .textView.textStorage string ];
135+ NSURL *currentDirectory;
136+ if (self.fileURL ) {
137+ currentDirectory = [self .fileURL URLByDeletingLastPathComponent ];
138+ } else {
139+ currentDirectory = [NSURL URLWithString: NSTemporaryDirectory ()];
140+ }
84141
85142 if ([markDown countOfString: @" {" ] == [markDown countOfString: @" }" ]) {
86143 // loop for parts of diag
87144 NSError *error;
88145 NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern: @" ^(blockdiag|seqdiag|actdiag|nwdiag|rackdiag|)(\\ s|)\\ {.*?^\\ }" options: NSRegularExpressionDotMatchesLineSeparators|NSRegularExpressionAnchorsMatchLines error: &error];
89146 NSArray *matches;
90- while ([matches = [re matchesInString: markDown options: 0 range: NSMakeRange (0 , markDown.length)] count ] > 0 ) {
147+ int count = 0 ;
148+ while ([matches = [re matchesInString: markDown options: 0 range: NSMakeRange (0 , markDown.length)]
149+ count ] > 0 ) {
150+
91151 NSTextCheckingResult *match = matches[0 ];
92152
93153 NSString *diag = [markDown substringWithRange: match.range];
@@ -104,10 +164,17 @@ -(void)textViewContentToWebView
104164 }
105165 command = [LDUtils pathTo: command];
106166
167+ NSString *directory = [NSString stringWithFormat: @" %@ /diagrams" , currentDirectory.path];
168+
169+ NSFileManager *fileManager = [NSFileManager defaultManager ];
170+ if (![fileManager fileExistsAtPath: directory]) {
171+ [fileManager createDirectoryAtPath: directory withIntermediateDirectories: YES attributes: nil error: NULL ];
172+ }
107173 // don't want to use image cache, so create filename by arc4random
108- NSString *outPath = [NSString stringWithFormat: @" %@ %u .png" , NSTemporaryDirectory (), arc4random ()];
174+ count = count + 1 ;
175+ NSString *outPath = [NSString stringWithFormat: @" %@ /%04d .svg" , directory, count];
109176
110- [echo setArguments: @[@" -c" , [NSString stringWithFormat: @" echo \" %@ \" | %@ --size=2048x2048 -o %@ /dev/stdin" , diag, command, outPath]]];
177+ [echo setArguments: @[@" -c" , [NSString stringWithFormat: @" echo \" %@ \" | %@ -Tsvg -o %@ /dev/stdin" , diag, command, outPath]]];
111178
112179 [echo launch ];
113180
@@ -128,12 +195,16 @@ -(void)textViewContentToWebView
128195 };
129196 };
130197 }
131-
198+
199+ NSString *externalFilePath = [NSURL fileURLWithPath: [[NSBundle mainBundle ] resourcePath ]];
132200 NSString *html = markDown.flavoredHTMLStringFromMarkdown ;
133- html = [NSString stringWithFormat: NSLocalizedString(@" %@%@ base.html" , nil ), html];
201+ html = [NSString stringWithFormat: NSLocalizedString(@" %@%@%@ base.html" , nil ),
202+ externalFilePath,
203+ externalFilePath,
204+ html];
134205
135206 // at this time, <img> has no 'src' attribute
136- [[self .webView mainFrame ] loadHTMLString: html baseURL: [ NSURL fileURLWithPath: [[ NSBundle mainBundle ] resourcePath ]] ];
207+ [[self .webView mainFrame ] loadHTMLString: html baseURL: currentDirectory ];
137208}
138209
139210@end
0 commit comments