Skip to content

Commit

Permalink
Merge pull request #2 from mlverse/updates
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
edgararuiz authored Dec 6, 2024
2 parents 15bf55c + c44eb72 commit adf673e
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 13 deletions.
13 changes: 8 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
Package: lang
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Title: Using an Large Language Model of your choice, it translates the R
help documentation to a specific language
Version: 0.0.0.9001
Authors@R:
person("First", "Last", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
person("Edgar", "Ruiz", , "[email protected]", role = c("aut", "cre"))
Description: Use an 'LLM' to translate a function's help documentation on-the-fly.
It overrides the `?` and `help()` functions in your R session. If you are
using RStudio or Positron, the translated help page will appear in the usual
help pane.
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Expand Down
4 changes: 2 additions & 2 deletions R/lang-help.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ rd_comment_translate <- function(x, lang) {
rd_char <- llm_vec_translate(rd_char, lang)
rd_char <- paste0("# ", rd_char, "\n")
} else {

}
rd_char <- gsub("%", "\\\\%", rd_char)
attributes(rd_char) <- attributes(x)
Expand Down Expand Up @@ -203,7 +203,7 @@ rd_extract_text <- function(x, collapse = TRUE) {
rd_txt <- paste0(rd_txt, collapse = "")
}
rd_txt <- gsub("\U2018", "'", rd_txt)
rd_txt <- gsub("\U2019", "'", rd_txt)
rd_txt <- gsub("\U2019", "'", rd_txt)
rd_txt
}

Expand Down
5 changes: 2 additions & 3 deletions R/lang.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#' @importFrom roxygen2 parse_file roxygenize env_package
#' @importFrom withr local_tempdir with_dir
#' @importFrom utils capture.output
#' @import tools
#' @import tools
#' @import rlang
#' @import glue
#' @import mall
#' @import cli
#' @import glue
#' @importFrom withr local_tempdir with_dir
#' @import fs
NULL
50 changes: 49 additions & 1 deletion R/process-roxygen.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
#' 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,
r_folder = "R",
source_folder = "man-lang",
target_folder = "inst/man-lang",
pkg_path = ".") {
Expand All @@ -28,8 +31,10 @@ 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)
full_source <- dir_ls(path(source_path), recurse = TRUE, glob = "*.R")
file_copy(
path = dir_ls(path(pkg_path, source_folder, folder)),
path = full_source,
new_path = path(copy_path, "R"),
overwrite = TRUE
)
Expand Down Expand Up @@ -64,8 +69,14 @@ process_roxygen_folder <- function(
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),
Expand All @@ -75,3 +86,40 @@ process_roxygen <- function(
)
}
}

compare_man_lang <- function(
folder = NULL,
r_folder = "R",
source_folder = "man-lang",
pkg_path = ".") {
pkg_dir <- path_abs(pkg_path)
r_scripts <- dir_ls(path(pkg_dir, r_folder), glob = "*.R")
r_comments <- lapply(r_scripts, roxy_comments)
source_path <- path(pkg_path, source_folder)
if (!is.null(folder)) {
source_path <- path(source_path, folder)
}
full_source <- dir_ls(path(source_path), recurse = TRUE, glob = "*.R")
man_comments <- lapply(full_source, roxy_existing)
man_diff <- NULL
found_diff <- FALSE
for (script in r_scripts) {
r_curr <- r_comments[names(r_comments) == script]
if (!is.null(r_curr[[1]])) {
man_curr <- man_comments[path_file(names(man_comments)) == path_file(script)]
is_same <- paste0(r_curr, collapse = "") == paste0(man_curr, collapse = "")
if (!is_same) {
if (!found_diff) {
cli_alert_warning(
c(
"The following R documentation has changed, ",
"translation may need to be revised:"
)
)
found_diff <- TRUE
}
cli_inform("|- {path_rel(script)} -x-> {path_rel(names(man_curr))}")
}
}
}
}
43 changes: 41 additions & 2 deletions R/translate-roxygen.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,16 @@ translate_roxygen_imp <- function(path,
}
pkg_env <- env_package(pkg_path)
}
current_roxy <- roxy_comments(path)
dir_create(dir)
rd_path <- path(dir, path_file(path))
cli_inform("[{no}/{of}] {path} --> {rd_path}")
if (file_exists(rd_path)) {
tr_roxy <- roxy_existing(rd_path)
if (paste0(tr_roxy, collapse = " ") == paste0(current_roxy, collapse = " ")) {
cli_inform("[{no}/{of}] {path} --> [Skipping, no changes]")
return(invisible())
}
}
parsed <- parse_file(path, env = pkg_env)
contents <- NULL
tg_label <- NULL
Expand Down Expand Up @@ -108,7 +115,6 @@ translate_roxygen_imp <- function(path,
if (tg == "section") {
raw <- glue("{raw[1]}:\n{raw[2]}")
}
# raw <- gsub("\n", "\n#'", raw)
if (tg != "title") {
if (length(raw) != 0 && raw != "") {
pre_raw <- paste0(tg, name, collapse = " ")
Expand All @@ -132,6 +138,39 @@ translate_roxygen_imp <- function(path,
contents <- c(contents, fn_str)
}
if (!is.null(contents)) {
cli_inform("[{no}/{of}] {path} --> {rd_path}")
contents <- c(
contents,
"# --- Created by `lang` do not edit by hand ---",
current_roxy
)
writeLines(contents, rd_path)
} else {
cli_inform("[{no}/{of}] {path} --> [Skipping, no content]")
}
}

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]
}
5 changes: 5 additions & 0 deletions man/process_roxygen_folder.Rd

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

0 comments on commit adf673e

Please sign in to comment.