Permalink
Browse files

Formatted diffs

  • Loading branch information...
dbloete committed Jan 31, 2010
1 parent 7587648 commit 764d9ccbfe92ffc4de5e566d619e043b7c707343
View
@@ -4,12 +4,12 @@
@interface DiffController : UIViewController <UIWebViewDelegate> {
NSArray *files;
NSUInteger index;
- UITextView *contentView;
+ UIWebView *contentView;
}
@property(nonatomic,retain)NSArray *files;
@property(nonatomic,assign)NSUInteger index;
-@property(nonatomic,retain)IBOutlet UITextView *contentView;
+@property(nonatomic,retain)IBOutlet UIWebView *contentView;
- (id)initWithFiles:(NSArray *)theFiles currentIndex:(NSUInteger)theCurrentIndex;
View
@@ -1,6 +1,11 @@
#import "DiffController.h"
+@interface DiffController ()
+- (NSString *)htmlFormatDiff:(NSString *)theDiff;
+@end
+
+
@implementation DiffController
@synthesize files;
@@ -25,22 +30,48 @@ - (void)viewDidLoad {
NSDictionary *fileInfo = [files objectAtIndex:index];
self.title = [[fileInfo objectForKey:@"filename"] lastPathComponent];
NSString *diff = [fileInfo objectForKey:@"diff"];
- DJLog(@"Diff: %@", diff);
-// NSString *stylePath = [[NSBundle mainBundle] pathForResource:@"styles" ofType:@"html"];
-// NSString *style = [NSString stringWithContentsOfFile:stylePath encoding:NSUTF8StringEncoding error:nil];
-// NSString *html = [NSString stringWithFormat:@"%@%@", style, diff];
-// [contentView loadHTMLString:html baseURL:nil];
- contentView.text = diff;
+ NSString *formattedDiff = [self htmlFormatDiff:diff];
+ DJLog(@"Diff:\n-----------------------------------\n%@", diff);
+ NSString *formatPath = [[NSBundle mainBundle] pathForResource:@"format" ofType:@"html"];
+ NSString *format = [NSString stringWithContentsOfFile:formatPath encoding:NSUTF8StringEncoding error:nil];
+ NSString *html = [NSString stringWithFormat:format, formattedDiff];
+ [contentView loadHTMLString:html baseURL:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
-// [contentView stopLoading];
-// contentView.delegate = nil;
+ [contentView stopLoading];
+ contentView.delegate = nil;
[super viewWillDisappear:animated];
}
- (void)viewDidUnload {
self.contentView = nil;
}
+- (NSString *)htmlFormatDiff:(NSString *)theDiff {
+ NSArray *lines = [theDiff componentsSeparatedByString:@"\n"];
+ NSMutableString *diff = [NSMutableString string];
+ for (NSString *line in lines) {
+ if ([line hasPrefix:@"@@"]) {
+ [diff appendFormat:@"<div class='lines'>%@</div>", line];
+ } else if ([line hasPrefix:@"+"]) {
+ [diff appendFormat:@"<div class='added'>%@</div>", line];
+ } else if ([line hasPrefix:@"-"]) {
+ [diff appendFormat:@"<div class='removed'>%@</div>", line];
+ } else {
+ [diff appendFormat:@"<div>%@</div>", line];
+ }
+ }
+ return [NSString stringWithFormat:@"<pre id='diff' class='diff'>%@</pre>", diff];
+}
+
+- (void)webViewDidFinishLoad:(UIWebView *)webView {
+ NSUInteger width = [[webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('diff').scrollWidth"] intValue];
+ if (width > webView.frame.size.width) {
+ NSString *js = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.width = '%dpx';", width];
+ [webView stringByEvaluatingJavaScriptFromString:js];
+ DJLog(@"Reset width: %@", js);
+ }
+}
+
@end
@@ -28,9 +28,10 @@ - (void)viewDidLoad {
[entry.user addObserver:self forKeyPath:kUserGravatarKeyPath options:NSKeyValueObservingOptionNew context:nil];
self.title = [entry.eventType capitalizedString];
titleLabel.text = entry.title;
- NSString *stylePath = [[NSBundle mainBundle] pathForResource:@"styles" ofType:@"html"];
- NSString *style = [NSString stringWithContentsOfFile:stylePath encoding:NSUTF8StringEncoding error:nil];
- NSString *html = [NSString stringWithFormat:@"%@%@", style, entry.content];
+ NSString *feedEntry = [NSString stringWithFormat:@"<div class='feed_entry'>%@</div>", entry.content];
+ NSString *formatPath = [[NSBundle mainBundle] pathForResource:@"format" ofType:@"html"];
+ NSString *format = [NSString stringWithContentsOfFile:formatPath encoding:NSUTF8StringEncoding error:nil];
+ NSString *html = [NSString stringWithFormat:format, feedEntry];
[contentView loadHTMLString:html baseURL:nil];
// Date
dateLabel.text = [entry.date prettyDate];
View
@@ -10,9 +10,6 @@
@interface NSDate (Nibware)
-
- (NSString*) prettyDate;
-
- (NSString*) prettyDateWithReference:(NSDate*)reference;
-
@end
View
@@ -11,11 +11,9 @@
@implementation NSDate (Nibware)
-- (NSString*) prettyDateWithReference:(NSDate*)reference
-{
+- (NSString *)prettyDateWithReference:(NSDate *)reference {
float diff = [reference timeIntervalSinceDate:self];
float day_diff = floor(diff / 86400);
-
if (day_diff <= 0) {
if (diff < 60) return @"just now";
if (diff < 120) return @"1 minute ago";
@@ -37,11 +35,8 @@ - (NSString*) prettyDateWithReference:(NSDate*)reference
return [self description];
}
-- (NSString*) prettyDate
-{
+- (NSString *)prettyDate {
return [self prettyDateWithReference:[NSDate date]];
}
-
-
@end
View
@@ -40,34 +40,30 @@
<int key="NSvFlags">292</int>
<object class="NSMutableArray" key="NSSubviews">
<bool key="EncodedWithXMLCoder">YES</bool>
- <object class="IBUITextView" id="591584933">
+ <object class="IBUIWebView" id="13877936">
<reference key="NSNextResponder" ref="191373211"/>
- <int key="NSvFlags">292</int>
- <string key="NSFrameSize">{320, 416}</string>
+ <int key="NSvFlags">274</int>
+ <string key="NSFrameSize">{320, 367}</string>
<reference key="NSSuperview" ref="191373211"/>
- <bool key="IBUIOpaque">NO</bool>
+ <object class="NSColor" key="IBUIBackgroundColor">
+ <int key="NSColorSpace">1</int>
+ <bytes key="NSRGB">MSAxIDEAA</bytes>
+ </object>
<bool key="IBUIClipsSubviews">YES</bool>
<bool key="IBUIMultipleTouchEnabled">YES</bool>
- <bool key="IBUIShowsHorizontalScrollIndicator">NO</bool>
- <bool key="IBUIDelaysContentTouches">NO</bool>
- <bool key="IBUICanCancelContentTouches">NO</bool>
- <bool key="IBUIBouncesZoom">NO</bool>
- <string key="IBUIText">Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</string>
- <object class="IBUITextInputTraits" key="IBUITextInputTraits">
- <int key="IBUIAutocapitalizationType">2</int>
- </object>
+ <bool key="IBUIScalesPageToFit">YES</bool>
</object>
</object>
- <string key="NSFrameSize">{320, 411}</string>
+ <string key="NSFrameSize">{320, 367}</string>
<reference key="NSSuperview"/>
<object class="NSColor" key="IBUIBackgroundColor">
<int key="NSColorSpace">3</int>
<bytes key="NSWhite">MQA</bytes>
- <object class="NSColorSpace" key="NSCustomColorSpace">
- <int key="NSID">2</int>
- </object>
</object>
<object class="IBUISimulatedStatusBarMetrics" key="IBUISimulatedStatusBarMetrics"/>
+ <object class="IBUISimulatedNavigationBarMetrics" key="IBUISimulatedTopBarMetrics">
+ <bool key="IBUIPrompted">NO</bool>
+ </object>
<object class="IBUISimulatedTabBarMetrics" key="IBUISimulatedBottomBarMetrics"/>
</object>
<object class="IBUIBarButtonItem" id="547951780">
@@ -125,9 +121,17 @@
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">contentView</string>
<reference key="source" ref="372490531"/>
- <reference key="destination" ref="591584933"/>
+ <reference key="destination" ref="13877936"/>
</object>
- <int key="connectionID">16</int>
+ <int key="connectionID">18</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBCocoaTouchOutletConnection" key="connection">
+ <string key="label">delegate</string>
+ <reference key="source" ref="13877936"/>
+ <reference key="destination" ref="372490531"/>
+ </object>
+ <int key="connectionID">19</int>
</object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
@@ -144,7 +148,7 @@
<reference key="object" ref="191373211"/>
<object class="NSMutableArray" key="children">
<bool key="EncodedWithXMLCoder">YES</bool>
- <reference ref="591584933"/>
+ <reference ref="13877936"/>
</object>
<reference key="parent" ref="0"/>
</object>
@@ -174,8 +178,8 @@
<reference key="parent" ref="547951780"/>
</object>
<object class="IBObjectRecord">
- <int key="objectID">15</int>
- <reference key="object" ref="591584933"/>
+ <int key="objectID">17</int>
+ <reference key="object" ref="13877936"/>
<reference key="parent" ref="191373211"/>
</object>
</object>
@@ -189,7 +193,7 @@
<string>1.IBEditorWindowLastContentRect</string>
<string>1.IBPluginDependency</string>
<string>11.IBPluginDependency</string>
- <string>15.IBPluginDependency</string>
+ <string>17.IBPluginDependency</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
@@ -217,7 +221,7 @@
</object>
</object>
<nil key="sourceID"/>
- <int key="maxID">16</int>
+ <int key="maxID">19</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -227,7 +231,7 @@
<string key="superclassName">UIViewController</string>
<object class="NSMutableDictionary" key="outlets">
<string key="NS.key.0">contentView</string>
- <string key="NS.object.0">UITextView</string>
+ <string key="NS.object.0">UIWebView</string>
</object>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
@@ -385,14 +389,6 @@
<string key="superclassName">NSObject</string>
<reference key="sourceIdentifier" ref="911357189"/>
</object>
- <object class="IBPartialClassDescription">
- <string key="className">UIScrollView</string>
- <string key="superclassName">UIView</string>
- <object class="IBClassDescriptionSource" key="sourceIdentifier">
- <string key="majorKey">IBFrameworkSource</string>
- <string key="minorKey">UIKit.framework/Headers/UIScrollView.h</string>
- </object>
- </object>
<object class="IBPartialClassDescription">
<string key="className">UISearchBar</string>
<string key="superclassName">UIView</string>
@@ -417,14 +413,6 @@
<string key="minorKey">UIKit.framework/Headers/UISegmentedControl.h</string>
</object>
</object>
- <object class="IBPartialClassDescription">
- <string key="className">UITextView</string>
- <string key="superclassName">UIScrollView</string>
- <object class="IBClassDescriptionSource" key="sourceIdentifier">
- <string key="majorKey">IBFrameworkSource</string>
- <string key="minorKey">UIKit.framework/Headers/UITextView.h</string>
- </object>
- </object>
<object class="IBPartialClassDescription">
<string key="className">UIView</string>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
@@ -462,6 +450,14 @@
<string key="minorKey">UIKit.framework/Headers/UIViewController.h</string>
</object>
</object>
+ <object class="IBPartialClassDescription">
+ <string key="className">UIWebView</string>
+ <string key="superclassName">UIView</string>
+ <object class="IBClassDescriptionSource" key="sourceIdentifier">
+ <string key="majorKey">IBFrameworkSource</string>
+ <string key="minorKey">UIKit.framework/Headers/UIWebView.h</string>
+ </object>
+ </object>
</object>
</object>
<int key="IBDocument.localizationMode">0</int>
View
@@ -0,0 +1,27 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us" >
+<head>
+<style type="text/css">
+* { margin:0; padding:0; }
+body { background-color:white; font:14px/1.4 Helvetica,sans-serif; }
+/* Feed Entry */
+div.feed_entry { padding:.9em; }
+div.feed_entry blockquote { color:#666; }
+div.feed_entry a { color:#4183C4; text-decoration:none; margin:0 .35em; }
+div.feed_entry span[style^="color"] { margin:0 .35em; }
+div.feed_entry b { margin:0 .35em 0 0; }
+div.feed_entry .details { white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
+div.feed_entry .details div { white-space:normal; }
+div.feed_entry .commits li { margin:1em 0 0 1.4em; }
+div.feed_entry .committer { font-weight:bold; margin:0 .35em 0 0; }
+/* Diff */
+.diff { padding:.5em;font-size:12px; }
+.diff .lines { background-color:#EAF2F5;color:#999; }
+.diff .added { background-color:#DFD; }
+.diff .removed { background-color:#FDD; }
+</style>
+</head>
+<body>
+%@
+</body>
+</html>
Oops, something went wrong.

0 comments on commit 764d9cc

Please sign in to comment.