Skip to content
This repository was archived by the owner on Aug 14, 2024. It is now read-only.

Commit cebd855

Browse files
committed
Checking that internally-defined links resolve.
An internally-defined Markdown link has the form [xxx][yyy]. If the ID 'yyy' doesn't resolve, the text is left as-is, so we check for that, then subtract those that reference configuration values using '{{'. It's a hack, but it'll catch at least a few things.
1 parent 9be4b22 commit cebd855

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

bin/lesson_check.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
# Pattern to match figure references in HTML.
4848
P_FIGURE_REFS = re.compile(r'<img[^>]+src="([^"]+)"[^>]*>')
4949

50+
# Pattern to match internally-defined Markdown links.
51+
P_INTERNALLY_DEFINED_LINK = re.compile(r'\[[^\]]+\]\[[^\]]+\]')
52+
5053
# What kinds of blockquotes are allowed?
5154
KNOWN_BLOCKQUOTES = {
5255
'callout',
@@ -274,6 +277,7 @@ def check(self):
274277
self.check_trailing_whitespace()
275278
self.check_blockquote_classes()
276279
self.check_codeblock_classes()
280+
self.check_defined_link_references()
277281

278282

279283
def check_metadata(self):
@@ -331,6 +335,26 @@ def check_codeblock_classes(self):
331335
cls)
332336

333337

338+
def check_defined_link_references(self):
339+
"""Check that defined links resolve in the file.
340+
341+
Internally-defined links match the pattern [text][label]. If
342+
the label contains '{{...}}', it is hopefully a references to
343+
a configuration value - we should check that, but don't right
344+
now.
345+
"""
346+
347+
result = set()
348+
for node in self.find_all(self.doc, {'type' : 'text'}):
349+
for match in P_INTERNALLY_DEFINED_LINK.findall(node['value']):
350+
if '{{' not in match:
351+
result.add(match)
352+
self.reporter.check(not result,
353+
self.filename,
354+
'Internally-defined links may be missing definitions: {0}',
355+
', '.join(sorted(result)))
356+
357+
334358
def find_all(self, node, pattern, accum=None):
335359
"""Find all matches for a pattern."""
336360

0 commit comments

Comments
 (0)