Skip to content

Commit

Permalink
change "with_flair" to "decorated" because it's more general
Browse files Browse the repository at this point in the history
  • Loading branch information
kbodwin committed Feb 10, 2021
1 parent 68d53ff commit bc0a195
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
10 changes: 6 additions & 4 deletions R/decorate_chunk.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Builds a \code{\link{with_flair}} object from a code chunk
#' Builds a \code{\link{decorated}} object from a code chunk
#'
#' This function reads the source code from a given named code chunk;
#' i.e., \code{{r chunk_name, echo = FALSE}}.
Expand All @@ -20,7 +20,7 @@
#'
#' @param ... Any number of other chunk options to override.
#'
#' @return An object of class \code{\link{with_flair}}
#' @return An object of class \code{\link{decorated}}
#'
#' @importFrom stringr str_c str_trim str_remove_all
#'
Expand Down Expand Up @@ -118,7 +118,7 @@ decorate_chunk <- function(chunk_name,
if (!is.null(my_opts$flair) && !my_opts$flair) {

placeholder <- list(NULL)
attr(placeholder, "class") = "with_flair"
attr(placeholder, "class") = "decorated"

return(placeholder)

Expand Down Expand Up @@ -161,10 +161,12 @@ decorate_chunk <- function(chunk_name,

where_sources <- map(knitted, ~attr(.x, "class")) == "source"

attr(knitted, "class") <- "with_flair"
attr(knitted, "class") <- "decorated"

attr(knitted, "orig_chunk_text") <- my_code

attr(knitted, "chunk_name") <- chunk_name

return(knitted)

}
Expand Down
10 changes: 6 additions & 4 deletions R/decorate_code.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#' Creates an object of the class \code{with_flair}
#' Creates an object of the class \code{decorated}
#'
#' @param text A string, presumably representing R code.
#' @param ... Any number of default chunk options to override.
#'
#'
#' @return A \code{with_flair} object.
#' @return A \code{decorated} object.
#'
#' @seealso \code{\link{flair}}
#'
Expand Down Expand Up @@ -51,7 +51,7 @@ decorate_code <- function(text, ...) {
if (!is.null(my_opts$flair) && !my_opts$flair) {

placeholder <- list(NULL)
attr(placeholder, "class") = "with_flair"
attr(placeholder, "class") = "decorated"

return(placeholder)

Expand All @@ -74,10 +74,12 @@ decorate_code <- function(text, ...) {
# convert knitted string to a list with sources separate from output
knitted <- knitted %>% src_to_list()

attr(knitted, "class") <- "with_flair"
attr(knitted, "class") <- "decorated"

attr(knitted, "orig_code_text") <- text

attr(knitted, "chunk_name") <- NA

return(knitted)

}
Expand Down
20 changes: 10 additions & 10 deletions R/with_flair.R → R/decorated-print.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
#' Create a \code{with_flair} object
#' Create a \code{decorated} object
#'
#' (The preferred function is \code{\link{decorate}})
#'
#' @param x A text object or code chunk label
#'
#' @return An object of class \code{with_flair}
#' @return An object of class \code{decorated}
#'
#' @export
with_flair <- function(x) {
decorated <- function(x) {

decorate(x)

}

#' S3 method for knitting a \code{with_flair} object
#' S3 method for knitting a \code{decorated} object
#'
#' @param x A \code{with_flair} object.
#' @param x A \code{decorated} object.
#' @param ... Other \code{knit_print} options
#'
#' @return "as-is" html output, to be rendered when knitted
#'
#' @importFrom purrr map
#'
#' @export
knit_print.with_flair <- function(x, ...) {
knit_print.decorated <- function(x, ...) {

get_doc_type <- purrr::safely(rmarkdown::all_output_formats)(knitr::current_input())

Expand All @@ -48,7 +48,7 @@ knit_print.with_flair <- function(x, ...) {

}

#' Helper for \code{knit_print.with_flair}
#' Helper for \code{knit_print.decorated}
#' @param x Text of source code.
#' @param doc_type Document type to knit to.
#'
Expand Down Expand Up @@ -118,16 +118,16 @@ prep_source <- function(x, doc_type = "unknown") {
}


#' When run interactively, a \code{with_flair} object should preview the
#' When run interactively, a \code{decorated} object should preview the
#' flaired source code in the viewer pane. (Only if in RStudio.)
#'
#' @param x A \code{with_flair} object.
#' @param x A \code{decorated} object.
#' @param ... Other \code{print} options
#'
#' @return None
#'
#' @export
print.with_flair <- function(x, ...) {
print.decorated <- function(x, ...) {

editorIsOpen <- tryCatch({
rstudioapi::getSourceEditorContext()
Expand Down
8 changes: 4 additions & 4 deletions R/mask.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Blanks out part of the string
#'
#' @param x A string object or \code{\link{decorate_code}} object.
#' @param x A string object or \code{\link{decorated}} object.
#' @param pattern A pattern to match
#' @param before Custom preceding html tag
#' @param after Custom ending html tag
Expand All @@ -23,12 +23,12 @@ mask_rx <- function(x, pattern,
UseMethod("mask_rx")
}

#' S3 method for \code{\link{with_flair}} objects
#' S3 method for \code{\link{decorated}} objects
#'
#' @importFrom purrr map
#' @rdname mask
#' @export
mask_rx.with_flair = function(x, pattern,
mask_rx.decorated = function(x, pattern,
before = NULL, after = NULL,
...) {

Expand All @@ -43,7 +43,7 @@ mask_rx.with_flair = function(x, pattern,
x[where_sources] <- purrr::map(x[where_sources],
function(x) structure(list(src = x), class = "source"))

attr(x, "class") <- "with_flair"
attr(x, "class") <- "decorated"

return(x)

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-flair.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test_that("flair_rx works with dots", {
})


test_that("flair_rx works for with_flair object", {
test_that("flair_rx works for decorated object", {

good_str = "ggplot(iris, aes(x = <span style='color:red;font-size:30px'>Sepal.Length</span>)) + geom_histogram()"

Expand All @@ -30,5 +30,5 @@ test_that("flair_rx works for with_flair object", {

expect_equal(test_result[[2]]$src, good_str)
expect_equal(class(test_result[[2]]), "source")
expect_equal(class(test_result), "with_flair")
expect_equal(class(test_result), "decorated")
})
4 changes: 2 additions & 2 deletions tests/testthat/test-flair_lines.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This is line 4.'
})


test_that("flair_lines works on with_flair objects", {
test_that("flair_lines works on decorated objects", {
test_wf <- decorate_code('ggplot(iris, aes(x = Sepal.Length)) +
geom_histogram()', eval = FALSE)

Expand All @@ -18,5 +18,5 @@ test_that("flair_lines works on with_flair objects", {

expect_equal(test_result[[2]]$src, good_str)
expect_equal(class(test_result[[2]]), "source")
expect_equal(class(test_result), "with_flair")
expect_equal(class(test_result), "decorated")
})

0 comments on commit bc0a195

Please sign in to comment.