From 435863ae95a73a0dec7d5f6c8638525ce9ea31ed Mon Sep 17 00:00:00 2001 From: Bibiko Date: Mon, 9 Aug 2010 13:10:28 +0000 Subject: =?UTF-8?q?=E2=80=A2=20SQL=20file=20Quicklook=20generator=20-=20sp?= =?UTF-8?q?ed=20up=20HTML=20rendering=20for=20larger=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Resources/English.lproj/SPQLPluginSQLTemplate.html | 6 +- Resources/German.lproj/SPQLPluginSQLTemplate.html | 6 +- Source/GeneratePreviewForURL.m | 112 +++++++++++---------- 3 files changed, 62 insertions(+), 62 deletions(-) diff --git a/Resources/English.lproj/SPQLPluginSQLTemplate.html b/Resources/English.lproj/SPQLPluginSQLTemplate.html index 3097b10f..52588352 100644 --- a/Resources/English.lproj/SPQLPluginSQLTemplate.html +++ b/Resources/English.lproj/SPQLPluginSQLTemplate.html @@ -6,8 +6,6 @@ body { font-family:'Lucida Grande'; font-size:10pt; - background-color:white; - color:black; } @@ -16,7 +14,7 @@ body {
- +
@@ -27,7 +25,7 @@ size: %@
-
+
 %@
 
diff --git a/Resources/German.lproj/SPQLPluginSQLTemplate.html b/Resources/German.lproj/SPQLPluginSQLTemplate.html index ca256c81..9ced9acd 100644 --- a/Resources/German.lproj/SPQLPluginSQLTemplate.html +++ b/Resources/German.lproj/SPQLPluginSQLTemplate.html @@ -6,8 +6,6 @@ body { font-family:'Lucida Grande'; font-size:10pt; - background-color:white; - color:black; } @@ -16,7 +14,7 @@ body {
- +
@@ -27,7 +25,7 @@ Größe: %@
-
+
 %@
 
diff --git a/Source/GeneratePreviewForURL.m b/Source/GeneratePreviewForURL.m index 697ee552..aaf8a903 100644 --- a/Source/GeneratePreviewForURL.m +++ b/Source/GeneratePreviewForURL.m @@ -201,12 +201,15 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, { NSNumber *filesize = [fileAttributes objectForKey:NSFileSize]; + NSUInteger kMaxSQLFileSize = (0.7 * 1024 * 1024); // compose the html and perform syntax highlighting // read the file and try to get a proper encoding NSString *sqlText = [NSString stringWithContentsOfFile:[myURL path] encoding:sqlEncoding error:&readError]; - + NSMutableString *sqlHTML = [NSMutableString string]; + NSString *truncatedString = @""; + if(readError != nil) { // cocoa tries to detect the encoding sqlText = [NSString stringWithContentsOfFile:[myURL path] usedEncoding:&sqlEncoding error:&readError]; @@ -217,65 +220,66 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, } } - // bail if nothing could be read + // if nothing could be read print ... SQL ... if(!sqlText) { - [html release]; - [pool release]; - return noErr; - } + [sqlHTML appendString:@"... SQL ..."]; + } else { - // truncate large files since Finder blocks - NSString *truncatedString = @""; - if([filesize unsignedLongValue] > 500000) { - sqlText = [sqlText substringToIndex:500000-1]; - truncatedString = @"\n ✂ ..."; - } - - NSMutableString *sqlHTML = [NSMutableString string]; - - NSRange textRange = NSMakeRange(0, [sqlText length]); - NSString *tokenColor; - size_t token; - NSRange tokenRange; - - // initialise flex - yyuoffset = 0; yyuleng = 0; - yy_switch_to_buffer(yy_scan_string([sqlText UTF8String])); - - // now loop through all the tokens - while (token=yylex()){ - switch (token) { - case SPT_SINGLE_QUOTED_TEXT: - case SPT_DOUBLE_QUOTED_TEXT: - tokenColor = @"#A7221C"; - break; - case SPT_BACKTICK_QUOTED_TEXT: - tokenColor = @"#001892"; - break; - case SPT_RESERVED_WORD: - tokenColor = @"#0041F6"; - break; - case SPT_NUMERIC: - tokenColor = @"#67350F"; - break; - case SPT_COMMENT: - tokenColor = @"#265C10"; - break; - case SPT_VARIABLE: - tokenColor = @"#6C6C6C"; - break; - case SPT_WHITESPACE: - tokenColor = @"black"; - break; - default: - tokenColor = @"black"; + // truncate large files since Finder blocks + if([filesize unsignedLongValue] > kMaxSQLFileSize) { + sqlText = [sqlText substringToIndex:kMaxSQLFileSize-1]; + truncatedString = @"\n ✂ ..."; } - tokenRange = NSMakeRange(yyuoffset, yyuleng); - [sqlHTML appendFormat:@"%@", tokenColor, [sqlText substringWithRange:tokenRange]]; + NSRange textRange = NSMakeRange(0, [sqlText length]); + NSString *tokenColor; + size_t token; + NSRange tokenRange; + + // initialise flex + yyuoffset = 0; yyuleng = 0; + yy_switch_to_buffer(yy_scan_string([sqlText UTF8String])); + BOOL skipFontTag; + + // now loop through all the tokens + while (token=yylex()){ + skipFontTag = NO; + switch (token) { + case SPT_SINGLE_QUOTED_TEXT: + case SPT_DOUBLE_QUOTED_TEXT: + tokenColor = @"#A7221C"; + break; + case SPT_BACKTICK_QUOTED_TEXT: + tokenColor = @"#001892"; + break; + case SPT_RESERVED_WORD: + tokenColor = @"#0041F6"; + break; + case SPT_NUMERIC: + tokenColor = @"#67350F"; + break; + case SPT_COMMENT: + tokenColor = @"#265C10"; + break; + case SPT_VARIABLE: + tokenColor = @"#6C6C6C"; + break; + case SPT_WHITESPACE: + skipFontTag = YES; + break; + default: + skipFontTag = YES; + } + + tokenRange = NSMakeRange(yyuoffset, yyuleng); + if(skipFontTag) + [sqlHTML appendString:[sqlText substringWithRange:tokenRange]]; + else + [sqlHTML appendFormat:@"%@", tokenColor, [sqlText substringWithRange:tokenRange]]; + } + [sqlHTML appendString:truncatedString]; } - [sqlHTML appendString:truncatedString]; html = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:template, [[iconImage TIFFRepresentationUsingCompression:NSTIFFCompressionJPEG factor:0.01] base64EncodingWithLineLength:0], -- cgit v1.2.3