Skip to content

Commit

Permalink
Minor doc changes
Browse files Browse the repository at this point in the history
- Minor elements refactoring
- extension tests for with_()*
- Minor edit to README
- Minor documentation changes
-
  • Loading branch information
ElianHugh committed Oct 21, 2023
1 parent f07e431 commit 531cb88
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 35 deletions.
38 changes: 18 additions & 20 deletions R/elements.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#' Create a quarto div element
#' @description
#' Create a div block element with optional attributes to assign to it. Dots
#' are collapsed into a single character vector with a newline between each element.
#' are collapsed into a single character vector with a newline between each
#' element.
#'
#' If attr is a vector with a length > 1, attributes will be combined into a
#' single character vector via [base::paste0].
#' If attr is a vector with a length > 1, attributes will be combined into
#' a single character vector via [base::paste0].
#' @param ... unnamed values that can be coerced to character vectors
#' @param attr values that can be coerced to character vectors
#' @examples
Expand All @@ -22,26 +23,25 @@
div <- function(..., attr = NULL) {
rlang::check_dots_unnamed()

out <- "\n\n:::{%s}\n\n%s\n\n:::\n"
args <- rlang::dots_list(...)
attr <- attr %||% "" |>
paste0(collapse = " ")
content <- args |>
paste0(collapse = "\n\n")
template_string <- "\n\n:::{%s}\n\n%s\n\n:::\n"
dots <- rlang::dots_list(...)
attribs <- paste0(attr %||% "", collapse = " ")
content <- paste0(dots, collapse = "\n\n")

structure(
sprintf(out, attr, content),
sprintf(template_string, attribs, content),
class = "knit_asis"
)
}

#' Create a quarto span element
#' @description
#' Create a span element with optional attributes to assign to it. Dots
#' are collapsed into a single character vector with a space between each element.
#' are collapsed into a single character vector with a space between
#' each element.
#'
#' If attr is a vector with a length > 1, attributes will be combined into a single
#' character vector via [base::paste0].
#' If attr is a vector with a length > 1, attributes will be combined into
#' a single character vector via [base::paste0].
#' @inheritParams div
#' @examples
#' span("Hello world!", attr = ".bold")
Expand All @@ -51,15 +51,13 @@ div <- function(..., attr = NULL) {
span <- function(..., attr = NULL) {
rlang::check_dots_unnamed()

out <- "[%s]{%s}"
args <- rlang::dots_list(...)
attr <- attr %||% "" |>
paste0(collapse = " ")
content <- args |>
paste0(collapse = " ")
template_string <- "[%s]{%s}"
dots <- rlang::dots_list(...)
attribs <- paste0(attr %||% "", collapse = " ")
content <- paste0(dots, collapse = " ")

structure(
sprintf(out, content, attr),
sprintf(template_string, content, attribs),
class = "knit_asis"
)
}
2 changes: 2 additions & 0 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#' the character vector as markdown content, rather than as an R value.
#' @param x object to convert to character vector
#' @inheritParams base::paste0
#' @examples
#' as_markdown(c("Hello world!"))
#' @return character vector of length 1
#' @export
as_markdown <- function(x, collapse = "") {
Expand Down
1 change: 1 addition & 0 deletions R/with.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' @export
#' @examples
#' with_body_column("Hello world!")
#' @return character vector of length 1
#' @rdname page-layout
with_body_column <- function(..., attr = NULL, outset = FALSE, extension = NULL) {
check_extension_arg(extension, c("left", "right"))
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ function calls. As *quartools* generates quarto-compliant markdown, and
## Why quartools?

At work, I ran into an issue where I was generating hundreds of
parameterised reports that would be similar with subtle differences in
content. I found myself leaning on R for programmatic markup creation,
which meant that I could have one master document that I worked on. My
prototype version (in other words, functions I threw together for work)
required a lot of constant chunk configuration, and wasn’t particularly
user-friendly nor elegant. *quartools* is a more streamlined version of
my prototype, with the added benefit of it requiring little to no setup
on the end user’s part.
parameterised reports that would require parts of the report to be
dynamically populated. I found myself leaning on R for programmatic
markup creation, which meant that I could have one master document that
I worked on. My prototype version (in other words, functions I threw
together for work) required a lot of constant chunk configuration, and
wasn’t particularly user-friendly nor elegant. *quartools* is a more
streamlined version of my prototype, with the added benefit of it
requiring little to no setup on the end user’s part.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion README.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ _quartools_ allows for the creation of quarto-compliant markdown via R function

## Why quartools?

At work, I ran into an issue where I was generating hundreds of parameterised reports that would be similar with subtle differences in content. I found myself leaning on R for programmatic markup creation, which meant that I could have one master document that I worked on. My prototype version (in other words, functions I threw together for work) required a lot of constant chunk configuration, and wasn't particularly user-friendly nor elegant. _quartools_ is a more streamlined version of my prototype, with the added benefit of it requiring little to no setup on the end user's part.
At work, I ran into an issue where I was generating hundreds of parameterised reports that would require parts of the report to be dynamically populated. I found myself leaning on R for programmatic markup creation, which meant that I could have one master document that I worked on. My prototype version (in other words, functions I threw together for work) required a lot of constant chunk configuration, and wasn't particularly user-friendly nor elegant. _quartools_ is a more streamlined version of my prototype, with the added benefit of it requiring little to no setup on the end user's part.


## Installation
Expand Down
3 changes: 3 additions & 0 deletions man/as_markdown.Rd

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

7 changes: 4 additions & 3 deletions man/div.Rd

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

3 changes: 3 additions & 0 deletions man/page-layout.Rd

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

7 changes: 4 additions & 3 deletions man/span.Rd

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

14 changes: 14 additions & 0 deletions tests/testthat/test_with.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,17 @@ test_that("with_*() output is as expected", {
expect_true(input[[i]] == expectations[[i]])
})
})

test_that("with_*() disallows invalid extensions", {
input <- list(
quote(with_body_column(extension = "shaded")),
quote(with_page_column(extension = "shaded")),
quote(with_screen_inset_column(extension = "bad")),
quote(with_screen_column(extension = "shaded")),
quote(with_margin_column(extension = "shaded"))
)
lapply(input, function(x) {
expect_error(eval(x))
})
})

0 comments on commit 531cb88

Please sign in to comment.