Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
fairify
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
A compter du 1er avril, attention à vos pipelines :
Nouvelles limitations de Docker Hub
Show more breadcrumbs
UMR G-EAU
fairify
Commits
306caca1
Commit
306caca1
authored
1 year ago
by
David Dorchies
Browse files
Options
Downloads
Patches
Plain Diff
feat(mermaid): improve cache management
Refs
#14
parent
7910db78
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Pipeline
#134177
passed
1 year ago
Stage: checks
Changes
3
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
R/mermaid.R
+11
-8
11 additions, 8 deletions
R/mermaid.R
man/mermaid.Rd
+13
-3
13 additions, 3 deletions
man/mermaid.Rd
tests/testthat/test-mermaid.R
+14
-2
14 additions, 2 deletions
tests/testthat/test-mermaid.R
with
38 additions
and
13 deletions
R/mermaid.R
+
11
−
8
View file @
306caca1
...
...
@@ -12,7 +12,9 @@
#' @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 Path to the downloaded image
#' @param file.dest Path to the downloaded image
#' @param link Link generated by [mermaid_gen_link]
#' @param server URL of the server used to generate the link
#'
#' @return The path to the downloaded image.
#' @export
...
...
@@ -33,12 +35,13 @@
mermaid
<-
function
(
diagram
,
format
=
"png"
,
theme
=
"default"
,
file
=
file.path
(
tempdir
(),
paste0
(
rlang
::
hash
(
diagram
),
"."
,
format
)))
{
if
(
!
file.exists
(
file
))
{
link
<-
mermaid_gen_link
(
diagram
,
theme
=
theme
,
format
=
format
)
download.file
(
link
,
file
,
quiet
=
TRUE
,
mode
=
"wb"
)
file.dest
=
file.path
(
tempdir
(),
paste0
(
rlang
::
hash
(
link
),
"."
,
format
)),
link
=
mermaid_gen_link
(
diagram
,
theme
=
theme
,
format
=
format
))
{
if
(
!
file.exists
(
file.dest
))
{
download.file
(
link
,
file.dest
,
quiet
=
TRUE
,
mode
=
"wb"
)
}
return
(
file
)
return
(
file
.dest
)
}
#' Compress data in pako format
...
...
@@ -65,7 +68,7 @@ return(compressed_data)
#' @rdname mermaid
#' @export
mermaid_gen_link
<-
function
(
diagram
,
theme
=
"default"
,
format
=
"png"
)
{
mermaid_gen_link
<-
function
(
diagram
,
theme
=
"default"
,
format
=
"png"
,
server
=
"https://mermaid.ink"
)
{
is_connection_or_file
<-
inherits
(
diagram
[
1
],
"connection"
)
||
file.exists
(
diagram
[
1
])
if
(
is_connection_or_file
)
{
...
...
@@ -80,7 +83,7 @@ mermaid_gen_link <- function(diagram, theme = "default", format = "png") {
deflated
<-
pako_deflate
(
jGraph
)
dEncode
=
gsub
(
"\n"
,
""
,
jsonlite
::
base64url_enc
(
deflated
))
mode
<-
ifelse
(
format
!=
"svg"
,
"img"
,
"svg"
)
link
=
sprintf
(
'https://mermaid.ink
/%s/pako:%s
'
,
mode
,
dEncode
)
link
=
sprintf
(
"%s
/%s/pako:%s
"
,
server
,
mode
,
dEncode
)
if
(
format
!=
"svg"
)
{
link
<-
paste0
(
link
,
"?type="
,
format
)
}
...
...
This diff is collapsed.
Click to expand it.
man/mermaid.Rd
+
13
−
3
View file @
306caca1
...
...
@@ -9,10 +9,16 @@ mermaid(
diagram,
format = "png",
theme = "default",
file = file.path(tempdir(), paste0(rlang::hash(diagram), ".", format))
file.dest = file.path(tempdir(), paste0(rlang::hash(link), ".", format)),
link = mermaid_gen_link(diagram, theme = theme, format = format)
)
mermaid_gen_link(diagram, theme = "default", format = "png")
mermaid_gen_link(
diagram,
theme = "default",
format = "png",
server = "https://mermaid.ink"
)
}
\arguments{
\item{diagram}{Diagram in mermaid markdown-like language or file (as a connection or file name) containing a diagram specification}
...
...
@@ -21,7 +27,11 @@ mermaid_gen_link(diagram, theme = "default", format = "png")
\item{theme}{Mermaid theme (See https://mermaid.js.org/config/theming.html#available-themes)}
\item{file}{Path to the downloaded image}
\item{file.dest}{Path to the downloaded image}
\item{link}{Link generated by \link{mermaid_gen_link}}
\item{server}{URL of the server used to generate the link}
}
\value{
The path to the downloaded image.
...
...
This diff is collapsed.
Click to expand it.
tests/testthat/test-mermaid.R
+
14
−
2
View file @
306caca1
test_that
(
"Conversion of mmd to pako works"
,
{
diagram
<-
"flowchart LR\n A --> B"
diagram
<-
"flowchart LR\n A --> B"
test_that
(
"mermaid_gen_link: conversion of mmd to pako works"
,
{
pako
<-
"https://mermaid.ink/img/pako:eNqrVkrOT0lVslJKy8kvT85ILCpR8AmKyVNQcFTQ1bVTcFLSUcpNLcpNzExRsqpWKslIzQUpTklNSyzNKVGqrQUAjIcUfg?type=png"
# multiline string
expect_equal
(
mermaid_gen_link
(
diagram
),
pako
)
# character vector
diagram
<-
strsplit
(
diagram
,
"\n"
)[[
1
]]
expect_equal
(
mermaid_gen_link
(
diagram
),
pako
)
# from file
f
<-
tempfile
()
writeLines
(
diagram
,
f
)
expect_equal
(
mermaid_gen_link
(
f
),
pako
)
})
test_that
(
"mermaid returns a file"
,
{
f
<-
mermaid
(
diagram
)
expect_true
(
file.exists
(
f
))
})
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment