Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit d3a2975f2588c80193da95d4229f2b3aaa8a902b @groue committed Nov 1, 2010
Showing with 2,455 additions and 0 deletions.
  1. +8 −0 .gitignore
  2. +11 −0 Classes/GRMustache.h
  3. +30 −0 Classes/GRMustache.m
  4. +15 −0 Classes/GRMustacheContext.h
  5. +68 −0 Classes/GRMustacheContext.m
  6. +13 −0 Classes/GRMustacheContext_private.h
  7. +9 −0 Classes/GRMustacheElement.h
  8. +15 −0 Classes/GRMustacheElement.m
  9. +12 −0 Classes/GRMustacheElement_private.h
  10. +23 −0 Classes/GRMustacheError.h
  11. +10 −0 Classes/GRMustacheError.m
  12. +13 −0 Classes/GRMustacheLambda.h
  13. +41 −0 Classes/GRMustacheLambda.m
  14. +12 −0 Classes/GRMustacheLambda_private.h
  15. +140 −0 Classes/GRMustacheSectionElement.m
  16. +19 −0 Classes/GRMustacheSectionElement_private.h
  17. +77 −0 Classes/GRMustacheTemplate.h
  18. +527 −0 Classes/GRMustacheTemplate.m
  19. +19 −0 Classes/GRMustacheTemplateLoader.h
  20. +168 −0 Classes/GRMustacheTemplateLoader.m
  21. +12 −0 Classes/GRMustacheTemplateLoader_private.h
  22. +14 −0 Classes/GRMustacheTemplate_private.h
  23. +38 −0 Classes/GRMustacheTextElement.m
  24. +15 −0 Classes/GRMustacheTextElement_private.h
  25. +59 −0 Classes/GRMustacheVariableElement.m
  26. +13 −0 Classes/GRMustacheVariableElement_private.h
  27. +20 −0 Classes/GRMustache_private.h
  28. +445 −0 GRMustache.xcodeproj/project.pbxproj
  29. +22 −0 GRMustacheTest-Info.plist
  30. +1 −0 Resources/comments.mustache
  31. +17 −0 Resources/complex_view.mustache
  32. +9 −0 Resources/crazy_recursive.mustache
  33. +8 −0 Resources/delimiters.mustache
  34. +7 −0 Resources/double_section.mustache
  35. +1 −0 Resources/escaped.mustache
  36. +1 −0 Resources/inner_partial.mustache
  37. +1 −0 Resources/inner_partial.txt
  38. +7 −0 Resources/inverted_section.mustache
  39. +7 −0 Resources/lambda.mustache
  40. +1 −0 Resources/namespaced.mustache
  41. +17 −0 Resources/nested_objects.mustache
  42. +8 −0 Resources/node.mustache
  43. +3 −0 Resources/partial_with_module.mustache
  44. +5 −0 Resources/passenger.conf
  45. +4 −0 Resources/recursive.mustache
  46. +5 −0 Resources/simple.mustache
  47. +2 −0 Resources/template_partial.mustache
  48. +4 −0 Resources/template_partial.txt
  49. +1 −0 Resources/unescaped.mustache
  50. +3 −0 Resources/utf8.mustache
  51. +1 −0 Resources/utf8_partial.mustache
  52. +9 −0 Tests/GRMustacheTest.h
  53. +321 −0 Tests/GRMustacheTest.m
  54. +17 −0 Tests/GRMustacheTestBase.h
  55. +34 −0 Tests/GRMustacheTestBase.m
  56. +12 −0 Tests/GRPartialTest.h
  57. +81 −0 Tests/GRPartialTest.m
