Last active
August 29, 2015 14:11
-
-
Save kqr/b0cc69485d1528c9fa39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Written for Python 3. Although it runs on Python 2 as | |
# well, if that's a thing you're doing you might want to | |
# change items() to iteritems() and all that standard stuff. | |
# | |
# 2014 © ~kqr | |
# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
# option. This file may not be copied, modified, or distributed | |
# except according to those terms. | |
import sys | |
import re | |
id = re.compile('id="([^"]+)"') | |
href = re.compile('href="#([^"]+)"') | |
if __name__ == '__main__': | |
for filename in sys.argv[1:]: | |
anchors, references = scan_file(filename) | |
for anchor, i in references.items(): | |
if anchor not in anchors: | |
print("{file}:{i}: {anchor} referenced".format(file=filename, i=i, anchor=anchor)) | |
def scan_file(filename): | |
"""Scans a HTML-like file for things that look like id and href attributes and | |
returns all of their values coupled with the lines they appear in""" | |
anchors = dict() | |
references = dict() | |
with open(filename, 'r') as file: | |
for i, line in enumerate(file): | |
anchors.update({ m.group(1): i for m in id.finditer(line) }) | |
references.update({ m.group(1): i for m in href.finditer(line) }) | |
return anchors, references |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment