Skip to content

Commit 005cd82

Browse files
authored
Merge pull request #3 from mlverse/updates
Cleans up functions
2 parents f18fc0e + b815455 commit 005cd82

20 files changed

+478
-336
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: lang
22
Title: Using an Large Language Model of your choice, it translates the R
33
help documentation to a specific language
4-
Version: 0.0.0.9001
4+
Version: 0.0.0.9002
55
Authors@R:
66
person("Edgar", "Ruiz", , "[email protected]", role = c("aut", "cre"))
77
Description: Use an 'LLM' to translate a function's help documentation on-the-fly.

NAMESPACE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ S3method(print,lang_topic)
44
export(lang_help)
55
export(llm_use)
66
export(process_roxygen)
7-
export(process_roxygen_folder)
7+
export(to_iso639)
88
export(translate_roxygen)
9-
export(translate_roxygen_file)
109
import(cli)
1110
import(fs)
1211
import(glue)

R/iso-639.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#' Convert to and from ISO 639 language code
2+
#'
3+
#' @param lang Name of the language to be converted
4+
#' @param silent Flag to indicate if the function should return a message
5+
#' with the result. If there is no match, it will be an error message
6+
#' @export
7+
to_iso639 <- function(lang, silent = TRUE) {
8+
codes <- readRDS(system.file("iso/codes.rds", package = "lang"))
9+
lang <- tolower(lang)
10+
match <- codes[codes$name == lang, "code"]
11+
if (nrow(match) > 0) {
12+
out <- as.character(match)[[1]]
13+
if (!silent) {
14+
cli_alert_success("'{lang}' converted to ISO 639 code: '{out}'")
15+
}
16+
} else {
17+
if (!silent) {
18+
cli_abort("'{lang}' could not be matched to an ISO 639 code")
19+
}
20+
out <- NULL
21+
}
22+
out
23+
}
24+
25+
#' @rdname to_iso639
26+
#' @param iso The two-letter ISO 639 code to search
27+
from_iso639 <- function(iso) {
28+
codes <- readRDS(system.file("iso/codes.rds", package = "lang"))
29+
iso <- tolower(iso)
30+
match <- codes[codes$code == iso, ]
31+
if (nrow(match) > 0) {
32+
out <- match$name
33+
} else {
34+
out <- NULL
35+
}
36+
out
37+
}

