Skip to content

Commit

Permalink
Merge pull request #91 from tattali/catalina-support
Browse files Browse the repository at this point in the history
Catalina support
  • Loading branch information
Phil Toland authored Nov 26, 2019
2 parents 13de2b6 + 230f984 commit ae397c6
Show file tree
Hide file tree
Showing 15 changed files with 780 additions and 509 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ColumnLimit: 120
50 changes: 28 additions & 22 deletions GeneratePreviewForURL.m
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
#import <QuickLook/QuickLook.h>
#import <Cocoa/Cocoa.h>
#include "markdown.h"
#import <CoreFoundation/CoreFoundation.h>
#import <CoreServices/CoreServices.h>
#import <Foundation/Foundation.h>
#import <QuickLook/QuickLook.h>

#import "markdown.h"

OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url,
CFStringRef contentTypeUTI, CFDictionaryRef options);
void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview);

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

OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
CFURLRef url, CFStringRef contentTypeUTI,
CFDictionaryRef options)
{
@autoreleasepool {
NSData *data = renderMarkdown((__bridge NSURL*) url);

if (data) {
NSDictionary *props = [[NSDictionary alloc] init];
QLPreviewRequestSetDataRepresentation(preview, (__bridge CFDataRef)data, kUTTypeHTML, (__bridge CFDictionaryRef)props);
}

return noErr;
}
-----------------------------------------------------------------------------
*/
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url,
CFStringRef contentTypeUTI, CFDictionaryRef options) {

NSString *content = renderMarkdown((__bridge NSURL *)url);

CFDictionaryRef previewProperties = (__bridge CFDictionaryRef) @{
(__bridge NSString *)kQLPreviewPropertyTextEncodingNameKey : @"UTF-8",
(__bridge NSString *)kQLPreviewPropertyMIMETypeKey : @"text/html",
};

QLPreviewRequestSetDataRepresentation(preview, (__bridge CFDataRef)[content dataUsingEncoding:NSUTF8StringEncoding],
kUTTypeHTML, previewProperties);

return noErr;
}

void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview)
{
// implement only if supported
void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview) {
// Implement only if supported
}
95 changes: 41 additions & 54 deletions GenerateThumbnailForURL.m
Original file line number Diff line number Diff line change
@@ -1,72 +1,59 @@
#import <QuickLook/QuickLook.h>
#import <Cocoa/Cocoa.h>
#import <CoreFoundation/CoreFoundation.h>
#import <CoreServices/CoreServices.h>
#import <Foundation/Foundation.h>
#import <QuickLook/QuickLook.h>
#import <WebKit/WebKit.h>
#include "markdown.h"

/* -----------------------------------------------------------------------------
Generate a thumbnail for file

This function's job is to create thumbnail for designated file as fast as
possible
-------------------------------------------------------------------------- */
OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url,
CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize);
void CancelThumbnailGeneration(void *thisInterface, QLThumbnailRequestRef thumbnail);

//The minimum aspect ratio (width / height) of a thumbnail.
#define MINIMUM_ASPECT_RATIO (1.0/2.0)


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

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));
OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url,
CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize) {

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];
NSString *content = [NSString stringWithContentsOfURL:(__bridge NSURL *)url encoding:NSUTF8StringEncoding error:nil];

while([webView isLoading]) {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
}
if (content) {
// Encapsulate the content of the .strings file in HTML
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];

[webView display];
NSRect _rect = 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));

CGContextRef context =
QLThumbnailRequestCreateContext(thumbnail, thumbSize, false, NULL);
// Create the webview to display the thumbnail
WebView *_webView = [[WebView alloc] initWithFrame:_rect];
[_webView scaleUnitSquareToSize:_scaleSize];
[_webView.mainFrame.frameView setAllowsScrolling:NO];
[_webView.mainFrame loadData:data MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:nil];

if (context) {
NSGraphicsContext* nsContext =
[NSGraphicsContext
graphicsContextWithGraphicsPort: (void*) context
flipped: [webView isFlipped]];
while ([_webView isLoading])
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true);
[_webView display];

[webView displayRectIgnoringOpacity: [webView bounds]
inContext: nsContext];
// Draw the webview in the correct context
CGContextRef _context = QLThumbnailRequestCreateContext(thumbnail, _thumbSize, false, NULL);
if (_context) {
NSGraphicsContext *_graphicsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:(void *)_context
flipped:_webView.isFlipped];
[_webView displayRectIgnoringOpacity:_webView.bounds inContext:_graphicsContext];

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

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

return noErr;
}
return noErr;
}

