Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcher committed Oct 12, 2011
1 parent 959f87b commit ac31a54
Show file tree
Hide file tree
Showing 8 changed files with 427 additions and 37 deletions.
41 changes: 31 additions & 10 deletions MultiMarkdown QuickLook.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
5A7859D61443BDBB00C7C824 /* CoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A7859D51443BDBB00C7C824 /* CoreServices.framework */; };
5A7859D81443BDBB00C7C824 /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A7859D71443BDBB00C7C824 /* CoreFoundation.framework */; };
5A7859DE1443BDBB00C7C824 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 5A7859DC1443BDBB00C7C824 /* InfoPlist.strings */; };
5A7859E11443BDBB00C7C824 /* GenerateThumbnailForURL.c in Sources */ = {isa = PBXBuildFile; fileRef = 5A7859E01443BDBB00C7C824 /* GenerateThumbnailForURL.c */; };
5A7859E31443BDBB00C7C824 /* GeneratePreviewForURL.c in Sources */ = {isa = PBXBuildFile; fileRef = 5A7859E21443BDBB00C7C824 /* GeneratePreviewForURL.c */; };
5A7859E51443BDBB00C7C824 /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = 5A7859E41443BDBB00C7C824 /* main.c */; };
5A7859F31443C1A000C7C824 /* GeneratePreviewForURL.m in Sources */ = {isa = PBXBuildFile; fileRef = 5A7859F11443C1A000C7C824 /* GeneratePreviewForURL.m */; };
5A7859F41443C1A000C7C824 /* GenerateThumbnailForURL.m in Sources */ = {isa = PBXBuildFile; fileRef = 5A7859F21443C1A000C7C824 /* GenerateThumbnailForURL.m */; };
5A7859FC1443C54800C7C824 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A7859FB1443C54800C7C824 /* Cocoa.framework */; };
5A7859FF1443C96D00C7C824 /* multimarkdown in Resources */ = {isa = PBXBuildFile; fileRef = 5A7859FE1443C96D00C7C824 /* multimarkdown */; };
5A785A0A1443CC1A00C7C824 /* opml2mmd.xslt in Resources */ = {isa = PBXBuildFile; fileRef = 5A785A091443CC1A00C7C824 /* opml2mmd.xslt */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -23,19 +26,23 @@
5A7859D31443BDBB00C7C824 /* ApplicationServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ApplicationServices.framework; path = System/Library/Frameworks/ApplicationServices.framework; sourceTree = SDKROOT; };
5A7859D51443BDBB00C7C824 /* CoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreServices.framework; path = System/Library/Frameworks/CoreServices.framework; sourceTree = SDKROOT; };
5A7859D71443BDBB00C7C824 /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = System/Library/Frameworks/CoreFoundation.framework; sourceTree = SDKROOT; };
5A7859DB1443BDBB00C7C824 /* MultiMarkdown QuickLook-Info.plist */ = {isa = PBXFileReference; path = "MultiMarkdown QuickLook-Info.plist"; sourceTree = "<group>"; };
5A7859DB1443BDBB00C7C824 /* MultiMarkdown QuickLook-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "MultiMarkdown QuickLook-Info.plist"; sourceTree = "<group>"; };
5A7859DD1443BDBB00C7C824 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
5A7859DF1443BDBB00C7C824 /* MultiMarkdown QuickLook-Prefix.pch */ = {isa = PBXFileReference; path = "MultiMarkdown QuickLook-Prefix.pch"; sourceTree = "<group>"; };
5A7859E01443BDBB00C7C824 /* GenerateThumbnailForURL.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = GenerateThumbnailForURL.c; sourceTree = "<group>"; };
5A7859E21443BDBB00C7C824 /* GeneratePreviewForURL.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = GeneratePreviewForURL.c; sourceTree = "<group>"; };
5A7859DF1443BDBB00C7C824 /* MultiMarkdown QuickLook-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "MultiMarkdown QuickLook-Prefix.pch"; sourceTree = "<group>"; };
5A7859E41443BDBB00C7C824 /* main.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
5A7859F11443C1A000C7C824 /* GeneratePreviewForURL.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratePreviewForURL.m; sourceTree = "<group>"; };
5A7859F21443C1A000C7C824 /* GenerateThumbnailForURL.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GenerateThumbnailForURL.m; sourceTree = "<group>"; };
5A7859FB1443C54800C7C824 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
5A7859FE1443C96D00C7C824 /* multimarkdown */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; name = multimarkdown; path = "MultiMarkdown QuickLook/multimarkdown"; sourceTree = "<group>"; };
5A785A091443CC1A00C7C824 /* opml2mmd.xslt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; name = opml2mmd.xslt; path = "MultiMarkdown QuickLook/opml2mmd.xslt"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
5A7859C91443BDBB00C7C824 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
5A7859FC1443C54800C7C824 /* Cocoa.framework in Frameworks */,
5A7859D21443BDBB00C7C824 /* QuickLook.framework in Frameworks */,
5A7859D41443BDBB00C7C824 /* ApplicationServices.framework in Frameworks */,
5A7859D61443BDBB00C7C824 /* CoreServices.framework in Frameworks */,
Expand All @@ -49,8 +56,10 @@
5A7859C11443BDBB00C7C824 = {
isa = PBXGroup;
children = (
5A7859FB1443C54800C7C824 /* Cocoa.framework */,
5A7859D91443BDBB00C7C824 /* MultiMarkdown QuickLook */,
5A7859D01443BDBB00C7C824 /* Frameworks */,
5A785A001443C97300C7C824 /* Resources */,
5A7859CF1443BDBB00C7C824 /* Products */,
);
sourceTree = "<group>";
Expand All @@ -77,8 +86,8 @@
5A7859D91443BDBB00C7C824 /* MultiMarkdown QuickLook */ = {
isa = PBXGroup;
children = (
5A7859E01443BDBB00C7C824 /* GenerateThumbnailForURL.c */,
5A7859E21443BDBB00C7C824 /* GeneratePreviewForURL.c */,
5A7859F11443C1A000C7C824 /* GeneratePreviewForURL.m */,
5A7859F21443C1A000C7C824 /* GenerateThumbnailForURL.m */,
5A7859E41443BDBB00C7C824 /* main.c */,
5A7859DA1443BDBB00C7C824 /* Supporting Files */,
);
Expand All @@ -95,6 +104,15 @@
name = "Supporting Files";
sourceTree = "<group>";
};
5A785A001443C97300C7C824 /* Resources */ = {
isa = PBXGroup;
children = (
5A7859FE1443C96D00C7C824 /* multimarkdown */,
5A785A091443CC1A00C7C824 /* opml2mmd.xslt */,
);
name = Resources;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -155,6 +173,8 @@
buildActionMask = 2147483647;
files = (
5A7859DE1443BDBB00C7C824 /* InfoPlist.strings in Resources */,
5A7859FF1443C96D00C7C824 /* multimarkdown in Resources */,
5A785A0A1443CC1A00C7C824 /* opml2mmd.xslt in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -175,9 +195,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5A7859E11443BDBB00C7C824 /* GenerateThumbnailForURL.c in Sources */,
5A7859E31443BDBB00C7C824 /* GeneratePreviewForURL.c in Sources */,
5A7859E51443BDBB00C7C824 /* main.c in Sources */,
5A7859F31443C1A000C7C824 /* GeneratePreviewForURL.m in Sources */,
5A7859F41443C1A000C7C824 /* GenerateThumbnailForURL.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -278,6 +298,7 @@
5A7859EA1443BDBB00C7C824 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
20 changes: 0 additions & 20 deletions MultiMarkdown QuickLook/GeneratePreviewForURL.c

This file was deleted.

131 changes: 131 additions & 0 deletions MultiMarkdown QuickLook/GeneratePreviewForURL.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#import <CoreFoundation/CoreFoundation.h>
#import <CoreServices/CoreServices.h>
#import <QuickLook/QuickLook.h>
#import <Cocoa/Cocoa.h>

/* -----------------------------------------------------------------------------
Generate a preview for file
This function's job is to create preview for designated file
----------------------------------------------------------------------------- */

NSData* processMMD(NSURL* url);
NSData* processOPML2MMD(NSURL* url);

BOOL logDebug = YES;


OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
{
if (logDebug)
NSLog(@"generate preview for content type: %@",contentTypeUTI);

CFDataRef previewData;

if (CFStringCompare(contentTypeUTI, CFSTR("org.opml.opml"), 0) == kCFCompareEqualTo)
{
// Preview an OPML file

previewData = (CFDataRef) processOPML2MMD((NSURL*) url);
} else {
// Preview a text file

previewData = (CFDataRef) processMMD((NSURL*) url);
}

if (previewData) {
if (logDebug)
NSLog(@"preview generated");

CFDictionaryRef properties = (CFDictionaryRef) [NSDictionary dictionary];
QLPreviewRequestSetDataRepresentation(preview, previewData, kUTTypeHTML, properties);
}

return noErr;
}

NSData* processOPML2MMD(NSURL* url)
{
if (logDebug)
NSLog(@"create preview for OPML file %@",[url path]);

NSString *path2MMD = [[NSBundle bundleWithIdentifier:@"net.fletcherpenney.quicklook"] pathForResource:@"multimarkdown" ofType:nil];

NSTask* task = [[NSTask alloc] init];
[task setLaunchPath: [path2MMD stringByExpandingTildeInPath]];

[task setArguments: [NSArray arrayWithObjects: nil]];

NSPipe *writePipe = [NSPipe pipe];
NSFileHandle *writeHandle = [writePipe fileHandleForWriting];
[task setStandardInput: writePipe];

NSPipe *readPipe = [NSPipe pipe];
[task setStandardOutput:readPipe];

[task launch];


NSString *theData = [NSString stringWithContentsOfFile:[url path] encoding:NSUTF8StringEncoding error:nil];

NSXMLDocument *opmlDocument = [[NSXMLDocument alloc] initWithXMLString:theData
options:0
error:nil];
NSURL *styleFilePath = [[NSBundle bundleWithIdentifier:@"net.fletcherpenney.quicklook"] URLForResource:@"opml2mmd"
withExtension:@"xslt"];

NSData *mmdContents = [opmlDocument objectByApplyingXSLTAtURL:styleFilePath
arguments:nil
error:nil];

[opmlDocument release];

[writeHandle writeData:mmdContents];

[writeHandle closeFile];


NSData *mmdData = [[readPipe fileHandleForReading] readDataToEndOfFile];

[task release];
return mmdData;
}

NSData* processMMD(NSURL* url)
{
if (logDebug)
NSLog(@"create preview for MMD file %@",[url path]);

NSString *path2MMD = [[NSBundle bundleWithIdentifier:@"net.fletcherpenney.quicklook"] pathForResource:@"multimarkdown" ofType:nil];

NSTask* task = [[NSTask alloc] init];
[task setLaunchPath: [path2MMD stringByExpandingTildeInPath]];

[task setArguments: [NSArray arrayWithObjects: nil]];

NSPipe *writePipe = [NSPipe pipe];
NSFileHandle *writeHandle = [writePipe fileHandleForWriting];
[task setStandardInput: writePipe];

NSPipe *readPipe = [NSPipe pipe];
[task setStandardOutput:readPipe];

[task launch];

NSString *theData = [NSString stringWithContentsOfFile:[url path] encoding:NSUTF8StringEncoding error:nil];

[writeHandle writeData:[theData dataUsingEncoding:NSUTF8StringEncoding]];

[writeHandle closeFile];


NSData *mmdData = [[readPipe fileHandleForReading] readDataToEndOfFile];

[task release];
return mmdData;
}

void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview)
{
// implement only if supported
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#include <QuickLook/QuickLook.h>
#import <CoreFoundation/CoreFoundation.h>
#import <CoreServices/CoreServices.h>
#import <QuickLook/QuickLook.h>
#import <Cocoa/Cocoa.h>

/* -----------------------------------------------------------------------------
Generate a thumbnail for file
This function's job is to create thumbnail for designated file as fast as possible
----------------------------------------------------------------------------- */

//NSData* processMMD(NSURL* url);

OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize)
{
#warning To complete your generator please implement the function GenerateThumbnailForURL in GenerateThumbnailForURL.c
return noErr;
}

Expand Down
36 changes: 33 additions & 3 deletions MultiMarkdown QuickLook/MultiMarkdown QuickLook-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@
<string>English</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>QLGenerator</string>
<key>LSItemContentTypes</key>
<array/>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>QLGenerator</string>
<key>LSItemContentTypes</key>
<array>
<string>SUPPORTED_UTI_TYPE</string>
<string>net.daringfireball.markdown</string>
<string>com.multimarkdown.text</string>
<string>org.vim.markdown-file</string>
<string>public.opml</string>
<string>com.multimarkdown.opml</string>
<string>org.opml.opml</string>
</array>
</dict>
</array>
Expand All @@ -20,7 +31,7 @@
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>net.fletcherpenney.${PRODUCT_NAME:rfc1034identifier}</string>
<string>net.fletcherpenney.quicklook</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand All @@ -45,10 +56,29 @@
<string>12B11958-4455-4E27-9FAA-A8E5F7E22211</string>
</array>
</dict>
<key>UTImportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>org.opml.opml</string>
</array>
<key>UTTypeIdentifier</key>
<string>public.opml</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<dict>
<key>item 0</key>
<string>opml</string>
</dict>
</dict>
</dict>
</array>
<key>CFPlugInUnloadFunction</key>
<string></string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2011 __MyCompanyName__. All rights reserved.</string>
<string>Copyright © 2011 Fletcher T. Penney. All rights reserved.</string>
<key>QLNeedsToBeRunInMainThread</key>
<false/>
<key>QLPreviewHeight</key>
Expand Down
Binary file added MultiMarkdown QuickLook/multimarkdown
Binary file not shown.
Loading

0 comments on commit ac31a54

Please sign in to comment.