def identify_broken_links()

in link-verifier/verify-links.py [0:0]


    def identify_broken_links(self, files, verbose):
        """Tests links for existence"""

        dirname = os.path.dirname(self.name)
        # Only print the file name once
        file_printed = False
        for link in self.internal_links:
            # First, look for anchors in the same document.
            link_elements = link.split('#')
            path = link_elements[0]
            id = None
            if len(link_elements) > 1:
                id = link_elements[1]
            if path == '':
                if id is not None:
                    if id.lower() not in self.ids:
                        self.broken_links.append(link)
                        file_printed = self.print_filename(files[self.name], file_printed)
                        cprint(f'\tUnknown link: {link}', 'red')
                    elif verbose:
                        file_printed = self.print_filename(files[self.name], file_printed)
                        cprint(f'\t{link}', 'green')
                continue

            # At this point, this is probably a link to a file in the same repo,
            # so we test if the file exists.
            filename = os.path.join(dirname, path)
            absfile = os.path.abspath(filename)
            # Note: We don't test whether the link target exists, just the file.
            if not os.path.exists(absfile):
                self.broken_links.append(link)
                file_printed = self.print_filename(files[self.name], file_printed)
                cprint(f'\tUnknown file: {path}', 'red')
            elif verbose:
                file_printed = self.print_filename(files[self.name], file_printed)
                cprint(f'\t{link}','green')

        for link in self.external_links:
            is_broken, status_code = test_url(link)
            if is_broken:
                self.broken_links.append(link)
                file_printed = self.print_filename(files[self.name], file_printed)
                cprint(f'  {status_code}\t{link}', 'red')
            else:
                if verbose:
                    file_printed = self.print_filename(files[self.name], file_printed)
                    cprint(f'  {status_code}\t{link}', 'green')