Skip to content

Commit

Permalink
Pass failing tests for #46
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Mar 3, 2013
1 parent 5db8560 commit 3682eae
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 81 deletions.
128 changes: 86 additions & 42 deletions src/classes/GRMustacheBuffer.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ - (instancetype)initWithContentType:(GRMustacheContentType)contentType outputBuf
#pragma mark - Abstract class GRMustacheBuffer

@interface GRMustacheBuffer()
@property (nonatomic, readonly) GRMustacheContentType contentType;
@property (nonatomic, retain) NSString *prefix;
- (id)initWithContentType:(GRMustacheContentType)contentType;
- (void)appendSafeString:(NSString *)string blank:(BOOL)blank prefix:(BOOL)prefix suffix:(BOOL)suffix;
- (NSString *)appendSafeRendering:(NSString *)string;
- (void)appendSafeString:(NSString *)string inputType:(GRMustacheBufferInputType)inputType;
@end

@implementation GRMustacheBuffer
@synthesize contentType=_contentType;
@synthesize prefix=_prefix;

+ (instancetype)bufferWithContentType:(GRMustacheContentType)contentType outputString:(NSMutableString *)outputString
{
Expand All @@ -82,47 +82,102 @@ - (id)initWithContentType:(GRMustacheContentType)contentType
self = [super init];
if (self) {
_contentType = contentType;
_swallowsBlankEndOfLine = YES;
}
return self;
}

- (void)appendString:(NSString *)string contentType:(GRMustacheContentType)contentType blank:(BOOL)blank prefix:(BOOL)prefix suffix:(BOOL)suffix
- (NSString *)appendString:(NSString *)string contentType:(GRMustacheContentType)contentType inputType:(GRMustacheBufferInputType)inputType
{
if (string == nil) return;
if (_contentType == GRMustacheContentTypeHTML && contentType != GRMustacheContentTypeHTML) {
string = [GRMustache escapeHTML:string];
if (string == nil) {
string = @"";
}

[self appendSafeString:string blank:blank prefix:prefix suffix:suffix];
}

- (NSString *)appendRendering:(NSString *)string contentType:(GRMustacheContentType)contentType
{
if (string == nil) return @"";
if (_contentType == GRMustacheContentTypeHTML && contentType != GRMustacheContentTypeHTML) {
string = [GRMustache escapeHTML:string];
}

return [self appendSafeRendering:string];
}

- (void)flush
{

}

- (void)appendSafeString:(NSString *)string blank:(BOOL)blank prefix:(BOOL)prefix suffix:(BOOL)suffix
{
NSAssert(NO, @"Subclasses must override");
switch (inputType) {
case GRMustacheBufferInputTypeUserContent:
if (string.length > 0) {
if (self.prefix) {
[self appendSafeString:self.prefix inputType:GRMustacheBufferInputTypeBlankPrefix];
self.prefix = nil;
}
_swallowsBlankEndOfLine = NO;
[self appendSafeString:string inputType:inputType];
return string;
} else {
return @"";
}
break;

case GRMustacheBufferInputTypeContent:
if (self.prefix) {
[self appendSafeString:self.prefix inputType:GRMustacheBufferInputTypeBlankPrefix];
self.prefix = nil;
}
_swallowsBlankEndOfLine = NO;
[self appendSafeString:string inputType:inputType];
return string;
break;

case GRMustacheBufferInputTypeContentEndOfLine:
if (self.prefix) {
[self appendSafeString:self.prefix inputType:GRMustacheBufferInputTypeBlankPrefix];
self.prefix = nil;
}
_swallowsBlankEndOfLine = YES;
[self appendSafeString:string inputType:inputType];
return string;
break;

case GRMustacheBufferInputTypeBlankLine:
_swallowsBlankEndOfLine = YES;
[self appendSafeString:string inputType:inputType];
return string;
break;

case GRMustacheBufferInputTypeBlankEndOfLine:
if (_swallowsBlankEndOfLine) {
return @"";
} else {
if (self.prefix) {
[self appendSafeString:self.prefix inputType:GRMustacheBufferInputTypeBlankPrefix];
self.prefix = nil;
}
_swallowsBlankEndOfLine = YES;
[self appendSafeString:string inputType:inputType];
return string;
}
break;

case GRMustacheBufferInputTypeBlankPrefix:
self.prefix = string;
_swallowsBlankEndOfLine = YES;
return string;
break;

case GRMustacheBufferInputTypeBlankSuffix:
if (_swallowsBlankEndOfLine) {
return @"";
} else {
if (self.prefix) {
[self appendSafeString:self.prefix inputType:GRMustacheBufferInputTypeBlankPrefix];
self.prefix = nil;
}
_swallowsBlankEndOfLine = NO;
[self appendSafeString:string inputType:inputType];
return string;
}
break;
}
}

- (NSString *)appendSafeRendering:(NSString *)string
- (void)appendSafeString:(NSString *)string inputType:(GRMustacheBufferInputType)inputType
{
NSAssert(NO, @"Subclasses must override");
return nil;
}


@end


Expand All @@ -146,17 +201,11 @@ - (instancetype)initWithContentType:(GRMustacheContentType)contentType outputStr
return self;
}