R/lang-help.R

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,9 @@ rd_inst <- function(topic, package, lang) {
6666
} else if (substr(lang, 3, 3) == "_") {
6767
folder <- substr(lang, 1, 2)
6868
} else {
69-
codes <- readRDS(system.file("iso/codes.rds", package = "lang"))
70-
lang <- tolower(lang)
71-
match <- codes[codes$name == lang, "code"]
72-
if (nrow(match) > 0) {
73-
folder <- as.character(match)[[1]]
69+
code <- to_iso639(lang)
70+
if (!is.null(code)) {
71+
folder <- code[[1]]
7472
}
7573
}
7674
if (!is.null(folder)) {

R/process-roxygen.R

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,48 @@
11
#' Creates the Rd files based on translated Roxygen scripts
2-
#' @param folder Source sub-folder where the source Roxygen R scripts are
3-
#' @param source_folder Base source folder where the different translations are located.
4-
#' Defaults to 'man-lang'.
2+
#' @param source_sub_folder Source sub-folder where the source Roxygen R scripts
3+
#' are. Defaults to NULL. If left null, all of the sub-folders in the
4+
#' `source_folder` will be processed
5+
#' @param source_folder Base source folder where the different translations are
6+
#' located. Defaults to 'man-lang'.
57
#' @param target_folder Base target folder where the different translations will be
68
#' located. Defaults to 'inst/man-lang'
79
#' @param r_folder Source of the original R scripts. Only used to see if the
810
#' Roxygen documentation is different from what is capture in the `source_folder`
911
#' @param pkg_path The path to the package
1012
#' @returns Multiple Rd files based on the source R scripts
1113
#' @export
12-
process_roxygen_folder <- function(
13-
folder,
14+
process_roxygen <- function(
15+
source_sub_folder = NULL,
16+
source_folder = "man-lang",
17+
target_folder = "inst/man-lang",
1418
r_folder = "R",
19+
pkg_path = ".") {
20+
if (!dir_exists(path(pkg_path, source_folder))) {
21+
cli_abort("Source folder: '{source_folder}', is not found")
22+
}
23+
if (is.null(source_sub_folder)) {
24+
compare_man_lang(
25+
r_folder = r_folder,
26+
source_folder = source_folder,
27+
pkg_path = pkg_path
28+
)
29+
sub_folders <- dir_ls(path(pkg_path, source_folder), type = "directory")
30+
sub_folders <- path_file(sub_folders)
31+
} else {
32+
sub_folders <- source_sub_folder
33+
}
34+
for (source_sub_folder in sub_folders) {
35+
process_roxygen_folder(
36+
source_sub_folder = source_sub_folder,
37+
source_folder = source_folder,
38+
target_folder = target_folder,
39+
pkg_path = pkg_path
40+
)
41+
}
42+
}
43+
44+
process_roxygen_folder <- function(
45+
source_sub_folder = NULL,
1546
source_folder = "man-lang",
1647
target_folder = "inst/man-lang",
1748
pkg_path = ".") {
@@ -31,22 +62,27 @@ process_roxygen_folder <- function(
3162
}
3263
# Copies content of the translated script to the R folder
3364
# of the temp copy
34-
source_path <- path(pkg_path, source_folder, folder)
65+
source_path <- path(pkg_path, source_folder, source_sub_folder)
3566
full_source <- dir_ls(path(source_path), recurse = TRUE, glob = "*.R")
3667
file_copy(
3768
path = full_source,
3869
new_path = path(copy_path, "R"),
3970
overwrite = TRUE
4071
)
72+
cli_alert_info(
73+
paste0(
74+
"Creating Rd files from {path_rel(source_path)}",
75+
" ({to_title(from_iso639(source_sub_folder)[[1]]) })"
76+
)
77+
)
4178
# Uses `callr` to run roxygenize, mainly to avoid the messages from roxygen2
42-
cli_h3("Creating Rd files for '{folder}'")
4379
callr::r_safe(
4480
func = function(x) roxygen2::roxygenize(x, roclets = "rd"),
4581
args = list(copy_path)
4682
)
4783
# Copies the new contents in 'man' from the temp copy
4884
# into target folder, under the language's sub-folder
49-
target_path <- path(pkg_path, target_folder, folder)
85+
target_path <- path(pkg_path, target_folder, source_sub_folder)
5086
if (dir_exists(target_path)) {
5187
dir_delete(target_path)
5288
}
@@ -64,29 +100,6 @@ process_roxygen_folder <- function(
64100
invisible()
65101
}
66102

67-
#' @rdname process_roxygen_folder
68-
#' @export
69-
process_roxygen <- function(
70-
source_folder = "man-lang",
71-
target_folder = "inst/man-lang",
72-
r_folder = "R",
73-
pkg_path = ".") {
74-
sub_folders <- dir_ls(path(pkg_path, source_folder), type = "directory")
75-
compare_man_lang(
76-
r_folder = r_folder,
77-
source_folder = source_folder,
78-
pkg_path = pkg_path
79-
)
80-
for (folder in sub_folders) {
81-
process_roxygen_folder(
82-
folder = path_file(folder),
83-
source_folder = source_folder,
84-
target_folder = target_folder,
85-
pkg_path = pkg_path
86-
)
87-
}
88-
}
89-
90103
compare_man_lang <- function(
91104
folder = NULL,
92105
r_folder = "R",

R/roxy-comments.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
roxy_comments <- function(x) {
2+
script_contents <- readLines(x)
3+
roxy_comment <- substr(script_contents, 1, 2) == "#'"
4+
just_roxy <- script_contents[roxy_comment]
5+
just_roxy <- just_roxy[just_roxy != "#'"]
6+
7+
if (length(just_roxy) == 0) {
8+
return(NULL)
9+
} else {
10+
just_roxy <- paste0("#-", just_roxy)
11+
no_exports <- !any(grepl("#' @export", just_roxy))
12+
no_name <- !any(grepl("#' @name", just_roxy))
13+
if (no_exports && no_name) {
14+
return(NULL)
15+
}
16+
}
17+
just_roxy
18+
}
19+
20+
roxy_existing <- function(x) {
21+
script_contents <- readLines(x)
22+
roxy_comment <- substr(script_contents, 1, 4) == "#-#'"
23+
script_contents[roxy_comment]
24+
}

0 commit comments

Comments
 (0)