Skip to content

Commit 22cf90b

Browse files
committed
feat: correct img path when extracted from html
- embedded html code in qmd now behave like other img
1 parent 85a49d4 commit 22cf90b

File tree

8 files changed

+117
-7
lines changed

8 files changed

+117
-7
lines changed

DESCRIPTION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ Description: Compile n .qmd (under format revealjs) to one single html
1111
License: MIT + file LICENSE
1212
Imports:
1313
cli,
14+
dplyr,
1415
furrr,
1516
future,
1617
htmltools,
1718
progressr,
1819
purrr,
1920
quarto,
2021
rvest,
22+
stats,
2123
tools,
2224
utils,
2325
yaml

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
export(clean_rendering_files)
44
export(compile_qmd_course)
5+
export(copy_img_and_edit_path)
56
export(create_template_html)
67
export(extract_html_slides)
78
export(render_single_qmd)
89
importFrom(cli,cli_alert_danger)
910
importFrom(cli,cli_alert_info)
1011
importFrom(cli,cli_alert_success)
1112
importFrom(cli,cli_alert_warning)
13+
importFrom(dplyr,filter)
1214
importFrom(furrr,furrr_options)
1315
importFrom(furrr,future_map_lgl)
1416
importFrom(future,plan)
@@ -19,14 +21,18 @@ importFrom(htmltools,save_html)
1921
importFrom(progressr,handlers)
2022
importFrom(progressr,progressor)
2123
importFrom(progressr,with_progress)
24+
importFrom(purrr,discard)
2225
importFrom(purrr,map)
2326
importFrom(purrr,map_lgl)
27+
importFrom(purrr,walk)
28+
importFrom(purrr,walk2)
2429
importFrom(quarto,quarto_inspect)
2530
importFrom(quarto,quarto_render)
2631
importFrom(rvest,html_attr)
2732
importFrom(rvest,html_children)
2833
importFrom(rvest,html_elements)
2934
importFrom(rvest,read_html)
35+
importFrom(stats,setNames)
3036
importFrom(tools,file_ext)
3137
importFrom(utils,download.file)
3238
importFrom(utils,unzip)

