Skip to content

Commit 23ea6d8

Browse files
committed
This commit converts the project to use the Highlight library instead of the
Pygments library. Highlight is about 10x faster, supports many more languages, and requires less hackery (or different hackery, at least) to satisfy me.
1 parent 29189a3 commit 23ea6d8

File tree

14 files changed

+368
-215
lines changed

14 files changed

+368
-215
lines changed

ChangeLog.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
0.2 (in progress):
2+
- Switched from Pygments to Highlight
3+
- Added thumbnailing support
24
- Changed .tex UTI to agree with TeXShop's.
3-
- Let the system pick a different plugin if pygmentize fails
5+
- (Try to) let the system pick a different plugin if ours fails
46

57
0.1: Initial release

Common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@
1616
#endif
1717

1818
// Status is 0 on success, nonzero on error (like a shell command)
19-
// If thumbnail is 1, only render the top 50 lines of the file
19+
// If thumbnail is 1, only render enough of the file for a thumbnail
2020
NSData *colorizeURL(CFBundleRef myBundle, CFURLRef url, int *status,
2121
int thumbnail);

Common.m

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,29 @@
4545
return data;
4646
}
4747

48+
NSString *pathOfURL(CFURLRef url)
49+
{
50+
NSString *targetCFS = [[(NSURL *)url absoluteURL] path];
51+
return [targetCFS stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
52+
}
53+
4854
NSData *colorizeURL(CFBundleRef bundle, CFURLRef url, int *status, int thumbnail)
4955
{
5056
NSData *output = NULL;
51-
unsigned char *targetBuf = malloc(PATH_MAX);
52-
unsigned char *rsrcDirBuf = malloc(PATH_MAX);
53-
char *thumbString;
5457
CFURLRef rsrcDirURL = CFBundleCopyResourcesDirectoryURL(bundle);
58+
//n8log(@"rsrcDirURL = %@", CFURLGetString(rsrcDirURL));
59+
NSString *rsrcEsc = pathOfURL(rsrcDirURL);
60+
CFRelease(rsrcDirURL);
61+
NSString *targetEsc = pathOfURL(url);
5562

56-
if (!CFURLGetFileSystemRepresentation(url, YES, targetBuf, PATH_MAX)
57-
|| !CFURLGetFileSystemRepresentation(rsrcDirURL, YES, rsrcDirBuf, PATH_MAX))
58-
{
59-
NSLog(@"QLColorCode: CFURLGetFileSystemRepresentation failed");
60-
*status = 1;
61-
goto done;
62-
}
63-
if (thumbnail)
64-
thumbString = "1";
65-
else
66-
thumbString = "0";
6763
NSString *cmd = [NSString stringWithFormat:
68-
@"\"%s/colorize.sh\" \"%s\" \"%s\" %s",
69-
rsrcDirBuf, rsrcDirBuf, targetBuf, thumbString];
64+
@"\"%@/colorize.sh\" \"%@\" \"%@\" %s",
65+
rsrcEsc, rsrcEsc, targetEsc, thumbnail ? "1" : "0"];
66+
n8log(@"cmd = %@", cmd);
7067

7168
output = runTask(cmd, status);
7269
if (*status != 0) {
7370
NSLog(@"QLColorCode: colorize.sh failed with exit code %d", *status);
7471
}
75-
done:
76-
free(targetBuf);
77-
free(rsrcDirBuf);
7872
return output;
7973
}

GeneratePreviewForURL.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
1717
CFURLRef url, CFStringRef contentTypeUTI,
1818
CFDictionaryRef options)
1919
{
20+
NSDate *startDate = [NSDate date];
2021
n8log(@"Generating Preview");
2122
if (QLPreviewRequestIsCancelled(preview))
2223
return noErr;
@@ -27,9 +28,12 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
2728
CFBundleRef bundle = QLPreviewRequestGetGeneratorBundle(preview);
2829
int status;
2930
NSData *output = colorizeURL(bundle, url, &status, 0);
31+
n8log(@"Generated preview html page in %.3f sec", -[startDate timeIntervalSinceNow] );
3032

3133
if (status != 0 || QLPreviewRequestIsCancelled(preview)) {
34+
#ifndef DEBUG
3235
goto done;
36+
#endif
3337
}
3438
// Now let WebKit do its thing
3539
CFDictionaryRef emptydict =
@@ -40,6 +44,7 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
4044
emptydict);
4145

