diff --git a/R/render.R b/R/render.R index 402fa2c..70944a9 100644 --- a/R/render.R +++ b/R/render.R @@ -197,6 +197,18 @@ render_quarto_lang <- function(language_code, path, output_dir, type) { config_path <- file.path(temporary_directory, project_name, "_quarto.yml") config <- read_yaml(config_path) + freeze_directory_exists <- fs::dir_exists( + file.path(temporary_directory, project_name, "_freeze") + ) + + if (freeze_directory_exists) { + filter_freeze_directory( + temporary_directory, + project_name, + language_code + ) + } + config[["lang"]] <- language_code config[[type]][["title"]] <- config[[sprintf("title-%s", language_code)]] %||% # nolint: line_length_linter @@ -280,6 +292,43 @@ render_quarto_lang <- function(language_code, path, output_dir, type) { } +#' Filter the freeze directory +#' +#' This function removes from freeze directory the files +#' that do not match the language code to avoid using the +#' wrong cache values for code computations. +#' +#' @param temporary_directory Temporary directory where the project is +#' @param project_name Name of the project directory +#' @param language_code The Language code for the current rendering +#' @dev +filter_freeze_directory <- function(temporary_directory, + project_name, + language_code) { + freeze_path <- fs::path(temporary_directory, project_name, "_freeze") + freeze_temp <- fs::path( + temporary_directory, + project_name, paste0("_freeze.", language_code) + ) + freeze_ls <- fs::dir_ls(freeze_path, recurse = TRUE) + + freeze_lang <- purrr::keep( + freeze_ls, \(x) grepl(paste0("\\.", language_code, "$"), x) + ) + freeze_dirs <- fs::path_rel(freeze_lang, start = freeze_path) + freeze_dirs <- gsub( + paste0(".", language_code), "", + freeze_dirs, fixed = TRUE + ) + + fs::dir_copy(freeze_lang, fs::path(freeze_temp, freeze_dirs)) + fs::dir_copy(fs::path(freeze_path, "site_libs"), freeze_temp) + + fs::dir_delete(freeze_path) + fs::dir_copy(freeze_temp, freeze_path) + fs::dir_delete(freeze_temp) +} + use_lang_chapter <- function(chapters_list, language_code, book_name, directory) { withr::local_dir(file.path(directory, book_name)) diff --git a/tests/testthat/test-render.R b/tests/testthat/test-render.R index 972fbd9..fc4f1be 100644 --- a/tests/testthat/test-render.R +++ b/tests/testthat/test-render.R @@ -729,3 +729,78 @@ test_that("render_website() works - sidebar in language profile", { expect_identical(sidebar_fr_id, "quarto-sidebar") }) + +# https://github.com/ropensci-review-tools/babelquarto/issues/76 +test_that("render_website() works - quarto freeze for languages is working", { + parent_dir <- withr::local_tempdir() + project_dir <- "blop" + + quarto_multilingual_website( + parent_dir = parent_dir, + project_dir = project_dir, + further_languages = "fr", + main_language = "en", + site_url = "https://ropensci.org" + ) + + config <- brio::read_lines(file.path(parent_dir, project_dir, "_quarto.yml")) + config <- c( + config, + "", + "execute:", + " freeze: true" + ) + brio::write_lines(config, file.path(parent_dir, project_dir, "_quarto.yml")) + + index <- brio::read_lines(file.path(parent_dir, project_dir, "index.qmd")) + index <- c( + index, + "", + "```{r}", + "print('Hello World!')", + "```" + ) + brio::write_lines(index, file.path(parent_dir, project_dir, "index.qmd")) + + index_fr <- brio::read_lines( + file.path(parent_dir, project_dir, "index.fr.qmd") + ) + index_fr <- c( + index_fr, + "", + "```{r}", + "print('Bonjour le monde!')", + "```" + ) + brio::write_lines( + index_fr, file.path(parent_dir, project_dir, "index.fr.qmd") + ) + + # Render with quarto to generate the _freeze folder + withr::with_dir( + parent_dir, + quarto::quarto_render(input = project_dir, as_job = FALSE) + ) + + withr::with_dir(parent_dir, render_website(project_dir)) + + index_html <- brio::read_lines( + file.path(parent_dir, project_dir, "_site", "index.html") + ) + hello_world_present <- any(grepl( + "Hello World!", + index_html, + fixed = TRUE + )) + expect_true(hello_world_present) + + index_html_fr <- brio::read_lines( + file.path(parent_dir, project_dir, "_site", "fr", "index.html") + ) + bonjour_monde_present <- any(grepl( + "Bonjour le monde!", + index_html_fr, + fixed = TRUE + )) + expect_true(bonjour_monde_present) +})