|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import sys |
| 4 | +import argparse |
| 5 | +from pathlib import Path |
| 6 | +from html.parser import HTMLParser |
| 7 | +from urllib.parse import urlsplit |
| 8 | + |
| 9 | + |
| 10 | +class MetricsParser(HTMLParser): |
| 11 | + def __init__(self): |
| 12 | + super().__init__() |
| 13 | + self.int_link_count = 0 |
| 14 | + self.ext_link_count = 0 |
| 15 | + self.fragment_count = 0 |
| 16 | + self.image_count = 0 |
| 17 | + self.in_object = 0 |
| 18 | + |
| 19 | + @property |
| 20 | + def link_count(self): |
| 21 | + return self.fragment_count + self.int_link_count + self.ext_link_count |
| 22 | + |
| 23 | + def read(self, file): |
| 24 | + """ |
| 25 | + Read *file* (a file-like object with a ``read`` method returning |
| 26 | + strings) a chunk at a time, feeding each chunk to the parser. |
| 27 | + """ |
| 28 | + # Ensure the parser state is reset before each file (just in case |
| 29 | + # there's an erroneous dangling <object>) |
| 30 | + self.reset() |
| 31 | + self.in_object = 0 |
| 32 | + buf = '' |
| 33 | + while True: |
| 34 | + # Parse 1MB chunks at a time |
| 35 | + buf = file.read(1024**2) |
| 36 | + if not buf: |
| 37 | + break |
| 38 | + self.feed(buf) |
| 39 | + |
| 40 | + def handle_starttag(self, tag, attrs): |
| 41 | + """ |
| 42 | + Count <a>, <img>, and <object> tags to determine the number of internal |
| 43 | + and external links, and the number of images. |
| 44 | + """ |
| 45 | + attrs = dict(attrs) |
| 46 | + if tag == 'a' and 'href' in attrs: |
| 47 | + # If there's no href, it's an anchor; if there's no hostname |
| 48 | + # (netloc) or path, it's just a fragment link within the page |
| 49 | + url = urlsplit(attrs['href']) |
| 50 | + if url.netloc: |
| 51 | + self.ext_link_count += 1 |
| 52 | + elif url.path: |
| 53 | + self.int_link_count += 1 |
| 54 | + else: |
| 55 | + self.fragment_count += 1 |
| 56 | + elif tag == 'object': |
| 57 | + # <object> tags are a bit complex as they nest to offer fallbacks |
| 58 | + # and may contain an <img> fallback. We only want to count the |
| 59 | + # outer-most <object> in this case |
| 60 | + if self.in_object == 0: |
| 61 | + self.image_count += 1 |
| 62 | + self.in_object += 1 |
| 63 | + elif tag == 'img' and self.in_object == 0: |
| 64 | + self.image_count += 1 |
| 65 | + |
| 66 | + def handle_endtag(self, tag): |
| 67 | + if tag == 'object': |
| 68 | + # Never let in_object be negative |
| 69 | + self.in_object = max(0, self.in_object - 1) |
| 70 | + |
| 71 | + |
| 72 | +def main(args=None): |
| 73 | + parser = argparse.ArgumentParser() |
| 74 | + parser.add_argument( |
| 75 | + 'build_dir', metavar='build-dir', nargs='?', default='.', |
| 76 | + help="The directory to scan for HTML files") |
| 77 | + config = parser.parse_args(args) |
| 78 | + |
| 79 | + parser = MetricsParser() |
| 80 | + for path in Path(config.build_dir).rglob('*.html'): |
| 81 | + with path.open('r', encoding='utf-8', errors='replace') as f: |
| 82 | + parser.read(f) |
| 83 | + |
| 84 | + print('Summarising metrics for build files (.html)...') |
| 85 | + print(f'\tlinks: {parser.link_count} (' |
| 86 | + f'{parser.fragment_count} #frag…, ' |
| 87 | + f'{parser.int_link_count} /int…, ' |
| 88 | + f'{parser.ext_link_count} https://ext…' |
| 89 | + ')') |
| 90 | + print(f'\timages: {parser.image_count}') |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + sys.exit(main()) |
0 commit comments