Skip to content

Commit

Permalink
Add rawNamespace.
Browse files Browse the repository at this point in the history
Fixes #385
  • Loading branch information
hadley committed Oct 20, 2015
1 parent 1407088 commit 08442dd
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Expand Up @@ -3,7 +3,8 @@
* New tag `@rawRd` allows you to insert raw (unescaped) Rd code. `@evalRd()` is
similar, but instead of literal Rd, you give it R code that produces literal
Rd code when run. This should make it easier to experiment with new types of
output (#385).
output. `@rawNamespace` allows you to insert literal text in your namespace:
this can be useful for conditional imports (#385).

* `register.preref.parser()` and `register.preref.parsers()` have been
deprecated - please use `register_tags()` instead.
Expand Down
2 changes: 1 addition & 1 deletion R/parse-preref.R
Expand Up @@ -88,7 +88,7 @@ parse.code <- function(x) {
tag_warning(x, "requires a value")
} else {
tryCatch({
x$val <- parse(text = x$val)
parse(text = x$val)
x
}, error = function(e) {
tag_warning(x, "code failed to parse.\n", e$message)
Expand Down
4 changes: 3 additions & 1 deletion R/roclet-namespace.R
Expand Up @@ -10,12 +10,13 @@ register_tags(
importClassesFrom = words_parser(2),
importFrom = words_parser(2),
importMethodsFrom = words_parser(2),
rawNamespace = parse.code,
S3method = words_parser(2, 2),
useDynLib = words_parser(1)
)

ns_tags <- c('export', 'exportClass', 'exportMethod', 'exportPattern',
'S3method', 'import', 'importFrom', 'importClassesFrom',
'rawNamespace', 'S3method', 'import', 'importFrom', 'importClassesFrom',
'importMethodsFrom', 'useDynLib')

#' Roclet: make NAMESPACE.
Expand Down Expand Up @@ -121,6 +122,7 @@ ns_useDynLib <- function(tag, part) {
repeat_first("useDynLib", tag)
}
}
ns_rawNamespace <- function(tag, part) tag

# Functions used by both default_export and ns_* functions
export <- function(x) one_per_line("export", x)
Expand Down
3 changes: 2 additions & 1 deletion R/roclet-rd.R
Expand Up @@ -115,7 +115,8 @@ block_to_rd <- function(block, base_path, env) {
add_tag(rd, process_doc_type(block))
add_tag(rd, process_tag(block, "rawRd"))
add_tag(rd, process_tag(block, "evalRd", function(tag, param) {
out <- eval(param, envir = env)
expr <- parse(text = param)
out <- eval(expr, envir = env)
new_tag("rawRd", as.character(out))
}))
add_tag(rd, process_tag(block, "title"))
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-raw.R
Expand Up @@ -30,3 +30,22 @@ test_that("rawRd inserted unchanged", {
args <- get_tag(out, "rawRd")$values
expect_equal(args, "20")
})

test_that("rawNamespace must be valid code", {
expect_warning(
roc_proc_text(namespace_roclet(), "
#' @rawNamespace if() {
#' @name a
NULL"),
"code failed to parse"
)
})

test_that("rawNamespace inserted unchanged", {
out <- roc_proc_text(namespace_roclet(), "
#' @rawNamespace xyz
#' abc
NULL")

expect_equal(out, "xyz\n abc")
})

0 comments on commit 08442dd

Please sign in to comment.