Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement {{import}} within the parser

  • Loading branch information...
commit 3c164eca8b5b2b9ea87830d7e08a3fbffa22787b 1 parent 82c4003
@hiddenmemory authored
Showing with 27 additions and 13 deletions.
  1. +4 −1 Tipi/TPTemplateParser.h
  2. +22 −0 Tipi/TPTemplateParser.m
  3. +1 −12 Tipi/main.m
View
5 Tipi/TPTemplateParser.h
@@ -14,6 +14,9 @@
@property (readonly) TPTemplateNode *root;
+ (TPTemplateParser*)parserForFile:(NSString*)path;
-- (NSString*)expansionUsingEnvironment:(NSDictionary*)values;
+
- (NSString*)expansion;
+- (NSString*)expansionUsingEnvironment:(NSDictionary*)values;
+- (NSString*)expansionUsingImportEnvironment:(NSDictionary*)values;
+
@end
View
22 Tipi/TPTemplateParser.m
@@ -21,6 +21,8 @@ @interface TPTemplateParser () {
NSString *tagStart, *tagEnd;
NSString *tagBlockOpen, *tagBlockClose;
+
+ NSString *sourcePath;
}
- (id)initWithFileAtPath:(NSString*)path;
- (void)parseContent:(NSMutableString*)content parent:(TPTemplateNode*)parent;
@@ -46,6 +48,8 @@ - (id)initWithFileAtPath:(NSString*)path {
encoding:NSUTF8StringEncoding
error:nil];
+ sourcePath = [path stringByDeletingLastPathComponent];
+
[self parseContent:content parent:root];
ParserLog(@"Node: %@", [root description]);
@@ -67,6 +71,24 @@ - (id)expandValue:(id)value environment:(NSMutableDictionary*)currentEnvironment
return value;
}
+- (NSString*)expansionUsingImportEnvironment:(NSDictionary*)values {
+ NSMutableDictionary *environment = [NSMutableDictionary dictionaryWithDictionary:values];
+
+ id importBlock = ^NSString*( TPTemplateNode *node, NSMutableDictionary *environment ) {
+ NSString *importPath = [NSString stringWithFormat:@"%@/%@", sourcePath, [node.valuesMap objectForKey:@"source"]];
+
+ if( [[NSFileManager defaultManager] fileExistsAtPath:importPath] ) {
+ TPTemplateParser *importParser = [TPTemplateParser parserForFile:importPath];
+ return [importParser.root expansionUsingEnvironment:environment];
+ }
+
+ return @"";
+ };
+
+ [environment setObject:[importBlock copy] forKey:@"import"];
+
+ return [self expansionUsingEnvironment:environment];
+}
- (NSString*)expansionUsingEnvironment:(NSDictionary*)values {
NSMutableDictionary *environment = [NSMutableDictionary dictionary];
View
13 Tipi/main.m
@@ -33,18 +33,7 @@ int main(int argc, const char * argv[]) {
TPTemplateParser *q = [TPTemplateParser parserForFile:inputPath];
- id importBlock = ^NSString*( TPTemplateNode *node, NSMutableDictionary *environment ) {
- NSString *importPath = [NSString stringWithFormat:@"%@%@", testPath, [node.valuesMap objectForKey:@"source"]];
- if( [[NSFileManager defaultManager] fileExistsAtPath:importPath] ) {
- TPTemplateParser *importParser = [TPTemplateParser parserForFile:importPath];
- return [importParser.root expansionUsingEnvironment:environment];
- }
- else {
- return @"";
- }
- };
-
- NSString *expansion = [q expansionUsingEnvironment:[NSDictionary dictionaryWithObject:[importBlock copy] forKey:@"import"]];
+ NSString *expansion = [q expansionUsingImportEnvironment:[NSDictionary dictionary]];
if( [[NSFileManager defaultManager] fileExistsAtPath:outputPath] ) {
NSString *goldenExpansion = [NSString stringWithContentsOfFile:outputPath
Please sign in to comment.
Something went wrong with that request. Please try again.