1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2024-12-22 17:34:18 +03:00

scripts: check-html-references: Improve error messages and don't mess with relative paths

Now that we have the source file name as a custom attribute we can use
it to report which file actually needs to be edited to fix the error:

 ERROR: 'docs/uri.rst': broken link to: 'drvqemu.html#exaple'

rather than:

 broken link targets:
 docs/uri.html broken link: drvqemu.html#exaple

which pointed to file which does not exist in the source directory.

This also allows us to delete all the relative path handling needed to
report at least somewhat user-legible errors before.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Peter Krempa 2023-02-14 12:35:23 +01:00
parent 70211d7368
commit 00956b55bf

View File

@ -30,13 +30,6 @@ def get_file_list(prefix):
filelist = []
for root, dir, files in os.walk(prefix):
prefixbase = os.path.dirname(prefix)
if root.startswith(prefixbase):
relroot = root[len(prefixbase):]
else:
relroot = root
for file in files:
if not re.search('\\.html$', file):
continue
@ -45,20 +38,21 @@ def get_file_list(prefix):
if '404.html' in file:
continue
fullfilename = os.path.join(root, file)
relfilename = os.path.join(relroot, file)
filelist.append((fullfilename, relfilename))
filelist.append(os.path.join(root, file))
return filelist
# loads an XHTML and extracts all anchors, local and remote links for the one file
def process_file(filetuple):
filename, relfilename = filetuple
def process_file(filename):
tree = ET.parse(filename)
root = tree.getroot()
docname = root.get('data-sourcedoc')
anchors = [relfilename]
if not docname:
docname = filename
anchors = [filename]
targets = []
for elem in root.findall('.//html:a', ns):
@ -66,30 +60,30 @@ def process_file(filetuple):
an = elem.get('id')
if an:
anchors.append(relfilename + '#' + an)
anchors.append(filename + '#' + an)
if target:
if re.search('://', target):
externallinks.append(target)
elif target[0] != '#' and 'mailto:' not in target:
dirname = os.path.dirname(relfilename)
targetname = os.path.normpath(os.path.join(dirname, target))
dirname = os.path.dirname(filename)
targetfull = os.path.normpath(os.path.join(dirname, target))
targets.append((targetname, filename, target))
targets.append((filename, docname, targetfull, target))
# older docutils generate "<div class='section'"
for elem in root.findall('.//html:div/[@class=\'section\']', ns):
an = elem.get('id')
if an:
anchors.append(relfilename + '#' + an)
anchors.append(filename + '#' + an)
# modern docutils generate a <section element
for elem in root.findall('.//html:section', ns):
an = elem.get('id')
if an:
anchors.append(relfilename + '#' + an)
anchors.append(filename + '#' + an)
return (anchors, targets)
@ -98,8 +92,8 @@ def process_all(filelist):
anchors = []
targets = []
for filetuple in filelist:
anchor, target = process_file(filetuple)
for file in filelist:
anchor, target = process_file(file)
targets = targets + target
anchors = anchors + anchor
@ -109,17 +103,15 @@ def process_all(filelist):
def check_targets(targets, anchors):
errors = []
for target, targetfrom, targetorig in targets:
for _, docname, target, targetorig in targets:
if target not in anchors:
errors.append((targetfrom, targetorig))
errors.append((docname, targetorig))
if errors:
errors.sort()
print('broken link targets:')
for file, target in errors:
print(file + " broken link: " + target)
print(f'ERROR: \'{file}\': broken link to: \'{target}\'')
return True
@ -134,7 +126,7 @@ parser.add_argument('--external', action="store_true",
args = parser.parse_args()
files = get_file_list(args.webroot)
files = get_file_list(os.path.abspath(args.webroot))
targets, anchors = process_all(files)