File: reformat.py

package info (click to toggle)
opencv 2.4.9.1%2Bdfsg-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 126,800 kB
  • ctags: 62,729
  • sloc: xml: 509,055; cpp: 490,794; lisp: 23,208; python: 21,174; java: 19,317; ansic: 1,038; sh: 128; makefile: 72
file content (44 lines) | stat: -rwxr-xr-x 1,003 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python

import os, sys, re

finput=open(sys.argv[1], "rt")

# read the whole file content to s
s = "".join(finput.readlines())
finput.close()

# normalize line endings
s = re.sub(r"\r\n", "\n", s)

# remove trailing whitespaces
s = re.sub(r"[ \t]+\n", "\n", s)

# compress multiple empty lines
for i in range(5):
    s = re.sub(r"\n\n\n", "\n\n", s)

# remove empty line before ".." that terminates a code block
s = re.sub(r"\n\n\.\.\n", "\n..\n", s)

# move :: starting a code block to the end of previous line
s = re.sub(r"\n\n::\n", " ::\n", s)

# remove extra line breaks before/after _ or ,
s = re.sub(r"\n[ \t]*([_,])\n", r"\1", s)

# remove extra line breaks after `
s = re.sub(r"`\n", "` ", s)

# remove extra line breaks before `
s = re.sub(r"\n[ \t]*`", " `", s)

# remove links to wiki
s = re.sub(r"\n[ \t]*`id=\d[^`]+`__\n", "", s)

# remove trailing whitespaces one more time
s = re.sub(r"[ \t]+\n", "\n", s)

foutput=open(sys.argv[2], "wt")
foutput.write(s)
foutput.close()