-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformat.py
executable file
·90 lines (75 loc) · 2.09 KB
/
format.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
import os
import re
import subprocess
def get_linesep(lines):
"""Returns string containing autodetected line separator for file.
Keyword arguments:
lines -- file contents string
"""
# Find potential line separator
pos = lines.find("\n")
# If a newline character was found and the character preceding it is a
# carriage return, assume CRLF line endings. LF line endings are assumed
# for empty files.
if pos > 0 and lines[pos - 1] == "\r":
return "\r\n"
else:
return "\n"
def strip_trailing_whitespace(filename):
"""Removes trailing whitespace from the file.
Keyword arguments:
filename -- name of file to strip
"""
with open(filename) as input:
lines = input.read()
linesep = get_linesep(lines)
output = ""
for line in lines.splitlines():
line = line[: len(line)].rstrip()
output += line + linesep
if lines != output:
with open(filename, "wb") as file:
file.write(output.encode())
def main():
# Format HTML
files = [
os.path.join(dp, f)
for dp, dn, fn in os.walk(".")
for f in fn
if f.endswith(".html")
]
files = [
f
for f in files
if not f.startswith("./MathJax/")
and not f.startswith("./reveal.js/")
and not f.startswith("./archives/angelscript/docs/")
and not re.search(r"google.*?\.html$", f)
]
for f in files:
subprocess.check_output(
[
"tidy",
"-config",
"html-tidy.conf",
"-modify",
"-quiet",
"--tidy-mark",
"false",
f,
]
)
strip_trailing_whitespace(f)
# Format Python
files = [
os.path.join(dp, f)
for dp, dn, fn in os.walk(".")
for f in fn
if f.endswith(".py")
]
for f in files:
subprocess.check_output(["python3", "-m", "black", "-q", f])
strip_trailing_whitespace(f)
if __name__ == "__main__":
main()