Skip to content
Snippets Groups Projects
Commit 353a8d7d authored by David Dorchies's avatar David Dorchies :zany_face:
Browse files

feat(mermaid): add dir.dest argument

Refs #14
parent 306caca1
No related branches found
No related tags found
No related merge requests found
Pipeline #134792 passed
......@@ -12,7 +12,10 @@
#' @param diagram Diagram in mermaid markdown-like language or file (as a connection or file name) containing a diagram specification
#' @param theme Mermaid theme (See https://mermaid.js.org/config/theming.html#available-themes)
#' @param format Image format (either `"jpg"`, or `"png"`, or `"svg"`)
#' @param file.dest Path to the downloaded image
#' @param dir.dest Destination folder for the downloaded image. This parameter is
#' ignored if `file.dest` contains a folder path.
#' @param file.dest Path to the downloaded image. It's combined with `dir.dest`
#' if it only contains the name of the file without a folder path.
#' @param link Link generated by [mermaid_gen_link]
#' @param server URL of the server used to generate the link
#'
......@@ -35,9 +38,11 @@
mermaid <- function(diagram,
format = "png",
theme = "default",
file.dest = file.path(tempdir(),
paste0(rlang::hash(link), ".", format)),
dir.dest = tempdir(),
file.dest = paste0(rlang::hash(link), ".", format),
link = mermaid_gen_link(diagram, theme = theme, format = format)) {
if (!dir.exists(dir.dest)) dir.create(dir.dest, recursive = TRUE, showWarnings = FALSE)
if (dirname(file.dest) == ".") file.dest <- file.path(dir.dest, file.dest)
if (!file.exists(file.dest)) {
download.file(link, file.dest, quiet = TRUE, mode = "wb")
}
......
......@@ -9,7 +9,8 @@ mermaid(
diagram,
format = "png",
theme = "default",
file.dest = file.path(tempdir(), paste0(rlang::hash(link), ".", format)),
dir.dest = tempdir(),
file.dest = paste0(rlang::hash(link), ".", format),
link = mermaid_gen_link(diagram, theme = theme, format = format)
)
......@@ -27,7 +28,11 @@ mermaid_gen_link(
\item{theme}{Mermaid theme (See https://mermaid.js.org/config/theming.html#available-themes)}
\item{file.dest}{Path to the downloaded image}
\item{dir.dest}{Destination folder for the downloaded image. This parameter is
ignored if \code{file.dest} contains a folder path.}
\item{file.dest}{Path to the downloaded image. It's combined with \code{dir.dest}
if it only contains the name of the file without a folder path.}
\item{link}{Link generated by \link{mermaid_gen_link}}
......
......@@ -15,5 +15,15 @@ test_that("mermaid_gen_link: conversion of mmd to pako works", {
test_that("mermaid returns a file", {
f <- mermaid(diagram)
file.dest <- basename(f)
expect_true(file.exists(f))
f <- mermaid(diagram, file.dest = "toto.png")
expect_equal(f, file.path(tempdir(), "toto.png"))
expect_true(file.exists(f))
f <- mermaid(diagram, dir.dest = file.path(tempdir(), "try-me"))
expect_equal(f, file.path(tempdir(), "try-me", file.dest))
expect_true(file.exists(f))
f <- mermaid(diagram, dir.dest = file.path(tempdir(), "try-me"), file.dest = "toto.png")
expect_equal(f, file.path(tempdir(), "try-me", "toto.png"))
expect_true(file.exists(f))
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment