forked from openplanet-nl/nadeoapi-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·101 lines (79 loc) · 2.6 KB
/
test.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
#!/usr/bin/python3
import os
import re
import yaml
def parse_frontmatter(fh):
text = ''
while True:
line = fh.readline()
if not line or line == '---\n':
break
text += line
return yaml.safe_load(text)
def test_valid_link(link):
if not link.startswith('/'):
raise Exception('Internal link must start with a slash: "' + link + '"')
path, anchor = re.findall('^/(.+?)(#.+)?$', link)[0]
if not os.path.exists('docs/' + path) and not os.path.exists('docs/' + path + '.md'):
raise Exception('Internal link goes to a non-existing page: "' + link + '"')
def test_valid_link_targets(content):
for link in re.findall('\\[.+\\]\\((.+?)\\)', content):
if link.startswith('http://'):
raise Exception('Insecure link on page: "' + link + '"')
elif link.startswith('https://'):
pass # External links are fine
elif link.startswith('#'):
pass # Anchor links are fine
else:
test_valid_link(link)
def test_valid_frontmatter(path, frontmatter):
if frontmatter == False:
return
relative_path = os.path.dirname(path)
if 'pages' in frontmatter:
for page in frontmatter['pages']:
page_path = relative_path + '/' + page + '.md'
if not os.path.exists(page_path):
raise Exception('Page in frontmatter does not exist: "' + page + '" (looking for "' + page_path + '")')
if 'dirs' in frontmatter:
for dir in frontmatter['dirs']:
dir_path = relative_path + '/' + dir
if not os.path.exists(dir_path):
raise Exception('Directory in frontmatter does not exist: "' + dir + '" (looking for "' + dir_path + '")')
if 'roots' in frontmatter:
for root in frontmatter['roots']:
root_path = relative_path + '/' + root
if not os.path.exists(root_path):
raise Exception('Root in frontmatter does not exist: "' + root + '" (looking for "' + root_path + '")')
def test_page(path, frontmatter, content):
print('Page: ' + path)
test_valid_frontmatter(path, frontmatter)
test_valid_link_targets(content)
if 'API\'s' in content:
raise Exception('Do not write "API\'s" with an apostrophe. Write "APIs" instead!')
def test_file(path):
line_number = 0
frontmatter = False
content = ''
fh = open(path, 'r')
while True:
line = fh.readline()
if not line:
break
if line_number == 0 and line == '---\n':
frontmatter = parse_frontmatter(fh)
line_number += 1
continue
content += line
line_number += 1
test_page(path, frontmatter, content)
fh.close()
def test_dir(path = ''):
with os.scandir('docs/' + path) as it:
for entry in it:
if entry.is_file():
test_file('docs/' + path + entry.name)
else:
test_dir(path + entry.name + '/')
test_dir()
print('\nAll tests passed!')