forked from mattgemmell/MGTemplateEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMGTemplateStandardFilters.m
113 lines (96 loc) · 3.96 KB
/
MGTemplateStandardFilters.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// MGTemplateStandardFilters.m
//
// Created by Matt Gemmell on 13/05/2008.
// Copyright 2008 Instinctive Code. All rights reserved.
//
#import "MGTemplateStandardFilters.h"
#define UPPERCASE @"uppercase"
#define LOWERCASE @"lowercase"
#define CAPITALIZED @"capitalized"
#define DATE_FORMAT @"date_format"
#define COLOR_FORMAT @"color_format"
#define DEFAULT @"default"
@implementation MGTemplateStandardFilters
- (NSArray *)filters
{
return [NSArray arrayWithObjects:
UPPERCASE, LOWERCASE, CAPITALIZED,
DATE_FORMAT, COLOR_FORMAT,
DEFAULT,
nil];
}
// Returns a concatenated version of an array of strings.
- (NSString*) stringsConcatenatedWithSpaces: (NSArray*) array {
NSMutableString* result = [NSMutableString string];
for (NSString* item in array) {
[result appendFormat: @"%@ ", item];
}
return result;
}
- (id)filterInvoked:(NSString *)filter withArguments:(NSArray *)args onValue:(id)value
{
if ([filter isEqualToString:UPPERCASE]) {
return [[NSString stringWithFormat:@"%@", value] uppercaseString];
} else if ([filter isEqualToString:LOWERCASE]) {
return [[NSString stringWithFormat:@"%@", value] lowercaseString];
} else if ([filter isEqualToString:CAPITALIZED]) {
return [[NSString stringWithFormat:@"%@", value] capitalizedString];
} else if ([filter isEqualToString:DATE_FORMAT]) {
// Formats NSDates according to Unicode syntax:
// http://unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns
// e.g. "dd MM yyyy" etc.
if ([value isKindOfClass:[NSDate class]] && [args count] == 1) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
NSString *format = [args objectAtIndex:0];
[dateFormatter setDateFormat:format];
return [dateFormatter stringFromDate:(NSDate *)value];
}
} else if ([filter isEqualToString:COLOR_FORMAT]) {
#if TARGET_OS_IPHONE
if ([value isKindOfClass:[UIColor class]] && [args count] == 1) {
#else
if ([value isKindOfClass:[NSColor class]] && [args count] == 1) {
#endif
NSString *format = [[args objectAtIndex:0] lowercaseString];
if ([format isEqualToString:@"hex"]) {
// Output color in hex format RRGGBB (without leading # character).
#if TARGET_OS_IPHONE
CGColorRef color = [(UIColor *)value CGColor];
CGColorSpaceRef colorSpace = CGColorGetColorSpace(color);
CGColorSpaceModel colorSpaceModel = CGColorSpaceGetModel(colorSpace);
if (colorSpaceModel != kCGColorSpaceModelRGB)
return @"000000";
const CGFloat *components = CGColorGetComponents(color);
NSString *colorHex = [NSString stringWithFormat:@"%02x%02x%02x",
(unsigned int)(components[0] * 255),
(unsigned int)(components[1] * 255),
(unsigned int)(components[2] * 255)];
return colorHex;
#else
NSColor *color = [(NSColor *)value colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
if (!color) { // happens if the colorspace couldn't be converted
return @"000000"; // black
} else {
NSString *colorHex = [NSString stringWithFormat:@"%02x%02x%02x",
(unsigned int)([color redComponent] * 255),
(unsigned int)([color greenComponent] * 255),
(unsigned int)([color blueComponent] * 255)];
return colorHex;
}
#endif
}
}
} else if ([filter isEqualToString: DEFAULT]) {
// If argument to default is either nil or the empty string, use all arguments after the default keyword instead.
if (!value) {
return [self stringsConcatenatedWithSpaces: args];
}
if ([value isKindOfClass: [NSString class]] && [(NSString*)value length] == 0) {
return [self stringsConcatenatedWithSpaces: args];
}
}
return value;
}
@end