- (void)appendSafeString:(NSString *)string blank:(BOOL)blank prefix:(BOOL)prefix suffix:(BOOL)suffix
- (void)appendSafeString:(NSString *)string inputType:(GRMustacheBufferInputType)inputType
{
[_outputString appendString:string];
}

- (NSString *)appendSafeRendering:(NSString *)string
{
[_outputString appendString:string];
return string;
}

@end


Expand All @@ -181,14 +230,9 @@ - (instancetype)initWithContentType:(GRMustacheContentType)contentType outputBuf
}


- (void)appendSafeString:(NSString *)string blank:(BOOL)blank prefix:(BOOL)prefix suffix:(BOOL)suffix
{
[_outputBuffer appendString:string contentType:self.contentType blank:blank prefix:prefix suffix:suffix];
}

- (NSString *)appendSafeRendering:(NSString *)string
- (void)appendSafeString:(NSString *)string inputType:(GRMustacheBufferInputType)inputType
{
return [_outputBuffer appendRendering:string contentType:self.contentType];
[_outputBuffer appendString:string contentType:self.contentType inputType:inputType];
}

@end
24 changes: 16 additions & 8 deletions src/classes/GRMustacheBuffer_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,44 @@
#import "GRMustacheAvailabilityMacros_private.h"
#import "GRMustacheConfiguration_private.h"

typedef NS_ENUM(NSInteger, GRMustacheBufferInputType) {
GRMustacheBufferInputTypeUserContent,
GRMustacheBufferInputTypeContent,
GRMustacheBufferInputTypeContentEndOfLine,
GRMustacheBufferInputTypeBlankLine,
GRMustacheBufferInputTypeBlankEndOfLine,
GRMustacheBufferInputTypeBlankPrefix,
GRMustacheBufferInputTypeBlankSuffix,
};

/**
* TODO
*/
@interface GRMustacheBuffer : NSObject {
@private
GRMustacheContentType _contentType;
NSString *_prefix;
BOOL _swallowsBlankEndOfLine;
}

/**
* TODO
*/
+ (instancetype)bufferWithContentType:(GRMustacheContentType)contentType outputString:(NSMutableString *)outputString GRMUSTACHE_API_INTERNAL;
@property (nonatomic, readonly) GRMustacheContentType contentType;

/**
* TODO
*/
+ (instancetype)bufferWithContentType:(GRMustacheContentType)contentType outputBuffer:(GRMustacheBuffer *)outputBuffer GRMUSTACHE_API_INTERNAL;
+ (instancetype)bufferWithContentType:(GRMustacheContentType)contentType outputString:(NSMutableString *)outputString GRMUSTACHE_API_INTERNAL;

/**
* TODO
*/
- (void)appendString:(NSString *)string contentType:(GRMustacheContentType)contentType blank:(BOOL)blank prefix:(BOOL)prefix suffix:(BOOL)suffix GRMUSTACHE_API_INTERNAL;
+ (instancetype)bufferWithContentType:(GRMustacheContentType)contentType outputBuffer:(GRMustacheBuffer *)outputBuffer GRMUSTACHE_API_INTERNAL;

/**
* TODO
*/
- (NSString *)appendRendering:(NSString *)string contentType:(GRMustacheContentType)contentType GRMUSTACHE_API_INTERNAL;
- (NSString *)appendString:(NSString *)string contentType:(GRMustacheContentType)contentType inputType:(GRMustacheBufferInputType)inputType GRMUSTACHE_API_INTERNAL;

/**
* TODO
*/
- (void)flush GRMUSTACHE_API_INTERNAL;
@end
10 changes: 5 additions & 5 deletions src/classes/GRMustacheCompiler.m
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ - (BOOL)parser:(GRMustacheParser *)parser shouldContinueAfterParsingToken:(GRMus
NSAssert(token.templateSubstring.length > 0, @"WTF parser?");

// Success: append GRMustacheTextComponent
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring blank:NO prefix:NO suffix:NO]];
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring inputType:GRMustacheBufferInputTypeContent]];
break;


Expand All @@ -248,31 +248,31 @@ - (BOOL)parser:(GRMustacheParser *)parser shouldContinueAfterParsingToken:(GRMus
NSAssert(token.templateSubstring.length > 0, @"WTF parser?");

// Success: append GRMustacheTextComponent
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring blank:YES prefix:YES suffix:YES]];
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring inputType:GRMustacheBufferInputTypeBlankLine]];
break;

