From bba7992e070b9e2dda7b6ff189769f0ccb191ab7 Mon Sep 17 00:00:00 2001 From: David Dorchies <david.dorchies@inrae.fr> Date: Sat, 2 Mar 2024 11:43:03 +0100 Subject: [PATCH] fix: missing babel language support dependency Fix #21 --- R/add_report.R | 21 +++++++++------------ R/render_report.R | 8 +++++++- R/utils.R | 10 ++++++++++ 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/R/add_report.R b/R/add_report.R index b037202..c202398 100644 --- a/R/add_report.R +++ b/R/add_report.R @@ -40,12 +40,14 @@ add_report <- function(name, destpath ) + bookdown_config <- list( + language = list(ui = list( + chapter_name = paste0(stringr::str_to_title(i18n("chapter", lang)), " ") + )), + babel_lang = lang + ) cat( - yaml::as.yaml(list(language = list( - ui = list(chapter_name = paste0(stringr::str_to_title( - i18n("chapter", lang) - ), " ")) - ))), + yaml::as.yaml(bookdown_config), file = file.path(destpath, "_bookdown.yml"), append = TRUE ) @@ -68,13 +70,8 @@ add_report <- function(name, "% Add your own settings below to append or overwrite template settings") writeLines(tex_content, file.path(destpath, sprintf("%s.tex", x))) }) - if ("tinytex" %in% rownames(installed.packages())) { - # Install babel LaTeX package for the specified language - babel_pkg <- paste0("babel-", lang) - if (! babel_pkg %in% tinytex::tl_pkgs(only_installed = TRUE)) { - tinytex::tlmgr_install(babel_pkg) - } - } + + tinytex_install_babel_language_support(lang) return(destpath) } diff --git a/R/render_report.R b/R/render_report.R index a80b316..98fcd45 100644 --- a/R/render_report.R +++ b/R/render_report.R @@ -10,7 +10,13 @@ render_report <- function(input, stopifnot(output_format %in% c("bookdown::gitbook", "bookdown::pdf_book")) if (output_format == "bookdown::pdf_book") { - if (!requireNamespace("tinytex", quietly=TRUE)) install.packages("tinytex") + if (!requireNamespace("tinytex", quietly = TRUE)) install.packages("tinytex") + if (dir.exists(input)) { + babel_lang <- yaml::read_yaml(file.path(input, "_bookdown.yml"))$babel_lang + if (!is.null(babel_lang)) { + tinytex_install_babel_language_support(babel_lang) + } + } } message("output_format=", output_format) diff --git a/R/utils.R b/R/utils.R index f4865bc..74dea29 100644 --- a/R/utils.R +++ b/R/utils.R @@ -53,3 +53,13 @@ purge_string <- function(x, replacement = " ") { from = "UTF-8", to = "ASCII//TRANSLIT") } + +tinytex_install_babel_language_support <- function(lang) { + if ("tinytex" %in% rownames(installed.packages())) { + # Install babel LaTeX package for the specified language + babel_pkg <- paste0("babel-", lang) + if (! babel_pkg %in% tinytex::tl_pkgs(only_installed = TRUE)) { + tinytex::tlmgr_install(babel_pkg) + } + } +} -- GitLab