Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed spaces after new lines by moving <br> handling up into html par…

…ser and out of markup transformer.
  • Loading branch information...
commit fe30a36f990fe7490e011b5b5acffd62571acc23 1 parent aac0a1e
@schwa schwa authored
View
8 Source/CMarkupValueTransformer.m
@@ -94,6 +94,7 @@ - (id)transformedValue:(id)value error:(NSError **)outError
CSimpleHTMLParser *theParser = [[CSimpleHTMLParser alloc] init];
theParser.openTagHandler = ^(NSString *inTag, NSDictionary *inAttributes, NSArray *tagStack) {
+ printf("<>");
if ([inTag isEqualToString:@"a"] == YES)
{
@@ -117,13 +118,10 @@ - (id)transformedValue:(id)value error:(NSError **)outError
[theAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"." attributes:theImageAttributes]];
}
}
- else if ([inTag isEqualToString:@"br"] == YES)
- {
- [theAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n" attributes:theTextAttributes]];
- }
};
theParser.closeTagHandler = ^(NSString *inTag, NSArray *tagStack) {
+ printf("</>");
if ([inTag isEqualToString:@"a"] == YES)
{
@@ -133,6 +131,7 @@ - (id)transformedValue:(id)value error:(NSError **)outError
};
theParser.textHandler = ^(NSString *inString, NSArray *tagStack) {
+ printf("\"%s\"", [inString UTF8String]);
theTextAttributes = [[self attributesForTagStack:tagStack] mutableCopy];
if (theCurrentLink != NULL)
@@ -143,6 +142,7 @@ - (id)transformedValue:(id)value error:(NSError **)outError
[theAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:inString attributes:theTextAttributes]];
};
+ NSLog(@"%@", theMarkup);
if ([theParser parseString:theMarkup error:outError] == NO)
{
View
15 Source/CSimpleHTMLParser.m
@@ -121,7 +121,7 @@ - (BOOL)parseString:(NSString *)inString error:(NSError **)outError
if (theString.length > 0)
{
- theLastCharacterWasWhitespace = [[NSCharacterSet whitespaceAndNewlineCharacterSet] characterIsMember:[theString characterAtIndex:theString.length - 1]];
+ theLastCharacterWasWhitespace = [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[theString characterAtIndex:theString.length - 1]];
self.textHandler(theString, theTagStack);
}
theString = [NSMutableString string];
@@ -147,9 +147,18 @@ - (BOOL)parseString:(NSString *)inString error:(NSError **)outError
theString = [NSMutableString string];
}
- self.openTagHandler(theTag, theAttributes, theTagStack);
+ if ([theTag isEqualToString:@"br"])
+ {
+ theLastCharacterWasWhitespace = YES;
+ self.textHandler(@"\n", theTagStack);
+ theString = [NSMutableString string];
+ }
+ else
+ {
+ self.openTagHandler(theTag, theAttributes, theTagStack);
- [theTagStack addObject:theTag];
+ [theTagStack addObject:theTag];
+ }
}
else if ([theScanner scanString:@"&" intoString:NULL] == YES)
{
View
6 Test/CoreText.xcodeproj/project.pbxproj
@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
+ 4528EA61145386F500814B3E /* NSAttributedString_DebugExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = 4528EA60145386F500814B3E /* NSAttributedString_DebugExtensions.m */; };
453E8B7E1453255000C8A559 /* MissingImage.png in Resources */ = {isa = PBXBuildFile; fileRef = 453E8B7D1453255000C8A559 /* MissingImage.png */; };
453E8B85145334F500C8A559 /* CCoreTextRenderer.m in Sources */ = {isa = PBXBuildFile; fileRef = 453E8B84145334F500C8A559 /* CCoreTextRenderer.m */; };
45DEC480142BBEDB009FBCC6 /* CCoreTextLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = 45DEC477142BBEDB009FBCC6 /* CCoreTextLabel.m */; };
@@ -43,6 +44,8 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
+ 4528EA5F145386F500814B3E /* NSAttributedString_DebugExtensions.h */ = {isa = PBXFileReference; fileEncoding = 4; path = NSAttributedString_DebugExtensions.h; sourceTree = "<group>"; };
+ 4528EA60145386F500814B3E /* NSAttributedString_DebugExtensions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NSAttributedString_DebugExtensions.m; sourceTree = "<group>"; };
453E8B7D1453255000C8A559 /* MissingImage.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = MissingImage.png; sourceTree = "<group>"; };
453E8B83145334F500C8A559 /* CCoreTextRenderer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCoreTextRenderer.h; sourceTree = "<group>"; };
453E8B84145334F500C8A559 /* CCoreTextRenderer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCoreTextRenderer.m; sourceTree = "<group>"; };
@@ -165,6 +168,8 @@
45FF284413CD6920006D9574 /* MainStoryboard.storyboard */,
45FF284713CD6920006D9574 /* CoreTextViewController.h */,
45FF284813CD6920006D9574 /* CoreTextViewController.m */,
+ 4528EA5F145386F500814B3E /* NSAttributedString_DebugExtensions.h */,
+ 4528EA60145386F500814B3E /* NSAttributedString_DebugExtensions.m */,
45FF283913CD6920006D9574 /* Supporting Files */,
);
path = CoreText;
@@ -318,6 +323,7 @@
45DEC483142BBEDB009FBCC6 /* NSScanner_HTMLExtensions.m in Sources */,
45DEC484142BBEDB009FBCC6 /* UIFont_CoreTextExtensions.m in Sources */,
453E8B85145334F500C8A559 /* CCoreTextRenderer.m in Sources */,
+ 4528EA61145386F500814B3E /* NSAttributedString_DebugExtensions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
3  Test/CoreText/CoreTextViewController.m
@@ -35,6 +35,7 @@
#import "CMarkupValueTransformer.h"
#import "CCoreTextLabel.h"
+#import "NSAttributedString_DebugExtensions.h"
@interface CoreTextViewController () <UITextViewDelegate>
@end
@@ -77,7 +78,7 @@ - (void)textViewDidChange:(UITextView *)textView;
theText = [[NSAttributedString alloc] initWithString:[theError description]];
}
- self.attributedView.text = [theText description];
+ self.attributedView.text = [theText betterDescription];
self.previewView.text = theText;
}
View
15 Test/CoreText/NSAttributedString_DebugExtensions.h
@@ -0,0 +1,15 @@
+//
+// NSAttributedString_DebugExtensions.h
+// CoreText
+//
+// Created by Jonathan Wight on 10/22/11.
+// Copyright (c) 2011 toxicsoftware.com. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface NSAttributedString (NSAttributedString_DebugExtensions)
+
+- (NSString *)betterDescription;
+
+@end
View
33 Test/CoreText/NSAttributedString_DebugExtensions.m
@@ -0,0 +1,33 @@
+//
+// NSAttributedString_DebugExtensions.m
+// CoreText
+//
+// Created by Jonathan Wight on 10/22/11.
+// Copyright (c) 2011 toxicsoftware.com. All rights reserved.
+//
+
+#import "NSAttributedString_DebugExtensions.h"
+
+@implementation NSAttributedString (NSAttributedString_DebugExtensions)
+
+- (NSString *)betterDescription
+ {
+
+ NSMutableArray *theComponents = [NSMutableArray array];
+
+
+ [self enumerateAttributesInRange:(NSRange){ .length = self.length } options:0 usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) {
+
+ NSString *theString = [NSString stringWithFormat:@"\"%@\"", [self.string substringWithRange:range]];
+
+ [theComponents addObject:theString];
+
+ }];
+
+
+ return([NSString stringWithFormat:@"%@ %@", [super description], [theComponents componentsJoinedByString:@", "]]);
+
+ }
+
+
+@end
View
3  Test/CoreText/main.m
@@ -33,9 +33,12 @@
#import "CoreTextAppDelegate.h"
+#import "CSimpleHTMLParser.h"
+
int main(int argc, char *argv[])
{
@autoreleasepool {
+
return UIApplicationMain(argc, argv, nil, NSStringFromClass([CoreTextAppDelegate class]));
}
}
View
27 Test/ParserTest/main.m
@@ -41,24 +41,25 @@ int main (int argc, const char * argv[])
// NSLog(@"%@", [@"hello http://world how are you?" stringByMarkingUpString]);
CSimpleHTMLParser *theParser = [[CSimpleHTMLParser alloc] init];
- theParser.openTagHandler = ^(NSString *text, NSDictionary *attribites, NSArray *tagStack) { if ([text isEqualToString:@"br"]) printf("\n"); };
-// theParser.closeTagHandler = ^(NSString *text, NSArray *tagStack) { printf("", [text UTF8String]); };
+// theParser.openTagHandler = ^(NSString *text, NSDictionary *attribites, NSArray *tagStack) { printf("<>"); if ([text isEqualToString:@"br"]) printf("\\n\n"); };
+// theParser.closeTagHandler = ^(NSString *text, NSArray *tagStack) { printf("</>"); };
theParser.textHandler = ^(NSString *text, NSArray *tagStack) { printf("[%s]", [text UTF8String]); };
NSError *theError = NULL;
-// NSString *theMarkup = @"\n\n\n\n\n";
+ NSString *theMarkup = @"<b>hello <i>world</i></b><br>\n\
+A lot of entites are supported. &amp; &lt; &gt;<br>\n";
- NSString *theMarkup = @"<b>hello <i>world</i></b><br>\
-A lot of entites are supported. &amp; &lt; &gt;<br>\
-White space mostly follows normal HTML rules. (But a bit buggy?)<br>\
-<purple>Custom tags can be used for simple styling</purple><br>\
-<purple><b>Styles will</b><i>accumulate</i></purple><br>\
-<img href=\"placeholder.png\">image tags might work too<br>\
-Links will work too:<br>\
-<a href=\"http://apple.com\">Apple</a><br>\
-<a href=\"http://google.com\">Google</a><br>\
-";
+// NSString *theMarkup = @"<b>hello <i>world</i></b><br>\n\
+//A lot of entites are supported. &amp; &lt; &gt;<br>\n\
+//White space mostly follows normal HTML rules. (But a bit buggy?)<br>\n\
+//<purple>Custom tags can be used for simple styling</purple><br>\n\
+//<purple><b>Styles will</b><i>accumulate</i></purple><br>\n\
+//<img href=\"placeholder.png\">image tags might work too<br>\n\
+//Links will work too:<br>\n\
+//<a href=\"http://apple.com\">Apple</a><br>\n\
+//<a href=\"http://google.com\">Google</a><br>\n\
+//";
if ([theParser parseString:theMarkup error:&theError] == NO)
{
Please sign in to comment.
Something went wrong with that request. Please try again.