case GRMustacheTokenTypeBlankEndOfLine:
// Parser validation
NSAssert(token.templateSubstring.length > 0, @"WTF parser?");

// Success: append GRMustacheTextComponent
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring blank:YES prefix:NO suffix:YES]];
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring inputType:GRMustacheBufferInputTypeBlankEndOfLine]];
break;

case GRMustacheTokenTypeBlankPrefix:
// Parser validation
NSAssert(token.templateSubstring.length > 0, @"WTF parser?");

// Success: append GRMustacheTextComponent
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring blank:YES prefix:YES suffix:NO]];
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring inputType:GRMustacheBufferInputTypeBlankPrefix]];
break;

case GRMustacheTokenTypeBlankSuffix:
// Parser validation
NSAssert(token.templateSubstring.length > 0, @"WTF parser?");

// Success: append GRMustacheTextComponent
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring blank:YES prefix:NO suffix:NO]];
[_currentComponents addObject:[GRMustacheTextComponent textComponentWithString:token.templateSubstring inputType:GRMustacheBufferInputTypeBlankSuffix]];
break;


Expand Down
24 changes: 17 additions & 7 deletions src/classes/GRMustacheParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -550,13 +550,23 @@ - (void)parseTemplateString:(NSString *)templateString templateID:(id)templateID
break;

case stateSpaceRun: {
// Blank suffix
GRMustacheToken *token = [GRMustacheToken tokenWithType:GRMustacheTokenTypeBlankSuffix
templateString:templateString
templateID:templateID
line:lineNumber
range:(NSRange){ .location = start, .length = i-start}];
if (![self.delegate parser:self shouldContinueAfterParsingToken:token]) return;
if (start == lineStart) {
// Content
GRMustacheToken *token = [GRMustacheToken tokenWithType:GRMustacheTokenTypeContent
templateString:templateString
templateID:templateID
line:lineNumber
range:(NSRange){ .location = start, .length = i-start}];
if (![self.delegate parser:self shouldContinueAfterParsingToken:token]) return;
} else {
// Blank suffix
GRMustacheToken *token = [GRMustacheToken tokenWithType:GRMustacheTokenTypeBlankSuffix
templateString:templateString
templateID:templateID
line:lineNumber
range:(NSRange){ .location = start, .length = i-start}];
if (![self.delegate parser:self shouldContinueAfterParsingToken:token]) return;
}
} break;

case stateContent: {
Expand Down
1 change: 0 additions & 1 deletion src/classes/GRMustacheSectionTag.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ - (NSString *)renderContentWithContext:(GRMustacheContext *)context HTMLSafe:(BO
if (HTMLSafe != NULL) {
*HTMLSafe = (self.contentType == GRMustacheContentTypeHTML);
}
[buffer flush];
return rendering;
}

Expand Down
2 changes: 1 addition & 1 deletion src/classes/GRMustacheTag.m
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ - (BOOL)renderContentType:(GRMustacheContentType)requiredContentType inBuffer:(G
// promote
renderingContentType = GRMustacheContentTypeHTML;
}
rendering = [buffer appendRendering:rendering contentType:renderingContentType];
rendering = [buffer appendString:rendering contentType:renderingContentType inputType:GRMustacheBufferInputTypeUserContent];

// Tag delegates post-rendering callbacks

Expand Down
7 changes: 2 additions & 5 deletions src/classes/GRMustacheTemplate.m
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ - (NSString *)renderContentWithContext:(GRMustacheContext *)context HTMLSafe:(BO
if (HTMLSafe != NULL) {
*HTMLSafe = (self.contentType == GRMustacheContentTypeHTML);
}
[buffer flush];
return rendering;
}

Expand Down Expand Up @@ -138,8 +137,8 @@ - (BOOL)renderContentType:(GRMustacheContentType)requiredContentType inBuffer:(G
return NO;
}

GRMustacheBuffer *localBuffer = [GRMustacheBuffer bufferWithContentType:_contentType outputBuffer:buffer];

GRMustacheBuffer *localBuffer = (_contentType == buffer.contentType) ? buffer : [GRMustacheBuffer bufferWithContentType:_contentType outputBuffer:buffer];
for (id<GRMustacheTemplateComponent> component in _components) {
// component may be overriden by a GRMustacheTemplateOverride: resolve it.
component = [context resolveTemplateComponent:component];
Expand All @@ -150,8 +149,6 @@ - (BOOL)renderContentType:(GRMustacheContentType)requiredContentType inBuffer:(G
}
}

[localBuffer flush];

return YES;
}

Expand Down
Loading

0 comments on commit 3682eae

Please sign in to comment.