Skip to content
This repository
Newer
Older
100644 118 lines (101 sloc) 2.757 kb
17163002 »
2010-05-03 Added auto-linking
1 //
2 // IGKWordMembership.m
3 // Ingredients
4 //
5 // Created by Alex Gordon on 02/05/2010.
f1097128 »
2010-07-03 Adjusted things
6 // Written in 2010 by Fileability.
17163002 »
2010-05-03 Added auto-linking
7 //
8
9 #import "IGKWordMembership.h"
10
11
12 @implementation IGKWordMembership
13
14 - (id)initWithCapacity:(NSUInteger)capacity
15 {
16 if (self = [super init])
17 {
18 words = [[NSHashTable alloc] initWithOptions:NSHashTableStrongMemory capacity:capacity];
19 }
20 }
21 - (void)addWord:(NSString *)word
22 {
23 [words addObject:word];
24 }
25
26 - (NSString *)addHyperlinksToPassage:(NSString *)passage
27 {
28 NSUInteger length = [passage length];
29
30 NSMutableString *newString = [[NSMutableString alloc] initWithCapacity:length];
31 __block NSRange previousRange = NSMakeRange(0, 0);
32
33 [passage enumerateSubstringsInRange:NSMakeRange(0, length)
34 options:NSStringEnumerationByWords
35 usingBlock:
36 ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
37
38 NSInteger inbetweenLength = substringRange.location - NSMaxRange(previousRange);
39 if (inbetweenLength > 0)
40 {
41 [newString appendString:[passage substringWithRange:NSMakeRange(NSMaxRange(previousRange), inbetweenLength)]];
42 }
43
375b1257 »
2010-06-05 Now does deprecated methods... sorta
44 if ([substring length] > 1 && [words containsObject:substring])
17163002 »
2010-05-03 Added auto-linking
45 {
46 [newString appendString:@"<a href='http://ingr-link/"];
47 [newString appendString:substring];
48 [newString appendString:@"' class='semistealth'><span>"];
49 [newString appendString:substring];
50 [newString appendString:@"</span></a>"];
51 }
52 else
53 {
54 [newString appendString:substring];
55 }
56
57 previousRange = substringRange;
58 }];
59
60 NSInteger inbetweenLength = length - NSMaxRange(previousRange);
61 if (inbetweenLength > 0)
62 {
63 [newString appendString:[passage substringWithRange:NSMakeRange(NSMaxRange(previousRange), inbetweenLength)]];
64 }
65
66 return newString;
67 }
68
69 #pragma mark Singleton
70
71 static IGKWordMembership *sharedManager = nil;
72
73 + (IGKWordMembership *)sharedManager
74 {
75 return [self sharedManagerWithCapacity:0];
76 }
77 + (IGKWordMembership *)sharedManagerWithCapacity:(NSUInteger)capacity
78 {
79 @synchronized(self) {
80 if (sharedManager == nil) {
81 [[self alloc] initWithCapacity:capacity]; // assignment not done here
82 }
83 }
84 return sharedManager;
85 }
86 + (id)allocWithZone:(NSZone *)zone
87 {
88 @synchronized(self) {
89 if (sharedManager == nil) {
90 sharedManager = [super allocWithZone:zone];
91 return sharedManager; // assignment and return on first allocation
92 }
93 }
94 return nil; //on subsequent allocation attempts return nil
95 }
96 - (id)copyWithZone:(NSZone *)zone
97 {
98 return self;
99 }
100 - (id)retain
101 {
102 return self;
103 }
104 - (unsigned)retainCount
105 {
106 return UINT_MAX; //denotes an object that cannot be released
107 }
108 - (void)release
109 {
110 //do nothing
111 }
112 - (id)autorelease
113 {
114 return self;
115 }
116
117 @end
Something went wrong with that request. Please try again.