Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nenuial/issue76 #77

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,35 @@ 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_folder_exists <- fs::dir_exists(
file.path(temporary_directory, project_name, "_freeze")
)

if (freeze_folder_exists) {
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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should all of this go into a well-named helper function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow. You're suggesting a helper function for the copy and move/delete part, or the entire logic around freeze folders (L205-L224)?

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)
}

config[["lang"]] <- language_code

config[[type]][["title"]] <- config[[sprintf("title-%s", language_code)]] %||% # nolint: line_length_linter
Expand Down
74 changes: 74 additions & 0 deletions tests/testthat/test-render.R
Original file line number Diff line number Diff line change
Expand Up @@ -729,3 +729,77 @@ test_that("render_website() works - sidebar in language profile", {

expect_identical(sidebar_fr_id, "quarto-sidebar")
})

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)
})
Loading