Skip to content

Commit 54ba042

Browse files
committed
Check that xrefs are path-based; issue #11
1 parent 9e6b69a commit 54ba042

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/validation.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ mod content {
336336
reports.push(experimental_issue);
337337
}
338338

339+
reports.append(check_path_xrefs(content).as_mut());
340+
339341
reports
340342
}
341343

@@ -408,6 +410,34 @@ mod content {
408410
None
409411
}
410412
}
413+
414+
/// Check that supported xrefs are path-based.
415+
/// Report issues as warnings because ID-based xrefs might still work in some instances, such as within the file.
416+
fn check_path_xrefs(content: &str) -> Vec<IssueReport> {
417+
// Check only those xrefs that have labels. Skip unsupported, label-less xrefs, which are reported elsewhere.
418+
let xref_regex = Regex::new(r"xref:(\S+)\[\S+.*?\]").unwrap();
419+
// This regex checks only the captured content of the xref.
420+
let path_based_regex = Regex::new(r"\S+\.(?:adoc|asciidoc)").unwrap();
421+
422+
let mut issues = Vec::new();
423+
424+
for (index, line) in content.lines().enumerate() {
425+
if let Some(xref) = xref_regex.captures(line) {
426+
if path_based_regex.is_match(xref.get(1).expect("Cannot capture the xref content.").as_str()) {
427+
// This xref is path-based. This is the correct form. Skip.
428+
} else {
429+
// This xref is not path-based. Report.
430+
issues.push(IssueReport {
431+
line_number: Some(index),
432+
description: "This xref does not appear to be path-based. Make sure that it works in your assembly.",
433+
severity: IssueSeverity::Warning,
434+
});
435+
}
436+
}
437+
}
438+
439+
issues
440+
}
411441
}
412442

413443
// This section groups all module requirements;

0 commit comments

Comments
 (0)