Skip to content

Commit

Permalink
Doesn't crash if package contains custom macros
Browse files Browse the repository at this point in the history
  • Loading branch information
eliocamp committed Nov 5, 2024
1 parent f180d18 commit 5ae98a1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
5 changes: 4 additions & 1 deletion R/module-create.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ i18n_module_create <- function(module_name = NULL,

rd_files <- list.files(file.path(package_path, "man"), pattern = "*.Rd", full.names = TRUE)

i18n_translation_templates(rd_files, file.path(module_path, "translations"))
macros <- tools::loadPkgRdMacros(package_path)

i18n_translation_templates(rd_files, file.path(module_path, "translations"),
macros = macros)

for (file in rd_files) {
file.copy(file, file.path(module_path, "man_original", basename(file)))
Expand Down
3 changes: 3 additions & 0 deletions R/rd-flatten.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ remove_newlines <- function(x) {

to_text <- function(x) {
tag <- attr(x, "Rd_tag")
if (length(x) == 0) {
return("")
}
if (is.character(x) || !is.null(tag) && !startsWith(tag, "\\")) {
return(x[[1]])
}
Expand Down
15 changes: 10 additions & 5 deletions R/translation-templates.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,35 @@
#' @param rd_files character vector of rd files.
#' @param folder folder where to save the templates. Will be created if it
#' doesn't exist.
#' @param macros a set of macros definitions.
#'
#' @examples
#' rd_files <- system.file("extdata", "periodic.Rd", package = "rhelpi18n")
#' template_folder <- tempdir()
#' i18n_translation_templates(rd_files, template_folder)
#'
#' @export
i18n_translation_templates <- function(rd_files, folder) {
i18n_translation_templates <- function(rd_files, folder, macros = NULL) {
invalid_files <- !file.exists(rd_files)

if (any(invalid_files)) {
stop("Some files don't exist")
}

macros <- macros %||% default_macros()

dir.create(folder, showWarnings = FALSE, recursive = TRUE)

base_names <- tools::file_path_sans_ext(basename(rd_files))
template_files <- file.path(folder, paste0(base_names, ".yaml"))

mapply(i18n_translation_template, rd_files, template_files)
mapply(i18n_translation_template, rd_files, template_files, MoreArgs = list(macros = macros))
}

i18n_translation_template <- function(rd_file, template_file) {
rd_parsed <- tools::parse_Rd(rd_file)
default_macros <- function() file.path(R.home("share"), "Rd", "macros", "system.Rd")

i18n_translation_template <- function(rd_file, template_file,
macros = default_macros()) {
rd_parsed <- tools::parse_Rd(rd_file, macros = macros)
rd_flatten <- rd_flatten(rd_parsed)
rd_flat_write(rd_flatten, template_file)
template_file
Expand Down
4 changes: 3 additions & 1 deletion man/i18n_translation_templates.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5ae98a1

Please sign in to comment.