R/compile_qmd_course.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
#' @param mail character. Mail of the trainer
1414
#' @param phone character. Phone number of the trainer
1515
#' @param quiet logical. Output info in user console
16+
#' @param fix_img_path logical. If image path are present as raw html inside files,
17+
#' use this option to correctly edit their path.
1618
#'
1719
#' @importFrom tools file_ext
1820
#' @importFrom htmltools htmlTemplate renderDocument save_html
1921
#' @importFrom furrr future_map_lgl furrr_options
22+
#' @importFrom purrr walk
2023
#' @importFrom future plan
2124
#' @importFrom cli cli_alert_info cli_alert_warning cli_alert_success
2225
#' @importFrom progressr handlers progressor with_progress
@@ -81,7 +84,8 @@ compile_qmd_course <- function(
8184
trainer = "ThinkR",
8285
mail = "thinkr.fr",
8386
phone = "+33 0 00 00 00 00",
84-
quiet = TRUE
87+
quiet = TRUE,
88+
fix_img_path = FALSE
8589
) {
8690
# check paths
8791
not_all_files_are_qmd <- any(
@@ -163,9 +167,16 @@ compile_qmd_course <- function(
163167
}
164168
}
165169

166-
# read html and extract slides elements
170+
# correct remaining img path
167171
vec_html_path <- gsub("\\.qmd", "\\.html", vec_qmd_path)
168172

173+
if (fix_img_path) {
174+
walk(.x = vec_html_path, .f = \(x) {
175+
copy_img_and_edit_path(html_path = x, img_root_dir = img_root_dir)
176+
})
177+
}
178+
179+
# read html and extract slides elements
169180
html_content <- extract_html_slides(
170181
vec_html_path = vec_html_path
171182
)

R/copy_img_and_edit_path.R

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#' Copy img to root img directory and edit path in html
2+
#'
3+
#' @param html_path character. Path to html file.
4+
#' @param img_root_dir character. Path to img root dir
5+
#'
6+
#' @importFrom purrr map discard walk2
7+
#' @importFrom rvest html_attr read_html html_elements
8+
#' @importFrom stats setNames
9+
#' @importFrom dplyr filter
10+
#'
11+
#' @return None. Side effet : edit img path in html.
12+
#' @export
13+
copy_img_and_edit_path <- function(
14+
html_path,
15+
img_root_dir
16+
){
17+
# set image sub-folder name
18+
chapter <- dirname(html_path)
19+
20+
img_dir <- file.path(
21+
img_root_dir,
22+
paste0(basename(chapter), "_img")
23+
)
24+
25+
# read html image path
26+
img_list <- html_path |>
27+
read_html() |>
28+
html_elements("img")
29+
30+
img_df <- map(
31+
.x = c("src", "data-src", "class"),
32+
.f = \(x){img_list |> html_attr(x)}
33+
) |>
34+
setNames(c("src", "data", "img_class")) |>
35+
data.frame()
36+
37+
img_path <- discard(unique(c(img_df$src, img_df$data)), is.na)
38+
39+
# correct the ones not in img_root_dir
40+
img_correct_path <- grepl(pattern = paste0("^", img_dir), x = img_path)
41+
img_to_fix <- img_path[!img_correct_path]
42+
43+
if (length(img_to_fix) > 0){
44+
45+
html_content <- readLines(html_path, warn = FALSE)
46+
47+
walk2(
48+
.x = file.path(dirname(html_path), img_to_fix),
49+
.y = file.path(dirname(html_path), img_dir, img_to_fix),
50+
.f = \(x, y){
51+
dir.create(dirname(y), recursive = TRUE, showWarnings = FALSE)
52+
file.copy(from = x, to = y, overwrite = TRUE)
53+
})
54+
55+
html_content <- gsub(
56+
pattern = paste0('"(',paste0(img_to_fix, collapse = "|"), ')"'),
57+
replacement = paste0('"', img_dir, "\\/", "\\1", '"'),
58+
x = html_content
59+
)
60+
61+
writeLines(text = html_content, con = html_path)
62+
}
63+
64+
}

inst/courses/M01/M01S02/C01-qmd3_for_test.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ Slide with chapter name and png file name identical to a slide from the first ch
1818

1919
This should be a {ggplot2} image.
2020

21-
![](img/logo_1.png)
21+
<img src="img/logo_1.png">

man/compile_qmd_course.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/copy_img_and_edit_path.Rd

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-compile_qmd_course.R

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ test_that("compile_qmd_course renders all input courses inside a unique html out
5959
html_output <- compile_qmd_course(
6060
vec_qmd_path = qmds,
6161
output_dir = temp_dir,
62-
output_html = "complete_course.html"
62+
output_html = "complete_course.html",
63+
fix_img_path = TRUE
6364
)
6465

6566
file_present_after_rendering <- list.files(
@@ -127,6 +128,9 @@ test_that("compile_qmd_course renders all input courses inside a unique html out
127128
rvest::html_text()
128129
})
129130

131+
#' @description test that img path from embedded html is correct
132+
expect_true(grepl("complete_course_img/M01S02_img/img/logo_1.png", slide_content))
133+
130134
#' @description test html content is present
131135
expect_snapshot(x = slide_content_by_cat)
132136

@@ -147,8 +151,8 @@ test_that("compile_qmd_course HTML preview looks ok", {
147151
"\nYou have 26 slides, all with footer and logo ?",
148152
"\nMain titles are centered, all titles are orange ?",
149153
"\nImage and code chunk appear properly sized and colored ?",
150-
"\nGraphics are visible in slides 19 and 20 ?",
151-
"\nTable is visible in slide 21 ?"
154+
"\nGraphics are visible in slides 23 and 24 ?",
155+
"\nTable is visible in slide 25 ?"
152156
)
153157

154158
answers <- sapply(

0 commit comments

Comments
 (0)