1 # The MIT License (MIT)
3 # Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com>
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 from termcolor
import colored
24 import lxml
.etree
as etree
32 def _perror(msg
, exit
=True):
33 print('{} {}'.format(colored('Error:', 'red'), colored(msg
, 'red', attrs
=['bold'])),
41 print('{} {}'.format(colored('::', 'blue'), colored(msg
, 'blue', attrs
=['bold'])))
44 def _get_script_dir():
45 return os
.path
.dirname(os
.path
.realpath(__file__
))
49 def __init__(self
, infile
, verbose
):
51 self
._verbose
= verbose
52 self
._has
_error
= False
54 self
._pverbose
('asciidoc -> DocBook')
61 return self
._has
_error
63 def _pverbose(self
, msg
):
67 def _perror(self
, msg
, fatal
=False):
68 self
._has
_error
= True
72 self
._indir
= os
.path
.dirname(self
._infile
)
73 self
._imgexportdir
= os
.path
.join(self
._indir
, 'images', 'export')
74 self
._builddir
= os
.path
.join(_get_script_dir(), 'check', os
.path
.basename(self
._infile
))
75 self
._outfile
= os
.path
.join(self
._builddir
, 'out.xml')
78 conf
= os
.path
.join(_get_script_dir(), 'asciidoc.check.conf')
79 os
.makedirs(self
._builddir
, mode
=0o755, exist_ok
=True)
90 cmd
.append(self
._infile
)
91 res
= subprocess
.run(cmd
)
93 if res
.returncode
!= 0:
94 self
._perror
('asciidoc did not finish successfully', True)
97 tree
= etree
.ElementTree(file=self
._outfile
)
98 self
._root
= tree
.getroot()
101 self
._pverbose
('Checking links')
103 self
._pverbose
('Checking images')
106 def _check_links(self
):
107 sections_anchors
= self
._root
.findall('.//section')
108 sections_anchors
+= self
._root
.findall('.//anchor')
109 sections_anchors
+= self
._root
.findall('.//glossary')
110 sections_anchors
+= self
._root
.findall('.//important')
111 sections_anchors
+= self
._root
.findall('.//tip')
112 sections_anchors
+= self
._root
.findall('.//caution')
113 sections_anchors
+= self
._root
.findall('.//warning')
114 sections_anchors
+= self
._root
.findall('.//note')
115 links
= self
._root
.findall('.//link')
118 for sa
in sections_anchors
:
119 end_id
= sa
.get('id')
121 if sa
.tag
in ('section', 'anchor') and end_id
is None:
122 self
._perror
('Found a section/anchor with no ID', True)
129 end
= link
.get('linkend')
132 self
._perror
('Found a link with no end', True)
138 for end
in link_ends
:
139 if end
not in end_ids
:
140 self
._perror
('Link end "{}" does not name a section/anchor ID'.format(end
))
142 def _check_images(self
):
143 image_datas
= self
._root
.findall('.//imagedata')
145 for image_data
in image_datas
:
146 fileref
= image_data
.get('fileref')
147 path
= os
.path
.join(self
._imgexportdir
, fileref
)
149 if not os
.path
.isfile(path
):
150 self
._perror
('Cannot find image "{}"'.format(fileref
))
154 parser
= argparse
.ArgumentParser()
155 parser
.add_argument('-v', '--verbose', action
='store_true')
156 parser
.add_argument('infile')
157 args
= parser
.parse_args()
159 if not os
.path
.isfile(args
.infile
):
160 _perror('"{}" is not an existing file'.format(args
.infile
))
167 checker
= _Checker(args
.infile
, args
.verbose
)
169 if checker
.has_error
:
172 print(colored('All good!', 'green', attrs
=['bold']))
177 if __name__
== '__main__':
This page took 0.068702 seconds and 5 git commands to generate.