Skip to content

Commit

Permalink
Filter out manual line wrapping from licenses text
Browse files Browse the repository at this point in the history
  • Loading branch information
vtourraine committed Sep 12, 2018
1 parent 4658d3d commit f072580
Show file tree
Hide file tree
Showing 10 changed files with 654 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 1.5 (work in progress)

- Filter out manual line wrapping from licenses text


## 1.4.1 (25 June 2018)

- Fix text view inset on `VTAcknowledgementViewController` (support layout margins, safe area insets)
Expand Down
21 changes: 20 additions & 1 deletion Classes/VTAcknowledgementsParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,28 @@ - (nonnull instancetype)initWithAcknowledgementsPlistPath:(nonnull NSString *)ac

+ (nonnull VTAcknowledgement *)acknowledgementFromPreferenceSpecifier:(nonnull NSDictionary *)preferenceSpecifier {
NSString *title = preferenceSpecifier[@"Title"];
NSString *text = preferenceSpecifier[@"FooterText"];
NSString *text = [VTAcknowledgementsParser stringByFilteringOutPrematureLineBreaksFromString:preferenceSpecifier[@"FooterText"]];
NSString *license = preferenceSpecifier[@"License"];
return [[VTAcknowledgement alloc] initWithTitle:title text:text license:license];
}

+ (nonnull NSString *)stringByFilteringOutPrematureLineBreaksFromString:(nonnull NSString *)string {
// This regex replaces single newlines with spaces, while preserving multiple newlines used for formatting.
// This prevents issues such as https://github.com/vtourraine/AcknowList/issues/41
//
// The issue arises when licenses contain premature line breaks in the middle of a sentance, often used
// to limit license texts to 80 characters. When applied on an iPad, the resulting licenses are misaligned.
//
// The expression (?<=.)(\h)*(\R)(\h)*(?=.) can be broken down as:
//
// (?<=.) Positive lookbehind matching any non-newline character (matches but does not capture)
// (\h)* Matches and captures zero or more horizontal spaces (trailing newlines)
// (\R) Matches and captures any single Unicode-compliant newline character
// (\h)* Matches and captures zero or more horizontal spaces (leading newlines)
// (?=.) Positive lookahead matching any non-newline character (matches but does not capture)
NSRegularExpression *singleNewLineFinder = [[NSRegularExpression alloc] initWithPattern:@"(?<=.)(\\h)*(\\R)(\\h)*(?=.)" options:kNilOptions error:nil];

return [singleNewLineFinder stringByReplacingMatchesInString:string options:kNilOptions range:NSMakeRange(0, string.length) withTemplate:@" "];
}

@end
17 changes: 17 additions & 0 deletions Tests/Tests/VTAcknowledgementsParserTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,21 @@ - (void)testParsingFailure {
XCTAssertNil(parser.acknowledgements);
}

- (void)testFilterLineBreaks {
NSBundle *bundle = [NSBundle bundleForClass:self.class];
NSString *path = [bundle pathForResource:@"Pods-acknowledgements-LineBreakFilterTesting" ofType:@"plist"];
VTAcknowledgementsParser *parser = [[VTAcknowledgementsParser alloc] initWithAcknowledgementsPlistPath:path];

XCTAssertEqual(parser.acknowledgements.count, 5);

for (VTAcknowledgement *acknowledgement in parser.acknowledgements) {
NSString *groundTruthPath = [bundle pathForResource:[NSString stringWithFormat:@"LineBreakFilterTesting-GroundTruth-%@", acknowledgement.title] ofType:@"txt"];
XCTAssertNotNil(groundTruthPath);
NSString *groundTruth = [NSString stringWithContentsOfFile:groundTruthPath encoding:NSUTF8StringEncoding error:nil];
XCTAssertNotNil(groundTruth);

XCTAssertEqualObjects(acknowledgement.text, groundTruth);
}
}

