diff --git a/pre_commit_hooks/mixed_line_ending.py b/pre_commit_hooks/mixed_line_ending.py index 7b6a4bf..c4b2a6e 100644 --- a/pre_commit_hooks/mixed_line_ending.py +++ b/pre_commit_hooks/mixed_line_ending.py @@ -12,14 +12,15 @@ class CLIOption(Enum): class LineEnding(CLIOption): - CR = '\r', '\\r', 'cr' - CRLF = '\r\n', '\\r\\n', 'crlf' - LF = '\n', '\\n', 'lf' + CR = '\r', '\\r', 'cr', re.compile(r'\r', re.DOTALL) + CRLF = '\r\n', '\\r\\n', 'crlf', re.compile(r'\r\n', re.DOTALL) + LF = '\n', '\\n', 'lf', re.compile(r'(? 0 lf_found = lf_nb > 0 @@ -118,5 +130,20 @@ def _detect_line_ending(filename): return MixedLineDetection.MIXED_MOSTLY_LF +def _convert_line_ending(filename, line_ending): + # read the file + fin = open(filename, 'r') + bufin = fin.read() + fin.close() + + # convert line ending + bufout = ANY_LINE_ENDING_PATTERN.sub(line_ending, bufin) + + # write the result in the file + fout = open(filename, 'w') + fout.write(bufout) + fout.close() + + if __name__ == '__main__': sys.exit(mixed_line_ending())