Debian Codegraph @

devtools/fix_newlines

raw
1 #!/usr/bin/python
2 #
3 # Try to reconstruct a saved chunked response after an editor has mangled all
4 # the newlines. Not 100% foolproof, but decent enough that the result requires
5 # very little manual mangling.
6
7 import sys
8 import re
9
10 data = sys.stdin.read()
11 header, body = data.split('\n\n', 1)
12 header = header.replace('\n', '\r\n')
13
14 # All <?xml ... ?> stanzas end with CRLF. Only match on ?> as the stanza may be split over 2 chunks
15 body = body.replace('?>', '?>\r')
16 # Chunked response: each chunk ends with CRLF, as does the line with the chunk size
17 body = re.sub(r'\n([0-9a-f]{1,3})\n', r'\r\n\1\r\n', body)
18 # First chunk. Don't try to match the last newline of a previous chunk.
19 body = re.sub('^([0-9a-f]{1,3})\n', r'\1\r\n', body, 1)
20
21 print '\r\n\r\n'.join([header, body])