Skip to content

Commit

Permalink
Merge pull request #3 from mlverse/updates
Browse files Browse the repository at this point in the history
Cleans up functions
  • Loading branch information
edgararuiz authored Dec 11, 2024
2 parents f18fc0e + b815455 commit 005cd82
Show file tree
Hide file tree
Showing 20 changed files with 478 additions and 336 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: lang
Title: Using an Large Language Model of your choice, it translates the R
help documentation to a specific language
Version: 0.0.0.9001
Version: 0.0.0.9002
Authors@R:
person("Edgar", "Ruiz", , "[email protected]", role = c("aut", "cre"))
Description: Use an 'LLM' to translate a function's help documentation on-the-fly.
Expand Down
3 changes: 1 addition & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ S3method(print,lang_topic)
export(lang_help)
export(llm_use)
export(process_roxygen)
export(process_roxygen_folder)
export(to_iso639)
export(translate_roxygen)
export(translate_roxygen_file)
import(cli)
import(fs)
import(glue)
Expand Down
37 changes: 37 additions & 0 deletions R/iso-639.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#' Convert to and from ISO 639 language code
#'
#' @param lang Name of the language to be converted
#' @param silent Flag to indicate if the function should return a message
#' with the result. If there is no match, it will be an error message
#' @export
to_iso639 <- function(lang, silent = TRUE) {
codes <- readRDS(system.file("iso/codes.rds", package = "lang"))
lang <- tolower(lang)
match <- codes[codes$name == lang, "code"]
if (nrow(match) > 0) {
out <- as.character(match)[[1]]
if (!silent) {
cli_alert_success("'{lang}' converted to ISO 639 code: '{out}'")
}
} else {
if (!silent) {
cli_abort("'{lang}' could not be matched to an ISO 639 code")
}
out <- NULL
}
out
}

#' @rdname to_iso639
#' @param iso The two-letter ISO 639 code to search
from_iso639 <- function(iso) {
codes <- readRDS(system.file("iso/codes.rds", package = "lang"))
iso <- tolower(iso)
match <- codes[codes$code == iso, ]
if (nrow(match) > 0) {
out <- match$name
} else {
out <- NULL
}
out
}
8 changes: 3 additions & 5 deletions R/lang-help.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@ rd_inst <- function(topic, package, lang) {
} else if (substr(lang, 3, 3) == "_") {
folder <- substr(lang, 1, 2)
} else {
codes <- readRDS(system.file("iso/codes.rds", package = "lang"))
lang <- tolower(lang)
match <- codes[codes$name == lang, "code"]
if (nrow(match) > 0) {
folder <- as.character(match)[[1]]
code <- to_iso639(lang)
if (!is.null(code)) {
folder <- code[[1]]
}
}
if (!is.null(folder)) {
Expand Down
75 changes: 44 additions & 31 deletions R/process-roxygen.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
#' Creates the Rd files based on translated Roxygen scripts
#' @param folder Source sub-folder where the source Roxygen R scripts are
#' @param source_folder Base source folder where the different translations are located.
#' Defaults to 'man-lang'.
#' @param source_sub_folder Source sub-folder where the source Roxygen R scripts
#' are. Defaults to NULL. If left null, all of the sub-folders in the
#' `source_folder` will be processed
#' @param source_folder Base source folder where the different translations are
#' located. Defaults to 'man-lang'.
#' @param target_folder Base target folder where the different translations will be
#' located. Defaults to 'inst/man-lang'
#' @param r_folder Source of the original R scripts. Only used to see if the
#' Roxygen documentation is different from what is capture in the `source_folder`
#' @param pkg_path The path to the package
#' @returns Multiple Rd files based on the source R scripts
#' @export
process_roxygen_folder <- function(
folder,
process_roxygen <- function(
source_sub_folder = NULL,
source_folder = "man-lang",
target_folder = "inst/man-lang",
r_folder = "R",
pkg_path = ".") {
if (!dir_exists(path(pkg_path, source_folder))) {
cli_abort("Source folder: '{source_folder}', is not found")
}
if (is.null(source_sub_folder)) {
compare_man_lang(
r_folder = r_folder,
source_folder = source_folder,
pkg_path = pkg_path
)
sub_folders <- dir_ls(path(pkg_path, source_folder), type = "directory")
sub_folders <- path_file(sub_folders)
} else {
sub_folders <- source_sub_folder
}
for (source_sub_folder in sub_folders) {
process_roxygen_folder(
source_sub_folder = source_sub_folder,
source_folder = source_folder,
target_folder = target_folder,
pkg_path = pkg_path
)
}
}

process_roxygen_folder <- function(
source_sub_folder = NULL,
source_folder = "man-lang",
target_folder = "inst/man-lang",
pkg_path = ".") {
Expand All @@ -31,22 +62,27 @@ process_roxygen_folder <- function(
}
# Copies content of the translated script to the R folder
# of the temp copy
source_path <- path(pkg_path, source_folder, folder)
source_path <- path(pkg_path, source_folder, source_sub_folder)
full_source <- dir_ls(path(source_path), recurse = TRUE, glob = "*.R")
file_copy(
path = full_source,
new_path = path(copy_path, "R"),
overwrite = TRUE
)
cli_alert_info(
paste0(
"Creating Rd files from {path_rel(source_path)}",
" ({to_title(from_iso639(source_sub_folder)[[1]]) })"
)
)
# Uses `callr` to run roxygenize, mainly to avoid the messages from roxygen2
cli_h3("Creating Rd files for '{folder}'")
callr::r_safe(
func = function(x) roxygen2::roxygenize(x, roclets = "rd"),
args = list(copy_path)
)
# Copies the new contents in 'man' from the temp copy
# into target folder, under the language's sub-folder
target_path <- path(pkg_path, target_folder, folder)
target_path <- path(pkg_path, target_folder, source_sub_folder)
if (dir_exists(target_path)) {
dir_delete(target_path)
}
Expand All @@ -64,29 +100,6 @@ process_roxygen_folder <- function(
invisible()
}

#' @rdname process_roxygen_folder
#' @export
process_roxygen <- function(
source_folder = "man-lang",
target_folder = "inst/man-lang",
r_folder = "R",
pkg_path = ".") {
sub_folders <- dir_ls(path(pkg_path, source_folder), type = "directory")
compare_man_lang(
r_folder = r_folder,
source_folder = source_folder,
pkg_path = pkg_path
)
for (folder in sub_folders) {
process_roxygen_folder(
folder = path_file(folder),
source_folder = source_folder,
target_folder = target_folder,
pkg_path = pkg_path
)
}
}

compare_man_lang <- function(
folder = NULL,
r_folder = "R",
Expand Down
24 changes: 24 additions & 0 deletions R/roxy-comments.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
roxy_comments <- function(x) {
script_contents <- readLines(x)
roxy_comment <- substr(script_contents, 1, 2) == "#'"
just_roxy <- script_contents[roxy_comment]
just_roxy <- just_roxy[just_roxy != "#'"]

if (length(just_roxy) == 0) {
return(NULL)
} else {
just_roxy <- paste0("#-", just_roxy)
no_exports <- !any(grepl("#' @export", just_roxy))
no_name <- !any(grepl("#' @name", just_roxy))
if (no_exports && no_name) {
return(NULL)
}
}
just_roxy
}

roxy_existing <- function(x) {
script_contents <- readLines(x)
roxy_comment <- substr(script_contents, 1, 4) == "#-#'"
script_contents[roxy_comment]
}
Loading

0 comments on commit 005cd82

Please sign in to comment.