Skip to content

Commit

Permalink
Tests, minor doc changes, readme
Browse files Browse the repository at this point in the history
- Add print method so it's not so obtuse to look at the output of div and span functions
- Add test for mdapply
- Minor refactor of with_*
- Use new print method in README
  • Loading branch information
ElianHugh committed Oct 23, 2023
1 parent 531cb88 commit 0fed7c8
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 56 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
^LICENSE\.md$
^\.lintr$
^codecov\.yml$
^vignettes/articles$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ po/*~

# RStudio Connect folder
rsconnect/
inst/doc
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ RoxygenNote: 7.2.3.9000
Imports:
rlang
Suggests:
knitr,
rmarkdown,
testthat (>= 3.0.0)
Config/testthat/edition: 3
URL: https://github.com/ElianHugh/quartools
BugReports: https://github.com/ElianHugh/quartools/issues
VignetteBuilder: knitr
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(print,quarto_block)
export(as_markdown)
export(div)
export(mdapply)
Expand Down
4 changes: 2 additions & 2 deletions R/elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ div <- function(..., attr = NULL) {

structure(
sprintf(template_string, attribs, content),
class = "knit_asis"
class = c("knit_asis", "quarto_block")
)
}

Expand Down Expand Up @@ -58,6 +58,6 @@ span <- function(..., attr = NULL) {

structure(
sprintf(template_string, content, attribs),
class = "knit_asis"
class = c("knit_asis", "quarto_block")
)
}
2 changes: 1 addition & 1 deletion R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
as_markdown <- function(x, collapse = "") {
structure(
paste0(x, collapse = collapse),
class = "knit_asis"
class = c("knit_asis", "quarto_block")
)
}

Expand Down
4 changes: 4 additions & 0 deletions R/print.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#' @export
print.quarto_block <- function(x, ...) {
cat(x, sep = "\n")
}
16 changes: 5 additions & 11 deletions R/with.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' See [the Quarto documentation for a full list
#' of available extension options](https://quarto.org/docs/authoring/article-layout.html#available-columns).
#' @param outset should the outset affix be applied to the column class?
#' @param extension affix class to apply to column class
#' @param extension affix to apply to column class
#' @inheritParams div
#' @export
#' @examples
Expand All @@ -16,9 +16,7 @@ with_body_column <- function(..., attr = NULL, outset = FALSE, extension = NULL)
check_extension_arg(extension, c("left", "right"))
outset_string <- ifelse(isTRUE(outset), "-outset", "")
extension_string <- ifelse(!is.null(extension), paste0("-", extension), "")
cls <- ".column-body%s%s" |>
sprintf(outset_string, extension_string)

cls <- sprintf(".column-body%s%s", outset_string, extension_string)
div(..., attr = c(cls, attr))
}

Expand All @@ -27,9 +25,7 @@ with_body_column <- function(..., attr = NULL, outset = FALSE, extension = NULL)
with_page_column <- function(..., attr = NULL, extension = NULL) {
check_extension_arg(extension, c("left", "right"))
extension_string <- ifelse(!is.null(extension), paste0("-", extension), "")
cls <- ".column-page%s" |>
sprintf(extension_string)

cls <- sprintf(".column-page%s", extension_string)
div(..., attr = c(cls, attr))
}

Expand All @@ -38,8 +34,7 @@ with_page_column <- function(..., attr = NULL, extension = NULL) {
with_screen_inset_column <- function(..., attr = NULL, extension = NULL) {
check_extension_arg(extension, c("left", "right", "shaded"))
extension_string <- ifelse(!is.null(extension), paste0("-", extension), "")
cls <- ".column-screen-inset%s" |>
sprintf(extension_string)
cls <- sprintf(".column-screen-inset%s", extension_string)
div(..., attr = c(cls, attr))
}

Expand All @@ -48,8 +43,7 @@ with_screen_inset_column <- function(..., attr = NULL, extension = NULL) {
with_screen_column <- function(..., attr = NULL, extension = NULL) {
check_extension_arg(extension, c("left", "right"))
extension_string <- ifelse(!is.null(extension), paste0("-", extension), "")
cls <- ".column-screen%s" |>
sprintf(extension_string)
cls <- sprintf(".column-screen%s", extension_string)
div(..., attr = c(cls, attr))
}

Expand Down
61 changes: 28 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,24 @@ devtools::install_github("ElianHugh/quartools")

### My First Div

``` r
library(quartools)
div(
"The simplest way to create a div block element with quartools is via the `div` function.",
"Note that the chunk configuration of `results: asis` is not necessary."
)
```

<div>

The simplest way to create a div block element with quartools is via the
`div` function.

Note that the chunk configuration of `results: asis` is not necessary.

</div>
`div` function. Note that the chunk configuration of `results: asis` is
not necessary.

``` r
div(
"It is also possible to supply attributes to the div block element via the `attr` parameter.",
attr = ".callout-tip"
)
) |> print()
```

<div>

> **Tip**
>
> It is also possible to supply attributes to the div block element via
> the `attr` parameter.

</div>
:::{.callout-tip}

It is also possible to supply attributes to the div block element via the `attr` parameter.

:::

### Apply

Expand All @@ -89,24 +75,33 @@ apply”) which allows for directly printing the result as valid markdown
content.

``` r
my_favourite_letters <- letters[1L:3L]
mdapply(my_favourite_letters, div)
input <- list(
list(type = "tip", msg = "a tip"),
list(type = "warning", msg = "a warning"),
list(type = "message", msg = "a message")
)
mdapply(input, function(x) div(x$msg, attr = sprintf(".callout-%s", x$type))) |>
print()
```

<div>

a

</div>
:::{.callout-tip}

a tip

:::


:::{.callout-warning}

<div>
a warning

b
:::

</div>

<div>
:::{.callout-message}

c
a message

</div>
:::
17 changes: 10 additions & 7 deletions README.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,30 @@ devtools::install_github("ElianHugh/quartools")

### My First Div

The simplest way to create a div block element with quartools is via the `div` function. Note that the chunk configuration of `results: asis` is not necessary.

```{r example}
#| echo: false
library(quartools)
div(
"The simplest way to create a div block element with quartools is via the `div` function.",
"Note that the chunk configuration of `results: asis` is not necessary."
)
```

```{r}
div(
"It is also possible to supply attributes to the div block element via the `attr` parameter.",
attr = ".callout-tip"
)
) |> print()
```

### Apply

We can leverage the `apply` family of functions ability to loop over list elements to simplify creating many divs at once. Normally this would require a workaround to allow for printing directly as markdown content, but _quartools_ exports the `mdapply` function (read: "markdown apply") which allows for directly printing the result as valid markdown content.

```{r}
my_favourite_letters <- letters[1L:3L]
mdapply(my_favourite_letters, div)
input <- list(
list(type = "tip", msg = "a tip"),
list(type = "warning", msg = "a warning"),
list(type = "message", msg = "a message")
)
mdapply(input, function(x) div(x$msg, attr = sprintf(".callout-%s", x$type))) |>
print()
```
2 changes: 1 addition & 1 deletion man/page-layout.Rd

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

17 changes: 17 additions & 0 deletions tests/testthat/test_misc.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

test_that("mdapply works", {
input <- list(
list(type = "tip", msg = "a tip"),
list(type = "warning", msg = "a warning"),
list(type = "message", msg = "a message")
)
res <- mdapply(
input,
function(x) {
div(x$msg, attr = sprintf(".callout-%s", x$type))
}
)
expect_true(
res == "\n\n:::{.callout-tip}\n\na tip\n\n:::\n\n\n:::{.callout-warning}\n\na warning\n\n:::\n\n\n:::{.callout-message}\n\na message\n\n:::\n" # nolint: line_length_linter.
)
})
1 change: 0 additions & 1 deletion tests/testthat/test_with.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ test_that("with_*() disallows invalid extensions", {
expect_error(eval(x))
})
})

2 changes: 2 additions & 0 deletions vignettes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.html
*.R

0 comments on commit 0fed7c8

Please sign in to comment.