Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: c79f6cca67
Fetching contributors…

Cannot retrieve contributors at this time

executable file 136 lines (105 sloc) 4.995 kb
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
//
// 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
{
NSString* mdScriptPath = [[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*)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)
Something went wrong with that request. Please try again.