Skip to content

Commit

Permalink
Also look for templates in man/roxygen/templates
Browse files Browse the repository at this point in the history
Fixes #888
  • Loading branch information
hadley committed Sep 10, 2019
1 parent 3fa7c41 commit 87eab9a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 27 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# roxygen2 (development version)

* roxygen now looks for templates in `man/roxygen/templates` (#888).

* Files generated on Windows systems now retain their existing line endings, or use
unix-style line endings for new files (@jonthegeek, @jimhester, #840).

Expand Down
8 changes: 6 additions & 2 deletions R/rd-template.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
template_find <- function(base_path, template_name) {
path <- file.path(base_path, "man-roxygen", paste0(template_name, ".", c("R", "r")))
file_name <- paste0(template_name, ".", c("R", "r"))
path <- c(
file.path(base_path, "man-roxygen", file_name),
file.path(base_path, "man", "roxygen", "templates", file_name)
)
path_exists <- file.exists(path)

if (!any(path_exists)) {
stop("Can't find template '", template_name, "'", call. = FALSE)
}

path[path_exists][1]
path[path_exists][[1]]
}

template_eval <- function(template_path, vars) {
Expand Down
Empty file.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions tests/testthat/templates/man/roxygen/templates/new-path.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#' <%= x %>
#' @param <%= y %> <%= z %>
45 changes: 20 additions & 25 deletions tests/testthat/test-rd-template.R
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
context("Rd: template")

test_that("template_find finds files with .r and .R extension, and fails to find missing files", {
my.tempdir <- "."
my.mandir <- file.path(my.tempdir, "man-roxygen")
my.ucase <- file.path(my.mandir, "UCase.R")
my.regex <- file.path(my.mandir, "reg.ex.R")
my.lcase <- file.path(my.mandir, "lcase.r")
test_that("can find template from name", {
base <- test_path("templates/")

expect_equal(template_find(my.tempdir, "UCase"), my.ucase)
expect_error(template_find(my.tempdir, "Case"))
expect_error(template_find(my.tempdir, "UCas"))
expect_equal(template_find(my.tempdir, "reg.ex"), my.regex)
expect_error(template_find(my.tempdir, "reggex"))
expect_error(template_find(my.tempdir, "nada"))
expect_equal(
template_find(base, "UCase"),
file.path(base, "man-roxygen", "UCase.R")
)

# On case-insentive file systems, will find upper case version first
expect_equal(tolower(template_find(my.tempdir, "lcase")), tolower(my.lcase))
})
expect_equal(
tolower(template_find(base, "lcase")),
tolower(file.path(base, "man-roxygen", "lcase.r"))
)

test_that("templates replace variables with their values", {
out <- roc_proc_text(rd_roclet(), "
#' @template values
#' @templateVar x a
#' @templateVar y b
#' @templateVar z c
x <- 10")[[1]]
expect_equal(
template_find(base, "new-path"),
file.path(base, "man" , "roxygen", "templates", "new-path.R")
)

expect_equal(get_tag(out, "title")$values, "a")
expect_equal(get_tag(out, "param")$values, c(b = "c"))
expect_error(
template_find(base, "missing"),
"Can't find template"
)
})

test_that("allow empty line after @template", {
test_that("templates replace variables with their values", {
out <- roc_proc_text(rd_roclet(), "
#' @template values
#'
#' @templateVar x a
#' @templateVar y b
#' @templateVar z c
x <- 10")[[1]]

expect_equal(get_tag(out, "title")$values, "a")
expect_equal(get_tag(out, "param")$values, c(b = "c"))
})

0 comments on commit 87eab9a

Please sign in to comment.