Skip to content

Commit

Permalink
Merge pull request #22 from quantsch/fix-user-defined-macros
Browse files Browse the repository at this point in the history
Fix user defined macros
  • Loading branch information
quantsch authored May 8, 2024
2 parents 11c3be2 + 557c20c commit 1f46f3a
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 27 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: Rd2md
Title: Markdown Reference Manuals
Version: 1.0.0
Version: 1.0.1
Authors@R: person("Julian", "Busch", email = "[email protected]", role = c("aut", "cre"))
Description: Native R only allows PDF exports of reference manuals.
The 'Rd2md' package converts the package documentation files into
Expand Down
21 changes: 2 additions & 19 deletions R/rdfile-operations.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ read_package_rdfiles <- function(pkg = ".", subclass = NULL) {
#'
#' And return object of class `rdfile`
#' @param path File path of `*.Rd`-file
#' @param pkg_path Package path, required to load macros, see
#' [tools::loadPkgRdMacros()].
#' @param subclass Potential subclass of returned `rdfile` object
#' @export
read_rdfile <- function(path, pkg_path = NULL, subclass = NULL) {
parsed_rd <- parse_rd_file(path, pkg_path)
read_rdfile <- function(path, subclass = NULL) {
parsed_rd <- tools::parse_Rd(path, encoding = "UTF-8")
as_rdfile(parsed_rd, subclass = subclass)
}

Expand All @@ -36,21 +34,6 @@ as_rdfragment <- function(x, subclass = NULL, ...) {
)
}

parse_rd_file <- function(path, pkg_path = NULL) {
enc <- "UTF-8"
if (getRversion() >= "3.4.0") {
# loadPkgRdMacros returns NULL if input pkg_path is NULL
macros <- tools::loadPkgRdMacros(pkg_path)
parsed_rd <- tools::parse_Rd(path, macros = macros, encoding = enc)
} else if (getRversion() >= "3.2.0") {
macros <- tools::loadPkgRdMacros(pkg_path, TRUE)
parsed_rd <- tools::parse_Rd(path, macros = macros, encoding = enc)
} else {
parsed_rd <- tools::parse_Rd(path, encoding = enc)
}
parsed_rd
}

# Convert RD attributes to S3 classes -------------------------------------

set_classes <- function(x) {
Expand Down
5 changes: 1 addition & 4 deletions man/read_rdfile.Rd

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

21 changes: 18 additions & 3 deletions tests/testthat/test-rdfile.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ test_that("Output file of function is correct", {
test_output_value <- as_markdown(
read_rdfile(
path = input_file,
pkg_path = "testdoc",
subclass = "refman_rdfile"
)
)
Expand All @@ -16,14 +15,31 @@ test_that("Output file of function is correct", {
)
})

test_that("System user-defined macros are handled correctly", {
input_file <- "testdoc/man/fundoc_with_ud_macro.Rd"
comp_file <- "testdoc-md/fundoc_with_ud_macro.md"

test_output_value <- as_markdown(
read_rdfile(
path = input_file,
subclass = "refman_rdfile"
)
)
test_expected_value <- read_file(comp_file, collapse = "\n")
expect_equal(
test_output_value,
test_expected_value
)
})


test_that("Output file of S4 class is correct", {
input_file <- "testdoc/man/Account-class.Rd"
comp_file <- "testdoc-md/Account-class.md"

test_output_value <- as_markdown(
read_rdfile(
path = input_file,
pkg_path = "testdoc",
subclass = "refman_rdfile"
)
)
Expand All @@ -40,7 +56,6 @@ test_that("Output file of R6 class is correct", {
test_output_value <- as_markdown(
read_rdfile(
path = input_file,
pkg_path = "testdoc",
subclass = "refman_rdfile"
)
)
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/testdoc-md/fundoc_with_ud_macro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# `fundoc_with_ud_macro`: Function With User Defined Macro In Description

## Description

CRANpkg macro: [`mycranpkg`](https://CRAN.R-project.org/package=mycranpkg)

## Usage

```r
fundoc_with_ud_macro(Rd)
```

## Arguments

* `Rd`: Object of class `Rd`

## Value

Some return value

8 changes: 8 additions & 0 deletions tests/testthat/testdoc/R/fundoc.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ bind_cols <- function(
.name_repair = c("unique", "universal", "check_unique", "minimal")
) ""

#' Function With User Defined Macro In Description
#'
#' @description
#' CRANpkg macro: \CRANpkg{mycranpkg}
#' @param Rd Object of class `Rd`
#' @return Some return value
fundoc_with_ud_macro <- function(Rd) ""

#' An S4 class to represent a bank account.
#'
#' @slot balance A length-one numeric vector
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/testdoc/man/fundoc_with_ud_macro.Rd

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

0 comments on commit 1f46f3a

Please sign in to comment.