4246
done:
47+
n8log(@"Finished preview in %.3f sec", -[startDate timeIntervalSinceNow] );
4348
[pool release];
4449
return noErr;
4550
}

GenerateThumbnailForURL.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ OSStatus GenerateThumbnailForURL(void *thisInterface,
2525
if (maxSize.width < minSize || maxSize.height < minSize)
2626
return noErr;
2727
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
28+
NSDate *startDate = [NSDate date];
2829

2930
// Render as though there is an 600x800 window, and fill the thumbnail
3031
// vertically. This code could be more general. I'm assuming maxSize is
@@ -43,8 +44,11 @@ OSStatus GenerateThumbnailForURL(void *thisInterface,
4344
CFBundleRef bundle = QLThumbnailRequestGetGeneratorBundle(thumbnail);
4445
NSData *data = colorizeURL(bundle, url, &status, 1);
4546
//NSLog(@"%s", [data bytes]);
47+
n8log(@"Generated thumbnail html page in %.3f sec", -[startDate timeIntervalSinceNow] );
4648
if (status != 0) {
49+
#ifndef DEBUG
4750
goto done;
51+
#endif
4852
}
4953
//NSRect previewRect;
5054
//previewRect.size = previewSize;
@@ -78,6 +82,7 @@ OSStatus GenerateThumbnailForURL(void *thisInterface,
7882
CFRelease(context);
7983
}
8084
done:
85+
n8log(@"Finished thumbnail after %.3f sec\n\n", -[startDate timeIntervalSinceNow] );
8186
[pool release];
8287
return noErr;
8388
}

Info.plist

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -188,31 +188,48 @@
188188
<string>text/x-ini</string>
189189
</dict>
190190
</dict>
191-
<!-- This is from TexShop. I don't know if I agree with it, but they're
192-
a bigger fish as far as TeX is concerned... -->
193-
<dict>
194-
<key>UTTypeConformsTo</key>
195-
<array>
196-
<string>public.text</string>
197-
</array>
198-
<key>UTTypeDescription</key>
199-
<string>TeX text file</string>
200-
<key>UTTypeIdentifier</key>
201-
<string>org.tug.tex</string>
202-
<key>UTTypeTagSpecification</key>
203-
<dict>
204-
<key>com.apple.ostype</key>
205-
<string>TEXT</string>
206-
<key>public.filename-extension</key>
207-
<array>
208-
<string>tex</string>
209-
<string>latex</string>
210-
<string>ltx</string>
211-
<string>texi</string>
212-
<string>ctx</string>
213-
</array>
214-
</dict>
215-
</dict>
191+
<!-- This is from TexShop. I don't know if I agree with it, but they're
192+
a bigger fish as far as TeX is concerned... -->
193+
<dict>
194+
<key>UTTypeConformsTo</key>
195+
<array>
196+
<string>public.text</string>
197+
</array>
198+
<key>UTTypeDescription</key>
199+
<string>TeX text file</string>
200+
<key>UTTypeIdentifier</key>
201+
<string>org.tug.tex</string>
202+
<key>UTTypeTagSpecification</key>
203+
<dict>
204+
<key>com.apple.ostype</key>
205+
<string>TEXT</string>
206+
<key>public.filename-extension</key>
207+
<array>
208+
<string>tex</string>
209+
<string>latex</string>
210+
<string>ltx</string>
211+
<string>texi</string>
212+
<string>ctx</string>
213+
</array>
214+
</dict>
215+
</dict>
216+
<dict>
217+
<key>UTTypeIdentifier</key>
218+
<string>org.erlang.erlang-source</string>
219+
<key>UTTypeDescription</key>
220+
<string>Erlang Source File</string>
221+
<key>UTTypeConformsTo</key>
222+
<array>
223+
<string>public.source-code</string>
224+
</array>
225+
<key>UTTypeTagSpecification</key>
226+
<dict>
227+
<key>public.filename-extension</key>
228+
<array>
229+
<string>erl</string>
230+
</array>
231+
</dict>
232+
</dict>
216233
</array>
217234
</dict>
218235
</plist>

0 commit comments

Comments
 (0)