Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Make it so that we have proper scoping

  • Loading branch information...
commit 7fd1416036bcf1daf2792f24e984248957ee9e1f 1 parent 4ae180e
hiddenGithub authored
1  Tests/Test02.html
@@ -32,6 +32,7 @@
32 32 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.
33 33 {{/ bind }}
34 34 {{# bind client_cs }}Box and Arrow Ltd{{/ bind }}
  35 + {{#bind technologies}}ALL OF THEM!{{/bind}}
35 36 {{/case_study}}
36 37 {{/include}}
37 38
38 Tests/Test03.html
... ... @@ -1 +1,37 @@
1   -{{ a long item to get "right out of the" box 'of magic' "fun \"and\" games" }}
  1 +
  2 +{{#def A}}
  3 + {{ValueB}}
  4 +{{/def}}
  5 +
  6 +{{global ValueB "This is ValueB"}}
  7 +
  8 +{{#def B}}
  9 + {{ValueB}}
  10 +{{/def}}
  11 +
  12 +{{#def X}}
  13 + Begin X
  14 + {{#def Z}}
  15 + Z has been here.
  16 + {{/def Z}}
  17 + Begin Y
  18 + {{#def Y}}
  19 + [1] Z has been here:
  20 + {{Z}}
  21 + Begin Z
  22 + {{#def Z}}
  23 + Z hath been here.
  24 + {{/def Z}}
  25 + [2] Z hath been here:
  26 + {{Z}}
  27 + {{/def Y}}
  28 + {{Y}}
  29 + [3] Z has been here:
  30 + {{Z}}
  31 +{{/def X}}
  32 +
  33 +{{A}}
  34 +
  35 +{{B}}
  36 +
  37 +{{X}}
3  Tipi/TPTemplateNode.h
@@ -23,6 +23,5 @@ typedef enum {
23 23 @property (readonly) NSMutableArray *childNodes;
24 24
25 25 + (TPTemplateNode*)node;
26   -- (NSString*)expansionUsingValues:(NSDictionary*)values
27   - global:(NSMutableDictionary*)global;
  26 +- (NSString*)expansionUsingEnvironment:(NSMutableDictionary*)environment;
28 27 @end
57 Tipi/TPTemplateNode.m
@@ -50,49 +50,52 @@ - (NSString*)descriptionWithDepth:(int)depth {
50 50 - (NSString*)description {
51 51 return [self descriptionWithDepth:0];
52 52 }
53   -- (NSString*)expansionUsingValues:(NSDictionary *)_values
54   - global:(NSMutableDictionary*)global
55   -{
56   - NSMutableString *expansion = [NSMutableString string];
  53 +- (NSString*)expansionUsingEnvironment:(NSMutableDictionary*)environment {
57 54
58 55 if( self.type == TPNodeText ) {
59   - [expansion appendString:self.originalValue];
  56 + return self.originalValue;
60 57 }
61 58 else {
  59 + NSMutableString *expansion = [NSMutableString string];
62 60 NSString *key = [self.name lowercaseString];
63 61
64   - if( [global objectForKey:key] ) {
65   - NSString *(^expansionBlock)( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) = [global objectForKey:key];
  62 + if( [environment objectForKey:key] ) {
  63 + id value = [environment objectForKey:key];
66 64
67   - NSMutableArray *parameters = [NSMutableArray array];
68   - for( NSString *_valueName in self.values ) {
69   - NSString *valueName = [_valueName lowercaseString];
  65 + if( [[value class] isSubclassOfClass:[NSString class]] ) {
  66 + [expansion appendString:value];
  67 + }
  68 + else {
  69 + NSString *(^expansionBlock)( TPTemplateNode *node, NSMutableDictionary *global, NSArray *parameters ) = value;
70 70
71   - NSString *(^valueExpansionBlock)( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) = [global objectForKey:valueName];
72   - if( valueExpansionBlock ) {
73   - [parameters addObject:valueExpansionBlock(self, _values, global, nil)];
74   - }
75   - else if( [_values objectForKey:valueName] ) {
76   - [parameters addObject:[_values objectForKey:valueName]];
77   - }
78   - else {
79   - [parameters addObject:_valueName];
  71 + NSMutableArray *parameters = [NSMutableArray array];
  72 +
  73 + for( NSString *_valueName in self.values ) {
  74 + NSString *valueName = [_valueName lowercaseString];
  75 + id valueValue = [environment objectForKey:valueName];
  76 +
  77 + if( valueValue == nil ) {
  78 + [parameters addObject:_valueName];
  79 + }
  80 + else if( [[valueValue class] isSubclassOfClass:[NSString class]] ) {
  81 + [parameters addObject:valueValue];
  82 + }
  83 + else {
  84 + NSString *(^valueExpansionBlock)( TPTemplateNode *node, NSMutableDictionary *global, NSArray *parameters ) = valueValue;
  85 + [parameters addObject:valueExpansionBlock(self, environment, nil)];
  86 + }
80 87 }
  88 +
  89 + [expansion appendString:expansionBlock(self, environment, parameters)];
81 90 }
82   -
83   - [expansion appendString:expansionBlock(self, _values, global, parameters)];
84   - }
85   - else if( [_values objectForKey:key] ) {
86   - [expansion appendString:[_values objectForKey:key]];
87 91 }
88 92 else {
89 93 for( TPTemplateNode *node in childNodes ) {
90   - [expansion appendString:[node expansionUsingValues:_values global:global]];
  94 + [expansion appendString:[node expansionUsingEnvironment:environment]];
91 95 }
92 96 }
  97 + return expansion;
93 98 }
94   -
95   - return expansion;
96 99 }
97 100
98 101 @end
2  Tipi/TPTemplateParser.h
@@ -10,5 +10,5 @@
10 10
11 11 @interface TPTemplateParser : NSObject
12 12 + (TPTemplateParser*)parserForFile:(NSString*)path;
13   -- (NSString*)expansionUsingValues:(NSDictionary*)values;
  13 +- (NSString*)expansionUsingEnvironment:(NSDictionary*)values;
14 14 @end
86 Tipi/TPTemplateParser.m
@@ -46,70 +46,74 @@ - (id)initWithFileAtPath:(NSString*)path {
46 46 }
47 47 return self;
48 48 }
49   -- (NSString*)expansionUsingValues:(NSDictionary*)values {
50   - NSMutableDictionary *globals = [NSMutableDictionary dictionary];
  49 +- (NSString*)expansionUsingEnvironment:(NSDictionary*)values {
  50 + NSMutableDictionary *environment = [NSMutableDictionary dictionaryWithDictionary:values];
51 51
52   - [globals setObject:[^NSString*( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) { return @""; } copy]
  52 + [environment setObject:[^NSString*( TPTemplateNode *node, NSMutableDictionary *environment, NSArray *parameters ) { return @""; } copy]
53 53 forKey:@"bind"];
54 54
55   - [globals setObject:[^NSString*( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *_ ) {
  55 + [environment setObject:[^NSString*( TPTemplateNode *node, NSMutableDictionary *environment, NSArray *parameters ) {
56 56 NSString *key = [[node.values objectAtIndex:0] lowercaseString];
57   - if( [global objectForKey:key] == nil ) {
58   - [global setObject:[^NSString*( TPTemplateNode *_, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) {
59   - NSMutableString *expansion = [NSMutableString string];
60   - NSMutableDictionary *environment = [NSMutableDictionary dictionary];
61   -
62   - NSMutableString *thisExpansion = [NSMutableString string];
63   - for( TPTemplateNode *childNode in _.childNodes ) {
64   - [thisExpansion appendString:[childNode expansionUsingValues:values global:global]];
65   - }
66   - [environment setObject:thisExpansion forKey:@"this"];
67   -
68   - [[node.values subarrayWithRange:NSMakeRange(1, [node.values count] - 1)] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
69   - [environment setObject:[parameters objectAtIndex:idx] forKey:[obj lowercaseString]];
70   - }];
71   -
72   - for( TPTemplateNode *childNode in _.childNodes ) {
73   - if( [childNode.name isEqualToString:@"bind"] ) {
74   - [environment setObject:[childNode expansionUsingValues:values global:global]
75   - forKey:[[childNode.values objectAtIndex:0] lowercaseString]];
76   - }
77   - }
78   -
79   - for( TPTemplateNode *childNode in node.childNodes ) {
80   - [expansion appendString:[childNode expansionUsingValues:environment global:global]];
  57 + NSMutableDictionary *freshEnvironment = [NSMutableDictionary dictionaryWithDictionary:environment];
  58 +
  59 + [environment setObject:[^NSString*( TPTemplateNode *currentNode, NSMutableDictionary *environment, NSArray *parameters ) {
  60 + NSMutableString *expansion = [NSMutableString string];
  61 + NSMutableDictionary *invokeEnvironment = [NSMutableDictionary dictionaryWithDictionary:freshEnvironment];
  62 +
  63 + NSMutableString *thisExpansion = [NSMutableString string];
  64 + for( TPTemplateNode *childNode in currentNode.childNodes ) {
  65 + [thisExpansion appendString:[childNode expansionUsingEnvironment:freshEnvironment]];
  66 + }
  67 + [invokeEnvironment setObject:thisExpansion forKey:@"this"];
  68 +
  69 + [[node.values subarrayWithRange:NSMakeRange(1, [node.values count] - 1)] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  70 + [invokeEnvironment setObject:[parameters objectAtIndex:idx] forKey:[obj lowercaseString]];
  71 + }];
  72 +
  73 + for( TPTemplateNode *bindNode in currentNode.childNodes ) {
  74 + if( [bindNode.name isEqualToString:@"bind"] ) {
  75 + [bindNode.childNodes enumerateObjectsUsingBlock:^(TPTemplateNode *obj, NSUInteger idx, BOOL *stop) {
  76 + [invokeEnvironment setObject:[obj expansionUsingEnvironment:freshEnvironment]
  77 + forKey:[[bindNode.values objectAtIndex:0] lowercaseString]];
  78 + }];
81 79 }
82   -
83   - return expansion;
84   - } copy] forKey:key];
85   - }
  80 + }
  81 +
  82 + for( TPTemplateNode *childNode in node.childNodes ) {
  83 + [expansion appendString:[childNode expansionUsingEnvironment:invokeEnvironment]];
  84 + }
  85 +
  86 + return expansion;
  87 + } copy] forKey:key];
  88 +
86 89 return @"";
87   - } copy] forKey:@"template"];
  90 + } copy] forKey:@"def"];
88 91
89   - [globals setObject:[^NSString*( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) {
90   - [global setObject:[^NSString*( TPTemplateNode *_node, NSDictionary *_values, NSMutableDictionary *_global, NSArray *_parameters ) {
  92 + [environment setObject:[^NSString*( TPTemplateNode *node, NSMutableDictionary *environment, NSArray *parameters ) {
  93 + [environment setObject:[^NSString*( TPTemplateNode *_node, NSMutableDictionary *environment, NSArray *parameters ) {
91 94 return [node.values objectAtIndex:1];
92 95 } copy] forKey:[[node.values objectAtIndex:0] lowercaseString]];
93 96
94   - NSLog(@"Globals: %@", global);
  97 + NSLog(@"Globals: %@", environment);
95 98
96 99 return @"";
97 100 } copy] forKey:@"global"];
98 101
99   - [globals setObject:[^NSString*( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) {
  102 + [environment setObject:[^NSString*( TPTemplateNode *node, NSMutableDictionary *environment, NSArray *parameters ) {
100 103 NSMutableString *expansion = [NSMutableString string];
101 104 TPMarkdownDataParser *parser = [TPMarkdownDataParser parserForFile:[node.values objectAtIndex:0]];
102   - NSDictionary *environment = parser.values;
  105 +
  106 + NSMutableDictionary *invokeEnvironment = [NSMutableDictionary dictionaryWithDictionary:environment];
  107 + [invokeEnvironment addEntriesFromDictionary:parser.values];
103 108
104 109 for( TPTemplateNode *childNode in node.childNodes ) {
105   - [expansion appendString:[childNode expansionUsingValues:environment global:global]];
  110 + [expansion appendString:[childNode expansionUsingEnvironment:invokeEnvironment]];
106 111 }
107 112
108 113 return expansion;
109 114 } copy] forKey:@"include"];
110 115
111   - return [root expansionUsingValues:values
112   - global:globals];
  116 + return [root expansionUsingEnvironment:environment];
113 117 }
114 118 - (void)parseContent:(NSMutableString*)content parent:(TPTemplateNode*)parent {
115 119 while( [content length] ) {
4 Tipi/main.m
@@ -18,8 +18,8 @@ int main(int argc, const char * argv[]) {
18 18 TPDataParser *p = [TPMarkdownDataParser parserForFile:@"/Users/chris/Repositories/git/hiddenMemory/Tipi/Tests/Test01.txt"];
19 19 NSLog(@"p.values = %@", [p values]);
20 20
21   - TPTemplateParser *q = [TPTemplateParser parserForFile:@"/Users/chris/Repositories/git/hiddenMemory/Tipi/Tests/Test02.html"];
22   - NSLog(@"Expansion: %@", [q expansionUsingValues:p.values]);
  21 + TPTemplateParser *q = [TPTemplateParser parserForFile:@"/Users/chris/Repositories/git/hiddenMemory/Tipi/Tests/Test03.html"];
  22 + NSLog(@"Expansion: %@", [q expansionUsingEnvironment:p.values]);
23 23 }
24 24 return 0;
25 25 }

0 comments on commit 7fd1416

Please sign in to comment.
Something went wrong with that request. Please try again.