Skip to content

Commit

Permalink
Merge pull request #4 from r-lib/main
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
SebKrantz authored Jun 4, 2024
2 parents 329fed9 + b683f8a commit b301d8a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 24 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pkgdown (development version)

* `build_reference()` does a better job of parsing `\value{}` blocks (#2371).
* When built on GitHub, source urls now use the name of the current upstream branch (rather than `HEAD`), which is more likely to generate correct links (#2597).
* New `vignette("non-english")` that discusses non-English sites including how to submit new translations (#2605).
* `build_reference()` now generates the usage that users actually type for infix and replacement methods (#2303).
Expand Down
51 changes: 36 additions & 15 deletions R/rd-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,46 @@ as_data.tag_value <- function(x, ...) {
}

describe_contents <- function(x, ..., id_prefix = NULL) {
# Drop pure whitespace nodes between items
is_ws <- purrr::map_lgl(x, is_whitespace)

# Group contiguous \items{}/whitespace into a <dl>
is_item <- purrr::map_lgl(x, inherits, "tag_item") | is_ws
changed <- is_item[-1] != is_item[-length(is_item)]
group <- cumsum(c(TRUE, changed))

parse_piece <- function(x) {
if (length(x) == 0) {
NULL
} else if (any(purrr::map_lgl(x, inherits, "tag_item"))) {
paste0("<dl>\n", parse_descriptions(x, ..., id_prefix = id_prefix), "</dl>")
if (length(x) == 0) {
return("")
}

# Group contiguous \items{}/whitespace into a <dl>; everything else
# is handled as is
block_id <- integer(length(x))
block_id[[1]] <- 1
cur_block_is_dl <- inherits(x[[1]], "tag_item")

for (i in seq2(2, length(x))) {
is_item <- inherits(x[[i]], "tag_item")
if (cur_block_is_dl) {
same_type <- is_item || is_whitespace(x[[i]])
} else {
same_type <- !is_item
}

if (same_type) {
block_id[[i]] <- block_id[[i - 1]]
} else {
block_id[[i]] <- block_id[[i - 1]] + 1
cur_block_is_dl <- !cur_block_is_dl
}
}

parse_block <- function(x) {
is_dl <- any(purrr::map_lgl(x, inherits, "tag_item"))
if (is_dl) {
paste0(
"<dl>\n",
parse_descriptions(x, ..., id_prefix = id_prefix),
"</dl>"
)
} else {
flatten_para(x, ...)
}
}
pieces <- split(x, group)
out <- purrr::map(pieces, parse_piece)
blocks <- split(x, block_id)
out <- purrr::map(blocks, parse_block)

paste(unlist(out), collapse = "\n")
}
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ skip_if_no_pandoc <- function(version = "1.12.3") {
}

has_internet <- function() {
return(getOption("pkgdown.internet", default = TRUE))
getOption("pkgdown.internet", default = TRUE)
}

# remove '' quoting
Expand Down
21 changes: 14 additions & 7 deletions tests/testthat/test-rd-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ test_that("leading whitespace doesn't break items", {
value2html("\n\\item{a}{b}\n\n\\item{c}{d}\n\n\\item{e}{f}"),
c(
"<dl>",
"",
"<dt>a</dt>", "<dd><p>b</p></dd>", "", "",
"<dt>c</dt>", "<dd><p>d</p></dd>", "", "",
"<dt>e</dt>", "<dd><p>f</p></dd>",
Expand All @@ -57,14 +56,10 @@ test_that("leading whitespace doesn't break items", {
)
})

test_that("whitespace between text is preserved", {
test_that("whitespace between text is not preserved", {
expect_equal(
value2html("a\n\nb\n\nc"),
c(
"<p>a</p>", "", "",
"<p>b</p>", "", "",
"<p>c</p>"
)
c("<p>a</p>", "<p>b</p>", "<p>c</p>")
)
})

Expand All @@ -82,3 +77,15 @@ test_that("can have multiple interleaved blocks", {
)
)
})

test_that("other tags don't affect breaking (#2371)", {
expect_equal(
value2html("1\\code{xxx}\n2\n3"),
c("<p>1<code>xxx</code>", "2", "3</p>")
)
# additionally teading whitespace
expect_equal(
value2html("1\\code{xxx}\n 2\n 3"),
c("<p>1<code>xxx</code>", "2", "3</p>")
)
})
2 changes: 1 addition & 1 deletion vignettes/translations.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Current available translations are:
- `tr`: Turkish
- `zh_CN`: Chinese (simplified)

As you can see, most language codes are two letters, but if a language has multiple variants, it gets a longer form which can be used to disamiguate the options. For example, Chinese can use one of two forms: simplified (used in China and Singapore) or traditional (used in Taiwan and Hong Kong). Another example would be providing specific French Canadian translations by using code `fr_CN`.
As you can see, most language codes are two letters, but if a language has multiple variants, it gets a longer form which can be used to disambiguate the options. For example, Chinese can use one of two forms: simplified (used in China and Singapore) or traditional (used in Taiwan and Hong Kong). Another example would be providing specific French Canadian translations by using code `fr_CN`.

## Translations

Expand Down

0 comments on commit b301d8a

Please sign in to comment.