All pastes #487955 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-05-14 19:12 UTC
rendered paste body
//
//  ALMarkdownPlugin.m
//  AdiumMarkdown
//
//  Created by Ryan Govostes on 5/14/07.
//  Copyright 2007 Alacatia Labs. All rights reserved.
//

#import "ALMarkdownPlugin.h"

@implementation ALMarkdownPlugin

- (void)installPlugin {
	// Register an incoming content filter
    [[adium contentController] registerContentFilter:self
											  ofType:AIFilterMessageDisplay
										   direction:AIFilterIncoming];
	
	// Register an outgoing content filter
	[[adium contentController] registerContentFilter:self
											  ofType:AIFilterMessageDisplay
										   direction:AIFilterOutgoing];
}

- (void)uninstallPlugin {
	// This unregisters all content filters associated with self
	[[adium contentController] unregisterContentFilter:self];
}

#pragma mark Content Filtering

- (NSAttributedString *)filterAttributedString:(NSAttributedString *)inAttributedString context:(id)context {
	NSMutableAttributedString *outAttributedString;
	outAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:inAttributedString];
	
	// Convert the message to HTML and write it out to a file
	NSString *htmlified = [AIHTMLDecoder encodeHTML:inAttributedString
											headers:NO
										   fontTags:NO /* These settings are pretty arbitrary, experiment */
								 includingColorTags:NO
									  closeFontTags:NO
										  styleTags:YES
						 closeStyleTagsOnFontChange:YES
									 encodeNonASCII:YES
									   encodeSpaces:NO
										 imagesPath:@""
								  attachmentsAsText:NO
						  onlyIncludeOutgoingImages:NO
									 simpleTagsOnly:YES
									 bodyBackground:NO];
	
	NSLog(@"In: %@", htmlified);
	
	[htmlified writeToFile:@"/tmp/AIMsg.text" /* FIXME: This probably shouldn't be hard-coded */
				atomically:NO
				  encoding:NSUTF8StringEncoding
					 error:NULL];
	
	NSLog(@"Markdown path: %@", [NSString stringWithFormat:@"%@/Markdown.pl", [[NSBundle bundleForClass:@"ALMarkdownPlugin"] resourcePath]]);
	
	// Call upon Markdown to process the text
	NSMutableArray *args = [NSMutableArray array];
	[args addObject:[NSString stringWithFormat:@"%@/Markdown.pl", [[NSBundle bundleForClass:@"ALMarkdownPlugin"] resourcePath]]];
	[args addObject:@"-html4tags"];
	[args addObject:@"/tmp/AIMsg.text"];
	
	NSTask *aTask = [[NSTask alloc] init];
	[aTask setLaunchPath:@"/usr/bin/perl"];
	[aTask setArguments:args];
	
	NSPipe *ceciNestPasUnePipe = [NSPipe pipe];
	[aTask setStandardOutput:ceciNestPasUnePipe];
	
	NSLog(@"Starting Markdown...");
	[aTask launch];
	
	// Return the HTML again
	NSString *outHTML = [[NSString alloc] initWithData:[[ceciNestPasUnePipe fileHandleForReading] readDataToEndOfFile]
											  encoding:NSUTF8StringEncoding];
	
	NSLog(@"Out: %@", outHTML);
	
	NSLog(@"Markdown finished.");
	[aTask release];
	
	return outHTML;
}

- (float)filterPriority {
	return DEFAULT_FILTER_PRIORITY;
}

#pragma mark About This Plugin

- (NSString *)pluginAuthor {
	return @"Ryan Govostes";
}

- (NSString *)pluginVersion {
	return @"1.0";
}

- (NSString *)pluginDescription {
	return @"Interprets Markdown syntax in outgoing and incoming messages.";
}

- (NSString *)pluginURL {
	// TODO: Replace this with a page about the plugin
	return @"http://daringfireball.net/projects/markdown/";
}

@end