Revision: 3038
          http://skim-app.svn.sourceforge.net/skim-app/?rev=3038&view=rev
Author:   hofman
Date:     2007-10-13 04:21:24 -0700 (Sat, 13 Oct 2007)

Log Message:
-----------
Remove only empty spaces before/after a newline after/before a collection or 
condition tag in templates.

Modified Paths:
--------------
    trunk/NSString_SKExtensions.h
    trunk/NSString_SKExtensions.m
    trunk/SKTemplateParser.m

Modified: trunk/NSString_SKExtensions.h
===================================================================
--- trunk/NSString_SKExtensions.h       2007-10-12 21:52:55 UTC (rev 3037)
+++ trunk/NSString_SKExtensions.h       2007-10-13 11:21:24 UTC (rev 3038)
@@ -58,7 +58,9 @@
 - (NSRange)rangeOfLeadingEmptyLine;
 - (NSRange)rangeOfLeadingEmptyLineInRange:(NSRange)range;
 - (NSRange)rangeOfTrailingEmptyLine;
+- (NSRange)rangeOfTrailingEmptyLine:(BOOL *)onlyWhite;
 - (NSRange)rangeOfTrailingEmptyLineInRange:(NSRange)range;
+- (NSRange)rangeOfTrailingEmptyLine:(BOOL *)onlyWhite range:(NSRange)range;
 
 - (NSString *)typeName;
 

Modified: trunk/NSString_SKExtensions.m
===================================================================
--- trunk/NSString_SKExtensions.m       2007-10-12 21:52:55 UTC (rev 3037)
+++ trunk/NSString_SKExtensions.m       2007-10-13 11:21:24 UTC (rev 3038)
@@ -237,9 +237,7 @@
     NSRange firstCharRange = [self rangeOfCharacterFromSet:[NSCharacterSet 
nonWhitespaceCharacterSet] options:0 range:range];
     NSRange wsRange = NSMakeRange(NSNotFound, 0);
     unsigned int start = range.location;
