2
0
mirror of https://github.com/lnbook/lnbook synced 2024-11-01 03:20:53 +00:00
lnbook/tools/reconcile_references.py
2021-09-21 13:06:05 +02:00

38 lines
1.1 KiB
Python

from __future__ import print_function
import glob
import re
markup_files = glob.glob('*.asciidoc')
anchor_re = re.compile("\[\[(.*)\]\]")
ref_re = re.compile(".*\<\<([^\>]*)\>\>.")
refs = []
anchors = []
dup_anchors = []
for markup_file in markup_files:
markup_f = open(markup_file, 'r')
markup_contents = markup_f.read()
markup_f.close()
for line in markup_contents.splitlines():
ref_match = ref_re.match(line)
if ref_match:
if ref_match.group(1) not in refs:
refs.append(ref_match.group(1))
anchor_match = anchor_re.match(line)
if anchor_match:
if anchor_match.group(1) not in anchors:
anchors.append(anchor_match.group(1))
else:
dup_anchors.append(anchor_match.group(1))
print("\nAnchors: ", len(anchors))
print("\nDuplicated Anchors: ", len(dup_anchors))
print(dup_anchors)
print("\nReferences: ", len(refs))
print(refs)
broken_refs = list(set(refs) - set(anchors))
print("\nBroken references: ", len(broken_refs), broken_refs)
missing_refs = list(set(anchors) - set(refs))
print("\nUn-referenced Anchors: ", len(missing_refs), missing_refs)