Skip to content

Commit

Permalink
Refactor filter out line breaks to its own method
Browse files Browse the repository at this point in the history
  • Loading branch information
vtourraine committed Aug 20, 2018
1 parent 8f3c3b9 commit 2bba9ef
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions Source/AcknowParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,7 @@ open class AcknowParser {
(preferenceSpecifier: AnyObject) -> Acknow in
if let title = preferenceSpecifier["Title"] as! String?,
let text = preferenceSpecifier["FooterText"] 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: " ")

let textWithoutNewlines = AcknowParser.filterOutPrematureLineBreaks(text: text)
return Acknow(title: title, text: textWithoutNewlines, license: preferenceSpecifier["License"] as? String)
}
else {
Expand All @@ -120,4 +104,29 @@ open class AcknowParser {

return []
}

/**
Filters out all premature line breaks (i.e. removes manual wrapping).

- parameter text: The text to process.

- returns: The text without the premature line breaks.
*/
class func filterOutPrematureLineBreaks(text: String) -> 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)*(?=.)")
return singleNewLineFinder.stringByReplacingMatches(in: text, range: NSRange(0..<text.count), withTemplate: " ")
}
}

0 comments on commit 2bba9ef

Please sign in to comment.