3 # The MIT License (MIT)
5 # Copyright (c) 2014 Philippe Proulx <eepp.ca>
6 # Copyright (c) 2014 The LTTng Project <lttng.org>
8 # Permission is hereby granted, free of charge, to any person obtaining a copy
9 # of this software and associated documentation files (the "Software"), to deal
10 # in the Software without restriction, including without limitation the rights
11 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 # copies of the Software, and to permit persons to whom the Software is
13 # furnished to do so, subject to the following conditions:
15 # The above copyright notice and this permission notice shall be included in
16 # all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 from termcolor
import colored
32 TOC_PATH
= 'toc/docs.yml'
33 CONTENTS_ROOT_PATH
= 'contents'
36 def _perror(filename
, msg
):
37 s
= '{} {} {}'.format(filename
, colored('Error:', 'red'),
38 colored(msg
, 'red', attrs
=['bold']))
42 def _pwarn(filename
, msg
):
43 s
= '{} {} {}'.format(filename
, colored('Warning:', 'yellow'),
44 colored(msg
, 'yellow', attrs
=['bold']))
51 for dirpath
, dirnames
, filenames
in os
.walk(root
):
53 files
.append(os
.path
.join(dirpath
, f
))
58 def _get_toc_ids(path
):
59 p
= re
.compile(r
'id\s*:\s*(.+)$', flags
=re
.M
)
62 orig_ids
= p
.findall(f
.read())
66 if len(ids
) != len(orig_ids
):
67 _perror(path
, 'Duplicate IDs')
73 def _check_file_links(toc_ids
, path
, c
):
74 ilinkp
= re
.compile(r
'\[[^\]]+\]\(([^)]+)\)', flags
=re
.M
)
75 elinkp
= re
.compile(r
'<a(?:\s+[^>]+|\s*)>')
79 ilinks
= ilinkp
.findall(c
)
80 elinks
= elinkp
.findall(c
)
83 if not link
.startswith('#doc-'):
84 s
= 'Internal link does not start with "#doc-": "{}"'.format(link
)
91 if sid
not in toc_ids
:
92 _perror(path
, 'Dead internal link: "{}"'.format(link
))
95 hrefp
= re
.compile(r
'href="([^"]+)"')
96 classesp
= re
.compile(r
'class="([^"]+)"')
99 href
= hrefp
.search(link
)
100 classes
= classesp
.search(link
)
103 _pwarn(path
, 'External link has no "ext" class: "{}"'.format(link
))
105 classes
= classes
.group(1).split(' ')
107 if 'int' not in classes
and 'ext' not in classes
:
108 _pwarn(path
, 'External link has no "ext" class: "{}"'.format(link
))
111 if href
.group(1).startswith('#'):
112 _pwarn(path
, 'External link starts with #: "{}"'.format(href
))
114 _perror(path
, 'External link with no "href": "{}"'.format(link
))
120 def _check_contents(toc_ids
, contents_files
):
123 for path
in contents_files
:
124 with
open(path
) as f
:
127 ret
&= _check_file_links(toc_ids
, path
, c
)
132 def _check_non_md(files
):
136 if not f
.endswith('.md'):
137 _perror(f
, 'Wrong, non-Markdown file')
144 toc_ids
= _get_toc_ids(TOC_PATH
)
149 contents_files
= _get_files(CONTENTS_ROOT_PATH
)
151 if not _check_non_md(contents_files
):
154 if not _check_contents(toc_ids
, contents_files
):
160 if __name__
== '__main__':
161 sys
.exit(0 if checkdocs() else 1)
This page took 0.03492 seconds and 4 git commands to generate.