Permalink
Browse files

Merge branch 'global-change'

  • Loading branch information...
2 parents 4ae180e + 5d775fa commit a140a269f30c3dae6844e073113d99c8338386ca @hiddenmemory committed Apr 5, 2012
Showing with 118 additions and 75 deletions.
  1. +1 −0 Tests/Test02.html
  2. +37 −1 Tests/Test03.html
  3. +1 −2 Tipi/TPTemplateNode.h
  4. +30 −27 Tipi/TPTemplateNode.m
  5. +1 −1 Tipi/TPTemplateParser.h
  6. +46 −42 Tipi/TPTemplateParser.m
  7. +2 −2 Tipi/main.m
View
@@ -32,6 +32,7 @@
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
{{/ bind }}
{{# bind client_cs }}Box and Arrow Ltd{{/ bind }}
+ {{#bind technologies}}ALL OF THEM!{{/bind}}
{{/case_study}}
{{/include}}
View
@@ -1 +1,37 @@
-{{ a long item to get "right out of the" box 'of magic' "fun \"and\" games" }}
+
+{{#def A}}
+ {{ValueB}}
+{{/def}}
+
+{{env ValueB "This is ValueB"}}
+
+{{#def B}}
+ {{ValueB}}
+{{/def}}
+
+{{#def X}}
+ Begin X
+ {{#def Z}}
+ Z has been here.
+ {{/def Z}}
+ Begin Y
+ {{#def Y}}
+ [1] Z has been here:
+ {{Z}}
+ Begin Z
+ {{#def Z}}
+ Z hath been here.
+ {{/def Z}}
+ [2] Z hath been here:
+ {{Z}}
+ {{/def Y}}
+ {{Y}}
+ [3] Z has been here:
+ {{Z}}
+{{/def X}}
+
+{{A}}
+
+{{B}}
+
+{{X}}
@@ -23,6 +23,5 @@ typedef enum {
@property (readonly) NSMutableArray *childNodes;
+ (TPTemplateNode*)node;
-- (NSString*)expansionUsingValues:(NSDictionary*)values
- global:(NSMutableDictionary*)global;
+- (NSString*)expansionUsingEnvironment:(NSMutableDictionary*)environment;
@end
View
@@ -50,49 +50,52 @@ - (NSString*)descriptionWithDepth:(int)depth {
- (NSString*)description {
return [self descriptionWithDepth:0];
}
-- (NSString*)expansionUsingValues:(NSDictionary *)_values
- global:(NSMutableDictionary*)global
-{
- NSMutableString *expansion = [NSMutableString string];
+- (NSString*)expansionUsingEnvironment:(NSMutableDictionary*)environment {
if( self.type == TPNodeText ) {
- [expansion appendString:self.originalValue];
+ return self.originalValue;
}
else {
+ NSMutableString *expansion = [NSMutableString string];
NSString *key = [self.name lowercaseString];
- if( [global objectForKey:key] ) {
- NSString *(^expansionBlock)( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) = [global objectForKey:key];
+ if( [environment objectForKey:key] ) {
+ id value = [environment objectForKey:key];
- NSMutableArray *parameters = [NSMutableArray array];
- for( NSString *_valueName in self.values ) {
- NSString *valueName = [_valueName lowercaseString];
+ if( [[value class] isSubclassOfClass:[NSString class]] ) {
+ [expansion appendString:value];
+ }
+ else {
+ NSString *(^expansionBlock)( TPTemplateNode *node, NSMutableDictionary *global, NSArray *parameters ) = value;
- NSString *(^valueExpansionBlock)( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) = [global objectForKey:valueName];
- if( valueExpansionBlock ) {
- [parameters addObject:valueExpansionBlock(self, _values, global, nil)];
- }
- else if( [_values objectForKey:valueName] ) {
- [parameters addObject:[_values objectForKey:valueName]];
- }
- else {
- [parameters addObject:_valueName];
+ NSMutableArray *parameters = [NSMutableArray array];
+
+ for( NSString *_valueName in self.values ) {
+ NSString *valueName = [_valueName lowercaseString];
+ id valueValue = [environment objectForKey:valueName];
+
+ if( valueValue == nil ) {
+ [parameters addObject:_valueName];
+ }
+ else if( [[valueValue class] isSubclassOfClass:[NSString class]] ) {
+ [parameters addObject:valueValue];
+ }
+ else {
+ NSString *(^valueExpansionBlock)( TPTemplateNode *node, NSMutableDictionary *global, NSArray *parameters ) = valueValue;
+ [parameters addObject:valueExpansionBlock(self, environment, nil)];
+ }
}
+
+ [expansion appendString:expansionBlock(self, environment, parameters)];
}
-
- [expansion appendString:expansionBlock(self, _values, global, parameters)];
- }
- else if( [_values objectForKey:key] ) {
- [expansion appendString:[_values objectForKey:key]];
}
else {
for( TPTemplateNode *node in childNodes ) {
- [expansion appendString:[node expansionUsingValues:_values global:global]];
+ [expansion appendString:[node expansionUsingEnvironment:environment]];
}
}
+ return expansion;
}
-
- return expansion;
}
@end
@@ -10,5 +10,5 @@
@interface TPTemplateParser : NSObject
+ (TPTemplateParser*)parserForFile:(NSString*)path;
-- (NSString*)expansionUsingValues:(NSDictionary*)values;
+- (NSString*)expansionUsingEnvironment:(NSDictionary*)values;
@end
@@ -46,70 +46,74 @@ - (id)initWithFileAtPath:(NSString*)path {
}
return self;
}
-- (NSString*)expansionUsingValues:(NSDictionary*)values {
- NSMutableDictionary *globals = [NSMutableDictionary dictionary];
+- (NSString*)expansionUsingEnvironment:(NSDictionary*)values {
+ NSMutableDictionary *environment = [NSMutableDictionary dictionaryWithDictionary:values];
- [globals setObject:[^NSString*( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) { return @""; } copy]
+ [environment setObject:[^NSString*( TPTemplateNode *node, NSMutableDictionary *environment, NSArray *parameters ) { return @""; } copy]
forKey:@"bind"];
- [globals setObject:[^NSString*( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *_ ) {
+ [environment setObject:[^NSString*( TPTemplateNode *node, NSMutableDictionary *environment, NSArray *parameters ) {
NSString *key = [[node.values objectAtIndex:0] lowercaseString];
- if( [global objectForKey:key] == nil ) {
- [global setObject:[^NSString*( TPTemplateNode *_, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) {
- NSMutableString *expansion = [NSMutableString string];
- NSMutableDictionary *environment = [NSMutableDictionary dictionary];
-
- NSMutableString *thisExpansion = [NSMutableString string];
- for( TPTemplateNode *childNode in _.childNodes ) {
- [thisExpansion appendString:[childNode expansionUsingValues:values global:global]];
- }
- [environment setObject:thisExpansion forKey:@"this"];
-
- [[node.values subarrayWithRange:NSMakeRange(1, [node.values count] - 1)] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- [environment setObject:[parameters objectAtIndex:idx] forKey:[obj lowercaseString]];
- }];
-
- for( TPTemplateNode *childNode in _.childNodes ) {
- if( [childNode.name isEqualToString:@"bind"] ) {
- [environment setObject:[childNode expansionUsingValues:values global:global]
- forKey:[[childNode.values objectAtIndex:0] lowercaseString]];
- }
- }
-
- for( TPTemplateNode *childNode in node.childNodes ) {
- [expansion appendString:[childNode expansionUsingValues:environment global:global]];
+ NSMutableDictionary *freshEnvironment = [NSMutableDictionary dictionaryWithDictionary:environment];
+
+ [environment setObject:[^NSString*( TPTemplateNode *currentNode, NSMutableDictionary *environment, NSArray *parameters ) {
+ NSMutableString *expansion = [NSMutableString string];
+ NSMutableDictionary *invokeEnvironment = [NSMutableDictionary dictionaryWithDictionary:freshEnvironment];
+
+ NSMutableString *thisExpansion = [NSMutableString string];
+ for( TPTemplateNode *childNode in currentNode.childNodes ) {
+ [thisExpansion appendString:[childNode expansionUsingEnvironment:freshEnvironment]];
+ }
+ [invokeEnvironment setObject:thisExpansion forKey:@"this"];
+
+ [[node.values subarrayWithRange:NSMakeRange(1, [node.values count] - 1)] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
+ [invokeEnvironment setObject:[parameters objectAtIndex:idx] forKey:[obj lowercaseString]];
+ }];
+
+ for( TPTemplateNode *bindNode in currentNode.childNodes ) {
+ if( [bindNode.name isEqualToString:@"bind"] ) {
+ [bindNode.childNodes enumerateObjectsUsingBlock:^(TPTemplateNode *obj, NSUInteger idx, BOOL *stop) {
+ [invokeEnvironment setObject:[obj expansionUsingEnvironment:freshEnvironment]
+ forKey:[[bindNode.values objectAtIndex:0] lowercaseString]];
+ }];
}
-
- return expansion;
- } copy] forKey:key];
- }
+ }
+
+ for( TPTemplateNode *childNode in node.childNodes ) {
+ [expansion appendString:[childNode expansionUsingEnvironment:invokeEnvironment]];
+ }
+
+ return expansion;
+ } copy] forKey:key];
+
return @"";
- } copy] forKey:@"template"];
+ } copy] forKey:@"def"];
- [globals setObject:[^NSString*( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) {
- [global setObject:[^NSString*( TPTemplateNode *_node, NSDictionary *_values, NSMutableDictionary *_global, NSArray *_parameters ) {
+ [environment setObject:[^NSString*( TPTemplateNode *node, NSMutableDictionary *environment, NSArray *parameters ) {
+ [environment setObject:[^NSString*( TPTemplateNode *_node, NSMutableDictionary *environment, NSArray *parameters ) {
return [node.values objectAtIndex:1];
} copy] forKey:[[node.values objectAtIndex:0] lowercaseString]];
- NSLog(@"Globals: %@", global);
+ NSLog(@"Environment: %@", environment);
return @"";
- } copy] forKey:@"global"];
+ } copy] forKey:@"env"];
- [globals setObject:[^NSString*( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) {
+ [environment setObject:[^NSString*( TPTemplateNode *node, NSMutableDictionary *environment, NSArray *parameters ) {
NSMutableString *expansion = [NSMutableString string];
TPMarkdownDataParser *parser = [TPMarkdownDataParser parserForFile:[node.values objectAtIndex:0]];
- NSDictionary *environment = parser.values;
+
+ NSMutableDictionary *invokeEnvironment = [NSMutableDictionary dictionaryWithDictionary:environment];
+ [invokeEnvironment addEntriesFromDictionary:parser.values];
for( TPTemplateNode *childNode in node.childNodes ) {
- [expansion appendString:[childNode expansionUsingValues:environment global:global]];
+ [expansion appendString:[childNode expansionUsingEnvironment:invokeEnvironment]];
}
return expansion;
} copy] forKey:@"include"];
- return [root expansionUsingValues:values
- global:globals];
+ return [root expansionUsingEnvironment:environment];
}
- (void)parseContent:(NSMutableString*)content parent:(TPTemplateNode*)parent {
while( [content length] ) {
View
@@ -18,8 +18,8 @@ int main(int argc, const char * argv[]) {
TPDataParser *p = [TPMarkdownDataParser parserForFile:@"/Users/chris/Repositories/git/hiddenMemory/Tipi/Tests/Test01.txt"];
NSLog(@"p.values = %@", [p values]);
- TPTemplateParser *q = [TPTemplateParser parserForFile:@"/Users/chris/Repositories/git/hiddenMemory/Tipi/Tests/Test02.html"];
- NSLog(@"Expansion: %@", [q expansionUsingValues:p.values]);
+ TPTemplateParser *q = [TPTemplateParser parserForFile:@"/Users/chris/Repositories/git/hiddenMemory/Tipi/Tests/Test03.html"];
+ NSLog(@"Expansion: %@", [q expansionUsingEnvironment:p.values]);
}
return 0;
}

0 comments on commit a140a26

Please sign in to comment.