Skip to content

Commit

Permalink
base URL relative to markdown doc
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnthe committed Jun 4, 2011
1 parent 3437a29 commit dc5cd32
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 33 deletions.
69 changes: 38 additions & 31 deletions GenerateThumbnailForURL.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "markdown.h"

/* -----------------------------------------------------------------------------
Generate a thumbnail for file
Generate a thumbnail for file
This function's job is to create thumbnail for designated file as fast as
possible
Expand All @@ -19,52 +19,59 @@ OSStatus GenerateThumbnailForURL(void *thisInterface,
CFURLRef url, CFStringRef contentTypeUTI,
CFDictionaryRef options, CGSize maxSize)
{
NSData *data = renderMarkdown((NSURL*) url);
NSData *data = renderMarkdown((NSURL*) url);

if (data) {
if (data) {
NSRect viewRect = NSMakeRect(0.0, 0.0, 600.0, 800.0);
float scale = maxSize.height / 800.0;
NSSize scaleSize = NSMakeSize(scale, scale);
CGSize thumbSize = NSSizeToCGSize(
NSMakeSize((maxSize.width * (600.0/800.0)),
maxSize.height));
NSMakeSize((maxSize.width * (600.0/800.0)),
maxSize.height));

dispatch_sync(dispatch_get_main_queue(),
^{

WebView* webView = [[WebView alloc] initWithFrame: viewRect];
[webView scaleUnitSquareToSize: scaleSize];
[[[webView mainFrame] frameView] setAllowsScrolling:NO];
[[webView mainFrame] loadData: data
MIMEType: @"text/html"
textEncodingName: @"utf-8"
baseURL: nil];
WebView* webView = [[WebView alloc] initWithFrame: viewRect];
[webView scaleUnitSquareToSize: scaleSize];
[[[webView mainFrame] frameView] setAllowsScrolling:NO];
[[webView mainFrame] loadData: data
MIMEType: @"text/html"
textEncodingName: @"utf-8"
baseURL: [[url URLByDeletingLastPathComponent] filePathURL]];

while([webView isLoading]) {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
}
while([webView isLoading]) {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
}

[webView display];
[webView display];

CGContextRef context =
QLThumbnailRequestCreateContext(thumbnail, thumbSize, false, NULL);
CGContextRef context =
QLThumbnailRequestCreateContext(thumbnail, thumbSize, false, NULL);

if (context) {
NSGraphicsContext* nsContext =
[NSGraphicsContext
graphicsContextWithGraphicsPort: (void*) context
flipped: [webView isFlipped]];
if (context) {
NSGraphicsContext* nsContext =
[NSGraphicsContext
graphicsContextWithGraphicsPort: (void*) context
flipped: [webView isFlipped]];

[webView displayRectIgnoringOpacity: [webView bounds]
inContext: nsContext];
[webView displayRectIgnoringOpacity: [webView bounds]
inContext: nsContext];

QLThumbnailRequestFlushContext(thumbnail, context);
QLThumbnailRequestFlushContext(thumbnail, context);

CFRelease(context);
}
}
CFRelease(context);
}

return noErr;
});


}

return noErr;
}

void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail)
{
// implement only if supported
// implement only if supported
}
4 changes: 2 additions & 2 deletions markdown.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
NSString *html = [NSString stringWithFormat:@"<html>"
"<head>"
"<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />"
"<base href='%@' />"
"<style type=\"text/css\">%@</style>"
"</head>"
"<body>%@</body>"
"</html>",
styles, [NSString stringWithUTF8String:output]];

[[[url URLByDeletingLastPathComponent] filePathURL] absoluteString], styles, [NSString stringWithUTF8String:output]];
free(output);
return [html dataUsingEncoding:NSUTF8StringEncoding];
}

0 comments on commit dc5cd32

Please sign in to comment.