void CancelThumbnailGeneration(void* thisInterface, QLThumbnailRequestRef thumbnail)
{
// implement only if supported
void CancelThumbnailGeneration(void *thisInterface, QLThumbnailRequestRef thumbnail) {
// implement only if supported
}
4 changes: 2 additions & 2 deletions Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundleShortVersionString</key>
<string>1.3.5</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1.3.5</string>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>CFPlugInDynamicRegisterFunction</key>
<string></string>
<key>CFPlugInDynamicRegistration</key>
Expand Down
4 changes: 4 additions & 0 deletions QLMarkdown.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,13 @@
CLANG_ENABLE_OBJC_ARC = YES;
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1.3.7;
GCC_DYNAMIC_NO_PIC = NO;
GCC_PRECOMPILE_PREFIX_HEADER = NO;
HEADER_SEARCH_PATHS = "./discount-config";
INFOPLIST_FILE = Info.plist;
INSTALL_PATH = /Library/QuickLook;
MARKETING_VERSION = 1.3.7;
PRODUCT_BUNDLE_IDENTIFIER = com.fiatdev.QLMarkdown;
PRODUCT_NAME = QLMarkdown;
WRAPPER_EXTENSION = qlgenerator;
Expand All @@ -354,11 +356,13 @@
buildSettings = {
CLANG_ENABLE_OBJC_ARC = YES;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1.3.7;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
GCC_PRECOMPILE_PREFIX_HEADER = NO;
HEADER_SEARCH_PATHS = "./discount-config";
INFOPLIST_FILE = Info.plist;
INSTALL_PATH = /Library/QuickLook;
MARKETING_VERSION = 1.3.7;
PRODUCT_BUNDLE_IDENTIFIER = com.fiatdev.QLMarkdown;
PRODUCT_NAME = QLMarkdown;
WRAPPER_EXTENSION = qlgenerator;
Expand Down
10 changes: 5 additions & 5 deletions Readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ Introduction
------------

QLMarkdown is a simple QuickLook generator for Markdown files. It renders a
preview of the selected Markdown file using [Discount][Discount] -- a C implementation of
preview of the selected Markdown file using [Discount][Discount] -- a C implementation of
John Gruber's Markdown.pl script.

To update to the latest version of discount run `./discount-config/update-discount.sh`

For more information on Markdown see
For more information on Markdown see
<http://daringfireball.net/projects/markdown/>.


Expand All @@ -39,7 +39,7 @@ To uninstall:
`$ brew cask uninstall qlmarkdown`


**Note:** *QuickLook doesn't allow selecting text by default. If you want to select the text in the markdown preview, you will
**Note:** *QuickLook doesn't allow selecting text by default. If you want to select the text in the markdown preview, you will
need to enable text selection in QuickLook by running the following command in Terminal:*

`defaults write com.apple.finder QLEnableTextSelection -bool TRUE; killall Finder`
Expand All @@ -53,7 +53,7 @@ Downloads

Source code is available at <http://github.com/toland/qlmarkdown>.

You can download the [latest](https://github.com/toland/qlmarkdown/releases/latest) release from
You can download the [latest](https://github.com/toland/qlmarkdown/releases/latest) release from
<https://github.com/toland/qlmarkdown/releases>

License
Expand All @@ -79,7 +79,7 @@ Version 1.1 - Feb 11, 2009
* Adding a little bit of CSS styling. (mdk)
* Replace the Perl markdown renderer with a native C one (discount). (mdk)
* Conform to public plain-text. Will make spotlight index the file
contents. (mdk)
contents. (mdk)
* Added support for .md file extension (sant0sk1)

Version 1.0 - July 15, 2008
Expand Down
2 changes: 1 addition & 1 deletion discount-config/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Discount is included as a [fake submodule][]. To upgrade or test other versions
rm -rf discount/
git clone git://github.com/Orc/discount.git discount

The discount folder will contain a .git folder following the clone however this folder is not included in the enclosing repository and will not be propagated by a push or clone.
The discount folder will contain a .git folder following the clone however this folder is not included in the enclosing repository and will not be propagated by a push or clone.

## Config files ##

Expand Down
26 changes: 13 additions & 13 deletions discount-wrapper.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include <stdio.h>
#include <stdlib.h>

#include "discount/markdown.h"

char* convert_markdown_to_string(const char *str)
{
char *out = NULL;

Document *blob = mkd_string((char *)str, strlen(str), 0);
mkd_compile(blob, MKD_EXTRA_FOOTNOTE);
int sz = mkd_document(blob, &out);

if(sz == 0)
return NULL;

return out;
}
char *convert_markdown_to_string(const char *str) {
char *out = NULL;

Document *blob = mkd_string((char *)str, strlen(str), 0);
mkd_compile(blob, MKD_EXTRA_FOOTNOTE);
int sz = mkd_document(blob, &out);

if (sz == 0)
return NULL;

return out;
}
2 changes: 1 addition & 1 deletion discount-wrapper.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
char* convert_markdown_to_string(const char *str);
char *convert_markdown_to_string(const char *str);
Loading

0 comments on commit ae397c6

Please sign in to comment.