@end
32 changes: 32 additions & 0 deletions Tests/VTAck Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
D779DC4E1E5D88F6006D91C9 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D779DC4C1E5D88F6006D91C9 /* Main.storyboard */; };
D779DC501E5D88F6006D91C9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D779DC4F1E5D88F6006D91C9 /* Assets.xcassets */; };
D779DC551E5D89C0006D91C9 /* Pods-VTAck App-acknowledgements.plist in Resources */ = {isa = PBXBuildFile; fileRef = 1FDD9DBB1A9A7CEE000BAFF0 /* Pods-VTAck App-acknowledgements.plist */; };
D7A2E4DB214909940010F801 /* Pods-acknowledgements-LineBreakFilterTesting.plist in Resources */ = {isa = PBXBuildFile; fileRef = D7A2E4DA214909940010F801 /* Pods-acknowledgements-LineBreakFilterTesting.plist */; };
D7A2E4E221490A540010F801 /* LineBreakFilterTesting-GroundTruth-TYPFontAwesome.txt in Resources */ = {isa = PBXBuildFile; fileRef = D7A2E4DD21490A540010F801 /* LineBreakFilterTesting-GroundTruth-TYPFontAwesome.txt */; };
D7A2E4E321490A540010F801 /* LineBreakFilterTesting-GroundTruth-Charts.txt in Resources */ = {isa = PBXBuildFile; fileRef = D7A2E4DE21490A540010F801 /* LineBreakFilterTesting-GroundTruth-Charts.txt */; };
D7A2E4E421490A540010F801 /* LineBreakFilterTesting-GroundTruth-Alamofire.txt in Resources */ = {isa = PBXBuildFile; fileRef = D7A2E4DF21490A540010F801 /* LineBreakFilterTesting-GroundTruth-Alamofire.txt */; };
D7A2E4E521490A540010F801 /* LineBreakFilterTesting-GroundTruth-pop.txt in Resources */ = {isa = PBXBuildFile; fileRef = D7A2E4E021490A540010F801 /* LineBreakFilterTesting-GroundTruth-pop.txt */; };
D7A2E4E621490A540010F801 /* LineBreakFilterTesting-GroundTruth-TPKeyboardAvoiding.txt in Resources */ = {isa = PBXBuildFile; fileRef = D7A2E4E121490A540010F801 /* LineBreakFilterTesting-GroundTruth-TPKeyboardAvoiding.txt */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -75,6 +81,12 @@
D779DC4D1E5D88F6006D91C9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
D779DC4F1E5D88F6006D91C9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
D779DC511E5D88F6006D91C9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D7A2E4DA214909940010F801 /* Pods-acknowledgements-LineBreakFilterTesting.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "Pods-acknowledgements-LineBreakFilterTesting.plist"; path = "VTAck Tests/Pods-acknowledgements-LineBreakFilterTesting.plist"; sourceTree = SOURCE_ROOT; };
D7A2E4DD21490A540010F801 /* LineBreakFilterTesting-GroundTruth-TYPFontAwesome.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "LineBreakFilterTesting-GroundTruth-TYPFontAwesome.txt"; sourceTree = "<group>"; };
D7A2E4DE21490A540010F801 /* LineBreakFilterTesting-GroundTruth-Charts.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "LineBreakFilterTesting-GroundTruth-Charts.txt"; sourceTree = "<group>"; };
D7A2E4DF21490A540010F801 /* LineBreakFilterTesting-GroundTruth-Alamofire.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "LineBreakFilterTesting-GroundTruth-Alamofire.txt"; sourceTree = "<group>"; };
D7A2E4E021490A540010F801 /* LineBreakFilterTesting-GroundTruth-pop.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "LineBreakFilterTesting-GroundTruth-pop.txt"; sourceTree = "<group>"; };
D7A2E4E121490A540010F801 /* LineBreakFilterTesting-GroundTruth-TPKeyboardAvoiding.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "LineBreakFilterTesting-GroundTruth-TPKeyboardAvoiding.txt"; sourceTree = "<group>"; };
DAC74A064F3D61D20EDED115 /* Pods-VTAck TV App.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-VTAck TV App.release.xcconfig"; path = "Pods/Target Support Files/Pods-VTAck TV App/Pods-VTAck TV App.release.xcconfig"; sourceTree = "<group>"; };
EA3DDFED8AE99F39E24A335F /* Pods-VTAck TV App.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-VTAck TV App.debug.xcconfig"; path = "Pods/Target Support Files/Pods-VTAck TV App/Pods-VTAck TV App.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -176,6 +188,7 @@
148FE77F1C10483600228898 /* VTAcknowledgementsParserTests.m */,
1FDD9DB91A9A7B9A000BAFF0 /* VTAcknowledgementsViewControllerTests.m */,
1F6CD01A1A9BB139002D28DC /* VTAcknowledgementViewControllerTests.m */,
D7A2E4DC214909EE0010F801 /* Resources */,
1FDD9D971A9A757B000BAFF0 /* Supporting Files */,
);
path = "VTAck Tests";
Expand Down Expand Up @@ -222,6 +235,19 @@
name = "Supporting Files";
sourceTree = "<group>";
};
D7A2E4DC214909EE0010F801 /* Resources */ = {
isa = PBXGroup;
children = (
D7A2E4DA214909940010F801 /* Pods-acknowledgements-LineBreakFilterTesting.plist */,
D7A2E4DF21490A540010F801 /* LineBreakFilterTesting-GroundTruth-Alamofire.txt */,
D7A2E4DE21490A540010F801 /* LineBreakFilterTesting-GroundTruth-Charts.txt */,
D7A2E4E021490A540010F801 /* LineBreakFilterTesting-GroundTruth-pop.txt */,
D7A2E4E121490A540010F801 /* LineBreakFilterTesting-GroundTruth-TPKeyboardAvoiding.txt */,
D7A2E4DD21490A540010F801 /* LineBreakFilterTesting-GroundTruth-TYPFontAwesome.txt */,
);
name = Resources;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -345,6 +371,12 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D7A2E4E621490A540010F801 /* LineBreakFilterTesting-GroundTruth-TPKeyboardAvoiding.txt in Resources */,
D7A2E4DB214909940010F801 /* Pods-acknowledgements-LineBreakFilterTesting.plist in Resources */,
D7A2E4E321490A540010F801 /* LineBreakFilterTesting-GroundTruth-Charts.txt in Resources */,
D7A2E4E221490A540010F801 /* LineBreakFilterTesting-GroundTruth-TYPFontAwesome.txt in Resources */,
D7A2E4E421490A540010F801 /* LineBreakFilterTesting-GroundTruth-Alamofire.txt in Resources */,
D7A2E4E521490A540010F801 /* LineBreakFilterTesting-GroundTruth-pop.txt in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit f072580

Please sign in to comment.