-
Notifications
You must be signed in to change notification settings - Fork 997
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vignettes: avoid using _ or . in header IDs (#6784)
* vignettes: avoid using _ or . in header IDs data.table-intro on CRAN: <h3 id="h-great-but-how-can-i-refer-to-columns-by-names-in-j-like-in-a-data-frame" #refer_j>h) Great! But how can I refer to columns by names in <code>j</code> (like in a <code>data.frame</code>)?</h3> <h4 id="how-can-we-calculate-the-number-of-trips-for-each-origin-airport-for-carrier-code-quot-aa-quot" #origin-.N>– How can we calculate the number of trips for each origin airport for carrier code <code>"AA"</code>?</h4> <h4 id="how-can-we-get-the-total-number-of-trips-for-each-origin-dest-pair-for-carrier-code-quot-aa-quot" #origin-dest-.N>– How can we get the total number of trips for each <code>origin, dest</code> pair for carrier code <code>"AA"</code>?</h4> This is not valid HTML and the links to these headers don't work. "Intro" seems to be the only vignette affected. * Add a linter to validate vignette heading ids This doesn't parse the full range of Pandoc's header_attributes extension (one shouldn't be using regular expressions for that), but at least it captures the existing mistakes without obvious false positives. * Fix the heading ids in 'Introduction à data.table'
- Loading branch information
Showing
3 changed files
with
36 additions
and
10 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
any_mismatch = FALSE | ||
|
||
# ensure that ids are limited to alphanumerics and dashes | ||
# (in particular, dots and underscores break the links) | ||
check_header_ids = function(md) { | ||
# A bit surprisingly, some headings don't start with a letter. | ||
# We're interested in those that set an id to link to, i.e., end with {#id}. | ||
heading_captures = regmatches(md, regexec("^#+ \\S.*[{]#([^}]*)[}]$", md)) | ||
lines_with_id = which(lengths(heading_captures) > 0) | ||
ids = vapply(heading_captures[lines_with_id], `[`, '', 2) | ||
# ids must start with a letter and consist of alphanumerics or dashes. | ||
good_ids = grepl('^[A-Za-z][A-Za-z0-9-]*$', ids) | ||
for (line in lines_with_id[!good_ids]) cat(sprintf( | ||
"On line %d, bad heading id '%s':\n%s\n", | ||
line, heading_captures[[line]][2], heading_captures[[line]][1] | ||
)) | ||
!all(good_ids) | ||
} | ||
|
||
any_error = FALSE | ||
for (vignette in list.files('vignettes', pattern = "[.]Rmd$", recursive = TRUE, full.name = TRUE)) { | ||
cat(sprintf("Checking vignette file %s...\n", vignette)) | ||
rmd_lines = readLines(vignette) | ||
any_error = check_header_ids(rmd_lines) || any_error | ||
} | ||
if (any_error) stop("Please fix the vignette issues above.") |
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
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