Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Before the global environment storm

  • Loading branch information...
commit 4ae180e676e6e1fae6a83f8f3b571847c7db3cae 1 parent 370713e
@hiddenmemory authored
View
9 Tests/Test02.html
@@ -1,4 +1,4 @@
-{{#template case_study title_cs client_cs description_cs platform_cs testimonial_cs Technologies_cs}}
+{{#template case_study title_cs client_cs description_cs platform_cs testimonial_cs literal }}
<html>
<head>
<title>{{ title_cs }}</title>
@@ -17,18 +17,21 @@
{{Testimonial_cs}}
<h4>Technologies</h4>
- {{Technologies_cs}}
+ {{technologies}}
{{testvalue}}
+
+ version {{literal}}
</body>
</html>
{{/def}}
{{#include /Users/chris/Repositories/git/hiddenMemory/Tipi/Tests/Test01.txt}}
- {{#case_study title client description platform Testimonial Technologies}}
+ {{#case_study title "" description platform Testimonial "v1.0"}}
{{# bind testvalue }}
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 }}
{{/case_study}}
{{/include}}
View
4 Tipi/NSString+HiddenMemory.m
@@ -277,8 +277,6 @@ - (NSString*)stringByTrimmingWhitespace {
- (NSString*)removePrefix:(NSString*)prefix suffix:(NSString*)suffix {
NSString *str = self;
- NSLog(@"Before: %@, P: %@, S: %@", str, prefix, suffix);
-
if( prefix && [str hasPrefix:prefix] ) {
str = [str stringByReplacingCharactersInRange:NSMakeRange(0, [prefix length]) withString:@""];
}
@@ -286,8 +284,6 @@ - (NSString*)removePrefix:(NSString*)prefix suffix:(NSString*)suffix {
if( suffix && [str hasSuffix:suffix] ) {
str = [str stringByReplacingCharactersInRange:NSMakeRange([str length] - [suffix length], [suffix length]) withString:@""];
}
-
- NSLog(@"After: %@", str);
return str;
}
View
13 Tipi/TPTemplateNode.m
@@ -60,20 +60,14 @@ - (NSString*)expansionUsingValues:(NSDictionary *)_values
}
else {
NSString *key = [self.name lowercaseString];
-
- NSLog(@"Looking for value: %@", key);
if( [global objectForKey:key] ) {
- NSLog(@" -> Found global value: %@", key);
-
NSString *(^expansionBlock)( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) = [global objectForKey:key];
NSMutableArray *parameters = [NSMutableArray array];
for( NSString *_valueName in self.values ) {
NSString *valueName = [_valueName lowercaseString];
- NSLog(@" ---> Looking up parameter value %@ (%@)", valueName, _valueName);
-
NSString *(^valueExpansionBlock)( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) = [global objectForKey:valueName];
if( valueExpansionBlock ) {
[parameters addObject:valueExpansionBlock(self, _values, global, nil)];
@@ -81,18 +75,17 @@ - (NSString*)expansionUsingValues:(NSDictionary *)_values
else if( [_values objectForKey:valueName] ) {
[parameters addObject:[_values objectForKey:valueName]];
}
+ else {
+ [parameters addObject:_valueName];
+ }
}
[expansion appendString:expansionBlock(self, _values, global, parameters)];
}
else if( [_values objectForKey:key] ) {
- NSLog(@" -> Found local value: %@", key);
-
[expansion appendString:[_values objectForKey:key]];
}
else {
- NSLog(@" -> Unable to find value");
-
for( TPTemplateNode *node in childNodes ) {
[expansion appendString:[node expansionUsingValues:_values global:global]];
}
View
105 Tipi/TPTemplateParser.m
@@ -49,70 +49,70 @@ - (id)initWithFileAtPath:(NSString*)path {
- (NSString*)expansionUsingValues:(NSDictionary*)values {
NSMutableDictionary *globals = [NSMutableDictionary dictionary];
- [globals setObject:[^NSString*( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) {
- [global setObject:[^NSString*( TPTemplateNode *_, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) {
- NSMutableString *expansion = [NSMutableString string];
- NSMutableDictionary *environment = [NSMutableDictionary dictionary];
-
- [[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:[NSDictionary dictionary] global:global]
- forKey:[[childNode.values objectAtIndex:0] lowercaseString]];
+ [globals setObject:[^NSString*( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) { return @""; } copy]
+ forKey:@"bind"];
+
+ [globals setObject:[^NSString*( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *_ ) {
+ 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]];
}
- }
-
- NSLog(@"Environment: %@", environment);
-
- for( TPTemplateNode *childNode in node.childNodes ) {
- [expansion appendString:[childNode expansionUsingValues:environment global:global]];
- }
-
- return expansion;
- } copy]
- forKey:[[node.values objectAtIndex:0] lowercaseString]];
-
+ [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]];
+ }
+
+ return expansion;
+ } copy] forKey:key];
+ }
return @"";
- } copy]
- forKey:@"template"];
+ } copy] forKey:@"template"];
[globals setObject:[^NSString*( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) {
- [global setObject:[^NSString*( TPTemplateNode *_, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) {
+ [global setObject:[^NSString*( TPTemplateNode *_node, NSDictionary *_values, NSMutableDictionary *_global, NSArray *_parameters ) {
return [node.values objectAtIndex:1];
- } copy]
- forKey:[[node.values objectAtIndex:0] lowercaseString]];
+ } copy] forKey:[[node.values objectAtIndex:0] lowercaseString]];
NSLog(@"Globals: %@", global);
return @"";
- } copy]
- forKey:@"global"];
+ } copy] forKey:@"global"];
[globals setObject:[^NSString*( TPTemplateNode *node, NSDictionary *values, NSMutableDictionary *global, NSArray *parameters ) {
NSMutableString *expansion = [NSMutableString string];
TPMarkdownDataParser *parser = [TPMarkdownDataParser parserForFile:[node.values objectAtIndex:0]];
NSDictionary *environment = parser.values;
- NSLog(@"Environment: %@", environment);
-
for( TPTemplateNode *childNode in node.childNodes ) {
[expansion appendString:[childNode expansionUsingValues:environment global:global]];
}
return expansion;
- } copy]
- forKey:@"include"];
+ } copy] forKey:@"include"];
return [root expansionUsingValues:values
global:globals];
}
- (void)parseContent:(NSMutableString*)content parent:(TPTemplateNode*)parent {
while( [content length] ) {
- NSLog(@"Content: '%@'", content);
-
NSRange range = [content rangeOfString:tagStart];
BOOL shouldParseTag = YES;
@@ -145,37 +145,45 @@ - (BOOL)parseTag:(NSMutableString*)content parent:(TPTemplateNode*)parent {
TPTemplateNode *node = [TPTemplateNode node];
node.originalValue = [content substringToIndex:tagContentRange.location + tagEnd.length];
-
NSMutableArray *parts = [NSMutableArray array];
NSString *stringToParse = [node.originalValue removePrefix:tagStart suffix:tagEnd];
while( [stringToParse length] ) {
stringToParse = [stringToParse stringByTrimmingWhitespace];
- NSLog(@"String to parse: %@ (%@)", stringToParse, parts);
-
-
if( [stringToParse hasPrefix:@"\""] ) {
+ BOOL success = NO;
NSUInteger i = 0;
for( i = 1; i < [stringToParse length]; i++ ) {
unichar c = [stringToParse characterAtIndex:i];
if( c == '"' && [stringToParse characterAtIndex:i - 1] != '\\' ) {
[parts addObject:[stringToParse substringWithRange:NSMakeRange(1, i - 1)]];
stringToParse = [stringToParse substringFromIndex:i + 1];
+ success = YES;
break;
}
}
+ if( success == NO ) {
+ [parts addObject:stringToParse];
+ stringToParse = @"";
+ }
}
else if( [stringToParse hasPrefix:@"'"] ) {
+ BOOL success = NO;
NSUInteger i = 0;
for( i = 1; i < [stringToParse length]; i++ ) {
unichar c = [stringToParse characterAtIndex:i];
if( c == '\'' && [stringToParse characterAtIndex:i - 1] != '\\' ) {
[parts addObject:[stringToParse substringWithRange:NSMakeRange(1, i - 1)]];
stringToParse = [stringToParse substringFromIndex:i + 1];
+ success = YES;
break;
}
}
+ if( success == NO ) {
+ [parts addObject:stringToParse];
+ stringToParse = @"";
+ }
}
else {
NSRange range = [stringToParse rangeOfString:@" "];
@@ -192,8 +200,6 @@ - (BOOL)parseTag:(NSMutableString*)content parent:(TPTemplateNode*)parent {
}
}
- NSLog(@"Parts: %@", parts);
-
[content deleteCharactersInRange:NSMakeRange(0, node.originalValue.length)];
if( [[parts objectAtIndex:0] hasPrefix:tagBlockOpen] ) {
@@ -204,8 +210,6 @@ - (BOOL)parseTag:(NSMutableString*)content parent:(TPTemplateNode*)parent {
[parts replaceObjectAtIndex:0 withObject:[[parts objectAtIndex:0] removePrefix:tagBlockOpen suffix:nil]];
}
- NSLog(@"Parts: %@", parts);
-
node.type = TPNodeDefinition;
node.name = [parts objectAtIndex:0];
@@ -214,6 +218,11 @@ - (BOOL)parseTag:(NSMutableString*)content parent:(TPTemplateNode*)parent {
}
[parent.childNodes addObject:node];
+
+ if( [content characterAtIndex:0] == '\n' ) {
+ [content deleteCharactersInRange:NSMakeRange(0, 1)];
+ }
+
[self parseContent:content parent:node];
}
else if( [[parts objectAtIndex:0] hasPrefix:tagBlockClose] ) {
@@ -224,6 +233,10 @@ - (BOOL)parseTag:(NSMutableString*)content parent:(TPTemplateNode*)parent {
[parts replaceObjectAtIndex:0 withObject:[[parts objectAtIndex:0] removePrefix:tagBlockClose suffix:nil]];
}
+ if( [content characterAtIndex:0] == '\n' ) {
+ [content deleteCharactersInRange:NSMakeRange(0, 1)];
+ }
+
return YES;
}
else {
View
2  Tipi/main.m
@@ -18,7 +18,7 @@ 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/Test01.html"];
+ TPTemplateParser *q = [TPTemplateParser parserForFile:@"/Users/chris/Repositories/git/hiddenMemory/Tipi/Tests/Test02.html"];
NSLog(@"Expansion: %@", [q expansionUsingValues:p.values]);
}
return 0;
Please sign in to comment.
Something went wrong with that request. Please try again.