Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 33886765f6322bbe0b9624650f0f628a1904775c @michaeltyson committed Jul 31, 2010
@@ -0,0 +1,2 @@
+/* Localized versions of Info.plist keys */
+

Large diffs are not rendered by default.

Oops, something went wrong.
BIN Icon.icns
Binary file not shown.
@@ -0,0 +1,14 @@
+Reginald
+========
+
+Reginald is a kindly old gentleman devoted to assisting you with those tricky regular expressions.
+
+Provide some sample input, and your regular expression, and Reginald will provide you with colour-coded output and a list of all your matches and the corresponding capture groups for your exploration. Select a match or capture group in the list to the right, and the corresponding text will be selected in the panel to the left.
+
+Reginald is built on [RegexKitLite](http://regexkit.sourceforge.net/RegexKitLite/), and so uses the [ICU syntax](http://regexkit.sourceforge.net/RegexKitLite/index.html#ICUSyntax).
+
+
+
+----------
+
+Michael Tyson <michael at atastypixel.com>, [A Tasty Pixel](http://atastypixel.com)
@@ -0,0 +1,13 @@
+//
+// RGAttributedStringValueTransformer.h
+// Reginald
+//
+// Created by Michael Tyson on 31/07/2010.
+// Copyright 2010 A Tasty Pixel. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+
+@interface RGAttributedStringValueTransformer: NSValueTransformer {}
+@end
@@ -0,0 +1,28 @@
+//
+// RGAttributedStringValueTransformer.m
+// Reginald
+//
+// Created by Michael Tyson on 31/07/2010.
+// Copyright 2010 A Tasty Pixel. All rights reserved.
+//
+
+#import "RGAttributedStringValueTransformer.h"
+
+
+@implementation RGAttributedStringValueTransformer
+
++ (Class)transformedValueClass { return [NSString class]; }
++ (BOOL)allowsReverseTransformation { return YES; }
+
+- (id)transformedValue:(id)value {
+ return (value == nil) ? nil : [[[NSAttributedString alloc]
+ initWithString:(NSString*)value
+ attributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Monaco" size:14] forKey:NSFontAttributeName]] autorelease];
+}
+
+- (id)reverseTransformedValue:(id)value {
+ return (value == nil) ? nil : [(NSAttributedString*)value string];
+}
+
+
+@end
@@ -0,0 +1,17 @@
+//
+// RGMainViewController.h
+// Reginald
+//
+// Created by Michael Tyson on 31/07/2010.
+// Copyright 2010 A Tasty Pixel. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+@class RGRegexEvaluator;
+@interface RGMainViewController : NSObject <NSOutlineViewDelegate> {
+ IBOutlet RGRegexEvaluator *evaluator;
+ IBOutlet NSTextView *outputView;
+}
+
+@end
@@ -0,0 +1,25 @@
+//
+// RGMainViewController.m
+// Reginald
+//
+// Created by Michael Tyson on 31/07/2010.
+// Copyright 2010 A Tasty Pixel. All rights reserved.
+//
+
+#import "RGMainViewController.h"
+#import "RGRegexEvaluator.h"
+
+@implementation RGMainViewController
+
+- (void)awakeFromNib {
+ evaluator.input = NSLocalizedString(@"Enter text here to apply the above regular expression upon", @"");
+ evaluator.regex = @"[A-Za-z]+";
+}
+
+- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item {
+ NSRange range = [[[item representedObject] valueForKey:@"range"] rangeValue];
+ [outputView setSelectedRange:range];
+ return YES;
+}
+
+@end
@@ -0,0 +1,28 @@
+//
+// RGRegexEvaluator.h
+// Reginald
+//
+// Created by Michael Tyson on 31/07/2010.
+// Copyright 2010 A Tasty Pixel. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+
+@interface RGRegexEvaluator : NSObject {
+ NSString *regex;
+ NSString *input;
+
+ NSAttributedString *attributedOutput;
+ NSArray *captures;
+ NSString *errorDescription;
+}
+
+- (NSAttributedString*)attributedOutput;
+- (NSArray*)captures;
+- (NSString*)errorDescription;
+
+@property (nonatomic, retain) NSString *regex;
+@property (nonatomic, retain) NSString *input;
+
+@end
@@ -0,0 +1,105 @@
+//
+// RGRegexEvaluator.m
+// Reginald
+//
+// Created by Michael Tyson on 31/07/2010.
+// Copyright 2010 A Tasty Pixel. All rights reserved.
+//
+
+#import "RGRegexEvaluator.h"
+#import "RegexKitLite.h"
+
+@interface RGRegexEvaluator ()
+- (void)evaluate;
+@property (nonatomic, retain) NSAttributedString *attributedOutput;
+@property (nonatomic, retain) NSArray *captures;
+@property (nonatomic, retain) NSString *errorDescription;
+@end
+
+@implementation RGRegexEvaluator
+@synthesize input, regex, attributedOutput, captures, errorDescription;
+
++(NSSet *) keyPathsForValuesAffectingValueForKey:(NSString *)key {
+ if ( [key isEqualToString:@"attributedOutput"] || [key isEqualToString:@"captures"] || [key isEqualToString:@"errorDescription"] ) {
+ return [[super keyPathsForValuesAffectingValueForKey:key] setByAddingObjectsFromSet:[NSSet setWithObjects:@"regex", @"input", nil]];
+ }
+ return [super keyPathsForValuesAffectingValueForKey:key];
+}
+
+- (void)dealloc {
+ self.regex = nil;
+ self.input = nil;
+ self.attributedOutput = nil;
+ self.captures = nil;
+ self.errorDescription = nil;
+ [super dealloc];
+}
+
+- (void)setRegex:(NSString *)newRegex {
+ [newRegex retain];
+ if ( regex ) [regex release];
+ regex = newRegex;
+
+ [self evaluate];
+}
+
+- (void)setInput:(NSString *)newInput {
+ [newInput retain];
+ if ( input ) [input release];
+ input = newInput;
+
+ [self evaluate];
+}
+
+
+- (void)evaluate {
+ if ( !input || [input length] == 0 || !regex || [regex length] == 0 ) {
+ self.attributedOutput = nil;
+ self.captures = nil;
+ self.errorDescription = nil;
+ return;
+ }
+
+ attributedOutput = [[NSMutableAttributedString alloc] initWithString:input attributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Monaco" size:14] forKey:NSFontAttributeName]];
+ captures = [[NSMutableArray alloc] init];
+ errorDescription = nil;
+
+ NSArray *captureColours = [NSArray arrayWithObjects:
+ [NSColor colorWithCalibratedRed:0.121 green:0.288 blue:0.308 alpha:1.000],
+ [NSColor colorWithCalibratedRed:0.203 green:0.482 blue:0.516 alpha:1.000],
+ [NSColor colorWithCalibratedRed:0.266 green:0.630 blue:0.674 alpha:1.000],
+ [NSColor colorWithCalibratedRed:0.508 green:0.819 blue:0.846 alpha:1.000],
+ [NSColor colorWithCalibratedRed:0.304 green:0.367 blue:0.328 alpha:1.000],
+ [NSColor colorWithCalibratedRed:0.458 green:0.552 blue:0.494 alpha:1.000],
+ [NSColor colorWithCalibratedRed:0.582 green:0.701 blue:0.628 alpha:1.000],
+ [NSColor colorWithCalibratedRed:0.694 green:0.837 blue:0.749 alpha:1.000],
+ nil];
+
+ NSError *error = nil;
+ if ( ![input enumerateStringsMatchedByRegex:regex options:0 inRange:NSMakeRange(0, [input length]) error:&error enumerationOptions:0 usingBlock:^(NSInteger captureCount, NSString * const capturedStrings[captureCount], const NSRange capturedRanges[captureCount], volatile BOOL * const stop) {
+ NSMutableDictionary *match = [NSMutableDictionary dictionary];
+ [match setObject:[NSNumber numberWithInt:[self.captures count]+1] forKey:@"number"];
+ [match setObject:capturedStrings[0] forKey:@"content"];
+ [match setObject:[NSValue valueWithRange:capturedRanges[0]] forKey:@"range"];
+
+ NSMutableArray *caps = [NSMutableArray arrayWithCapacity:captureCount];
+ for ( NSInteger i=0; i<captureCount; i++ ) {
+ [caps addObject:[NSDictionary dictionaryWithObjectsAndKeys:
+ [NSNumber numberWithInt:i], @"number",
+ capturedStrings[i], @"content",
+ [NSValue valueWithRange:capturedRanges[i]], @"range", nil]];
+ [(NSMutableAttributedString*)attributedOutput addAttributes:[NSDictionary dictionaryWithObject:[captureColours objectAtIndex:(i%([captureColours count]/2)) + (([captureColours count]/2)*([captures count]%2))]
+ forKey:NSForegroundColorAttributeName]
+ range:capturedRanges[i]];
+
+ }
+ [match setObject:caps forKey:@"captures"];
+
+ [(NSMutableArray*)captures addObject:match];
+ }] ) {
+ self.errorDescription = [error localizedDescription];
+ }
+
+}
+
+@end
Oops, something went wrong.

0 comments on commit 3388676

Please sign in to comment.