From db069ad311cd831a709d8f1598ada81b10d6e635 Mon Sep 17 00:00:00 2001 From: Albert Zhang Date: Sat, 28 Jul 2018 20:27:48 -0500 Subject: [PATCH] Add regex to filter out premature line breaks This prevents text alignment issues, most visible on iPads, as in #41 --- Source/AcknowParser.swift | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Source/AcknowParser.swift b/Source/AcknowParser.swift index e43edca..0970f85 100644 --- a/Source/AcknowParser.swift +++ b/Source/AcknowParser.swift @@ -91,7 +91,24 @@ open class AcknowParser { (preferenceSpecifier: AnyObject) -> Acknow in if let title = preferenceSpecifier["Title"] as! String?, let text = preferenceSpecifier["FooterText"] as! String? { - return Acknow(title: title, text: text, license: preferenceSpecifier["License"] as? 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) + let singleNewLineFinder = try! NSRegularExpression(pattern: "(?<=.)(\\h)*(\\R)(\\h)*(?=.)") + let textWithoutNewlines = singleNewLineFinder.stringByReplacingMatches(in: text, range: NSRange(0..