Skip to content

Commit

Permalink
Add regex to filter out premature line breaks
Browse files Browse the repository at this point in the history
This prevents text alignment issues, most visible on iPads, as in vtourraine#41
  • Loading branch information
azhang66 committed Aug 3, 2018
1 parent 7c3559a commit db069ad
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Source/AcknowParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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..<text.count), withTemplate: " ")

return Acknow(title: title, text: textWithoutNewlines, license: preferenceSpecifier["License"] as? String)
}
else {
return Acknow(title: "", text: "", license: nil)
Expand Down

0 comments on commit db069ad

Please sign in to comment.