Skip to content

Commit 3d72b8d

Browse files
authored
Adding \preformatted code block rendering (#16)
* adding preformatted block handling
1 parent 930cdd3 commit 3d72b8d

File tree

9 files changed

+57
-15
lines changed

9 files changed

+57
-15
lines changed

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: rd2markdown
22
Title: Convert Rd Files into Markdown
3-
Version: 0.0.2
3+
Version: 0.0.2.9000
44
Authors@R: c(
55
person(
66
given = "Doug",
@@ -34,4 +34,4 @@ License: MIT + file LICENSE
3434
VignetteBuilder: knitr
3535
Encoding: UTF-8
3636
Roxygen: list(markdown = TRUE)
37-
RoxygenNote: 7.1.2
37+
RoxygenNote: 7.2.1

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ S3method(rd2markdown,list)
2929
S3method(rd2markdown,name)
3030
S3method(rd2markdown,note)
3131
S3method(rd2markdown,pkg)
32+
S3method(rd2markdown,preformatted)
3233
S3method(rd2markdown,references)
3334
S3method(rd2markdown,section)
3435
S3method(rd2markdown,seealso)

NEWS.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
rd2markdown 0.0.2
22
-----------------
33

4-
* minor change to S3 method exports (#1 @dgkf)
4+
* Adding preformatted code block handling (#15 @dgkf)
55

6-
* minor bug fix to header formatting (additional preceeding newline), which
6+
* Minor change to S3 method exports (#1 @dgkf)
7+
8+
* Minor bug fix to header formatting (additional preceeding newline), which
79
affects a subset of markdown renderers (#3 @dgkf)
810

911
* Add `macros` parameter to `get_rd()` function to allow handling of

R/_rd_sampler.R

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#' |----|---------|-------|
2525
#' | rd | sampler | table |
2626
#'
27+
#' ```
28+
#' preformatted code
29+
#' ```
30+
#'
2731
#' \eqn{Rd + sampler + inline + equation}
2832
#'
2933
#' \deqn{Rd * sampler * block * equation}
@@ -58,4 +62,8 @@
5862
#' @rdname rd_sampler
5963
#' @name rd_sampler
6064
#'
61-
NULL
65+
#' @importFrom utils help
66+
#' @keywords internal
67+
rd_sampler <- function(x, fragments, ...) {
68+
utils::help("rd_sampler", package = packageName())
69+
}

R/rd2markdown.R

+9-2
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,22 @@ rd2markdown.examples <- function(x, fragments = c(), ...) {
190190
rd2markdown.usage(x, fragments = fragments, ..., title = "Examples")
191191
}
192192

193+
#' @exportS3Method
194+
#' @rdname rd2markdown
195+
rd2markdown.usage <- function(...) {
196+
rd2markdown.preformatted(..., language = "r")
197+
}
198+
193199
#' @param title optional section title
200+
#' @param language language to use as code fence syntax highlighter
194201
#'
195202
#' @exportS3Method
196203
#' @rdname rd2markdown
197-
rd2markdown.usage <- function(x, fragments = c(), ..., title = NULL) {
204+
rd2markdown.preformatted <- function(x, fragments = c(), ..., title = NULL, language = "") {
198205
code <- capture.output(tools::Rd2txt(list(x), fragment = TRUE))
199206
code <- tail(code, -1L) # remove "usage" title
200207
code <- gsub("^\\n?\\s{5}", "", code) # remove leading white space
201-
code <- sprintf("\n```r\n%s\n```\n\n", trimws(paste0(code, collapse = "\n")))
208+
code <- sprintf("\n```%s\n%s\n```\n\n", language, trimws(paste0(code, collapse = "\n")))
202209
if (!is.null(title)) code <- sprintf("## %s\n%s", title, code)
203210
code
204211
}

man/rd2markdown.Rd

+6-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/rd_sampler.Rd

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/data/man/rd_sampler.Rd

+12-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-rd2markdown.R

+10
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ test_that("Rd document examples are properly rendered to markdown", {
7777
})
7878

7979
test_that("Rd document details are properly rendered to markdown", {
80+
rd <- get_rd(file = file.path(test_path(), "data", "man", "rd_sampler.Rd"))
81+
details_frag <- rd[sapply(rd, function(i) attr(i, "Rd_tag") == "\\details")][[1]]
82+
pre_frag <- details_frag[sapply(details_frag, function(i) attr(i, "Rd_tag") == "\\preformatted")][[1]]
83+
expect_silent(md <- rd2markdown(pre_frag))
84+
expect_match(trimws(md), trimws(pre_frag[[1L]]), fixed = TRUE)
85+
expect_match(md, "^\\s*```")
86+
expect_match(md, "```\\s*$")
87+
})
88+
89+
test_that("Rd document preformatted code blocks are properly rendered to markdown", {
8090
rd <- get_rd(file = file.path(test_path(), "data", "man", "rd_sampler.Rd"))
8191
detail_frag <- rd[sapply(rd, function(i) attr(i, "Rd_tag") == "\\details")]
8292
expect_silent(md <- rd2markdown(detail_frag))

0 commit comments

Comments
 (0)