Normalize line endings and whitespace
[profile/ivi/opencv.git] / doc / reformat.py
1 import os, sys, re
2
3 finput=open(sys.argv[1], "rt")
4
5 # read the whole file content to s
6 s = "".join(finput.readlines())
7 finput.close()
8
9 # normalize line endings
10 s = re.sub(r"\r\n", "\n", s)
11
12 # remove trailing whitespaces
13 s = re.sub(r"[ \t]+\n", "\n", s)
14
15 # compress multiple empty lines
16 for i in range(5):
17     s = re.sub(r"\n\n\n", "\n\n", s)
18
19 # remove empty line before ".." that terminates a code block
20 s = re.sub(r"\n\n\.\.\n", "\n..\n", s)
21
22 # move :: starting a code block to the end of previous line
23 s = re.sub(r"\n\n::\n", " ::\n", s)
24
25 # remove extra line breaks before/after _ or ,
26 s = re.sub(r"\n[ \t]*([_,])\n", r"\1", s)
27
28 # remove extra line breaks after `
29 s = re.sub(r"`\n", "` ", s)
30
31 # remove extra line breaks before `
32 s = re.sub(r"\n[ \t]*`", " `", s)
33
34 # remove links to wiki
35 s = re.sub(r"\n[ \t]*`id=\d[^`]+`__\n", "", s)
36
37 # remove trailing whitespaces one more time
38 s = re.sub(r"[ \t]+\n", "\n", s)
39
40 foutput=open(sys.argv[2], "wt")
41 foutput.write(s)
42 foutput.close()