Skip to content

Commit

Permalink
Turn NSFormatter and NSValueTransformer into filters
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Feb 4, 2013
1 parent d0f0013 commit a5a5c7a
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 5 deletions.
7 changes: 5 additions & 2 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Missing tests:

Experiments:
- {{.}}, {{..}}, {{...}}, {{.name}}, {{..name}}, {{...name}}, {{ROOT}}, {{ROOT.name}}
- Have filters put something in the scope: in `{{ dateFormat(date, ISO_8601) }}`, ISO_8601 would be defined by the dateFormat filter.
- Have `now` enter the standard library: `{{ dateFormat(now, ISO_8601) }}`
- date/time formatter: `{{ dateFormat(date, Short) }}`, `{{ timeFormat(date, Short) }}`, `{{ dateTimeFormat(date, Short, Full) }}`
- Have NSFormatter and NSValueTransformer be filter and "cross-platorm filters" tag delegate
- escapeJavascript
- escapeCGI
? Have filters put something in the scope: in `{{ dateFormat(date, ISO_8601) }}`, ISO_8601 would be defined by the dateFormat filter.
? date/time formatter: `{{ dateFormat(date, Short) }}`, `{{ timeFormat(date, Short) }}`, `{{ dateTimeFormat(date, Short, Full) }}`
30 changes: 27 additions & 3 deletions src/GRMustache.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@
56E2F2F016BD180100F01DC2 /* GRMustacheConfiguration6_3Test.m in Sources */ = {isa = PBXBuildFile; fileRef = 56E2F2EF16BD180100F01DC2 /* GRMustacheConfiguration6_3Test.m */; };
56E2F2F116BD180100F01DC2 /* GRMustacheConfiguration6_3Test.m in Sources */ = {isa = PBXBuildFile; fileRef = 56E2F2EF16BD180100F01DC2 /* GRMustacheConfiguration6_3Test.m */; };
56E2F2F216BD180100F01DC2 /* GRMustacheConfiguration6_3Test.m in Sources */ = {isa = PBXBuildFile; fileRef = 56E2F2EF16BD180100F01DC2 /* GRMustacheConfiguration6_3Test.m */; };
56E2F2F516C0095800F01DC2 /* NSFormatter+GRMustache.h in Headers */ = {isa = PBXBuildFile; fileRef = 56E2F2F316C0095800F01DC2 /* NSFormatter+GRMustache.h */; settings = {ATTRIBUTES = (Public, ); }; };
56E2F2F616C0095800F01DC2 /* NSFormatter+GRMustache.h in Headers */ = {isa = PBXBuildFile; fileRef = 56E2F2F316C0095800F01DC2 /* NSFormatter+GRMustache.h */; settings = {ATTRIBUTES = (Public, ); }; };
56E2F2F716C0095800F01DC2 /* NSFormatter+GRMustache.m in Sources */ = {isa = PBXBuildFile; fileRef = 56E2F2F416C0095800F01DC2 /* NSFormatter+GRMustache.m */; };
56E2F2F816C0095800F01DC2 /* NSFormatter+GRMustache.m in Sources */ = {isa = PBXBuildFile; fileRef = 56E2F2F416C0095800F01DC2 /* NSFormatter+GRMustache.m */; };
56E2F2FB16C00B4000F01DC2 /* NSValueTransformer+GRMustache.h in Headers */ = {isa = PBXBuildFile; fileRef = 56E2F2F916C00B3F00F01DC2 /* NSValueTransformer+GRMustache.h */; settings = {ATTRIBUTES = (Public, ); }; };
56E2F2FC16C00B4000F01DC2 /* NSValueTransformer+GRMustache.h in Headers */ = {isa = PBXBuildFile; fileRef = 56E2F2F916C00B3F00F01DC2 /* NSValueTransformer+GRMustache.h */; settings = {ATTRIBUTES = (Public, ); }; };
56E2F2FD16C00B4000F01DC2 /* NSValueTransformer+GRMustache.m in Sources */ = {isa = PBXBuildFile; fileRef = 56E2F2FA16C00B4000F01DC2 /* NSValueTransformer+GRMustache.m */; };
56E2F2FE16C00B4000F01DC2 /* NSValueTransformer+GRMustache.m in Sources */ = {isa = PBXBuildFile; fileRef = 56E2F2FA16C00B4000F01DC2 /* NSValueTransformer+GRMustache.m */; };
56EB54CB160ED8AB006A5F57 /* GRMustacheTemplateOverride_private.h in Headers */ = {isa = PBXBuildFile; fileRef = 56EB54C9160ED8AA006A5F57 /* GRMustacheTemplateOverride_private.h */; };
56EB54CC160ED8AB006A5F57 /* GRMustacheTemplateOverride_private.h in Headers */ = {isa = PBXBuildFile; fileRef = 56EB54C9160ED8AA006A5F57 /* GRMustacheTemplateOverride_private.h */; };
56EB54D1160EEC2B006A5F57 /* GRMustacheTemplateOverride.m in Sources */ = {isa = PBXBuildFile; fileRef = 56EB54D0160EEC2B006A5F57 /* GRMustacheTemplateOverride.m */; };
Expand Down Expand Up @@ -480,6 +488,10 @@
56E2F2E716BAEB6100F01DC2 /* GRMustacheStandardLibrary_private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GRMustacheStandardLibrary_private.h; sourceTree = "<group>"; };
56E2F2EB16BB099A00F01DC2 /* GRMustacheContextTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GRMustacheContextTest.m; sourceTree = "<group>"; };
56E2F2EF16BD180100F01DC2 /* GRMustacheConfiguration6_3Test.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GRMustacheConfiguration6_3Test.m; sourceTree = "<group>"; };
56E2F2F316C0095800F01DC2 /* NSFormatter+GRMustache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSFormatter+GRMustache.h"; sourceTree = "<group>"; };
56E2F2F416C0095800F01DC2 /* NSFormatter+GRMustache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSFormatter+GRMustache.m"; sourceTree = "<group>"; };
56E2F2F916C00B3F00F01DC2 /* NSValueTransformer+GRMustache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSValueTransformer+GRMustache.h"; sourceTree = "<group>"; };
56E2F2FA16C00B4000F01DC2 /* NSValueTransformer+GRMustache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSValueTransformer+GRMustache.m"; sourceTree = "<group>"; };
56EB54C9160ED8AA006A5F57 /* GRMustacheTemplateOverride_private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GRMustacheTemplateOverride_private.h; sourceTree = "<group>"; };
56EB54D0160EEC2B006A5F57 /* GRMustacheTemplateOverride.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GRMustacheTemplateOverride.m; sourceTree = "<group>"; };
56FAED0116B94FD600B26C6A /* GRMustacheFilter6_3Test.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GRMustacheFilter6_3Test.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -834,7 +846,7 @@
56DEC311152631A30031E8DC /* Parsing */,
56DEC310152631830031E8DC /* Compiling */,
56DEC312152631DE0031E8DC /* Runtime */,
56FE26B016B808FD00FECF56 /* StandardLibrary */,
56FE26B016B808FD00FECF56 /* Services */,
56DEC292152631300031E8DC /* GRMustache.h */,
56DEC294152631300031E8DC /* GRMustache_private.h */,
56DEC293152631300031E8DC /* GRMustache.m */,
Expand Down Expand Up @@ -983,15 +995,19 @@
path = v6.3;
sourceTree = "<group>";
};
56FE26B016B808FD00FECF56 /* StandardLibrary */ = {
56FE26B016B808FD00FECF56 /* Services */ = {
isa = PBXGroup;
children = (
56E2F2E716BAEB6100F01DC2 /* GRMustacheStandardLibrary_private.h */,
56CD619B15C5C7830067E2E7 /* GRMustacheStandardLibrary.m */,
56FE26B116B8095400FECF56 /* GRMustacheLocalizeHelper.h */,
56FE26B216B8095400FECF56 /* GRMustacheLocalizeHelper.m */,
56E2F2F316C0095800F01DC2 /* NSFormatter+GRMustache.h */,
56E2F2F416C0095800F01DC2 /* NSFormatter+GRMustache.m */,
56E2F2F916C00B3F00F01DC2 /* NSValueTransformer+GRMustache.h */,
56E2F2FA16C00B4000F01DC2 /* NSValueTransformer+GRMustache.m */,
);
name = StandardLibrary;
name = Services;
sourceTree = "<group>";
};
/* End PBXGroup section */
Expand Down Expand Up @@ -1038,6 +1054,8 @@
56B283D116B44A4E007C1A33 /* GRMustacheConfiguration_private.h in Headers */,
56FE26B316B8095400FECF56 /* GRMustacheLocalizeHelper.h in Headers */,
56E2F2E816BAEB6100F01DC2 /* GRMustacheStandardLibrary_private.h in Headers */,
56E2F2F516C0095800F01DC2 /* NSFormatter+GRMustache.h in Headers */,
56E2F2FB16C00B4000F01DC2 /* NSValueTransformer+GRMustache.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1082,6 +1100,8 @@
56B283D216B44A4E007C1A33 /* GRMustacheConfiguration_private.h in Headers */,
56FE26B416B8095400FECF56 /* GRMustacheLocalizeHelper.h in Headers */,
56E2F2E916BAEB6100F01DC2 /* GRMustacheStandardLibrary_private.h in Headers */,
56E2F2F616C0095800F01DC2 /* NSFormatter+GRMustache.h in Headers */,
56E2F2FC16C00B4000F01DC2 /* NSValueTransformer+GRMustache.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1390,6 +1410,8 @@
569EB2E7164030E300C09632 /* GRMustacheAccumulatorTag.m in Sources */,
56B11A1716B3A581009F184F /* GRMustacheConfiguration.m in Sources */,
56FE26B516B8095400FECF56 /* GRMustacheLocalizeHelper.m in Sources */,
56E2F2F716C0095800F01DC2 /* NSFormatter+GRMustache.m in Sources */,
56E2F2FD16C00B4000F01DC2 /* NSValueTransformer+GRMustache.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1459,6 +1481,8 @@
569EB2E8164030E300C09632 /* GRMustacheAccumulatorTag.m in Sources */,
56B11A1816B3A581009F184F /* GRMustacheConfiguration.m in Sources */,
56FE26B616B8095400FECF56 /* GRMustacheLocalizeHelper.m in Sources */,
56E2F2F816C0095800F01DC2 /* NSFormatter+GRMustache.m in Sources */,
56E2F2FE16C00B4000F01DC2 /* NSValueTransformer+GRMustache.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 2 additions & 0 deletions src/classes/GRMustache.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,5 @@ typedef struct {
#import "GRMustacheTag.h"
#import "GRMustacheConfiguration.h"
#import "GRMustacheLocalizeHelper.h"
#import "NSValueTransformer+GRMustache.h"
#import "NSFormatter+GRMustache.h"
29 changes: 29 additions & 0 deletions src/classes/NSFormatter+GRMustache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// The MIT License
//
// Copyright (c) 2013 Gwendal Roué
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import <Foundation/Foundation.h>
#import "GRMustacheRendering.h"
#import "GRMustacheFilter.h"
#import "GRMustacheTagDelegate.h"

@interface NSFormatter (GRMustache)<GRMustacheFilter, GRMustacheRendering, GRMustacheTagDelegate>
@end
54 changes: 54 additions & 0 deletions src/classes/NSFormatter+GRMustache.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// The MIT License
//
// Copyright (c) 2013 Gwendal Roué
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "NSFormatter+GRMustache.h"
#import "GRMustacheTag.h"
#import "GRMustacheContext.h"

@implementation NSFormatter (GRMustache)

#pragma mark - <GRMustacheFilter>

- (id)transformedValue:(id)object
{
return [self stringForObjectValue:object];
}

#pragma mark - <GRMustacheRendering>

- (NSString *)renderForMustacheTag:(GRMustacheTag *)tag context:(GRMustacheContext *)context HTMLSafe:(BOOL *)HTMLSafe error:(NSError **)error
{
context = [context contextByAddingTagDelegate:self];
return [tag renderContentWithContext:context HTMLSafe:HTMLSafe error:error];
}

#pragma mark - <GRMustacheTagDelegate>

- (id)mustacheTag:(GRMustacheTag *)tag willRenderObject:(id)object
{
if (tag.type == GRMustacheTagTypeVariable) {
return [self stringForObjectValue:object];
}
return object;
}

@end
29 changes: 29 additions & 0 deletions src/classes/NSValueTransformer+GRMustache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// The MIT License
//
// Copyright (c) 2013 Gwendal Roué
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import <Foundation/Foundation.h>
#import "GRMustacheRendering.h"
#import "GRMustacheFilter.h"
#import "GRMustacheTagDelegate.h"

@interface NSValueTransformer (GRMustache)<GRMustacheFilter, GRMustacheRendering, GRMustacheTagDelegate>
@end
47 changes: 47 additions & 0 deletions src/classes/NSValueTransformer+GRMustache.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// The MIT License
//
// Copyright (c) 2013 Gwendal Roué
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "NSValueTransformer+GRMustache.h"
#import "GRMustacheTag.h"
#import "GRMustacheContext.h"

@implementation NSValueTransformer (GRMustache)

#pragma mark - <GRMustacheRendering>

- (NSString *)renderForMustacheTag:(GRMustacheTag *)tag context:(GRMustacheContext *)context HTMLSafe:(BOOL *)HTMLSafe error:(NSError **)error
{
context = [context contextByAddingTagDelegate:self];
return [tag renderContentWithContext:context HTMLSafe:HTMLSafe error:error];
}

#pragma mark - <GRMustacheTagDelegate>

- (id)mustacheTag:(GRMustacheTag *)tag willRenderObject:(id)object
{
if (tag.type == GRMustacheTagTypeVariable) {
return [self transformedValue:object];
}
return object;
}

@end

0 comments on commit a5a5c7a

Please sign in to comment.