Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix how kable_mark() measures column width when there are hyperlinks #2148

Merged
merged 6 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: knitr
Type: Package
Title: A General-Purpose Package for Dynamic Report Generation in R
Version: 1.39.5
Version: 1.39.6
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
person("Abhraneel", "Sarma", role = "ctb"),
Expand Down Expand Up @@ -41,6 +41,7 @@ Authors@R: c(
person("Hiroaki", "Yutani", role = "ctb"),
person("Ian", "Lyttle", role = "ctb"),
person("Hodges", "Daniel", role = "ctb"),
person("Jacob", "Bien", role = "ctb"),
person("Jake", "Burkhead", role = "ctb"),
person("James", "Manton", role = "ctb"),
person("Jared", "Lander", role = "ctb"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

- Fixed `epub2` output not being considered as HTML format (thanks, @flipacholas, #2145).

- `kable(format = 'pipe')` calculates column widths correctly now if any text columns contain Markdown hyperlinks (thanks, @jacobbien, #2148).

## MINOR CHANGES

- When the inline R code cannot be correctly parsed, the error message will show the original code in addition to the parsing error, which can make it easier to identify the code error in the source document (thanks, @AlbertLei, #2141).
Expand Down
4 changes: 2 additions & 2 deletions R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,13 @@ kable_mark = function(x, sep.row = c('=', '=', '='), sep.col = ' ', padding = 0
if (sep.col == '|') for (j in seq_len(ncol(x))) {
x[, j] = gsub('\\|', '|', x[, j])
}
l = if (prod(dim(x)) > 0) apply(x, 2, function(z) max(nchar(z, type = 'width'), na.rm = TRUE))
l = if (prod(dim(x)) > 0) apply(x, 2, function(z) max(nchar(remove_urls(z), type = 'width'), na.rm = TRUE))
cn = colnames(x)
if (length(cn) > 0) {
cn[is.na(cn)] = "NA"
if (sep.col == '|') cn = gsub('\\|', '|', cn)
if (grepl('^\\s*$', cn[1L])) cn[1L] = rownames.name # no empty cells for reST
l = pmax(if (length(l) == 0) 0 else l, nchar(cn, type = 'width'))
l = pmax(if (length(l) == 0) 0 else l, nchar(remove_urls(cn), type = 'width'))
}
align = attr(x, 'align')
padding = padding * if (length(align) == 0) 2 else {
Expand Down
6 changes: 6 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -1059,3 +1059,9 @@ make_unique = function(x) {
#' browseURL('logo.html') # you can check its HTML source
#' }
image_uri = function(f) xfun::base64_uri(f)

# Change all "[label](url)" to "label" except leave "`[label](url)`" as is
remove_urls = function(x) {
# regex adapted from https://dev.to/mattkenefick/regex-convert-markdown-links-to-html-anchors-f7j
gsub("(?<!`)\\[([^]]+)\\]\\(([^)]+)\\)(?!`)", "\\1", x, perl = TRUE)
}
4 changes: 2 additions & 2 deletions man/output_type.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/testit/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,10 @@ assert('comment_out() add prefix and newlines if asked', {
(comment_out("a", prefix = "$") %==% "$ a\n")
(comment_out("a", prefix = NULL) %==% "a\n")
})

assert('remove_urls() removes the link', {
(remove_urls(c('[a](b)', '[a b](c)')) %==% c('a', 'a b'))
(remove_urls('a [b](c) d.') %==% 'a b d.')
(remove_urls('a [b](c) d [e f+g](h) i.') %==% 'a b d e f+g i.')
(remove_urls('a [b](c) `[d](e)` f.') %==% 'a b `[d](e)` f.')
})