-    if (firstCharRange.location == NSNotFound) {
-        wsRange = range;
-    } else {
+    if (firstCharRange.location != NSNotFound) {
         unichar firstChar = [self characterAtIndex:firstCharRange.location];
         unsigned int rangeEnd = NSMaxRange(firstCharRange);
         if([[NSCharacterSet newlineCharacterSet] characterIsMember:firstChar]) 
{
@@ -254,20 +252,30 @@
 
 // whitespace at the end of the string from the beginning or after a newline
 - (NSRange)rangeOfTrailingEmptyLine {
-    return [self rangeOfTrailingEmptyLineInRange:NSMakeRange(0, [self 
length])];
+    return [self rangeOfTrailingEmptyLine:NULL];
 }
 
+- (NSRange)rangeOfTrailingEmptyLine:(BOOL *)onlyWhite {
+    return [self rangeOfTrailingEmptyLine:onlyWhite range:NSMakeRange(0, [self 
length])];
+}
 - (NSRange)rangeOfTrailingEmptyLineInRange:(NSRange)range {
+    return [self rangeOfTrailingEmptyLine:NULL range:NSMakeRange(0, [self 
length])];
+}
+
+- (NSRange)rangeOfTrailingEmptyLine:(BOOL *)onlyWhite range:(NSRange)range {
     NSRange lastCharRange = [self rangeOfCharacterFromSet:[NSCharacterSet 
nonWhitespaceCharacterSet] options:NSBackwardsSearch range:range];
     NSRange wsRange = NSMakeRange(NSNotFound, 0);
     unsigned int end = NSMaxRange(range);
     if (lastCharRange.location == NSNotFound) {
-        wsRange = range;
+        if (onlyWhite)
+            *onlyWhite = YES;
     } else {
         unichar lastChar = [self characterAtIndex:lastCharRange.location];
         unsigned int rangeEnd = NSMaxRange(lastCharRange);
         if (rangeEnd < end && [[NSCharacterSet newlineCharacterSet] 
characterIsMember:lastChar]) 
             wsRange = NSMakeRange(rangeEnd, end - rangeEnd);
+        if (onlyWhite)
+            *onlyWhite = NO;
     }
     return wsRange;
 }

Modified: trunk/SKTemplateParser.m
===================================================================
--- trunk/SKTemplateParser.m    2007-10-12 21:52:55 UTC (rev 3037)
+++ trunk/SKTemplateParser.m    2007-10-13 11:21:24 UTC (rev 3038)
@@ -264,13 +264,18 @@
                 NSString *itemTemplate = nil, *separatorTemplate = nil;
                 NSString *endTag;
                 NSRange sepTagRange, wsRange;
+                BOOL onlyWhite;
                 
-                // collection template currentTag
-                // ignore whitespace before the currentTag. Should we also 
remove a newline?
+                // collection template tag
+                // ignore whitespace before the tag. Should we also remove a 
newline?
                 if (currentTag && [(SKTag *)currentTag type] == SKTextTagType) 
{
-                    wsRange = [[currentTag text] rangeOfTrailingEmptyLine];
-                    if (wsRange.location != NSNotFound)
+                    wsRange = [[currentTag text] 
rangeOfTrailingEmptyLine:&onlyWhite];
+                    if (wsRange.location != NSNotFound) {
                         [currentTag setText:[[currentTag text] 
substringToIndex:wsRange.location]];
+                    } else if ([result count] == 1 && onlyWhite) {
+                        [result removeAllObjects];
+                        currentTag = nil;
+                    }
                 }
                 
                 endTag = endMultiTagWithTag(tag);
@@ -279,7 +284,7 @@
                 if ([scanner scanString:endTag intoString:nil])
                     continue;
                 if ([scanner scanUpToString:endTag intoString:&itemTemplate] 
&& [scanner scanString:endTag intoString:nil]) {
-                    // ignore whitespace before the currentTag. Should we also 
remove a newline?
+                    // ignore whitespace before the tag. Should we also remove 
a newline?
                     wsRange = [itemTemplate rangeOfTrailingEmptyLine];
                     if (wsRange.location != NSNotFound)
                         itemTemplate = [itemTemplate 
substringToIndex:wsRange.location];
@@ -328,17 +333,22 @@
                     NSString *subTemplate = nil;
                     NSString *endTag, *altTag;
                     NSRange altTagRange, wsRange;
+                    BOOL onlyWhite;
                     
-                    // condition template currentTag
-                    // ignore whitespace before the currentTag. Should we also 
remove a newline?
+                    // condition template tag
+                    // ignore whitespace before the tag. Should we also remove 
a newline?
                     if (currentTag && [(SKTag *)currentTag type] == 
SKTextTagType) {
-                        wsRange = [[currentTag text] rangeOfTrailingEmptyLine];
-                        if (wsRange.location != NSNotFound)
+                        wsRange = [[currentTag text] 
rangeOfTrailingEmptyLine:&onlyWhite];
+                        if (wsRange.location != NSNotFound) {
                             [currentTag setText:[[currentTag text] 
substringToIndex:wsRange.location]];
+                        } else if ([result count] == 1 && onlyWhite) {
+                            [result removeAllObjects];
+                            currentTag = nil;
+                        }
                     }
                     
                     endTag = endConditionTagWithTag(tag);
-                    // ignore the rest of an empty line after the currentTag
+                    // ignore the rest of an empty line after the tag
                     [scanner scanEmptyLine];
                     if ([scanner scanString:endTag intoString:nil])
                         continue;
@@ -547,13 +557,18 @@
                 NSAttributedString *itemTemplate = nil, *separatorTemplate = 
nil;
                 NSString *endTag;
                 NSRange sepTagRange, wsRange;
+                BOOL onlyWhite;
                 
                 // collection template tag
                 // ignore whitespace before the tag. Should we also remove a 
newline?
                 if (currentTag && [(SKTag *)currentTag type] == SKTextTagType) 
{
-                    wsRange = [[[currentTag attributedText] string] 
rangeOfTrailingEmptyLine];
-                    if (wsRange.location != NSNotFound)
+                    wsRange = [[[currentTag attributedText] string] 
rangeOfTrailingEmptyLine:&onlyWhite];
+                    if (wsRange.location != NSNotFound) {
                         [currentTag setAttributedText:[[currentTag 
attributedText] attributedSubstringFromRange:NSMakeRange(0, wsRange.location)]];
+                    } else if ([result count] == 1 && onlyWhite) {
+                        [result removeAllObjects];
+                        currentTag = nil;
+                    }
                 }
                 
                 endTag = endMultiTagWithTag(tag);
@@ -612,13 +627,18 @@
                     NSAttributedString *subTemplate = nil;
                     NSString *endTag, *altTag;
                     NSRange altTagRange, wsRange;
+                    BOOL onlyWhite;
                     
                     // condition template tag
                     // ignore whitespace before the tag. Should we also remove 
a newline?
                     if (currentTag && [(SKTag *)currentTag type] == 
SKTextTagType) {
-                        wsRange = [[[currentTag attributedText] string] 
rangeOfTrailingEmptyLine];
-                        if (wsRange.location != NSNotFound)
+                        wsRange = [[[currentTag attributedText] string] 
rangeOfTrailingEmptyLine:&onlyWhite];
+                        if (wsRange.location != NSNotFound) {
                             [currentTag setAttributedText:[[currentTag 
attributedText] attributedSubstringFromRange:NSMakeRange(0, wsRange.location)]];
+                        } else if ([result count] == 1 && onlyWhite) {
+                            [result removeAllObjects];
+                            currentTag = nil;
+                        }
                     }
                     
                     endTag = endConditionTagWithTag(tag);


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to