@@ -0,0 +1,8 @@
+# xcode noise
+build
+*.pbxuser
+*.mode1v3
+
+# osx noise
+.DS_Store
+
@@ -0,0 +1,11 @@
+//
+// GRMustache.h
+//
+
+#import <Foundation/Foundation.h>
+#import "GRMustacheError.h"
+#import "GRMustacheContext.h"
+#import "GRMustacheLambda.h"
+#import "GRMustacheTemplateLoader.h"
+#import "GRMustacheTemplate.h"
+
@@ -0,0 +1,30 @@
+//
+// GRMustache.m
+//
+
+#import "GRMustache_private.h"
+#import "GRMustacheLambda_private.h"
+
+
+@implementation GRMustache
+
++ (GRMustacheObjectKind)objectKind:(id)object {
+ if (object == nil || object == [NSNull null]) {
+ return GRMustacheObjectKindFalseValue;
+ }
+ if ([object isKindOfClass:[NSDictionary class]]) {
+ return GRMustacheObjectKindContext;
+ }
+ if ([object conformsToProtocol:@protocol(GRMustacheContext)]) {
+ return GRMustacheObjectKindContext;
+ }
+ if ([object conformsToProtocol:@protocol(NSFastEnumeration)]) {
+ return GRMustacheObjectKindEnumerable;
+ }
+ if ([object isKindOfClass:[GRMustacheLambdaBlockWrapper class]]) {
+ return GRMustacheObjectKindLambda;
+ }
+ return GRMustacheObjectKindTrueValue;
+}
+
+@end
@@ -0,0 +1,15 @@
+//
+// GRMustacheContext.h
+//
+
+#import <Foundation/Foundation.h>
+
+
+@protocol GRMustacheContext
+@end
+
+@interface GRMustacheContext: NSObject {
+ NSMutableArray *objects;
+}
+- (id)valueForKey:(NSString *)key;
+@end
@@ -0,0 +1,68 @@
+//
+// GRMustacheContext.m
+//
+
+#import "GRMustache_private.h"
+#import "GRMustacheContext_private.h"
+
+
+@interface GRMustacheContext()
+- (id)initWithObject:(id)object;
+@end
+
+
+@implementation GRMustacheContext
+@synthesize objects;
+
++ (id)contextWithObject:(id)object {
+ return [[[self alloc] initWithObject:object] autorelease];
+}
+
+- (id)initWithObject:(id)object {
+ if (self = [self init]) {
+ objects = [[NSMutableArray arrayWithCapacity:4] retain];
+ [self pushObject:object];
+ }
+ return self;
+}
+
+- (void)pushObject:(id)object {
+ switch ([GRMustache objectKind:object]) {
+ case GRMustacheObjectKindFalseValue:
+ [objects addObject:[NSNull null]];
+ break;
+ case GRMustacheObjectKindContext:
+ case GRMustacheObjectKindLambda:
+ [objects addObject:object];
+ break;
+ default:
+ NSAssert(NO, @"object is not a NSDictionary, or does not conform to GRMustacheContext protocol, or is not a GRMustacheLambda.");
+ break;
+ }
+}
+
+- (void)pop {
+ [objects removeLastObject];
+}
+
+- (id)valueForKey:(NSString *)key {
+ id value;
+ for (id object in [objects reverseObjectEnumerator]) {
+ if (object == [NSNull null]) {
+ continue;
+ }
+ value = [object valueForKey:key];
+ if (value != nil) {
+ return value;
+ }
+ }
+
+ return nil;
+}
+
+- (void)dealloc {
+ [objects release];
+ [super dealloc];
+}
+
+@end
@@ -0,0 +1,13 @@
+//
+// GRMustacheContext_private.h
+//
+
+#import "GRMustacheContext.h"
+
+
+@interface GRMustacheContext()
+@property (nonatomic, retain) NSMutableArray *objects;
++ (id)contextWithObject:(id)object;
+- (void)pushObject:(id)object;
+- (void)pop;
+@end
@@ -0,0 +1,9 @@
+//
+// GRMustacheElement.h
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface GRMustacheElement: NSObject
+@end
@@ -0,0 +1,15 @@
+//
+// GRMustacheElement.h
+//
+
+#import "GRMustacheElement_private.h"
+
+
+@implementation GRMustacheElement
+
+- (NSString *)renderContext:(GRMustacheContext *)context {
+ // subclasses should override
+ return @"";
+}
+
+@end
@@ -0,0 +1,12 @@
+//
+// GRMustacheElement_private.h
+//
+
+#import "GRMustacheElement.h"
+
+
+@class GRMustacheContext;
+
+@interface GRMustacheElement()
+- (NSString *)renderContext:(GRMustacheContext *)context;
+@end
@@ -0,0 +1,23 @@
+//
+// GRMustacheError.h
+//
+
+#import <Foundation/Foundation.h>
+
+
+// The domain of returned errors
+extern NSString* const GRMustacheErrorDomain;
+
+// The key containing the url where the error occurred
+extern NSString* const GRMustacheErrorURL;
+
+// The key containing the error line
+extern NSString* const GRMustacheErrorLine;
+
+// The codes of returned errors
+typedef enum {
+ GRMustacheErrorCodeParseError,
+ GRMustacheErrorCodePartialNotFound,
+} GRMustacheErrorCode;
+
+
@@ -0,0 +1,10 @@
+//
+// GRMustacheError.m
+//
+
+#import "GRMustacheError.h"
+
+
+NSString* const GRMustacheErrorDomain = @"GRMustacheErrorDomain";
+NSString* const GRMustacheErrorLine = @"GRMustacheErrorLine";
+NSString* const GRMustacheErrorURL = @"GRMustacheErrorURL";
@@ -0,0 +1,13 @@
+//
+// GRMustacheLambda.h
+//
+
+#import <Foundation/Foundation.h>
+#import "GRMustacheContext.h"
+
+
+typedef NSString *(^GRMustacheRenderer)(NSString *, NSError **);
+typedef NSString *(^GRMustacheLambdaBlock)(GRMustacheContext *, NSString *, GRMustacheRenderer);
+typedef id GRMustacheLambda;
+
+GRMustacheLambda GRMustacheLambdaMake(GRMustacheLambdaBlock block);
@@ -0,0 +1,41 @@
+//
+// GRMustacheLambda.m
+//
+
+#import "GRMustacheLambda_private.h"
+
+
+@interface GRMustacheLambdaBlockWrapper()
++ (id)lambdaWithBlock:(GRMustacheLambdaBlock)block;
+- (id)initWithBlock:(GRMustacheLambdaBlock)block;
+@end
+
+
+@implementation GRMustacheLambdaBlockWrapper
+
++ (id)lambdaWithBlock:(GRMustacheLambdaBlock)block {
+ return [[[self alloc] initWithBlock:block] autorelease];
+}
+
+- (id)initWithBlock:(GRMustacheLambdaBlock)theBlock {
+ if (self = [self init]) {
+ block = [theBlock copy];
+ }
+ return self;
+}
+
+- (NSString *)renderContext:(GRMustacheContext *)context fromString:(NSString *)templateString renderer:(GRMustacheRenderer)renderer {
+ return block(context, templateString, renderer);
+}
+
+- (void)dealloc {
+ [block release];
+ [super dealloc];
+}
+
+@end
+
+
+GRMustacheLambda GRMustacheLambdaMake(GRMustacheLambdaBlock block) {
+ return [GRMustacheLambdaBlockWrapper lambdaWithBlock:block];
+}
@@ -0,0 +1,12 @@
+//
+// GRMustacheLambda_private.h
+//
+
+#import "GRMustacheLambda.h"
+
+
+@interface GRMustacheLambdaBlockWrapper: NSObject {
+ GRMustacheLambdaBlock block;
+}
+- (NSString *)renderContext:(GRMustacheContext *)context fromString:(NSString *)templateString renderer:(GRMustacheRenderer)renderer;
+@end
Oops, something went wrong.

0 comments on commit d3a2975

Please sign in to comment.