Jeff Standen
|
Posted: December 09, 2008 14:43 by Jeff Standen
|
|
So far mac-httpclient is a great little tool for my work on REST gateways where it's a lot more convenient than passing in little header changes from cURL in the shell. The syntax highlighting is a nice touch, but sometimes I'm dumping debug output from PHP or doing a JSON reply (application/json or text/javascript) and the highlighting code starts sporadically eating words as tokens. It works fine if you disable syntax highlighting, but it's annoying to keep toggling for different packets. Below is a quick patch for the syntax highlighting to be conscious of the content-type header of the response. At the moment XML and HTML will pass to the existing highlighting code, and everything else will be treated as plaintext (where the headers still colorize). I left it open for the syntax highlighting to support other mime types (like JSON) if someone feels like contributing them. @iTod: I tried to keep the patch minimalistic, but feel free to toss it if you have a better approach in mind. -Jeff Standen WebGroup Media, LLC. Index: src/TDHtmlSyntaxHighlighter.h =================================================================== --- src/TDHtmlSyntaxHighlighter.h (revision 40) +++ src/TDHtmlSyntaxHighlighter.h (working copy) @@ -30,6 +30,8 @@ TDToken *scriptToken; TDToken *endScriptToken; + NSString *contentType; + NSMutableAttributedString *highlightedString; NSDictionary *headerNameAttributes; NSDictionary *headerValueAttributes; @@ -44,7 +46,9 @@ - (id)initWithAttributesForDarkBackground:(BOOL)isDark; - (NSAttributedString *)attributedStringForString:(NSString *)s; +-(void) parseHeaderContentType:(NSString *)s; +@property (nonatomic, retain) NSString *contentType; @property (nonatomic, retain) NSMutableAttributedString *highlightedString; @property (nonatomic, retain) NSDictionary *headerNameAttributes; @property (nonatomic, retain) NSDictionary *headerValueAttributes; Index: src/TDHtmlSyntaxHighlighter.m =================================================================== --- src/TDHtmlSyntaxHighlighter.m (revision 40) +++ src/TDHtmlSyntaxHighlighter.m (working copy) @@ -70,6 +70,8 @@ if (self != nil) { isDarkBG = isDark; + self.contentType = @"text/plain"; + self.tokenizer = [TDTokenizer tokenizer]; // configure tokenizer @@ -192,6 +194,7 @@ self.eqToken = nil; self.scriptToken = nil; self.endScriptToken = nil; + self.contentType = nil; self.highlightedString = nil; self.headerNameAttributes = nil; self.headerValueAttributes = nil; @@ -220,6 +223,12 @@ NSInteger i = r.location + 1; NSString *name = [line substringToIndex:i]; NSString *value = [NSString stringWithFormat:@"%@\r\n", [line substringFromIndex:i]]; + + // [JAS]: Store the content-type for later use in formatting + if([name caseInsensitiveCompare:@"Content-Type"]) { + [self parseHeaderContentType:value]; + } + as = [[NSAttributedString alloc] initWithString:name attributes:headerNameAttributes]; [highlightedString appendAttributedString:as]; [as release]; @@ -233,7 +242,24 @@ [as release]; } +// [JAS]: Store the content type so it can determine syntax highlighting. +-(void) parseHeaderContentType:(NSString *)s { + NSRange rContentType = [s rangeOfString:@";"]; + NSMutableString *contentTypeString; + // Strip off a charset if provided + if(NSNotFound != rContentType.location) { + contentTypeString = [NSMutableString stringWithString:[s substringToIndex:rContentType.location+1]]; + } else { + contentTypeString = [NSMutableString stringWithString:s]; + } + + // Trim any whitespace + CFStringTrimWhitespace((CFMutableStringRef) contentTypeString); + + self.contentType = contentTypeString; +} + - (NSAttributedString *)attributedStringForString:(NSString *)s { self.stack = [NSMutableArray array]; self.highlightedString = [[[NSMutableAttributedString alloc] init] autorelease]; @@ -243,6 +269,11 @@ [self parseHeaders:[s substringToIndex:r.location]]; s = [s substringFromIndex:r.location + r.length]; } + + // [JAS]: HTML or XML + if(NSOrderedSame == [contentType caseInsensitiveCompare:@"text/html"] + || NSOrderedSame == [contentType caseInsensitiveCompare:@"text/xml"] + ) { NSInteger lengthOfHeaders = highlightedString.length; tokenizer.string = s; @@ -311,6 +342,14 @@ } } + // [JAS]: Not HTML or XML (plaintext, javascript/JSON, etc.) + // Can be expanded on to do new modes of syntax highlighting. + } else { + NSAttributedString *as = [[NSAttributedString alloc] initWithString:s attributes:textAttributes]; + [highlightedString appendAttributedString:as]; + [as release]; + } + NSAttributedString *result = [[highlightedString copy] autorelease]; self.stack = nil; self.highlightedString = nil; @@ -641,6 +680,7 @@ @synthesize eqToken; @synthesize scriptToken; @synthesize endScriptToken; +@synthesize contentType; @synthesize highlightedString; @synthesize headerNameAttributes; @synthesize headerValueAttributes; |
PATCH: Plaintext support in syntax highlighting
Replies: 0 - Last Post: December 09, 2008 14:43
by: Jeff Standen
by: Jeff Standen
« Older Topic
» Newer Topic
showing 1 - 1 of 1
Replies: 0 - Last Post: December 09, 2008 14:43
by: Jeff Standen
by: Jeff Standen
« Older Topic
» Newer Topic






