Skip to content

Commit

Permalink
Imrpove handling of verbatim blocks
Browse files Browse the repository at this point in the history
Closes #951.
  • Loading branch information
jayhesselberth committed Jul 2, 2019
1 parent 0ac6c78 commit f1127c0
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 9 deletions.
3 changes: 1 addition & 2 deletions R/rd-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ as_data.tag_item <- function(x, ...) {

parse_section <- function(x, title, ...) {
text <- flatten_para(x, ...)
paras <- split_at_linebreaks(text)

list(
title = title,
contents = paras
contents = text
)
}

Expand Down
6 changes: 6 additions & 0 deletions R/rd-html.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ flatten_para <- function(x, ...) {
groups <- cumsum(before_break | after_break)

html <- purrr::map_chr(x, as_html, ...)

# split at line breaks for everything except blocks
empty <- purrr::map_lgl(x, purrr::is_empty)
needs_split <- !is_block & !empty
html[needs_split] <- purrr::map(html[needs_split], split_at_linebreaks)

blocks <- html %>%
split(groups) %>%
purrr::map_chr(paste, collapse = "")
Expand Down
26 changes: 26 additions & 0 deletions R/test.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,29 @@ NULL
#'
#' warning(crayon::bold("This is bold"))
NULL

#' Test case: verbatim blocks
#'
#' This description block is required so that verbatim blocks are recognized
#' and rendered correctly.
#'
#' ```
#' foo
#'
#' bar
#' ```
#'
#' ```
#' yaml:
#' this
#'
#' OR:
#'
#' yaml:
#' that
#' ```
#'
#' @name test-verbatim
#' @keywords internal
#' @family tests
NULL
1 change: 1 addition & 0 deletions R/utils.r
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set_contains <- function(haystack, needles) {
all(needles %in% haystack)
}

split_at_linebreaks <- function(text) {
if (length(text) < 1)
return(character())
Expand Down
2 changes: 1 addition & 1 deletion man/test-crayon.Rd

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

2 changes: 1 addition & 1 deletion man/test-dont.Rd

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

2 changes: 1 addition & 1 deletion man/test-figures.Rd

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

2 changes: 1 addition & 1 deletion man/test-links.Rd

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

2 changes: 1 addition & 1 deletion man/test-lists.Rd

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

2 changes: 1 addition & 1 deletion man/test-output-styles.Rd

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

3 changes: 2 additions & 1 deletion man/test-params.Rd

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

31 changes: 31 additions & 0 deletions man/test-verbatim.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat/test-rd-html.R
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,18 @@ test_that("cr generates line break", {
expect_equal(out, "<p>a <br /> b</p>")
})

# Verbatim ----------------------------------------------------------------

test_that("newlines are preserved in preformatted blocks", {
out <- flatten_para(rd_text("\\preformatted{a\n\nb\n\nc}"))
expect_equal(out, "<pre>a\n\nb\n\nc</pre>")
})

test_that("spaces are preserved in preformatted blocks", {
out <- flatten_para(rd_text("\\preformatted{a\n\n b\n\n c}"))
expect_equal(out, "<pre>a\n\n b\n\n c</pre>")
})

# Usage -------------------------------------------------------------------

test_that("S4 methods gets comment", {
Expand Down

0 comments on commit f1127c0

Please sign in to comment.