├── res ├── en.lproj │ └── InfoPlist.strings ├── XP.parsergen ├── template.parsergen ├── TDTemplateEngineiOS-Info.plist ├── TDTemplateEngine-Info.plist └── TDTemplateEngine-Prefix.pch ├── test ├── en.lproj │ └── InfoPlist.strings ├── TDBaseExpressionTests.h ├── TDTemplateEngineTests-Info.plist ├── TDTestScaffold.h ├── TDBaseExpressionTests.m ├── TDPathExpressionTests.m ├── TDNewlineTagTests.m ├── TDIndentTagTests.m └── TDVerbatimTagTests.m ├── .externals ├── TDTemplateEngine.xcodeproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcshareddata │ └── xcschemes │ ├── TDTemplateEngineiOS.xcscheme │ ├── libTDTemplateEngineOSX.xcscheme │ └── TDTemplateEngine.xcscheme ├── src ├── TDSepTag.h ├── TDTrimTag.h ├── TDIndentTag.h ├── TDTabTag.h ├── TDVerbatimTag.h ├── TDSpaceTag.h ├── TDNewlineTag.h ├── TDDateFormatFilter.h ├── TDTabTag.m ├── TDSpaceTag.m ├── TDConditionalOutputTag.h ├── TDTemplateParser.h ├── TDSepTag.m ├── TDNewlineTag.m ├── TDDateFormatFilter.m ├── TDTrimTag.m ├── TDTextNode.h ├── TDPrintNode.h ├── TDForTag.h ├── TDIfTag.h ├── TDElseTag.h ├── TDSkipException.m ├── TDSkipTag.h ├── TDCommentTag.h ├── XPAssembler.h ├── TDBoolFilter.h ├── TDCeilFilter.h ├── TDFabsFilter.h ├── TDTrimFilter.h ├── TDFloorFilter.h ├── TDReplaceFilter.h ├── TDRoundFilter.h ├── TDSkipException.h ├── TDLowercaseFilter.h ├── TDUppercaseFilter.h ├── TDCapitalizeFilter.h ├── TDNullFormatFilter.h ├── TDNumberFormatFilter.h ├── TDUncapitalizeFilter.h ├── PKToken+Verbatim.h ├── TDElseIfTag.h ├── TDIndentTag.m ├── TDVerbatimTag.m ├── TDObjectValue.h ├── TDBooleanValue.h ├── TDNumericValue.h ├── TDElseTag.m ├── TDPathExpression.h ├── TDElseIfTag.m ├── TDEnumeration.h ├── TDUnaryExpression.h ├── TDNegationExpression.h ├── TDEnumeration.m ├── TDRootNode.h ├── TDStringValue.h ├── TDConditionalOutputTag.m ├── TDPadFilters.h ├── TDBooleanExpression.h ├── TDCollectionExpression.h ├── TDArithmeticExpression.h ├── TDRelationalExpression.h ├── TDBoolFilter.m ├── TDFilterExpression.h ├── TDParser.h ├── TDLowercaseFilter.m ├── TDUppercaseFilter.m ├── TDCeilFilter.m ├── TDFabsFilter.m ├── TDTextNode.m ├── PKToken+Verbatim.m ├── TDFloorFilter.m ├── TDRoundFilter.m ├── TDForLoop.h ├── TDTemplateEngine+ExpressionSupport.h ├── TDCommentTag.m ├── TDLoopExpression.h ├── TDRangeExpression.h ├── TDBinaryExpression.h ├── TDTrimFilter.m ├── TDRootNode.m ├── TDForLoop.m ├── TDNullFormatFilter.m ├── TDNumberFormatFilter.m ├── TDValue.h ├── TDSkipTag.m ├── TDPrintNode.m ├── TDCapitalizeFilter.m ├── TDUncapitalizeFilter.m ├── TDUnaryExpression.m ├── TDNegationExpression.m ├── TDBooleanValue.m ├── TDStringValue.m ├── TDWriter.m ├── TDNumericValue.m ├── TDIfTag.m ├── TDExpression.m ├── TDPadFilters.m ├── TDTemplateEngine+ExpressionSupport.m ├── TDRelationalExpression.m ├── TDTag.m ├── TDFilter.m ├── TDBinaryExpression.m ├── TDBooleanExpression.m ├── TDNode.m ├── TDReplaceFilter.m ├── TDArithmeticExpression.m ├── XPAssembler.m ├── TDForTag.m └── TDLoopExpression.m ├── TDTemplateEngine.xcworkspace ├── xcshareddata │ └── IDEWorkspaceChecks.plist └── contents.xcworkspacedata ├── .gitignore ├── include └── TDTemplateEngine │ ├── TDWriter.h │ ├── TDFilter.h │ ├── TDTag.h │ ├── TDNode.h │ ├── TDExpression.h │ └── TDTemplateContext.h └── SOFTWARE_LICENSE /res/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /test/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /res/XP.parsergen: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itod/tdtemplateengine/HEAD/res/XP.parsergen -------------------------------------------------------------------------------- /res/template.parsergen: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itod/tdtemplateengine/HEAD/res/template.parsergen -------------------------------------------------------------------------------- /.externals: -------------------------------------------------------------------------------- 1 | [.] 2 | scm = git 3 | 4 | [lib/pegkit] 5 | repository = git@github.com:itod/pegkit.git 6 | scm = git 7 | path = lib/pegkit -------------------------------------------------------------------------------- /TDTemplateEngine.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/TDSepTag.h: -------------------------------------------------------------------------------- 1 | // 2 | // TDSepTag.h 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 3/10/25. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDTag.h" 10 | 11 | @interface TDSepTag : TDTag 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /src/TDTrimTag.h: -------------------------------------------------------------------------------- 1 | // 2 | // TDTrimTag.h 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/7/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TDTrimTag : TDTag 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /src/TDIndentTag.h: -------------------------------------------------------------------------------- 1 | // 2 | // TDBlockTag.h 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/7/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TDIndentTag : TDTag 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /src/TDTabTag.h: -------------------------------------------------------------------------------- 1 | // 2 | // TDTabTag.h 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/7/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDConditionalOutputTag.h" 10 | 11 | @interface TDTabTag : TDConditionalOutputTag 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /src/TDVerbatimTag.h: -------------------------------------------------------------------------------- 1 | // 2 | // TDVerbatimTag.h 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/7/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TDVerbatimTag : TDTag 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /src/TDSpaceTag.h: -------------------------------------------------------------------------------- 1 | // 2 | // TDSpaceTag.h 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/7/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDConditionalOutputTag.h" 10 | 11 | @interface TDSpaceTag : TDConditionalOutputTag 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /src/TDNewlineTag.h: -------------------------------------------------------------------------------- 1 | // 2 | // TDNewlineTag.h 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/7/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDConditionalOutputTag.h" 10 | 11 | @interface TDNewlineTag : TDConditionalOutputTag 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /TDTemplateEngine.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/TDDateFormatFilter.h: -------------------------------------------------------------------------------- 1 | // 2 | // TDDateFormatFilter.h 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/8/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TDDateFormatFilter : TDFilter 12 | 13 | 14 | @end 15 | -------------------------------------------------------------------------------- /TDTemplateEngine.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/TDTabTag.m: -------------------------------------------------------------------------------- 1 | // 2 | // TDTabTag.m 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/7/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDTabTag.h" 10 | 11 | @implementation TDTabTag 12 | 13 | + (NSString *)tagName { 14 | return @"tab"; 15 | } 16 | 17 | 18 | + (NSString *)outputString { 19 | return @"\t"; 20 | } 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /src/TDSpaceTag.m: -------------------------------------------------------------------------------- 1 | // 2 | // TDSpaceTag.m 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/7/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDSpaceTag.h" 10 | 11 | @implementation TDSpaceTag 12 | 13 | + (NSString *)tagName { 14 | return @"nbsp"; 15 | } 16 | 17 | 18 | + (NSString *)outputString { 19 | return @" "; 20 | } 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /src/TDConditionalOutputTag.h: -------------------------------------------------------------------------------- 1 | // 2 | // TDConditionalOutputTag.h 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/7/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TDConditionalOutputTag : TDTag 12 | 13 | + (NSString *)outputString; 14 | - (NSString *)outputStringInContext:(TDTemplateContext *)ctx; 15 | @end 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata 19 | 20 | ## Other 21 | *.xccheckout 22 | *.moved-aside 23 | *.xcuserstate 24 | *.xcscmblueprint 25 | 26 | ## Obj-C/Swift specific 27 | *.hmap 28 | *.ipa 29 | 30 | lib/pegkit 31 | -------------------------------------------------------------------------------- /src/TDTemplateParser.h: -------------------------------------------------------------------------------- 1 | #import 2 | 3 | @class TDTemplateEngine; 4 | @class TDTemplateContext; 5 | 6 | enum { 7 | TDTEMPLATE_TOKEN_KIND_BLOCK_START_TAG = 14, 8 | TDTEMPLATE_TOKEN_KIND_PRINT, 9 | TDTEMPLATE_TOKEN_KIND_BLOCK_END_TAG, 10 | TDTEMPLATE_TOKEN_KIND_EMPTY_TAG, 11 | TDTEMPLATE_TOKEN_KIND_TEXT, 12 | }; 13 | 14 | @interface TDTemplateParser : PKParser 15 | 16 | @property (nonatomic, assign) TDTemplateEngine *engine; // weakref 17 | @property (nonatomic, retain) TDTemplateContext *staticContext; 18 | 19 | @end 20 | 21 | -------------------------------------------------------------------------------- /test/TDBaseExpressionTests.h: -------------------------------------------------------------------------------- 1 | // 2 | // TDBaseExpressionTests.m 3 | // TDTemplateEngineTests 4 | // 5 | // Created by Todd Ditchendorf on 3/28/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDTestScaffold.h" 10 | #import "TDExpression.h" 11 | #import "TDParser.h" 12 | 13 | @interface TDBaseExpressionTests : XCTestCase 14 | - (NSArray *)tokenize:(NSString *)input; 15 | 16 | @property (nonatomic, retain) TDTemplateEngine *eng; 17 | @property (nonatomic, retain) TDExpression *expr; 18 | @property (nonatomic, retain) PKTokenizer *t; 19 | @end -------------------------------------------------------------------------------- /include/TDTemplateEngine/TDWriter.h: -------------------------------------------------------------------------------- 1 | // 2 | // TDWriter.h 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/2/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface TDWriter : NSObject 12 | 13 | + (instancetype)writerWithOutputStream:(NSOutputStream *)output; 14 | - (instancetype)initWithOutputStream:(NSOutputStream *)output; 15 | 16 | @property (nonatomic, retain) NSOutputStream *output; 17 | 18 | - (void)appendString:(NSString *)str; 19 | - (void)appendFormat:(NSString *)fmt, ... NS_FORMAT_FUNCTION(1,2); 20 | 21 | @end 22 | -------------------------------------------------------------------------------- /src/TDSepTag.m: -------------------------------------------------------------------------------- 1 | // 2 | // TDSepTag.m 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/7/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDSepTag.h" 10 | #import "TDForLoop.h" 11 | #import 12 | 13 | @implementation TDSepTag 14 | 15 | + (NSString *)tagName { 16 | return @"sep"; 17 | } 18 | 19 | 20 | + (TDTagType)tagType { 21 | return TDTagTypeBlock; 22 | } 23 | 24 | 25 | - (void)doTagInContext:(TDTemplateContext *)ctx { 26 | TDForLoop *loop = [ctx resolveVariable:@"currentLoop"]; 27 | if (loop && !loop.last) { 28 | [self renderChildrenInContext:ctx]; 29 | } 30 | } 31 | 32 | @end 33 | -------------------------------------------------------------------------------- /test/TDTemplateEngineTests-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundlePackageType 14 | BNDL 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleSignature 18 | ???? 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /res/TDTemplateEngineiOS-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(CURRENT_PROJECT_VERSION) 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /test/TDTestScaffold.h: -------------------------------------------------------------------------------- 1 | // 2 | // TDTestScaffold.h 3 | // TDTemplateEngineTests 4 | // 5 | // Created by Todd Ditchendorf on 3/28/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | #import 12 | //#import 13 | #import 14 | #import 15 | #import "TDTemplateEngine+ExpressionSupport.h" 16 | 17 | #define TDTrue(e) XCTAssertTrue((e), @"") 18 | #define TDFalse(e) XCTAssertFalse((e), @"") 19 | #define TDNil(e) XCTAssertNil((e), @"") 20 | #define TDNotNil(e) XCTAssertNotNil((e), @"") 21 | #define TDEquals(e1, e2) XCTAssertEqual((e1), (e2), @"") 22 | #define TDEqualObjects(e1, e2) XCTAssertEqualObjects((e1), (e2), @"") 23 | 24 | //#define VERIFY() @try { [_mock verify]; } @catch (NSException *ex) { NSString *msg = [ex reason]; XCTAssertTrue(0, @"%@", msg); } 25 | -------------------------------------------------------------------------------- /src/TDNewlineTag.m: -------------------------------------------------------------------------------- 1 | // 2 | // TDNewlineTag.m 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/7/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDNewlineTag.h" 10 | #import 11 | 12 | @implementation TDNewlineTag 13 | 14 | + (NSString *)tagName { 15 | return @"br"; 16 | } 17 | 18 | 19 | + (NSString *)outputString { 20 | return @"\n"; 21 | } 22 | 23 | 24 | //- (NSString *)outputStringInContext:(TDTemplateContext *)ctx { 25 | // NSString *str = [[self class] outputString]; 26 | // 27 | // NSInteger depth = ctx.blockDepth; 28 | // if (depth > 0) { 29 | // NSMutableString *buff = [NSMutableString stringWithString:str]; 30 | // 31 | // for (NSUInteger i = 0; i < depth; ++i) { 32 | // [buff appendString:@" "]; 33 | // } 34 | // 35 | // str = buff; 36 | // } 37 | // 38 | // return str; 39 | //} 40 | 41 | @end 42 | -------------------------------------------------------------------------------- /test/TDBaseExpressionTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // TDBaseExpressionTests.m 3 | // TDTemplateEngineTests 4 | // 5 | // Created by Todd Ditchendorf on 3/28/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDBaseExpressionTests.h" 10 | 11 | @implementation TDBaseExpressionTests 12 | 13 | - (void)setUp { 14 | [super setUp]; 15 | 16 | self.eng = [TDTemplateEngine templateEngine]; // create thread local temp engine 17 | self.expr = nil; 18 | } 19 | 20 | - (void)tearDown { 21 | self.expr = nil; 22 | self.eng = nil; 23 | 24 | [super tearDown]; 25 | } 26 | 27 | - (NSArray *)tokenize:(NSString *)input { 28 | PKTokenizer *t = [TDParser tokenizer]; 29 | t.string = input; 30 | 31 | PKToken *tok = nil; 32 | PKToken *eof = [PKToken EOFToken]; 33 | 34 | NSMutableArray *toks = [NSMutableArray array]; 35 | 36 | while (eof != (tok = [t nextToken])) { 37 | [toks addObject:tok]; 38 | } 39 | return toks; 40 | } 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /test/TDPathExpressionTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // TDPathExpressionTests.m 3 | // TDTemplateEngineTests 4 | // 5 | // Created by Todd Ditchendorf on 3/28/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDBaseExpressionTests.h" 10 | 11 | @interface TDPathExpressionTests : TDBaseExpressionTests 12 | @end 13 | 14 | @implementation TDPathExpressionTests 15 | 16 | - (void)setUp { 17 | [super setUp]; 18 | 19 | } 20 | 21 | - (void)tearDown { 22 | 23 | [super tearDown]; 24 | } 25 | 26 | - (void)testPathFooBar8 { 27 | NSString *input = @"foo.bar"; 28 | NSArray *toks = [self tokenize:input]; 29 | 30 | id vars = @{@"foo": @{@"bar": @(8)}}; 31 | id ctx = [[[TDTemplateContext alloc] initWithVariables:vars output:nil] autorelease]; 32 | 33 | NSError *err = nil; 34 | TDExpression *expr = [self.eng expressionFromTokens:toks error:&err]; 35 | TDNil(err); 36 | TDNotNil(expr); 37 | TDEquals(8.0, [[expr simplify] evaluateAsNumberInContext:ctx]); 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /res/TDTemplateEngine-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIconFile 10 | 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | FMWK 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | NSHumanReadableCopyright 26 | Copyright © 2014 Todd Ditchendorf. All rights reserved. 27 | NSPrincipalClass 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /SOFTWARE_LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Todd Ditchendorf 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /src/TDDateFormatFilter.m: -------------------------------------------------------------------------------- 1 | // 2 | // TDDateFormatFilter.m 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/8/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDDateFormatFilter.h" 10 | 11 | @implementation TDDateFormatFilter 12 | 13 | + (NSString *)filterName { 14 | return @"fmtDate"; 15 | } 16 | 17 | 18 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 19 | TDAssert(input); 20 | 21 | [self validateArguments:args min:1 max:1]; 22 | 23 | NSDate *date = nil; 24 | if ([input isKindOfClass:[NSDate class]]) { 25 | date = input; 26 | } else { 27 | NSString *inStr = TDStringFromObject(input); 28 | date = [NSDate dateWithNaturalLanguageString:inStr]; 29 | } 30 | 31 | TDAssert(1 == [args count]); // already validated above 32 | NSString *fmtStr = args[0]; 33 | 34 | NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 35 | [formatter setDateFormat:fmtStr]; 36 | 37 | NSString *result = [formatter stringFromDate:date]; 38 | TDAssert([result length]); 39 | 40 | return result; 41 | } 42 | 43 | @end 44 | -------------------------------------------------------------------------------- /src/TDTrimTag.m: -------------------------------------------------------------------------------- 1 | // 2 | // TDTrimTag.m 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/7/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDTrimTag.h" 10 | #import 11 | #import 12 | 13 | @interface TDTag () 14 | @property (nonatomic, retain) PKToken *endTagToken; 15 | @end 16 | 17 | @implementation TDTrimTag 18 | 19 | + (NSString *)tagName { 20 | return @"trim"; 21 | } 22 | 23 | 24 | + (TDTagType)tagType { 25 | return TDTagTypeBlock; 26 | } 27 | 28 | 29 | - (void)dealloc { 30 | 31 | [super dealloc]; 32 | } 33 | 34 | 35 | - (void)doTagInContext:(TDTemplateContext *)ctx { 36 | //NSLog(@"%s %@", __PRETTY_FUNCTION__, self); 37 | TDAssert(ctx); 38 | 39 | BOOL oldTrim = ctx.trimLines; 40 | 41 | BOOL newTrim = YES; 42 | 43 | if (self.expression) { 44 | BOOL enable = [self.expression evaluateAsBooleanInContext:ctx]; 45 | if (!enable) { 46 | newTrim = NO; 47 | } 48 | } 49 | 50 | ctx.trimLines = newTrim; 51 | [self renderChildrenInContext:ctx]; 52 | ctx.trimLines = oldTrim; 53 | } 54 | 55 | @end 56 | -------------------------------------------------------------------------------- /src/TDTextNode.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDNode.h" 24 | 25 | @interface TDTextNode : TDNode 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDPrintNode.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDNode.h" 24 | 25 | @interface TDPrintNode : TDNode 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDForTag.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDForTag : TDTag 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDIfTag.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDIfTag : TDTag 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDElseTag.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDElseTag : TDTag 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDSkipException.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDSkipException.h" 24 | 25 | @implementation TDSkipException 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDSkipTag.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDSkipTag : TDTag 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDCommentTag.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDCommentTag : TDTag 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/XPAssembler.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface XPAssembler : NSObject 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDBoolFilter.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDBoolFilter : TDFilter 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDCeilFilter.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDCeilFilter : TDFilter 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDFabsFilter.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDFabsFilter : TDFilter 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDTrimFilter.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDTrimFilter : TDFilter 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDFloorFilter.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDFloorFilter : TDFilter 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDReplaceFilter.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDReplaceFilter : TDFilter 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDRoundFilter.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDRoundFilter : TDFilter 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDSkipException.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDSkipException : NSException 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDLowercaseFilter.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDLowercaseFilter : TDFilter 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDUppercaseFilter.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDUppercaseFilter : TDFilter 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDCapitalizeFilter.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDCapitalizeFilter : TDFilter 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDNullFormatFilter.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDNullFormatFilter : TDFilter 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDNumberFormatFilter.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDNumberFormatFilter : TDFilter 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDUncapitalizeFilter.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDUncapitalizeFilter : TDFilter 26 | 27 | @end 28 | -------------------------------------------------------------------------------- /src/PKToken+Verbatim.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface PKToken (Verbatim) 26 | @property (nonatomic, retain) NSString *verbatimString; 27 | @end 28 | -------------------------------------------------------------------------------- /src/TDElseIfTag.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDElseIfTag : TDTag 26 | 27 | @property (nonatomic, assign) BOOL incomplete; 28 | @end 29 | -------------------------------------------------------------------------------- /src/TDIndentTag.m: -------------------------------------------------------------------------------- 1 | // 2 | // TDBlockTag.m 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/7/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDIndentTag.h" 10 | #import 11 | #import 12 | 13 | @implementation TDIndentTag 14 | 15 | + (NSString *)tagName { 16 | return @"indent"; 17 | } 18 | 19 | 20 | + (TDTagType)tagType { 21 | return TDTagTypeBlock; 22 | } 23 | 24 | 25 | - (void)dealloc { 26 | 27 | [super dealloc]; 28 | } 29 | 30 | 31 | - (void)doTagInContext:(TDTemplateContext *)ctx { 32 | //NSLog(@"%s %@", __PRETTY_FUNCTION__, self); 33 | TDAssert(ctx); 34 | 35 | NSUInteger times = 1; 36 | 37 | if (self.expression) { 38 | times = (NSUInteger)[self.expression evaluateAsNumberInContext:ctx]; 39 | } 40 | 41 | 42 | [ctx increaseIndentDepth:times]; 43 | 44 | // leading indent WS 45 | // { 46 | // NSMutableString *str = [NSMutableString string]; 47 | // for (NSUInteger depth = 0; depth < ctx.indentDepth; ++depth) { 48 | // [str appendString:@" "]; 49 | // } 50 | // [ctx.writer appendString:str]; 51 | // } 52 | 53 | [self renderChildrenInContext:ctx]; 54 | 55 | [ctx decreaseIndentDepth:times]; 56 | } 57 | 58 | @end 59 | -------------------------------------------------------------------------------- /src/TDVerbatimTag.m: -------------------------------------------------------------------------------- 1 | // 2 | // TDVerbatimTag.m 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/7/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDVerbatimTag.h" 10 | #import 11 | #import 12 | #import "PKToken+Verbatim.h" 13 | 14 | @interface TDTag () 15 | @property (nonatomic, retain) PKToken *endTagToken; 16 | @end 17 | 18 | @implementation TDVerbatimTag 19 | 20 | + (NSString *)tagName { 21 | return @"verbatim"; 22 | } 23 | 24 | 25 | + (TDTagType)tagType { 26 | return TDTagTypeBlock; 27 | } 28 | 29 | 30 | - (void)dealloc { 31 | 32 | [super dealloc]; 33 | } 34 | 35 | 36 | - (void)doTagInContext:(TDTemplateContext *)ctx { 37 | //NSLog(@"%s %@", __PRETTY_FUNCTION__, self); 38 | TDAssert(ctx); 39 | 40 | for (TDNode *child in self.children) { 41 | [self render:child to:ctx]; 42 | } 43 | } 44 | 45 | 46 | - (void)render:(TDNode *)node to:(TDTemplateContext *)ctx { 47 | 48 | [ctx writeString:node.token.verbatimString]; 49 | 50 | for (TDNode *child in node.children) { 51 | [self render:child to:ctx]; 52 | } 53 | 54 | if ([node isKindOfClass:[TDTag class]]) { 55 | TDTag *tag = (id)node; 56 | [ctx writeString:tag.endTagToken.verbatimString]; 57 | } 58 | } 59 | 60 | @end 61 | -------------------------------------------------------------------------------- /src/TDObjectValue.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDValue.h" 24 | 25 | @interface TDObjectValue : TDValue 26 | 27 | + (TDObjectValue *)objectValueWithObject:(id)obj; 28 | 29 | - (instancetype)initWithObject:(id)obj; 30 | @end 31 | -------------------------------------------------------------------------------- /src/TDBooleanValue.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDValue.h" 24 | 25 | @interface TDBooleanValue : TDValue 26 | 27 | + (TDBooleanValue *)booleanValueWithBoolean:(BOOL)b; 28 | 29 | - (instancetype)initWithBoolean:(BOOL)b; 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /src/TDNumericValue.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDValue.h" 24 | 25 | @interface TDNumericValue : TDValue 26 | 27 | + (TDNumericValue *)numericValueWithNumber:(double)n; 28 | 29 | - (instancetype)initWithNumber:(double)n; 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /src/TDElseTag.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDElseTag.h" 24 | 25 | @implementation TDElseTag 26 | 27 | + (NSString *)tagName { 28 | return @"else"; 29 | } 30 | 31 | 32 | + (TDTagType)tagType { 33 | return TDTagTypeEmpty; 34 | } 35 | 36 | @end 37 | -------------------------------------------------------------------------------- /src/TDPathExpression.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDExpression.h" 24 | 25 | @interface TDPathExpression : TDExpression 26 | 27 | + (instancetype)pathExpressionWithSteps:(NSArray *)steps; 28 | 29 | - (instancetype)initWithSteps:(NSArray *)steps; 30 | @end 31 | -------------------------------------------------------------------------------- /src/TDElseIfTag.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDElseIfTag.h" 24 | 25 | @implementation TDElseIfTag 26 | 27 | + (NSString *)tagName { 28 | return @"elif"; 29 | } 30 | 31 | 32 | + (TDTagType)tagType { 33 | return TDTagTypeEmpty; 34 | } 35 | 36 | @end 37 | -------------------------------------------------------------------------------- /src/TDEnumeration.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDExpression.h" 24 | 25 | @interface TDEnumeration : TDExpression 26 | 27 | - (BOOL)hasMore; 28 | 29 | @property (nonatomic, retain) NSArray *values; 30 | @property (nonatomic, assign) NSInteger current; 31 | @end 32 | -------------------------------------------------------------------------------- /src/TDUnaryExpression.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDExpression.h" 24 | 25 | @interface TDUnaryExpression : TDExpression 26 | 27 | + (instancetype)unaryExpressionWithExpression:(TDExpression *)expr; 28 | 29 | - (instancetype)initWithExpression:(TDExpression *)expr; 30 | @end 31 | -------------------------------------------------------------------------------- /src/TDNegationExpression.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDExpression.h" 24 | 25 | @interface TDNegationExpression : TDExpression 26 | 27 | + (instancetype)negationExpressionWithExpression:(TDExpression *)expr; 28 | 29 | - (instancetype)initWithExpression:(TDExpression *)expr; 30 | @end 31 | -------------------------------------------------------------------------------- /src/TDEnumeration.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDEnumeration.h" 24 | 25 | @implementation TDEnumeration 26 | 27 | - (void)dealloc { 28 | self.values = nil; 29 | [super dealloc]; 30 | } 31 | 32 | 33 | - (BOOL)hasMore { 34 | return _current < [_values count]; 35 | } 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /src/TDRootNode.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDNode.h" 24 | 25 | @class TDTemplateContext; 26 | 27 | @interface TDRootNode : TDNode 28 | 29 | + (instancetype)rootNodeWithStaticContext:(TDTemplateContext *)ctx; 30 | 31 | @property (nonatomic, retain) TDTemplateContext *staticContext; 32 | @end 33 | -------------------------------------------------------------------------------- /src/TDStringValue.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDValue.h" 24 | 25 | @interface TDStringValue : TDValue 26 | 27 | + (TDStringValue *)stringValueWithString:(NSString *)s; 28 | 29 | - (instancetype)initWithString:(NSString *)s; 30 | 31 | - (BOOL)isEqualToStringValue:(TDStringValue *)v; 32 | 33 | @end 34 | -------------------------------------------------------------------------------- /src/TDConditionalOutputTag.m: -------------------------------------------------------------------------------- 1 | // 2 | // TDConditionalOutputTag.m 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/7/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDConditionalOutputTag.h" 10 | #import 11 | #import 12 | 13 | @implementation TDConditionalOutputTag 14 | 15 | + (TDTagType)tagType { 16 | return TDTagTypeEmpty; 17 | } 18 | 19 | 20 | + (NSString *)outputString { 21 | NSAssert2(0, @"%s is an abstract method and must be implemented in %@", __PRETTY_FUNCTION__, [self class]); 22 | return nil; 23 | } 24 | 25 | 26 | - (void)dealloc { 27 | 28 | [super dealloc]; 29 | } 30 | 31 | 32 | - (NSString *)outputStringInContext:(TDTemplateContext *)ctx { 33 | return [[self class] outputString]; 34 | } 35 | 36 | 37 | - (void)doTagInContext:(TDTemplateContext *)ctx { 38 | //NSLog(@"%s %@", __PRETTY_FUNCTION__, self); 39 | TDAssert(ctx); 40 | 41 | NSUInteger count = 1; 42 | 43 | if (self.expression) { 44 | count = [self.expression evaluateAsNumberInContext:ctx]; 45 | } 46 | 47 | BOOL oldTrim = ctx.trimLines; 48 | 49 | NSString *output = [self outputStringInContext:ctx]; 50 | 51 | ctx.trimLines = NO; { 52 | 53 | for (NSUInteger i = 0; i < count; ++i) { 54 | [ctx writeString:output]; 55 | } 56 | 57 | } ctx.trimLines = oldTrim; 58 | } 59 | 60 | @end 61 | -------------------------------------------------------------------------------- /src/TDPadFilters.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDAbstractPadFilter : TDFilter 26 | @property (nonatomic, assign) BOOL left; 27 | @end 28 | 29 | @interface TDLpadFilter : TDAbstractPadFilter 30 | 31 | @end 32 | 33 | @interface TDRpadFilter : TDAbstractPadFilter 34 | 35 | @end 36 | -------------------------------------------------------------------------------- /src/TDBooleanExpression.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDBinaryExpression.h" 24 | 25 | @interface TDBooleanExpression : TDBinaryExpression 26 | 27 | + (TDBooleanExpression *)booleanExpression; 28 | 29 | + (TDBooleanExpression *)booleanExpressionWithOperand:(TDExpression *)lhs operator:(NSInteger)op operand:(TDExpression *)rhs; 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /src/TDCollectionExpression.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDEnumeration.h" 24 | 25 | @interface TDCollectionExpression : TDEnumeration 26 | 27 | + (instancetype)collectionExpressionWithExpression:(TDExpression *)expr; 28 | - (instancetype)initWithExpression:(TDExpression *)expr; 29 | 30 | @property (nonatomic, retain) TDExpression *subExpression; 31 | @end 32 | -------------------------------------------------------------------------------- /src/TDArithmeticExpression.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDBinaryExpression.h" 24 | 25 | @interface TDArithmeticExpression : TDBinaryExpression 26 | 27 | + (TDArithmeticExpression *)arithmeticExpression; 28 | 29 | + (TDArithmeticExpression *)arithmeticExpressionWithOperand:(TDExpression *)lhs operator:(NSInteger)op operand:(TDExpression *)rhs; 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /src/TDRelationalExpression.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDBinaryExpression.h" 24 | 25 | @interface TDRelationalExpression : TDBinaryExpression 26 | 27 | + (TDRelationalExpression *)relationalExpression; 28 | 29 | + (TDRelationalExpression *)relationalExpressionWithOperand:(TDExpression *)lhs operator:(NSInteger)op operand:(TDExpression *)rhs; 30 | 31 | @end 32 | -------------------------------------------------------------------------------- /src/TDBoolFilter.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDBoolFilter.h" 24 | 25 | @implementation TDBoolFilter 26 | 27 | + (NSString *)filterName { 28 | return @"bool"; 29 | } 30 | 31 | 32 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 33 | TDAssert(input); 34 | BOOL yn = [input boolValue]; 35 | 36 | NSString *result = yn ? @"YES" : @"NO"; 37 | return result; 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /src/TDFilterExpression.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDExpression.h" 24 | 25 | @class TDFilter; 26 | 27 | @interface TDFilterExpression : TDExpression 28 | 29 | + (instancetype)filterExpressionWithExpression:(TDExpression *)expr filter:(TDFilter *)filter arguments:(NSArray *)args; 30 | 31 | - (instancetype)initWithExpression:(TDExpression *)expr filter:(TDFilter *)filter arguments:(NSArray *)args; 32 | @end 33 | -------------------------------------------------------------------------------- /src/TDParser.h: -------------------------------------------------------------------------------- 1 | #import 2 | 3 | @class PKTokenizer; 4 | @class TDTemplateEngine; 5 | 6 | enum { 7 | TD_TOKEN_KIND_GT = 14, 8 | TD_TOKEN_KIND_GE_SYM = 15, 9 | TD_TOKEN_KIND_DOUBLE_AMPERSAND = 16, 10 | TD_TOKEN_KIND_PIPE = 17, 11 | TD_TOKEN_KIND_TRUE = 18, 12 | TD_TOKEN_KIND_NOT_EQUAL = 19, 13 | TD_TOKEN_KIND_BANG = 20, 14 | TD_TOKEN_KIND_COLON = 21, 15 | TD_TOKEN_KIND_LT_SYM = 22, 16 | TD_TOKEN_KIND_MOD = 23, 17 | TD_TOKEN_KIND_LE = 24, 18 | TD_TOKEN_KIND_GT_SYM = 25, 19 | TD_TOKEN_KIND_LT = 26, 20 | TD_TOKEN_KIND_OPEN_PAREN = 27, 21 | TD_TOKEN_KIND_CLOSE_PAREN = 28, 22 | TD_TOKEN_KIND_EQ = 29, 23 | TD_TOKEN_KIND_NE = 30, 24 | TD_TOKEN_KIND_OR = 31, 25 | TD_TOKEN_KIND_NOT = 32, 26 | TD_TOKEN_KIND_TIMES = 33, 27 | TD_TOKEN_KIND_PLUS = 34, 28 | TD_TOKEN_KIND_DOUBLE_PIPE = 35, 29 | TD_TOKEN_KIND_COMMA = 36, 30 | TD_TOKEN_KIND_AND = 37, 31 | TD_TOKEN_KIND_YES_UPPER = 38, 32 | TD_TOKEN_KIND_MINUS = 39, 33 | TD_TOKEN_KIND_IN = 40, 34 | TD_TOKEN_KIND_DOT = 41, 35 | TD_TOKEN_KIND_DIV = 42, 36 | TD_TOKEN_KIND_BY = 43, 37 | TD_TOKEN_KIND_FALSE = 44, 38 | TD_TOKEN_KIND_LE_SYM = 45, 39 | TD_TOKEN_KIND_TO = 46, 40 | TD_TOKEN_KIND_GE = 47, 41 | TD_TOKEN_KIND_NO_UPPER = 48, 42 | TD_TOKEN_KIND_DOUBLE_EQUALS = 49, 43 | TD_TOKEN_KIND_NULL = 50, 44 | }; 45 | 46 | @interface TDParser : PKParser 47 | 48 | + (PKTokenizer *)tokenizer; 49 | 50 | @property (nonatomic, assign) TDTemplateEngine *engine; // weakref 51 | @property (nonatomic, assign) BOOL doLoopExpr; 52 | 53 | @end 54 | 55 | -------------------------------------------------------------------------------- /src/TDLowercaseFilter.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDLowercaseFilter.h" 24 | 25 | @implementation TDLowercaseFilter 26 | 27 | + (NSString *)filterName { 28 | return @"lowercase"; 29 | } 30 | 31 | 32 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 33 | TDAssert(input); 34 | NSString *inStr = TDStringFromObject(input); 35 | 36 | NSString *result = [inStr lowercaseString]; 37 | return result; 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /src/TDUppercaseFilter.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDUppercaseFilter.h" 24 | 25 | @implementation TDUppercaseFilter 26 | 27 | + (NSString *)filterName { 28 | return @"uppercase"; 29 | } 30 | 31 | 32 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 33 | TDAssert(input); 34 | NSString *inStr = TDStringFromObject(input); 35 | 36 | NSString *result = [inStr uppercaseString]; 37 | return result; 38 | } 39 | 40 | @end 41 | -------------------------------------------------------------------------------- /src/TDCeilFilter.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDCeilFilter.h" 24 | 25 | @implementation TDCeilFilter 26 | 27 | + (NSString *)filterName { 28 | return @"ceil"; 29 | } 30 | 31 | 32 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 33 | TDAssert(input); 34 | 35 | [self validateArguments:args min:0 max:0]; 36 | 37 | double num = [input doubleValue]; 38 | 39 | id result = @(ceil(num)); 40 | 41 | return result; 42 | } 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /src/TDFabsFilter.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDFabsFilter.h" 24 | 25 | @implementation TDFabsFilter 26 | 27 | + (NSString *)filterName { 28 | return @"fabs"; 29 | } 30 | 31 | 32 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 33 | TDAssert(input); 34 | 35 | [self validateArguments:args min:0 max:0]; 36 | 37 | double num = [input doubleValue]; 38 | 39 | id result = @(fabs(num)); 40 | 41 | return result; 42 | } 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /src/TDTextNode.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDTextNode.h" 24 | #import 25 | #import 26 | 27 | @implementation TDTextNode 28 | 29 | - (void)dealloc { 30 | [super dealloc]; 31 | } 32 | 33 | 34 | - (void)renderInContext:(TDTemplateContext *)ctx { 35 | NSParameterAssert(ctx); 36 | 37 | TDAssert([self.token.stringValue length]); 38 | 39 | [ctx writeString:self.token.stringValue]; 40 | } 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /src/PKToken+Verbatim.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "PKToken+Verbatim.h" 24 | #import 25 | 26 | @implementation PKToken (Verbatim) 27 | 28 | - (void)setVerbatimString:(NSString *)str { 29 | objc_setAssociatedObject(self, @selector(verbatimString), str, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 30 | } 31 | 32 | 33 | - (NSString *)verbatimString { 34 | return objc_getAssociatedObject(self, @selector(verbatimString)); 35 | } 36 | 37 | @dynamic verbatimString; 38 | @end 39 | -------------------------------------------------------------------------------- /src/TDFloorFilter.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDFloorFilter.h" 24 | 25 | @implementation TDFloorFilter 26 | 27 | + (NSString *)filterName { 28 | return @"floor"; 29 | } 30 | 31 | 32 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 33 | TDAssert(input); 34 | 35 | [self validateArguments:args min:0 max:0]; 36 | 37 | double num = [input doubleValue]; 38 | 39 | id result = @(floor(num)); 40 | 41 | return result; 42 | } 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /src/TDRoundFilter.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDRoundFilter.h" 24 | 25 | @implementation TDRoundFilter 26 | 27 | + (NSString *)filterName { 28 | return @"round"; 29 | } 30 | 31 | 32 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 33 | TDAssert(input); 34 | 35 | [self validateArguments:args min:0 max:0]; 36 | 37 | double num = [input doubleValue]; 38 | 39 | id result = @(round(num)); 40 | 41 | return result; 42 | } 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /res/TDTemplateEngine-Prefix.pch: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #ifdef __OBJC__ 24 | #import 25 | #endif 26 | 27 | #define TDAssert(b) NSAssert2((b), @" %s : assert(%@)", __PRETTY_FUNCTION__, @#b); 28 | #define TDAssertMainThread() NSAssert1([NSThread isMainThread], @"%s should be called on the main thread only.", __PRETTY_FUNCTION__); 29 | #define TDAssertNotMainThread() NSAssert1(![NSThread isMainThread], @"%s should be called on the main thread only.", __PRETTY_FUNCTION__); 30 | -------------------------------------------------------------------------------- /src/TDForLoop.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @interface TDForLoop : NSObject 26 | 27 | @property (nonatomic, assign) NSInteger currentIndex; 28 | @property (nonatomic, assign, getter=isFirst) BOOL first; 29 | @property (nonatomic, assign, getter=isLast) BOOL last; 30 | @property (nonatomic, assign, readonly, getter=isOdd) BOOL odd; 31 | @property (nonatomic, assign, readonly, getter=isEven) BOOL even; 32 | @property (nonatomic, assign) TDForLoop *parentLoop; // weakref 33 | @end 34 | -------------------------------------------------------------------------------- /src/TDTemplateEngine+ExpressionSupport.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @class PKTokenizer; 26 | @class TDExpression; 27 | 28 | @interface TDTemplateEngine (ExpressionSupport) 29 | - (PKTokenizer *)tokenizer; 30 | - (TDExpression *)expressionFromString:(NSString *)str error:(NSError **)outErr; 31 | - (TDExpression *)expressionFromTokens:(NSArray *)toks error:(NSError **)outErr; 32 | - (TDExpression *)loopExpressionFromTokens:(NSArray *)toks error:(NSError **)outErr; 33 | @end 34 | -------------------------------------------------------------------------------- /src/TDCommentTag.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDCommentTag.h" 24 | #import 25 | 26 | @interface TDCommentTag () 27 | @end 28 | 29 | @implementation TDCommentTag 30 | 31 | + (NSString *)tagName { 32 | return @"comment"; 33 | } 34 | 35 | 36 | + (TDTagType)tagType { 37 | return TDTagTypeBlock; 38 | } 39 | 40 | 41 | - (void)dealloc { 42 | 43 | [super dealloc]; 44 | } 45 | 46 | 47 | - (void)doTagInContext:(TDTemplateContext *)ctx { 48 | // noop 49 | } 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /include/TDTemplateEngine/TDFilter.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | NSString *TDStringFromObject(id obj); 26 | 27 | @interface TDFilter : NSObject 28 | 29 | - (void)validateArguments:(NSArray *)args min:(NSUInteger)min max:(NSUInteger)max; 30 | 31 | @property (nonatomic, assign, readonly) NSString *filterName; // convenience for class method 32 | @end 33 | 34 | @interface TDFilter (Override) 35 | + (NSString *)filterName; 36 | 37 | - (id)doFilter:(id)input withArguments:(NSArray *)args; 38 | @end 39 | -------------------------------------------------------------------------------- /src/TDLoopExpression.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDExpression.h" 24 | 25 | @class TDEnumeration; 26 | 27 | @interface TDLoopExpression : TDExpression 28 | 29 | + (instancetype)loopExpressionWithVariables:(NSArray *)vars enumeration:(TDEnumeration *)e; 30 | - (instancetype)initWithVariables:(NSArray *)vars enumeration:(TDEnumeration *)e; 31 | 32 | @property (nonatomic, copy) NSString *firstVariable; 33 | @property (nonatomic, copy) NSString *secondVariable; 34 | @property (nonatomic, retain) TDEnumeration *enumeration; 35 | @end 36 | -------------------------------------------------------------------------------- /src/TDRangeExpression.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDEnumeration.h" 24 | 25 | @interface TDRangeExpression : TDEnumeration 26 | 27 | + (instancetype)rangeExpressionWithStart:(TDExpression *)start stop:(TDExpression *)stop by:(TDExpression *)by; 28 | 29 | - (instancetype)initWithStart:(TDExpression *)start stop:(TDExpression *)stop by:(TDExpression *)by; 30 | 31 | @property (nonatomic, retain) TDExpression *start; 32 | @property (nonatomic, retain) TDExpression *stop; 33 | @property (nonatomic, retain) TDExpression *by; 34 | @end 35 | -------------------------------------------------------------------------------- /include/TDTemplateEngine/TDTag.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @class TDTemplateContext; 26 | @class TDExpression; 27 | @class PKToken; 28 | 29 | typedef NS_ENUM(NSUInteger, TDTagType) { 30 | TDTagTypeEmpty, 31 | TDTagTypeBlock, 32 | }; 33 | 34 | @interface TDTag : TDNode 35 | 36 | @end 37 | 38 | // Subclasses must override these methods 39 | @interface TDTag (Override) 40 | + (NSString *)tagName; 41 | + (TDTagType)tagType; 42 | 43 | - (void)doTagInContext:(TDTemplateContext *)ctx; 44 | @end 45 | -------------------------------------------------------------------------------- /src/TDBinaryExpression.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDExpression.h" 24 | 25 | @interface TDBinaryExpression : TDExpression 26 | 27 | + (TDBinaryExpression *)binaryExpression; 28 | 29 | + (TDBinaryExpression *)binaryExpressionWithOperand:(TDExpression *)lhs operator:(NSInteger)op operand:(TDExpression *)rhs; 30 | 31 | - (instancetype)initWithOperand:(TDExpression *)lhs operator:(NSInteger)op operand:(TDExpression *)rhs; 32 | 33 | - (void)setOperand:(TDExpression *)lhs operator:(NSInteger)op operand:(TDExpression *)rhs; 34 | 35 | @end 36 | -------------------------------------------------------------------------------- /src/TDTrimFilter.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDTrimFilter.h" 24 | 25 | @implementation TDTrimFilter 26 | 27 | + (NSString *)filterName { 28 | return @"trim"; 29 | } 30 | 31 | 32 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 33 | TDAssert(input); 34 | 35 | [self validateArguments:args min:0 max:0]; 36 | 37 | NSString *inStr = TDStringFromObject(input); 38 | 39 | NSString *result = [inStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 40 | 41 | return result; 42 | } 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /src/TDRootNode.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDRootNode.h" 24 | #import 25 | 26 | @implementation TDRootNode 27 | 28 | + (instancetype)rootNodeWithStaticContext:(TDTemplateContext *)ctx { 29 | PKToken *frag = [PKToken tokenWithTokenType:PKTokenTypeSymbol stringValue:@"" doubleValue:0.0]; 30 | 31 | TDRootNode *rootNode = [self nodeWithToken:frag parent:nil]; 32 | rootNode.staticContext = ctx; 33 | return rootNode; 34 | } 35 | 36 | 37 | - (void)dealloc { 38 | self.staticContext = nil; 39 | [super dealloc]; 40 | } 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /src/TDForLoop.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDForLoop.h" 24 | 25 | @implementation TDForLoop 26 | 27 | - (instancetype)init { 28 | self = [super init]; 29 | if (self) { 30 | self.currentIndex = 0; 31 | self.first = YES; 32 | self.last = NO; 33 | } 34 | return self; 35 | } 36 | 37 | 38 | - (void)dealloc { 39 | self.parentLoop = nil; 40 | [super dealloc]; 41 | } 42 | 43 | 44 | - (BOOL)isOdd { 45 | return (_currentIndex & 1) == 0; 46 | } 47 | 48 | 49 | - (BOOL)isEven { 50 | return (_currentIndex & 1) != 0; 51 | } 52 | 53 | @end 54 | -------------------------------------------------------------------------------- /src/TDNullFormatFilter.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDNullFormatFilter.h" 24 | #import "TDValue.h" 25 | 26 | @implementation TDNullFormatFilter 27 | 28 | + (NSString *)filterName { 29 | return @"fmtNull"; 30 | } 31 | 32 | 33 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 34 | TDAssert(input); 35 | 36 | if ([input isNullValue]) { 37 | return @"null"; 38 | } else if ([input isStringValue]) { 39 | return [NSString stringWithFormat:@"\"%@\"", [input stringValue]]; 40 | } else { 41 | return input; 42 | } 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /src/TDNumberFormatFilter.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDNumberFormatFilter.h" 24 | 25 | @implementation TDNumberFormatFilter 26 | 27 | + (NSString *)filterName { 28 | return @"fmt"; 29 | } 30 | 31 | 32 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 33 | TDAssert(input); 34 | 35 | [self validateArguments:args min:1 max:1]; 36 | 37 | double num = [input doubleValue]; 38 | 39 | NSString *fmt = TDStringFromObject([args firstObject]); 40 | 41 | NSString *result = [NSString stringWithFormat:fmt, num]; 42 | 43 | return result; 44 | } 45 | 46 | @end 47 | -------------------------------------------------------------------------------- /src/TDValue.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @class TDValue; 26 | 27 | TDValue *TDValueFromObject(id obj); 28 | double TDNumberFromString(NSString *s); 29 | 30 | @interface TDValue : TDExpression 31 | 32 | - (id)objectValue; 33 | - (NSString *)stringValue; 34 | - (double)doubleValue; 35 | - (BOOL)boolValue; 36 | 37 | - (BOOL)isEqualToValue:(TDValue *)other; 38 | - (BOOL)isNotEqualToValue:(TDValue *)other; 39 | 40 | - (BOOL)compareToValue:(TDValue *)other usingOperator:(NSInteger)op; 41 | 42 | // convenience 43 | - (BOOL)isBooleanValue; 44 | - (BOOL)isNumericValue; 45 | - (BOOL)isStringValue; 46 | - (BOOL)isNullValue; 47 | @end 48 | -------------------------------------------------------------------------------- /src/TDSkipTag.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDSkipTag.h" 24 | #import "TDSkipException.h" 25 | #import 26 | 27 | @implementation TDSkipTag 28 | 29 | + (NSString *)tagName { 30 | return @"skip"; 31 | } 32 | 33 | 34 | + (TDTagType)tagType { 35 | return TDTagTypeEmpty; 36 | } 37 | 38 | 39 | - (void)doTagInContext:(TDTemplateContext *)ctx { 40 | TDAssert(ctx); 41 | 42 | BOOL test = YES; 43 | 44 | if (self.expression) { 45 | test = [self.expression evaluateAsBooleanInContext:ctx]; 46 | } 47 | 48 | if (test) { 49 | [TDSkipException raise:@"TDSkipException" format:@""]; 50 | } 51 | } 52 | 53 | @end 54 | -------------------------------------------------------------------------------- /src/TDPrintNode.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDPrintNode.h" 24 | #import "TDExpression.h" 25 | #import 26 | 27 | @implementation TDPrintNode 28 | 29 | - (void)dealloc { 30 | self.expression = nil; 31 | [super dealloc]; 32 | } 33 | 34 | 35 | #pragma mark - 36 | #pragma mark Public 37 | 38 | - (void)renderInContext:(TDTemplateContext *)ctx { 39 | NSParameterAssert(ctx); 40 | 41 | TDAssert(self.expression); 42 | 43 | id val = [self.expression evaluateInContext:ctx]; 44 | if (val) { 45 | [ctx writeObject:val]; 46 | } 47 | } 48 | 49 | 50 | - (void)renderChildrenInContext:(TDTemplateContext *)ctx { 51 | // no-op 52 | } 53 | 54 | @end 55 | -------------------------------------------------------------------------------- /src/TDCapitalizeFilter.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDCapitalizeFilter.h" 24 | 25 | @implementation TDCapitalizeFilter 26 | 27 | + (NSString *)filterName { 28 | return @"capitalize"; 29 | } 30 | 31 | 32 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 33 | TDAssert(input); 34 | NSString *inStr = TDStringFromObject(input); 35 | NSString *result = inStr; 36 | NSUInteger len = [result length]; 37 | if (len) { 38 | unichar head = toupper([result characterAtIndex:0]); 39 | NSString *tail = @""; 40 | if (len > 1) { 41 | tail = [result substringFromIndex:1]; 42 | } 43 | result = [NSString stringWithFormat:@"%C%@", head, tail]; 44 | } 45 | return result; 46 | } 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /src/TDUncapitalizeFilter.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDUncapitalizeFilter.h" 24 | 25 | @implementation TDUncapitalizeFilter 26 | 27 | + (NSString *)filterName { 28 | return @"uncapitalize"; 29 | } 30 | 31 | 32 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 33 | TDAssert(input); 34 | NSString *inStr = TDStringFromObject(input); 35 | NSString *result = inStr; 36 | NSUInteger len = [result length]; 37 | if (len) { 38 | unichar head = tolower([result characterAtIndex:0]); 39 | NSString *tail = @""; 40 | if (len > 1) { 41 | tail = [result substringFromIndex:1]; 42 | } 43 | result = [NSString stringWithFormat:@"%C%@", head, tail]; 44 | } 45 | return result; 46 | } 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /include/TDTemplateEngine/TDNode.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @class PKToken; 26 | @class TDTemplateContext; 27 | @class TDExpression; 28 | 29 | @interface TDNode : PKAST 30 | 31 | + (instancetype)nodeWithToken:(PKToken *)frag parent:(TDNode *)parent; 32 | - (instancetype)initWithToken:(PKToken *)frag parent:(TDNode *)parent; 33 | 34 | - (void)renderInContext:(TDTemplateContext *)ctx; 35 | - (void)renderChildrenInContext:(TDTemplateContext *)ctx; 36 | 37 | - (TDNode *)firstAncestorOfClass:(Class)cls; 38 | - (TDNode *)firstAncestorOfTagName:(NSString *)tagName; 39 | 40 | @property (nonatomic, assign) TDNode *parent; // weakref 41 | 42 | @property (nonatomic, copy, readonly) NSString *tagName; 43 | @property (nonatomic, retain) TDExpression *expression; 44 | @end 45 | -------------------------------------------------------------------------------- /include/TDTemplateEngine/TDExpression.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @class TDTemplateContext; 26 | @class TDValue; 27 | @class PKTokenizer; 28 | 29 | typedef NS_ENUM(NSUInteger, TDDataType) { 30 | TDDataTypeBoolean, 31 | TDDataTypeNumber, 32 | TDDataTypeString, 33 | TDDataTypeObject, 34 | TDDataTypeAny 35 | }; 36 | 37 | @interface TDExpression : NSObject 38 | 39 | - (TDValue *)evaluateInContext:(TDTemplateContext *)ctx; 40 | - (BOOL)evaluateAsBooleanInContext:(TDTemplateContext *)ctx; 41 | - (double)evaluateAsNumberInContext:(TDTemplateContext *)ctx; 42 | - (NSString *)evaluateAsStringInContext:(TDTemplateContext *)ctx; 43 | - (id)evaluateAsObjectInContext:(TDTemplateContext *)ctx; 44 | 45 | - (BOOL)isValue; 46 | 47 | - (TDExpression *)simplify; 48 | 49 | - (TDDataType)dataType; 50 | @end 51 | -------------------------------------------------------------------------------- /include/TDTemplateEngine/TDTemplateContext.h: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | @class TDWriter; 26 | 27 | @interface TDTemplateContext : NSObject 28 | 29 | - (instancetype)initWithVariables:(NSDictionary *)vars output:(NSOutputStream *)output; 30 | 31 | @property (nonatomic, retain, readonly) TDWriter *writer; 32 | 33 | // Scope 34 | - (id)resolveVariable:(NSString *)name; 35 | - (void)defineVariable:(NSString *)name value:(id)value; 36 | 37 | - (void)writeObject:(id)obj; 38 | - (void)writeString:(NSString *)str; 39 | 40 | @property (nonatomic, retain) TDTemplateContext *enclosingScope; 41 | @property (nonatomic, assign) BOOL trimLines; 42 | 43 | - (void)increaseIndentDepth:(NSUInteger)times; 44 | - (void)decreaseIndentDepth:(NSUInteger)times; 45 | @property (nonatomic, assign) NSInteger indentDepth; 46 | @end 47 | -------------------------------------------------------------------------------- /src/TDUnaryExpression.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDUnaryExpression.h" 24 | #import "TDNumericValue.h" 25 | 26 | @interface TDUnaryExpression () 27 | @property (nonatomic, retain) TDExpression *expr; 28 | @end 29 | 30 | @implementation TDUnaryExpression 31 | 32 | + (instancetype)unaryExpressionWithExpression:(TDExpression *)expr { 33 | return [[[self alloc] initWithExpression:expr] autorelease]; 34 | } 35 | 36 | 37 | - (instancetype)initWithExpression:(TDExpression *)expr { 38 | self = [super init]; 39 | if (self) { 40 | self.expr = expr; 41 | } 42 | return self; 43 | } 44 | 45 | 46 | - (void)dealloc { 47 | self.expr = nil; 48 | [super dealloc]; 49 | } 50 | 51 | 52 | - (TDValue *)evaluateInContext:(TDTemplateContext *)ctx { 53 | double d = [_expr evaluateAsNumberInContext:ctx]; 54 | TDValue *res = [TDNumericValue numericValueWithNumber:-d]; 55 | return res; 56 | } 57 | 58 | @end 59 | -------------------------------------------------------------------------------- /src/TDNegationExpression.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDNegationExpression.h" 24 | #import "TDBooleanValue.h" 25 | 26 | @interface TDNegationExpression () 27 | @property (nonatomic, retain) TDExpression *expr; 28 | @end 29 | 30 | @implementation TDNegationExpression 31 | 32 | + (instancetype)negationExpressionWithExpression:(TDExpression *)expr { 33 | return [[[self alloc] initWithExpression:expr] autorelease]; 34 | } 35 | 36 | 37 | - (instancetype)initWithExpression:(TDExpression *)expr { 38 | self = [super init]; 39 | if (self) { 40 | self.expr = expr; 41 | } 42 | return self; 43 | } 44 | 45 | 46 | - (void)dealloc { 47 | self.expr = nil; 48 | [super dealloc]; 49 | } 50 | 51 | 52 | - (TDValue *)evaluateInContext:(TDTemplateContext *)ctx { 53 | BOOL b = [_expr evaluateAsBooleanInContext:ctx]; 54 | TDValue *res = [TDBooleanValue booleanValueWithBoolean:!b]; 55 | return res; 56 | } 57 | 58 | @end 59 | -------------------------------------------------------------------------------- /src/TDBooleanValue.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDBooleanValue.h" 24 | 25 | @implementation TDBooleanValue { 26 | BOOL _value; 27 | } 28 | 29 | + (TDBooleanValue *)booleanValueWithBoolean:(BOOL)b { 30 | return [[[self alloc] initWithBoolean:b] autorelease]; 31 | } 32 | 33 | 34 | - (instancetype)initWithBoolean:(BOOL)b { 35 | if (self = [super init]) { 36 | _value = b; 37 | } 38 | return self; 39 | } 40 | 41 | 42 | - (NSString *)stringValue { 43 | return _value ? @"true" : @"false"; 44 | } 45 | 46 | 47 | - (id)objectValue { 48 | return @(_value); 49 | } 50 | 51 | 52 | - (double)doubleValue { 53 | return _value ? 1.0 : 0.0; 54 | } 55 | 56 | 57 | - (BOOL)boolValue { 58 | return _value; 59 | } 60 | 61 | 62 | - (TDDataType)dataType { 63 | return TDDataTypeBoolean; 64 | } 65 | 66 | 67 | - (void)display:(NSInteger)level { 68 | //NSLog(@"%@boolean (%@)", [self indent:level], [self stringValue]); 69 | } 70 | 71 | 72 | - (NSString *)description { 73 | return [NSString stringWithFormat:@"", [self stringValue]]; 74 | } 75 | 76 | @end 77 | -------------------------------------------------------------------------------- /src/TDStringValue.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDStringValue.h" 24 | 25 | @interface TDStringValue () 26 | @property (nonatomic, copy) NSString *value; 27 | @end 28 | 29 | @implementation TDStringValue 30 | 31 | + (TDStringValue *)stringValueWithString:(NSString *)s { 32 | return [[[self alloc] initWithString:s] autorelease]; 33 | } 34 | 35 | 36 | - (instancetype)initWithString:(NSString *)s { 37 | if (self = [super init]) { 38 | self.value = (!s ? @"" : s); 39 | } 40 | return self; 41 | } 42 | 43 | 44 | - (void)dealloc { 45 | self.value = nil; 46 | [super dealloc]; 47 | } 48 | 49 | 50 | - (id)objectValue { 51 | return _value; 52 | } 53 | 54 | 55 | - (NSString *)stringValue { 56 | return _value; 57 | } 58 | 59 | 60 | - (double)doubleValue { 61 | return TDNumberFromString(_value); 62 | } 63 | 64 | 65 | - (BOOL)boolValue { 66 | return [_value length] > 0; 67 | } 68 | 69 | 70 | - (TDDataType)dataType { 71 | return TDDataTypeString; 72 | } 73 | 74 | 75 | - (BOOL)isEqualToStringValue:(TDStringValue *)v { 76 | return [_value isEqualToString:v->_value]; 77 | } 78 | 79 | @end 80 | -------------------------------------------------------------------------------- /src/TDWriter.m: -------------------------------------------------------------------------------- 1 | // 2 | // TDWriter.m 3 | // TDTemplateEngine 4 | // 5 | // Created by Todd Ditchendorf on 4/2/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @implementation TDWriter 13 | 14 | + (instancetype)writerWithOutputStream:(NSOutputStream *)output { 15 | return [[[self alloc] initWithOutputStream:output] autorelease]; 16 | } 17 | 18 | 19 | - (instancetype)initWithOutputStream:(NSOutputStream *)output { 20 | self = [super init]; 21 | if (self) { 22 | self.output = output; 23 | } 24 | return self; 25 | } 26 | 27 | 28 | - (void)dealloc { 29 | self.output = nil; 30 | [super dealloc]; 31 | } 32 | 33 | 34 | - (void)appendString:(NSString *)str { 35 | TDAssert(_output); 36 | TDAssert(str); 37 | 38 | // NSUInteger maxLen = [str maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding]; 39 | // if (maxLen) { 40 | // const uint8_t *zstr = (const uint8_t *)[str UTF8String]; 41 | // NSInteger written = [_output write:zstr maxLength:maxLen]; 42 | // if (-1 == written) { 43 | // [NSException raise:TDTemplateEngineErrorDomain format:@"Error while writing template output string"]; 44 | // } 45 | // } 46 | 47 | NSUInteger len = [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; 48 | if (len) { 49 | const uint8_t *zstr = (const uint8_t *)[str UTF8String]; 50 | NSUInteger remaining = len; 51 | do { 52 | NSInteger trim = remaining - len; 53 | if (trim > 0) { 54 | zstr += trim; 55 | } 56 | NSInteger written = [_output write:zstr maxLength:len]; 57 | if (-1 == written) { 58 | [NSException raise:TDTemplateEngineErrorDomain format:@"Error while writing template output string"]; 59 | } 60 | remaining -= written; 61 | } while (remaining > 0); 62 | } 63 | } 64 | 65 | 66 | - (void)appendFormat:(NSString *)fmt, ... { 67 | va_list vargs; 68 | va_start(vargs, fmt); 69 | 70 | NSString *str = [[[NSString alloc] initWithFormat:fmt arguments:vargs] autorelease]; 71 | [self appendString:str]; 72 | 73 | va_end(vargs); 74 | } 75 | 76 | @end 77 | -------------------------------------------------------------------------------- /src/TDNumericValue.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDNumericValue.h" 24 | 25 | @implementation TDNumericValue { 26 | double _value; 27 | } 28 | 29 | + (TDNumericValue *)numericValueWithNumber:(double)n { 30 | return [[[self alloc] initWithNumber:n] autorelease]; 31 | } 32 | 33 | 34 | - (instancetype)initWithNumber:(double)n { 35 | if (self = [super init]) { 36 | _value = n; 37 | } 38 | return self; 39 | } 40 | 41 | 42 | - (id)objectValue { 43 | return @(_value); 44 | } 45 | 46 | 47 | - (NSString *)stringValue { 48 | return [[NSNumber numberWithDouble:_value] stringValue]; 49 | } 50 | 51 | 52 | - (double)doubleValue { 53 | return _value; 54 | } 55 | 56 | 57 | - (BOOL)boolValue { 58 | return (_value != 0.0 && !isnan(_value)); 59 | } 60 | 61 | 62 | - (TDDataType)dataType { 63 | return TDDataTypeNumber; 64 | } 65 | 66 | 67 | - (void)display:(NSInteger)level { 68 | //NSLog(@"%@number (%@)", [self indent:level], [self stringValue]); 69 | } 70 | 71 | 72 | - (NSString *)description { 73 | return [NSString stringWithFormat:@"", self, [self stringValue]]; 74 | } 75 | 76 | @end 77 | -------------------------------------------------------------------------------- /src/TDIfTag.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDIfTag.h" 24 | #import "TDElseTag.h" 25 | #import "TDElseIfTag.h" 26 | #import 27 | #import 28 | 29 | @implementation TDIfTag 30 | 31 | + (NSString *)tagName { 32 | return @"if"; 33 | } 34 | 35 | 36 | + (TDTagType)tagType { 37 | return TDTagTypeBlock; 38 | } 39 | 40 | 41 | - (BOOL)isElse:(TDNode *)node { 42 | return [node.tagName isEqualToString:[TDElseTag tagName]]; 43 | } 44 | 45 | 46 | - (BOOL)isElseIf:(TDNode *)node { 47 | return [node.tagName isEqualToString:[TDElseIfTag tagName]]; 48 | } 49 | 50 | 51 | - (void)doTagInContext:(TDTemplateContext *)ctx { 52 | TDAssert(ctx); 53 | TDAssert(self.expression); 54 | 55 | BOOL test = [self.expression evaluateAsBooleanInContext:ctx]; 56 | 57 | for (TDNode *child in self.children) { 58 | if ([self isElseIf:child]) { 59 | if (test) break; 60 | TDElseIfTag *elseIf = (id)child; 61 | test = [elseIf.expression evaluateAsBooleanInContext:ctx]; 62 | } else if ([self isElse:child]) { 63 | if (test) break; 64 | test = YES; 65 | } else if (test) { 66 | [child renderInContext:ctx]; 67 | } 68 | } 69 | } 70 | 71 | @end 72 | -------------------------------------------------------------------------------- /test/TDNewlineTagTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // TDNewlineTagTests.m 3 | // TDTemplateEngineTests 4 | // 5 | // Created by Todd Ditchendorf on 3/28/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDTestScaffold.h" 10 | 11 | @interface TDNewlineTagTests : XCTestCase 12 | @property (nonatomic, retain) TDTemplateEngine *engine; 13 | @property (nonatomic, retain) NSOutputStream *output; 14 | @end 15 | 16 | @implementation TDNewlineTagTests 17 | 18 | - (void)setUp { 19 | [super setUp]; 20 | 21 | self.engine = [TDTemplateEngine templateEngine]; 22 | self.output = [NSOutputStream outputStreamToMemory]; 23 | } 24 | 25 | - (void)tearDown { 26 | self.engine = nil; 27 | self.output = nil; 28 | 29 | [super tearDown]; 30 | } 31 | 32 | - (NSString *)outputString { 33 | NSString *str = [[[NSString alloc] initWithData:[_output propertyForKey:NSStreamDataWrittenToMemoryStreamKey] encoding:NSUTF8StringEncoding] autorelease]; 34 | return str; 35 | } 36 | 37 | - (void)testNewline { 38 | NSString *input = @"{% br %}"; 39 | id vars = nil; 40 | 41 | NSError *err = nil; 42 | BOOL success = [_engine processTemplateString:input withVariables:vars toStream:_output error:&err]; 43 | TDTrue(success); 44 | TDNil(err); 45 | NSString *res = [self outputString]; 46 | TDEqualObjects(@"\n", res); 47 | } 48 | 49 | - (void)testNewline1 { 50 | NSString *input = @"{% br 1 %}"; 51 | id vars = nil; 52 | 53 | NSError *err = nil; 54 | BOOL success = [_engine processTemplateString:input withVariables:vars toStream:_output error:&err]; 55 | TDTrue(success); 56 | TDNil(err); 57 | NSString *res = [self outputString]; 58 | TDEqualObjects(@"\n", res); 59 | } 60 | 61 | - (void)testNewline2 { 62 | NSString *input = @"{% br 2 %}"; 63 | id vars = nil; 64 | 65 | NSError *err = nil; 66 | BOOL success = [_engine processTemplateString:input withVariables:vars toStream:_output error:&err]; 67 | TDTrue(success); 68 | TDNil(err); 69 | NSString *res = [self outputString]; 70 | TDEqualObjects(@"\n\n", res); 71 | } 72 | 73 | - (void)testNewlineNestedInTrim { 74 | NSString *input = @"{% trim %}{% br %}{% endtrim %}"; 75 | id vars = nil; 76 | 77 | NSError *err = nil; 78 | BOOL success = [_engine processTemplateString:input withVariables:vars toStream:_output error:&err]; 79 | TDTrue(success); 80 | TDNil(err); 81 | NSString *res = [self outputString]; 82 | TDEqualObjects(@"\n", res); 83 | } 84 | 85 | @end 86 | -------------------------------------------------------------------------------- /test/TDIndentTagTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // TDIndentTagTests.m 3 | // TDTemplateEngineTests 4 | // 5 | // Created by Todd Ditchendorf on 3/28/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDTestScaffold.h" 10 | 11 | @interface TDIndentTagTests : XCTestCase 12 | @property (nonatomic, retain) TDTemplateEngine *engine; 13 | @property (nonatomic, retain) NSOutputStream *output; 14 | @end 15 | 16 | @implementation TDIndentTagTests 17 | 18 | - (void)setUp { 19 | [super setUp]; 20 | 21 | self.engine = [TDTemplateEngine templateEngine]; 22 | self.output = [NSOutputStream outputStreamToMemory]; 23 | } 24 | 25 | - (void)tearDown { 26 | self.engine = nil; 27 | self.output = nil; 28 | 29 | [super tearDown]; 30 | } 31 | 32 | - (NSString *)outputString { 33 | NSString *str = [[[NSString alloc] initWithData:[_output propertyForKey:NSStreamDataWrittenToMemoryStreamKey] encoding:NSUTF8StringEncoding] autorelease]; 34 | return str; 35 | } 36 | 37 | - (void)testTrimF { 38 | NSString *input = 39 | @"{% trim %}\n" 40 | @" {% indent %}\n" 41 | @"f\n" 42 | @" {% endindent %}" 43 | @"{% endtrim %}"; 44 | id vars = nil; 45 | 46 | NSError *err = nil; 47 | BOOL success = [_engine processTemplateString:input withVariables:vars toStream:_output error:&err]; 48 | TDTrue(success); 49 | TDNil(err); 50 | NSString *res = [self outputString]; 51 | TDEqualObjects(@" f\n", res); 52 | } 53 | 54 | - (void)testTrimF2 { 55 | NSString *input = 56 | @"{% trim %}\n" 57 | @" {% indent %}\n" 58 | @"f\n" 59 | @" f\n" 60 | @"f\n" 61 | @" {% endindent %}" 62 | @"{% endtrim %}"; 63 | id vars = nil; 64 | 65 | NSError *err = nil; 66 | BOOL success = [_engine processTemplateString:input withVariables:vars toStream:_output error:&err]; 67 | TDTrue(success); 68 | TDNil(err); 69 | NSString *res = [self outputString]; 70 | TDEqualObjects(@" f\n f\n f\n", res); 71 | } 72 | 73 | - (void)testTrimF2Times { 74 | NSString *input = 75 | @"{% trim %}\n" 76 | @" {% indent 2 %}\n" 77 | @"f\n" 78 | @" f\n" 79 | @"f\n" 80 | @" {% endindent %}" 81 | @"{% endtrim %}"; 82 | id vars = nil; 83 | 84 | NSError *err = nil; 85 | BOOL success = [_engine processTemplateString:input withVariables:vars toStream:_output error:&err]; 86 | TDTrue(success); 87 | TDNil(err); 88 | NSString *res = [self outputString]; 89 | TDEqualObjects(@" f\n f\n f\n", res); 90 | } 91 | 92 | @end 93 | -------------------------------------------------------------------------------- /src/TDExpression.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | #import 25 | #import "TDValue.h" 26 | #import "TDParser.h" 27 | #import 28 | 29 | @implementation TDExpression 30 | 31 | - (void)dealloc { 32 | 33 | [super dealloc]; 34 | } 35 | 36 | 37 | - (TDValue *)evaluateInContext:(TDTemplateContext *)ctx { 38 | NSAssert2(0, @"%s is an abstract method and must be implemented in %@", __PRETTY_FUNCTION__, [self class]); 39 | return nil; 40 | } 41 | 42 | 43 | - (BOOL)evaluateAsBooleanInContext:(TDTemplateContext *)ctx { 44 | return [[self evaluateInContext:ctx] boolValue]; 45 | } 46 | 47 | 48 | - (double)evaluateAsNumberInContext:(TDTemplateContext *)ctx { 49 | return [[self evaluateInContext:ctx] doubleValue]; 50 | } 51 | 52 | 53 | - (NSString *)evaluateAsStringInContext:(TDTemplateContext *)ctx { 54 | return [[self evaluateInContext:ctx] stringValue]; 55 | } 56 | 57 | 58 | - (id)evaluateAsObjectInContext:(TDTemplateContext *)ctx { 59 | return [[self evaluateInContext:ctx] objectValue]; 60 | } 61 | 62 | 63 | - (BOOL)isValue { 64 | return [self isKindOfClass:[TDValue class]]; 65 | } 66 | 67 | 68 | - (TDExpression *)simplify { 69 | return self; 70 | } 71 | 72 | 73 | - (TDDataType)dataType { 74 | NSAssert2(0, @"%s is an abstract method and must be implemented in %@", __PRETTY_FUNCTION__, [self class]); 75 | return -1; 76 | } 77 | 78 | @end 79 | -------------------------------------------------------------------------------- /src/TDPadFilters.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDPadFilters.h" 24 | 25 | @implementation TDAbstractPadFilter 26 | 27 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 28 | TDAssert(input); 29 | 30 | [self validateArguments:args min:1 max:2]; 31 | 32 | NSString *inStr = TDStringFromObject(input); 33 | NSString *result = inStr; 34 | 35 | NSString *pad = @""; 36 | 37 | NSUInteger len = 0; 38 | if (args.count > 1) { 39 | len = [args[1] unsignedIntegerValue]; 40 | } 41 | 42 | if (0 == len && inStr.length) { 43 | pad = args[0]; 44 | } else if (result.length < len) { 45 | pad = args[0]; 46 | NSMutableString *buf = [NSMutableString stringWithCapacity:len]; 47 | 48 | for (NSUInteger i = len - result.length; i > 0; --i) { 49 | [buf appendString:pad]; 50 | } 51 | 52 | pad = buf; 53 | } 54 | 55 | if (self.left) { 56 | result = [NSString stringWithFormat:@"%@%@", pad, result]; 57 | } else { 58 | result = [NSString stringWithFormat:@"%@%@", result, pad]; 59 | } 60 | 61 | return result; 62 | } 63 | 64 | @end 65 | 66 | @implementation TDLpadFilter 67 | 68 | + (NSString *)filterName { 69 | return @"lpad"; 70 | } 71 | 72 | 73 | - (instancetype)init { 74 | self = [super init]; 75 | if (self) { 76 | self.left = YES; 77 | } 78 | return self; 79 | } 80 | 81 | @end 82 | 83 | @implementation TDRpadFilter 84 | 85 | + (NSString *)filterName { 86 | return @"rpad"; 87 | } 88 | 89 | @end 90 | -------------------------------------------------------------------------------- /TDTemplateEngine.xcodeproj/xcshareddata/xcschemes/TDTemplateEngineiOS.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 9 | 10 | 16 | 22 | 23 | 24 | 25 | 26 | 32 | 33 | 43 | 44 | 50 | 51 | 57 | 58 | 59 | 60 | 62 | 63 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /src/TDTemplateEngine+ExpressionSupport.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDTemplateEngine+ExpressionSupport.h" 24 | 25 | #import "TDParser.h" 26 | #import "TDExpression.h" 27 | 28 | #import 29 | 30 | @interface TDTemplateEngine () 31 | @property (nonatomic, retain) TDParser *expressionParser; 32 | @end 33 | 34 | @implementation TDTemplateEngine (ExpressionSupport) 35 | 36 | - (PKTokenizer *)tokenizer { 37 | TDAssert(self.expressionParser); 38 | return [self.expressionParser tokenizer]; 39 | } 40 | 41 | 42 | - (TDExpression *)expressionFromString:(NSString *)str error:(NSError **)outErr { 43 | TDAssert(self.expressionParser); 44 | PKAssembly *a = [self.expressionParser parseString:str error:outErr]; 45 | 46 | TDExpression *expr = [a pop]; 47 | 48 | expr = [expr simplify]; 49 | return expr; 50 | } 51 | 52 | 53 | - (TDExpression *)expressionFromTokens:(NSArray *)toks error:(NSError **)outErr { 54 | TDAssert(self.expressionParser); 55 | PKAssembly *a = [self.expressionParser parseTokens:toks error:outErr]; 56 | 57 | TDExpression *expr = [a pop]; 58 | 59 | expr = [expr simplify]; 60 | return expr; 61 | } 62 | 63 | 64 | - (TDExpression *)loopExpressionFromTokens:(NSArray *)toks error:(NSError **)outErr { 65 | TDAssert(self.expressionParser); 66 | self.expressionParser.doLoopExpr = YES; 67 | PKAssembly *a = [self.expressionParser parseTokens:toks error:outErr]; 68 | self.expressionParser.doLoopExpr = NO; 69 | 70 | TDExpression *expr = [a pop]; 71 | 72 | expr = [expr simplify]; 73 | return expr; 74 | } 75 | 76 | @end 77 | -------------------------------------------------------------------------------- /src/TDRelationalExpression.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDRelationalExpression.h" 24 | #import "TDValue.h" 25 | #import "TDBooleanValue.h" 26 | 27 | @interface TDBinaryExpression () 28 | @property (nonatomic, retain) TDExpression *p1; 29 | @property (nonatomic, retain) TDExpression *p2; 30 | @property (nonatomic, assign) NSInteger operator; 31 | @end 32 | 33 | @implementation TDRelationalExpression 34 | 35 | + (TDRelationalExpression *)relationalExpression { 36 | return [[[self alloc] init] autorelease]; 37 | } 38 | 39 | 40 | + (TDRelationalExpression *)relationalExpressionWithOperand:(TDExpression *)lhs operator:(NSInteger)op operand:(TDExpression *)rhs { 41 | return [[[self alloc] initWithOperand:lhs operator:op operand:rhs] autorelease]; 42 | } 43 | 44 | 45 | - (TDExpression *)simplify { 46 | self.p1 = [self.p1 simplify]; 47 | self.p2 = [self.p2 simplify]; 48 | 49 | // TODO 50 | 51 | if ([self.p1 isValue] && [self.p2 isValue]) { 52 | return [self evaluateInContext:nil]; 53 | } 54 | 55 | // TODO 56 | return self; 57 | } 58 | 59 | 60 | - (TDValue *)evaluateInContext:(TDTemplateContext *)ctx { 61 | BOOL b = [self evaluateAsBooleanInContext:ctx]; 62 | return [TDBooleanValue booleanValueWithBoolean:b]; 63 | } 64 | 65 | 66 | - (BOOL)evaluateAsBooleanInContext:(TDTemplateContext *)ctx { 67 | TDValue *s1 = [self.p1 evaluateInContext:ctx]; 68 | TDValue *s2 = [self.p2 evaluateInContext:ctx]; 69 | 70 | return [s1 compareToValue:s2 usingOperator:self.operator]; 71 | } 72 | 73 | 74 | - (TDDataType)dataType { 75 | return TDDataTypeBoolean; 76 | } 77 | 78 | @end 79 | -------------------------------------------------------------------------------- /src/TDTag.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDTag.h" 24 | #import 25 | #import 26 | #import 27 | #import 28 | #import 29 | #import 30 | #import 31 | 32 | @interface TDTag () 33 | @property (nonatomic, retain) PKToken *endTagToken; 34 | @end 35 | 36 | @implementation TDTag 37 | 38 | + (NSString *)tagName { 39 | NSAssert2(0, @"%s is an abstract method and must be implemented in %@", __PRETTY_FUNCTION__, [self class]); 40 | return nil; 41 | } 42 | 43 | 44 | + (TDTagType)tagType { 45 | NSAssert2(0, @"%s is an abstract method and must be implemented in %@", __PRETTY_FUNCTION__, [self class]); 46 | return 0; 47 | } 48 | 49 | 50 | - (instancetype)initWithToken:(PKToken *)frag { 51 | self = [super initWithToken:frag]; 52 | if (self) { 53 | 54 | } 55 | return self; 56 | } 57 | 58 | 59 | - (void)dealloc { 60 | self.endTagToken = nil; 61 | [super dealloc]; 62 | } 63 | 64 | 65 | #pragma mark - 66 | #pragma mark TDNode 67 | 68 | - (void)renderInContext:(TDTemplateContext *)ctx { 69 | NSParameterAssert(ctx); 70 | 71 | TDTemplateContext *local = [[ctx copy] autorelease]; 72 | 73 | [self doTagInContext:local]; 74 | } 75 | 76 | 77 | #pragma mark - 78 | #pragma mark TDTag 79 | 80 | - (void)doTagInContext:(TDTemplateContext *)ctx { 81 | NSAssert2(0, @"%s is an abstract method and must be implemented in %@", __PRETTY_FUNCTION__, [self class]); 82 | } 83 | 84 | 85 | - (NSString *)tagName { 86 | return [[self class] tagName]; 87 | } 88 | 89 | @end 90 | -------------------------------------------------------------------------------- /src/TDFilter.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | #import 25 | 26 | NSString *TDStringFromObject(id obj) { 27 | NSCAssert(obj, @""); 28 | NSString *str = nil; 29 | if ([obj isKindOfClass:[NSString class]]) { 30 | str = obj; 31 | } else if ([obj respondsToSelector:@selector(stringValue)]) { 32 | str = [obj stringValue]; 33 | } else { 34 | str = [obj description]; 35 | } 36 | return str; 37 | } 38 | 39 | @implementation TDFilter 40 | 41 | + (NSString *)filterName { 42 | NSAssert2(0, @"%s is an abstract method and must be implemented in %@", __PRETTY_FUNCTION__, [self class]); 43 | return nil; 44 | } 45 | 46 | 47 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 48 | NSAssert2(0, @"%s is an abstract method and must be implemented in %@", __PRETTY_FUNCTION__, [self class]); 49 | return nil; 50 | } 51 | 52 | 53 | - (NSString *)filterName { 54 | return [[self class] filterName]; 55 | } 56 | 57 | 58 | - (void)validateArguments:(NSArray *)args min:(NSUInteger)min max:(NSUInteger)max { 59 | NSUInteger actual = [args count]; 60 | 61 | NSString *plural = min > 1 ? @"s" : @""; 62 | if (actual < min) { 63 | [NSException raise:TDTemplateEngineErrorDomain format:@"Filter '%@' requires at least %lu argument%@. %lu given.", [[self class] filterName], min, plural, actual]; 64 | } 65 | 66 | plural = (0 == max || max > 1) ? @"s" : @""; 67 | if (actual > max) { 68 | [NSException raise:TDTemplateEngineErrorDomain format:@"Filter '%@' requires at most %lu argument%@. %lu given.", [[self class] filterName], max, plural, actual]; 69 | } 70 | } 71 | 72 | @end 73 | -------------------------------------------------------------------------------- /src/TDBinaryExpression.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDBinaryExpression.h" 24 | #import "TDValue.h" 25 | 26 | @interface TDBinaryExpression () 27 | @property (nonatomic, retain) TDExpression *p1; 28 | @property (nonatomic, retain) TDExpression *p2; 29 | @property (nonatomic, assign) NSInteger operator; 30 | @end 31 | 32 | @implementation TDBinaryExpression 33 | 34 | + (TDBinaryExpression *)binaryExpression { 35 | return [[[self alloc] init] autorelease]; 36 | } 37 | 38 | 39 | + (TDBinaryExpression *)binaryExpressionWithOperand:(TDExpression *)lhs operator:(NSInteger)op operand:(TDExpression *)rhs { 40 | return [[[self alloc] initWithOperand:lhs operator:op operand:rhs] autorelease]; 41 | } 42 | 43 | 44 | - (instancetype)init { 45 | return [self initWithOperand:nil operator:-1 operand:nil]; 46 | } 47 | 48 | 49 | - (instancetype)initWithOperand:(TDExpression *)lhs operator:(NSInteger)op operand:(TDExpression *)rhs { 50 | if (self = [super init]) { 51 | self.p1 = lhs; 52 | self.p2 = rhs; 53 | self.operator = op; 54 | } 55 | return self; 56 | } 57 | 58 | 59 | - (void)dealloc { 60 | self.p1 = nil; 61 | self.p2 = nil; 62 | [super dealloc]; 63 | } 64 | 65 | 66 | - (NSString *)description { 67 | return [NSString stringWithFormat:@"<%@ %p `%@ %ld %@`>", [self class], self, self.p1, self.operator, self.p2]; 68 | } 69 | 70 | 71 | - (void)setOperand:(TDExpression *)lhs operator:(NSInteger)op operand:(TDExpression *)rhs { 72 | self.p1 = lhs; 73 | self.p2 = rhs; 74 | self.operator = op; 75 | } 76 | 77 | 78 | - (TDExpression *)simplify { 79 | self.p1 = [_p1 simplify]; 80 | self.p2 = [_p2 simplify]; 81 | 82 | if ([_p1 isValue] && [_p2 isValue]) { 83 | return [self evaluateInContext:nil]; 84 | } 85 | 86 | return self; 87 | } 88 | 89 | @end 90 | -------------------------------------------------------------------------------- /src/TDBooleanExpression.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDBooleanExpression.h" 24 | #import "TDValue.h" 25 | #import "TDBooleanValue.h" 26 | #import "TDParser.h" 27 | 28 | @interface TDBinaryExpression () 29 | @property (nonatomic, retain) TDExpression *p1; 30 | @property (nonatomic, retain) TDExpression *p2; 31 | @property (nonatomic, assign) NSInteger operator; 32 | @end 33 | 34 | @implementation TDBooleanExpression 35 | 36 | + (TDBooleanExpression *)booleanExpression { 37 | return [[[self alloc] init] autorelease]; 38 | } 39 | 40 | 41 | + (TDBooleanExpression *)booleanExpressionWithOperand:(TDExpression *)lhs operator:(NSInteger)op operand:(TDExpression *)rhs { 42 | return [[[self alloc] initWithOperand:lhs operator:op operand:rhs] autorelease]; 43 | } 44 | 45 | 46 | - (TDExpression *)simplify { 47 | self.p1 = [self.p1 simplify]; 48 | self.p2 = [self.p2 simplify]; 49 | if ([self.p1 isValue] && [self.p2 isValue]) { 50 | return [self evaluateInContext:nil]; 51 | } 52 | 53 | // TODO 54 | 55 | return self; 56 | } 57 | 58 | 59 | - (TDValue *)evaluateInContext:(TDTemplateContext *)ctx { 60 | BOOL b = [self evaluateAsBooleanInContext:ctx]; 61 | return [TDBooleanValue booleanValueWithBoolean:b]; 62 | } 63 | 64 | 65 | - (BOOL)evaluateAsBooleanInContext:(TDTemplateContext *)ctx { 66 | BOOL b1 = [self.p1 evaluateAsBooleanInContext:ctx]; 67 | BOOL b2 = [self.p2 evaluateAsBooleanInContext:ctx]; 68 | 69 | BOOL result = NO; 70 | switch (self.operator) { 71 | case TD_TOKEN_KIND_AND: 72 | result = b1 && b2; 73 | break; 74 | case TD_TOKEN_KIND_OR: 75 | result = b1 || b2; 76 | break; 77 | default: 78 | TDAssert(0); 79 | break; 80 | } 81 | 82 | return result; 83 | } 84 | 85 | 86 | - (TDDataType)dataType { 87 | return TDDataTypeBoolean; 88 | } 89 | 90 | @end 91 | -------------------------------------------------------------------------------- /src/TDNode.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDNode.h" 24 | #import 25 | 26 | @implementation TDNode 27 | 28 | + (instancetype)nodeWithToken:(PKToken *)frag parent:(TDNode *)parent { 29 | return [[[self alloc] initWithToken:frag parent:parent] autorelease]; 30 | } 31 | 32 | 33 | - (instancetype)initWithToken:(PKToken *)frag parent:(TDNode *)parent { 34 | NSParameterAssert(frag); 35 | self = [super initWithToken:frag]; 36 | if (self) { 37 | self.parent = parent; 38 | } 39 | return self; 40 | } 41 | 42 | 43 | - (void)dealloc { 44 | self.parent = nil; 45 | self.expression = nil; 46 | 47 | [super dealloc]; 48 | } 49 | 50 | 51 | #pragma mark - 52 | #pragma mark Public 53 | 54 | - (TDNode *)firstAncestorOfClass:(Class)cls { 55 | NSParameterAssert(cls); 56 | TDAssert(_parent); 57 | 58 | TDNode *result = _parent; 59 | while (result && ![result isKindOfClass:cls]) { 60 | result = result.parent; 61 | } 62 | return result; 63 | } 64 | 65 | 66 | - (TDNode *)firstAncestorOfTagName:(NSString *)tagName { 67 | NSParameterAssert([tagName length]); 68 | TDAssert(_parent); 69 | 70 | TDNode *result = _parent; 71 | while (result && ![result.tagName isEqualToString:tagName]) { 72 | result = result.parent; 73 | } 74 | return result; 75 | } 76 | 77 | 78 | - (NSString *)tagName { 79 | return nil; 80 | } 81 | 82 | 83 | #pragma mark - 84 | #pragma mark Render 85 | 86 | - (void)renderInContext:(TDTemplateContext *)ctx { 87 | NSParameterAssert(ctx); 88 | [self renderChildrenInContext:ctx]; 89 | } 90 | 91 | 92 | - (void)renderChildrenInContext:(TDTemplateContext *)ctx { 93 | NSParameterAssert(ctx); 94 | for (TDNode *child in self.children) { 95 | [child renderInContext:ctx]; 96 | } 97 | } 98 | 99 | @end 100 | -------------------------------------------------------------------------------- /test/TDVerbatimTagTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // TDVerbatimTagTests.m 3 | // TDTemplateEngineTests 4 | // 5 | // Created by Todd Ditchendorf on 3/28/14. 6 | // Copyright (c) 2014 Todd Ditchendorf. All rights reserved. 7 | // 8 | 9 | #import "TDTestScaffold.h" 10 | 11 | @interface TDVerbatimTagTests : XCTestCase 12 | @property (nonatomic, retain) TDTemplateEngine *engine; 13 | @property (nonatomic, retain) NSOutputStream *output; 14 | @end 15 | 16 | @implementation TDVerbatimTagTests 17 | 18 | - (void)setUp { 19 | [super setUp]; 20 | 21 | self.engine = [TDTemplateEngine templateEngine]; 22 | self.output = [NSOutputStream outputStreamToMemory]; 23 | } 24 | 25 | - (void)tearDown { 26 | self.engine = nil; 27 | self.output = nil; 28 | 29 | [super tearDown]; 30 | } 31 | 32 | - (NSString *)outputString { 33 | NSString *str = [[[NSString alloc] initWithData:[_output propertyForKey:NSStreamDataWrittenToMemoryStreamKey] encoding:NSUTF8StringEncoding] autorelease]; 34 | return str; 35 | } 36 | 37 | - (void)testVerbatimF { 38 | NSString *input = @"{% verbatim %}f{% endverbatim %}"; 39 | id vars = nil; 40 | 41 | NSError *err = nil; 42 | BOOL success = [_engine processTemplateString:input withVariables:vars toStream:_output error:&err]; 43 | TDTrue(success); 44 | TDNil(err); 45 | NSString *res = [self outputString]; 46 | TDEqualObjects(@"f", res); 47 | } 48 | 49 | - (void)testVerbatimNestedVar { 50 | NSString *input = @"{% verbatim %}{{foo}}{% endverbatim %}"; 51 | id vars = nil; 52 | 53 | NSError *err = nil; 54 | BOOL success = [_engine processTemplateString:input withVariables:vars toStream:_output error:&err]; 55 | TDTrue(success); 56 | TDNil(err); 57 | NSString *res = [self outputString]; 58 | TDEqualObjects(@"{{foo}}", res); 59 | } 60 | 61 | - (void)testVerbatimNestedVarSpace { 62 | NSString *input = @"{% verbatim %}{{ foo }}{% endverbatim %}"; 63 | id vars = nil; 64 | 65 | NSError *err = nil; 66 | BOOL success = [_engine processTemplateString:input withVariables:vars toStream:_output error:&err]; 67 | TDTrue(success); 68 | TDNil(err); 69 | NSString *res = [self outputString]; 70 | TDEqualObjects(@"{{ foo }}", res); 71 | } 72 | 73 | - (void)testVerbatimNestedIfTagSpace { 74 | NSString *input = @"{% verbatim %}{% if 1 %}HI!{% endif %}{% endverbatim %}"; 75 | id vars = nil; 76 | 77 | NSError *err = nil; 78 | BOOL success = [_engine processTemplateString:input withVariables:vars toStream:_output error:&err]; 79 | TDTrue(success); 80 | TDNil(err); 81 | NSString *res = [self outputString]; 82 | TDEqualObjects(@"{% if 1 %}HI!{% endif %}", res); 83 | } 84 | 85 | - (void)testVerbatimNestedIfTag { 86 | NSString *input = @"{% verbatim %}{%if 1%}HI!{%endif%}{% endverbatim %}"; 87 | id vars = nil; 88 | 89 | NSError *err = nil; 90 | BOOL success = [_engine processTemplateString:input withVariables:vars toStream:_output error:&err]; 91 | TDTrue(success); 92 | TDNil(err); 93 | NSString *res = [self outputString]; 94 | TDEqualObjects(@"{%if 1%}HI!{%endif%}", res); 95 | } 96 | 97 | @end 98 | -------------------------------------------------------------------------------- /src/TDReplaceFilter.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDReplaceFilter.h" 24 | 25 | @implementation TDReplaceFilter 26 | 27 | + (NSString *)filterName { 28 | return @"replace"; 29 | } 30 | 31 | 32 | - (id)doFilter:(id)input withArguments:(NSArray *)args { 33 | TDAssert(input); 34 | 35 | [self validateArguments:args min:2 max:3]; 36 | 37 | NSString *inStr = TDStringFromObject(input); 38 | 39 | NSString *searchPat = args[0]; 40 | NSString *replacePat = args[1]; 41 | 42 | NSRegularExpressionOptions opts = 0; 43 | 44 | if (3 == [args count]) { 45 | NSString *optStr = args[2]; 46 | 47 | if ([optStr length]) { 48 | if ([optStr rangeOfString:@"i"].length) { 49 | opts |= NSRegularExpressionCaseInsensitive; 50 | } 51 | 52 | if ([optStr rangeOfString:@"m"].length) { 53 | opts |= NSRegularExpressionAnchorsMatchLines; 54 | } 55 | 56 | if ([optStr rangeOfString:@"x"].length) { 57 | opts |= NSRegularExpressionAllowCommentsAndWhitespace; 58 | } 59 | 60 | if ([optStr rangeOfString:@"s"].length) { 61 | opts |= NSRegularExpressionDotMatchesLineSeparators; 62 | } 63 | 64 | if ([optStr rangeOfString:@"u"].length) { 65 | opts |= NSRegularExpressionUseUnicodeWordBoundaries; 66 | } 67 | } 68 | } 69 | 70 | searchPat = [searchPat stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"]; 71 | 72 | NSError *err = nil; 73 | NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:searchPat options:opts error:&err]; 74 | 75 | NSString *result = [regex stringByReplacingMatchesInString:inStr options:NSMatchingReportCompletion range:NSMakeRange(0, [inStr length]) withTemplate:replacePat]; 76 | 77 | return result; 78 | } 79 | 80 | @end 81 | -------------------------------------------------------------------------------- /TDTemplateEngine.xcodeproj/xcshareddata/xcschemes/libTDTemplateEngineOSX.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 43 | 44 | 50 | 51 | 52 | 53 | 59 | 60 | 66 | 67 | 68 | 69 | 71 | 72 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /src/TDArithmeticExpression.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDArithmeticExpression.h" 24 | #import "TDValue.h" 25 | #import "TDNumericValue.h" 26 | #import "TDParser.h" 27 | 28 | @interface TDBinaryExpression () 29 | @property (nonatomic, retain) TDExpression *p1; 30 | @property (nonatomic, retain) TDExpression *p2; 31 | @property (nonatomic, assign) NSInteger operator; 32 | @end 33 | 34 | @implementation TDArithmeticExpression 35 | 36 | + (TDArithmeticExpression *)arithmeticExpression { 37 | return [[[self alloc] init] autorelease]; 38 | } 39 | 40 | 41 | + (TDArithmeticExpression *)arithmeticExpressionWithOperand:(TDExpression *)lhs operator:(NSInteger)op operand:(TDExpression *)rhs { 42 | return [[[self alloc] initWithOperand:lhs operator:op operand:rhs] autorelease]; 43 | } 44 | 45 | 46 | - (TDValue *)evaluateInContext:(TDTemplateContext *)ctx { 47 | double n = [self evaluateAsNumberInContext:ctx]; 48 | return [TDNumericValue numericValueWithNumber:n]; 49 | } 50 | 51 | 52 | - (double)evaluateAsNumberInContext:(TDTemplateContext *)ctx { 53 | double n1 = [self.p1 evaluateAsNumberInContext:ctx]; 54 | double n2 = [self.p2 evaluateAsNumberInContext:ctx]; 55 | 56 | double res = 0.0; 57 | switch (self.operator) { 58 | case TD_TOKEN_KIND_PLUS: 59 | res = n1 + n2; 60 | break; 61 | case TD_TOKEN_KIND_MINUS: 62 | res = n1 - n2; 63 | break; 64 | case TD_TOKEN_KIND_TIMES: 65 | res = n1 * n2; 66 | break; 67 | case TD_TOKEN_KIND_DIV: 68 | res = n1 / n2; 69 | break; 70 | case TD_TOKEN_KIND_MOD: 71 | res = lrint(n1) % lrint(n2); 72 | break; 73 | default: 74 | [NSException raise:@"TDTemplateEngineErrorDomain" format:@"invalid operator in arithmetic expr"]; 75 | res = NAN; 76 | break; 77 | } 78 | return res; 79 | } 80 | 81 | 82 | - (TDDataType)dataType { 83 | return TDDataTypeNumber; 84 | } 85 | 86 | @end 87 | -------------------------------------------------------------------------------- /TDTemplateEngine.xcodeproj/xcshareddata/xcschemes/TDTemplateEngine.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 9 | 10 | 16 | 22 | 23 | 24 | 25 | 26 | 32 | 33 | 35 | 41 | 42 | 43 | 44 | 45 | 55 | 56 | 62 | 63 | 69 | 70 | 71 | 72 | 74 | 75 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /src/XPAssembler.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "XPAssembler.h" 24 | #import 25 | #import 26 | #import 27 | #import 28 | #import 29 | #import 30 | 31 | @interface XPAssembler () 32 | @property (nonatomic, retain) PKToken *openParen; 33 | @end 34 | 35 | @implementation XPAssembler 36 | 37 | - (instancetype)init { 38 | self = [super init]; 39 | if (self) { 40 | self.openParen = [PKToken tokenWithTokenType:PKTokenTypeSymbol stringValue:@"(" doubleValue:0.0]; 41 | } 42 | return self; 43 | } 44 | 45 | 46 | - (void)dealloc { 47 | self.openParen = nil; 48 | [super dealloc]; 49 | } 50 | 51 | 52 | - (void)parser:(PKParser *)p didMatchSubExpr:(PKAssembly *)a { 53 | //NSLog(@"%s", __PRETTY_FUNCTION__); 54 | 55 | NSArray *objs = [a objectsAbove:_openParen]; 56 | [a pop]; // discard `(` 57 | for (id obj in [objs reverseObjectEnumerator]) { 58 | [a push:obj]; 59 | } 60 | } 61 | 62 | 63 | - (void)parser:(PKParser *)p didMatchStr:(PKAssembly *)a { 64 | //NSLog(@"%s", __PRETTY_FUNCTION__); 65 | 66 | PKToken *tok = [a pop]; 67 | NSString *str = tok.stringValue; 68 | str = [str substringWithRange:NSMakeRange(1, [str length]-2)]; 69 | XPValue *val = [XPStringValue stringValueWithString:str]; 70 | [a push:val]; 71 | } 72 | 73 | 74 | - (void)parser:(PKParser *)p didMatchNum:(PKAssembly *)a { 75 | //NSLog(@"%s", __PRETTY_FUNCTION__); 76 | 77 | PKToken *tok = [a pop]; 78 | XPValue *val = [XPNumericValue numericValueWithNumber:tok.doubleValue]; 79 | [a push:val]; 80 | } 81 | 82 | 83 | - (void)parser:(PKParser *)p didMatchTrue:(PKAssembly *)a { 84 | //NSLog(@"%s", __PRETTY_FUNCTION__); 85 | 86 | XPValue *val = [XPBooleanValue booleanValueWithBoolean:YES]; 87 | [a push:val]; 88 | } 89 | 90 | 91 | - (void)parser:(PKParser *)p didMatchFalse:(PKAssembly *)a { 92 | //NSLog(@"%s", __PRETTY_FUNCTION__); 93 | 94 | XPValue *val = [XPBooleanValue booleanValueWithBoolean:NO]; 95 | [a push:val]; 96 | } 97 | 98 | @end 99 | -------------------------------------------------------------------------------- /src/TDForTag.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDForTag.h" 24 | #import "TDForLoop.h" 25 | #import 26 | 27 | #import "TDLoopExpression.h" 28 | #import "TDEnumeration.h" 29 | #import "TDSkipException.h" 30 | 31 | @interface TDForTag () 32 | @property (nonatomic, retain) TDForLoop *currentLoop; 33 | @end 34 | 35 | @implementation TDForTag 36 | 37 | + (NSString *)tagName { 38 | return @"for"; 39 | } 40 | 41 | 42 | + (TDTagType)tagType { 43 | return TDTagTypeBlock; 44 | } 45 | 46 | 47 | - (void)dealloc { 48 | self.currentLoop = nil; 49 | [super dealloc]; 50 | } 51 | 52 | 53 | - (NSString *)description { 54 | return [NSString stringWithFormat:@"%p for %@", self, self.expression]; 55 | } 56 | 57 | 58 | - (void)doTagInContext:(TDTemplateContext *)ctx { 59 | //NSLog(@"%s %@", __PRETTY_FUNCTION__, self); 60 | TDAssert(ctx); 61 | 62 | TDLoopExpression *expr = (id)self.expression; 63 | TDAssert([expr isKindOfClass:[TDLoopExpression class]]); 64 | 65 | [self setUpForLoop:ctx]; 66 | 67 | while ([expr evaluateInContext:ctx]) { 68 | _currentLoop.last = ![expr.enumeration hasMore]; 69 | 70 | //NSLog(@"rendering body of %@", self); 71 | @try { 72 | [self renderChildrenInContext:ctx]; 73 | } 74 | @catch (TDSkipException *ex) { 75 | continue; 76 | } 77 | @finally { 78 | _currentLoop.currentIndex++; 79 | _currentLoop.first = NO; 80 | } 81 | } 82 | 83 | [self tearDownForLoop:ctx]; 84 | } 85 | 86 | 87 | - (void)setUpForLoop:(TDTemplateContext *)ctx { 88 | self.currentLoop = [[[TDForLoop alloc] init] autorelease]; 89 | 90 | TDForTag *enclosingForTag = (id)[self firstAncestorOfTagName:@"for"]; 91 | _currentLoop.parentLoop = enclosingForTag.currentLoop; 92 | 93 | [ctx defineVariable:@"currentLoop" value:_currentLoop]; 94 | } 95 | 96 | 97 | - (void)tearDownForLoop:(TDTemplateContext *)ctx { 98 | [ctx defineVariable:@"currentLoop" value:nil]; 99 | _currentLoop.parentLoop = nil; 100 | self.currentLoop = nil; 101 | } 102 | 103 | @end 104 | -------------------------------------------------------------------------------- /src/TDLoopExpression.m: -------------------------------------------------------------------------------- 1 | // The MIT License (MIT) 2 | // 3 | // Copyright (c) 2014 Todd Ditchendorf 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "TDLoopExpression.h" 24 | #import 25 | 26 | @implementation TDLoopExpression 27 | 28 | + (instancetype)loopExpressionWithVariables:(NSArray *)vars enumeration:(TDEnumeration *)e { 29 | return [[[self alloc] initWithVariables:vars enumeration:e] autorelease]; 30 | } 31 | 32 | 33 | - (instancetype)initWithVariables:(NSArray *)vars enumeration:(TDEnumeration *)e { 34 | self = [super init]; 35 | if (self) { 36 | NSUInteger c = [vars count]; 37 | if (2 == c) { 38 | self.firstVariable = vars[0]; 39 | self.secondVariable = vars[1]; 40 | } else if (1 == c) { 41 | self.firstVariable = vars[0]; 42 | } else { 43 | [NSException raise:@"" format:@""]; // TODO 44 | } 45 | 46 | self.enumeration = e; 47 | } 48 | return self; 49 | } 50 | 51 | 52 | - (void)dealloc { 53 | self.firstVariable = nil; 54 | self.secondVariable = nil; 55 | self.enumeration = nil; 56 | [super dealloc]; 57 | } 58 | 59 | 60 | - (NSString *)description { 61 | NSString *result = nil; 62 | if (_firstVariable) { 63 | result = [NSString stringWithFormat:@"%@,%@ in %@", _firstVariable, _secondVariable, _enumeration]; 64 | } else { 65 | result = [NSString stringWithFormat:@"%@ in %@", _secondVariable, _enumeration]; 66 | } 67 | return result; 68 | } 69 | 70 | 71 | - (id)evaluateInContext:(TDTemplateContext *)ctx { 72 | TDAssert([_firstVariable length]); 73 | TDAssert(_enumeration); 74 | 75 | id res = [_enumeration evaluateInContext:ctx]; 76 | id firstObj = nil; 77 | id secondObj = nil; 78 | 79 | if ([res isKindOfClass:[NSArray class]]) { 80 | TDAssert(2 == [res count]); 81 | firstObj = res[0]; 82 | secondObj = res[1]; 83 | } else { 84 | firstObj = res; 85 | } 86 | 87 | if (_secondVariable) { 88 | TDAssert([_secondVariable length]); 89 | [ctx defineVariable:_firstVariable value:firstObj]; 90 | [ctx defineVariable:_secondVariable value:secondObj]; 91 | } else { 92 | res = firstObj; 93 | [ctx defineVariable:_firstVariable value:firstObj]; 94 | } 95 | return res; 96 | } 97 | 98 | @end 99 | --------------------------------------------------------------------------------