forked from esphome/esphome-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
travis.py
125 lines (106 loc) · 3.9 KB
/
travis.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from pathlib import Path
import re
import sys
errors = []
def find_all(a_str, sub):
for i, line in enumerate(a_str.splitlines()):
column = 0
while True:
column = line.find(sub, column)
if column == -1:
break
yield i, column
column += len(sub)
section_regex = re.compile(r"^(=+|-+|\*+|~+)$")
directive_regex = re.compile(r"^(\s*)\.\. (.*)::.*$")
directive_arg_regex = re.compile(r"^(\s+):.*:\s*.*$")
esphome_io_regex = re.compile(r"https://esphome.io/")
for f in sorted(Path(".").glob("**/*.rst")):
try:
content = f.read_text("utf-8")
except UnicodeDecodeError:
errors.append(
"File {} is not readable as UTF-8. Please set your editor to UTF-8 mode."
"".format(f)
)
continue
if not content.endswith("\n"):
errors.append(
"Newline at end of file missing. Please insert an empty line at end "
"of file {}".format(f)
)
# Check tab character
for line, col in find_all(content, "\t"):
errors.append(
"File {} contains tab character on line {}:{}. "
"Please convert tabs to spaces.".format(f, line + 1, col)
)
# Check windows newline
for line, col in find_all(content, "\r"):
errors.append(
"File {} contains windows newline on line {}:{}. "
"Please set your editor to unix newline mode.".format(f, line + 1, col)
)
lines = content.splitlines(keepends=False)
# for i, line in enumerate(lines):
# if i == 0:
# continue
#
# if not section_regex.match(line):
# continue
# line_above = lines[i - 1]
# if len(line_above) != len(line):
# errors.append("The title length must match the bar length below it. See {}:{}"
# "".format(f, i+1))
# if i + 1 < len(lines) and lines[i + 1]:
# errors.append("Empty line after heading is missing. Please insert an "
# "empty line. See {}:{}".format(f, i+1))
for i, line in enumerate(lines):
m = directive_regex.match(line)
if m is None:
continue
base_indentation = len(m.group(1))
directive_name = m.group(2)
if directive_name.startswith("|") or directive_name == "seo":
continue
# Match directive args
for j in range(i + 1, len(lines)):
if not directive_arg_regex.match(lines[j]):
break
else:
# Reached end of file
continue
# Empty line must follow
if lines[j]:
errors.append(
"Directive '{}' is not followed by an empty line. Please insert an "
"empty line after {}:{}".format(directive_name, f, j)
)
continue
k = j + 1
for j in range(k, len(lines)):
if not lines[j]:
# Ignore Empty lines
continue
num_spaces = len(lines[j]) - len(lines[j].lstrip())
if num_spaces <= base_indentation:
# Finished with this directive
break
num_indent = num_spaces - base_indentation
if j == k and num_indent != 4:
errors.append(
"Directive '{}' must be indented with 4 spaces, not {}. See "
"{}:{}".format(directive_name, num_indent, f, j + 1)
)
break
for i, line in enumerate(lines):
if esphome_io_regex.search(line):
if "privacy.rst" in str(f) or "web_server.rst" in str(f):
continue
errors.append(
"All links to esphome.io should be relative, please remove esphome.io "
"from URL. See {}:{}".format(f, i + 1)
)
for error in errors:
print(error)
sys.exit(len(errors))